-
Notifications
You must be signed in to change notification settings - Fork 0
Add support for aggregator API keys #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d1bc067
Add support for aggregator API keys
ristoalas ee6357f
Merge branch 'main' into aggregator-subscription-support
ristoalas 7159060
Merge branch 'main' into aggregator-subscription-support
ristoalas c9f263e
Update src/main/java/org/unicitylabs/sdk/jsonrpc/RateLimitExceededExc…
ristoalas 7f7b197
Improve mock state resets between requests
ristoalas 6f2c5f3
Merge branch 'aggregator-subscription-support' of github.com:unicityn…
ristoalas 178ec08
Refactor error message construction
ristoalas d908839
Refactor JsonRpcHttpTransport to not depend on a higher level detail …
ristoalas 4ec68c8
Simplify error handling
ristoalas 2258d56
Remove unused code
ristoalas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/main/java/org/unicitylabs/sdk/jsonrpc/RateLimitExceededException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.unicitylabs.sdk.jsonrpc; | ||
|
||
ristoalas marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
public class RateLimitExceededException extends RuntimeException { | ||
|
||
private final int retryAfterSeconds; | ||
|
||
public RateLimitExceededException(String message, int retryAfterSeconds) { | ||
super(message); | ||
this.retryAfterSeconds = retryAfterSeconds; | ||
} | ||
|
||
public RateLimitExceededException(String message, int retryAfterSeconds, Throwable cause) { | ||
super(message, cause); | ||
this.retryAfterSeconds = retryAfterSeconds; | ||
} | ||
|
||
public int getRetryAfterSeconds() { | ||
return retryAfterSeconds; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/org/unicitylabs/sdk/jsonrpc/UnauthorizedException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.unicitylabs.sdk.jsonrpc; | ||
|
||
/** | ||
* Exception thrown when an API request is unauthorized (HTTP 401). | ||
* This typically occurs when an API key is missing or invalid. | ||
*/ | ||
public class UnauthorizedException extends RuntimeException { | ||
|
||
public UnauthorizedException(String message) { | ||
super(message); | ||
} | ||
|
||
public UnauthorizedException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
152 changes: 152 additions & 0 deletions
152
src/test/java/org/unicitylabs/sdk/MockAggregatorServer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package org.unicitylabs.sdk; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import okhttp3.mockwebserver.Dispatcher; | ||
import okhttp3.mockwebserver.MockResponse; | ||
import okhttp3.mockwebserver.MockWebServer; | ||
import okhttp3.mockwebserver.RecordedRequest; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.io.IOException; | ||
import java.util.Set; | ||
import java.util.HashSet; | ||
import java.util.UUID; | ||
|
||
public class MockAggregatorServer { | ||
|
||
private final MockWebServer server; | ||
private final ObjectMapper objectMapper; | ||
private final Set<String> protectedMethods; | ||
private volatile boolean simulateRateLimit = false; | ||
private volatile int rateLimitRetryAfter = 60; | ||
private volatile String expectedApiKey = null; | ||
ristoalas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public MockAggregatorServer() { | ||
this.server = new MockWebServer(); | ||
this.objectMapper = new ObjectMapper(); | ||
this.protectedMethods = new HashSet<>(); | ||
this.protectedMethods.add("submit_commitment"); | ||
|
||
server.setDispatcher(new Dispatcher() { | ||
@Override | ||
public MockResponse dispatch(RecordedRequest request) { | ||
return handleRequest(request); | ||
} | ||
}); | ||
} | ||
|
||
public void start() throws IOException { | ||
server.start(); | ||
} | ||
|
||
public void shutdown() throws IOException { | ||
server.shutdown(); | ||
} | ||
|
||
public String getUrl() { | ||
return server.url("/").toString(); | ||
} | ||
|
||
public RecordedRequest takeRequest() throws InterruptedException { | ||
return server.takeRequest(); | ||
} | ||
|
||
public void simulateRateLimitForNextRequest(int retryAfterSeconds) { | ||
this.simulateRateLimit = true; | ||
this.rateLimitRetryAfter = retryAfterSeconds; | ||
} | ||
|
||
public void setExpectedApiKey(String apiKey) { | ||
this.expectedApiKey = apiKey; | ||
} | ||
|
||
private MockResponse handleRequest(RecordedRequest request) { | ||
try { | ||
if (simulateRateLimit) { | ||
simulateRateLimit = false; // Reset for next request | ||
return new MockResponse() | ||
.setResponseCode(429) | ||
.setHeader("Retry-After", String.valueOf(rateLimitRetryAfter)) | ||
.setBody("Too Many Requests"); | ||
} | ||
|
||
String method = extractJsonRpcMethod(request); | ||
|
||
if (protectedMethods.contains(method) && expectedApiKey != null && !hasValidApiKey(request)) { | ||
return new MockResponse() | ||
.setResponseCode(401) | ||
.setHeader("WWW-Authenticate", "Bearer") | ||
.setBody("Unauthorized"); | ||
} | ||
|
||
return generateSuccessResponse(method); | ||
|
||
} catch (Exception e) { | ||
return new MockResponse() | ||
.setResponseCode(400) | ||
.setBody("Bad Request"); | ||
} | ||
} | ||
|
||
private boolean hasValidApiKey(RecordedRequest request) { | ||
String authHeader = request.getHeader("Authorization"); | ||
if (authHeader != null && authHeader.startsWith("Bearer ")) { | ||
String providedKey = authHeader.substring(7); | ||
return expectedApiKey.equals(providedKey); | ||
} | ||
return false; | ||
} | ||
|
||
private @Nullable String extractJsonRpcMethod(RecordedRequest request) throws JsonProcessingException { | ||
if (!"POST".equals(request.getMethod())) { | ||
return null; | ||
} | ||
JsonNode jsonRequest = objectMapper.readTree(request.getBody().readUtf8()); | ||
return jsonRequest.has("method") ? jsonRequest.get("method").asText() : null; | ||
} | ||
|
||
private MockResponse generateSuccessResponse(String method) { | ||
String responseBody; | ||
String id = UUID.randomUUID().toString(); | ||
|
||
switch (method != null ? method : "") { | ||
case "submit_commitment": | ||
responseBody = String.format( | ||
ristoalas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"{\n" + | ||
" \"jsonrpc\": \"2.0\",\n" + | ||
" \"result\": {\n" + | ||
" \"status\": \"SUCCESS\"\n" + | ||
" },\n" + | ||
" \"id\": \"%s\"\n" + | ||
"}", id); | ||
break; | ||
|
||
case "get_block_height": | ||
responseBody = String.format( | ||
"{\n" + | ||
" \"jsonrpc\": \"2.0\",\n" + | ||
" \"result\": {\n" + | ||
" \"blockNumber\": \"67890\"\n" + | ||
" },\n" + | ||
" \"id\": \"%s\"\n" + | ||
"}", id); | ||
break; | ||
|
||
default: | ||
responseBody = String.format( | ||
"{\n" + | ||
" \"jsonrpc\": \"2.0\",\n" + | ||
" \"result\": \"OK\",\n" + | ||
" \"id\": \"%s\"\n" + | ||
"}", id); | ||
break; | ||
} | ||
|
||
return new MockResponse() | ||
.setResponseCode(200) | ||
.setHeader("Content-Type", "application/json") | ||
.setBody(responseBody); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.