mirror of
https://github.com/haveno-dex/haveno.git
synced 2025-06-28 16:47:25 -04:00
Implement getMarketPrices API endpoint
- Increase rate limit to 10 calls per second. - Use the new API also for the getMarketPrice call, this makes the 'Can get market prices' API test pass
This commit is contained in:
parent
b1e69f9fdc
commit
f27e3e3d1a
7 changed files with 176 additions and 54 deletions
|
@ -18,9 +18,12 @@
|
|||
package bisq.daemon.grpc;
|
||||
|
||||
import bisq.core.api.CoreApi;
|
||||
import bisq.core.api.model.MarketPriceInfo;
|
||||
|
||||
import bisq.proto.grpc.MarketPriceReply;
|
||||
import bisq.proto.grpc.MarketPriceRequest;
|
||||
import bisq.proto.grpc.MarketPricesReply;
|
||||
import bisq.proto.grpc.MarketPricesRequest;
|
||||
|
||||
import io.grpc.ServerInterceptor;
|
||||
import io.grpc.stub.StreamObserver;
|
||||
|
@ -28,6 +31,7 @@ import io.grpc.stub.StreamObserver;
|
|||
import javax.inject.Inject;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
@ -58,17 +62,33 @@ class GrpcPriceService extends PriceImplBase {
|
|||
public void getMarketPrice(MarketPriceRequest req,
|
||||
StreamObserver<MarketPriceReply> responseObserver) {
|
||||
try {
|
||||
coreApi.getMarketPrice(req.getCurrencyCode(),
|
||||
price -> {
|
||||
var reply = MarketPriceReply.newBuilder().setPrice(price).build();
|
||||
responseObserver.onNext(reply);
|
||||
responseObserver.onCompleted();
|
||||
});
|
||||
double marketPrice = coreApi.getMarketPrice(req.getCurrencyCode());
|
||||
responseObserver.onNext(MarketPriceReply.newBuilder().setPrice(marketPrice).build());
|
||||
responseObserver.onCompleted();
|
||||
} catch (Throwable cause) {
|
||||
exceptionHandler.handleException(log, cause, responseObserver);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getMarketPrices(MarketPricesRequest request,
|
||||
StreamObserver<MarketPricesReply> responseObserver) {
|
||||
try {
|
||||
responseObserver.onNext(mapMarketPricesReply(coreApi.getMarketPrices()));
|
||||
responseObserver.onCompleted();
|
||||
} catch (Throwable cause) {
|
||||
exceptionHandler.handleException(log, cause, responseObserver);
|
||||
}
|
||||
}
|
||||
|
||||
private MarketPricesReply mapMarketPricesReply(List<MarketPriceInfo> marketPrices) {
|
||||
MarketPricesReply.Builder builder = MarketPricesReply.newBuilder();
|
||||
marketPrices.stream()
|
||||
.map(MarketPriceInfo::toProtoMessage)
|
||||
.forEach(builder::addMarketPrice);
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
final ServerInterceptor[] interceptors() {
|
||||
Optional<ServerInterceptor> rateMeteringInterceptor = rateMeteringInterceptor();
|
||||
return rateMeteringInterceptor.map(serverInterceptor ->
|
||||
|
@ -79,7 +99,7 @@ class GrpcPriceService extends PriceImplBase {
|
|||
return getCustomRateMeteringInterceptor(coreApi.getConfig().appDataDir, this.getClass())
|
||||
.or(() -> Optional.of(CallRateMeteringInterceptor.valueOf(
|
||||
new HashMap<>() {{
|
||||
put(getGetMarketPriceMethod().getFullMethodName(), new GrpcCallRateMeter(1, SECONDS));
|
||||
put(getGetMarketPriceMethod().getFullMethodName(), new GrpcCallRateMeter(10, SECONDS));
|
||||
}}
|
||||
)));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue