Skip to content

Commit

Permalink
Added support for public API methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jorn committed May 26, 2018
1 parent b0d4370 commit 7094c8f
Show file tree
Hide file tree
Showing 26 changed files with 511 additions and 1,245 deletions.
248 changes: 166 additions & 82 deletions src/main/java/io/yogh/bl3p/api/v1/Bl3pClient.java

Large diffs are not rendered by default.

61 changes: 32 additions & 29 deletions src/main/java/io/yogh/bl3p/api/v1/Bl3pClientRequestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.concurrent.Future;
import java.util.function.Consumer;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
Expand All @@ -18,10 +17,11 @@
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.async.Callback;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.mashape.unirest.request.GetRequest;
import com.mashape.unirest.request.body.RequestBodyEntity;

import io.yogh.bl3p.api.v1.request.ApiCall;
import io.yogh.bl3p.api.v1.request.RequestUriBuilder;
import io.yogh.bl3p.api.v1.request.authenticated.RequestUriBuilder;
import io.yogh.bl3p.api.v1.response.exception.Bl3pException;
import io.yogh.bl3p.api.v1.response.exception.Bl3pException.Reason;

Expand All @@ -32,47 +32,37 @@ public final class Bl3pClientRequestUtil {

private Bl3pClientRequestUtil() {}

public static JsonNode doCall(final String uuid, final String key, final ApiCall call) throws Bl3pException {
public static JsonNode doAuthenticatedCall(final String uuid, final String key, final ApiCall call) throws Bl3pException {
try {
return createCall(uuid, key, call).asJson().getBody();
return createAuthenticatedCall(uuid, key, call).asJson().getBody();
} catch (final UnirestException e) {
LOG.error("Failure while executing request.", call, e);
throw new Bl3pException(Reason.UNSPECIFIED_CLIENT_ERROR, e.getMessage(), e);
}
}

public static Future<HttpResponse<JsonNode>> doCallAsync(final String uuid, final String key, final ApiCall call) {
return createCall(uuid, key, call).asJsonAsync();
return createAuthenticatedCall(uuid, key, call).asJsonAsync();
}

public static void doCallAsync(final String uuid, final String key, final Callback<JsonNode> complete, final ApiCall call) {
createCall(uuid, key, call).asJsonAsync(complete);
public static void doAuthenticatedCallAsync(final String uuid, final String key, final Callback<JsonNode> complete, final ApiCall call) {
createAuthenticatedCall(uuid, key, call).asJsonAsync(complete);
}

/**
* This method does not report back any errors and as such should not be used in
* the common case.
*/
public static void doCallAsyncNaive(final String uuid, final String key, final ApiCall call, final Consumer<JsonNode> complete) {
createCall(uuid, key, call).asJsonAsync(new Callback<JsonNode>() {
@Override
public void completed(final HttpResponse<JsonNode> response) {
complete.accept(response.getBody());
}

@Override
public void failed(final UnirestException e) {
LOG.error("Failure while executing request.", call, e);
}

@Override
public void cancelled() {
LOG.warn("Request cancelled.", call);
}
});
public static JsonNode doPublicCall(final ApiCall call) throws Bl3pException {
try {
return createPublicCall(call).asJson().getBody();
} catch (final UnirestException e) {
LOG.error("Failure while executing request.", call, e);
throw new Bl3pException(Reason.UNSPECIFIED_CLIENT_ERROR, e.getMessage(), e);
}
}

public static void doPublicCallAsync(final Callback<JsonNode> complete, final ApiCall call) {
createPublicCall(call).asJsonAsync(complete);
}

private static RequestBodyEntity createCall(final String uuid, final String key, final ApiCall call) {
private static RequestBodyEntity createAuthenticatedCall(final String uuid, final String key, final ApiCall call) {
try {
final String uri = RequestUriBuilder.getUri(call);
final String post = RequestUriBuilder.getPostData(call);
Expand All @@ -95,4 +85,17 @@ private static RequestBodyEntity createCall(final String uuid, final String key,
throw new RuntimeException("Unrecoverable error relating to signing requests.", e);
}
}

private static GetRequest createPublicCall(final ApiCall call) {
final StringBuilder builder = new StringBuilder(HOST);
builder.append(RequestUriBuilder.getUri(call));

final String post = RequestUriBuilder.getPostData(call);
if (post != null && !post.isEmpty()) {
builder.append("?");
builder.append(post);
}

return Unirest.get(builder.toString());
}
}
Loading

0 comments on commit 7094c8f

Please sign in to comment.