Skip to content

Commit

Permalink
Add pagination; cleanup; add missing endpoitns and fields [#4]
Browse files Browse the repository at this point in the history
  • Loading branch information
dnl-blkv committed Sep 3, 2017
1 parent d91d7da commit 73d2985
Show file tree
Hide file tree
Showing 119 changed files with 2,727 additions and 1,123 deletions.
12 changes: 10 additions & 2 deletions src/main/java/com/bunq/sdk/examples/PaymentListExample.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.bunq.sdk.examples;

import com.bunq.sdk.context.ApiContext;
import com.bunq.sdk.http.Pagination;
import com.bunq.sdk.model.generated.Payment;
import java.util.HashMap;
import java.util.List;

/**
Expand All @@ -18,8 +20,14 @@ public class PaymentListExample {
*/
public static void main(String[] args) {
ApiContext apiContext = ApiContext.restore(API_CONTEXT_FILE_PATH);
List<Payment> payments = Payment.list(apiContext, USER_ITEM_ID, MONETARY_ACCOUNT_ITEM_ID)
.getValue();
HashMap<String, String> params = new HashMap<>();
params.put(Pagination.PARAM_COUNT, "3");
List<Payment> payments = Payment.list(
apiContext,
USER_ITEM_ID,
MONETARY_ACCOUNT_ITEM_ID,
params
).getValue();
printPayments(payments);
}

Expand Down
32 changes: 24 additions & 8 deletions src/main/java/com/bunq/sdk/http/ApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.UUID;
import org.apache.http.Header;
import org.apache.http.HttpHost;
Expand All @@ -32,6 +35,7 @@
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
Expand Down Expand Up @@ -135,13 +139,24 @@ public BunqResponseRaw post(String uri, byte[] requestBodyBytes,
CloseableHttpResponse response = executeRequest(httpPost, customHeaders);

return createBunqResponseRaw(response);
} catch (IOException exception) {
} catch (IOException | URISyntaxException exception) {
throw new UncaughtExceptionError(exception);
}
}

private URI determineFullUri(String uri) {
return URI.create(apiContext.getBaseUri().toString() + uri);
private URI determineFullUri(String uri) throws URISyntaxException {
return determineFullUri(uri, new HashMap<>());
}

private URI determineFullUri(String uri, Map<String, String> params) throws URISyntaxException {
URIBuilder builder = new URIBuilder(apiContext.getBaseUri().toString() + uri);
SortedMap<String, String> paramsSorted = new TreeMap<>(params);

for (Map.Entry<String, String> param : paramsSorted.entrySet()) {
builder.addParameter(param.getKey(), param.getValue());
}

return builder.build();
}

private CloseableHttpResponse executeRequest(HttpUriRequest request,
Expand Down Expand Up @@ -279,13 +294,14 @@ private static Map<String, String> getHeadersMap(CloseableHttpResponse response)
*
* @return The raw response of the GET request.
*/
public BunqResponseRaw get(String uri, Map<String, String> customHeaders) {
public BunqResponseRaw get(String uri, Map<String, String> params,
Map<String, String> customHeaders) {
try {
HttpGet httpGet = new HttpGet(determineFullUri(uri));
HttpGet httpGet = new HttpGet(determineFullUri(uri, params));
CloseableHttpResponse response = executeRequest(httpGet, customHeaders);

return createBunqResponseRaw(response);
} catch (IOException exception) {
} catch (IOException | URISyntaxException exception) {
throw new UncaughtExceptionError(exception);
}
}
Expand All @@ -303,7 +319,7 @@ public BunqResponseRaw put(String uri, byte[] requestBodyBytes,
CloseableHttpResponse response = executeRequest(httpPut, customHeaders);

return createBunqResponseRaw(response);
} catch (IOException exception) {
} catch (IOException | URISyntaxException exception) {
throw new UncaughtExceptionError(exception);
}
}
Expand All @@ -319,7 +335,7 @@ public BunqResponseRaw delete(String uri, Map<String, String> customHeaders) {
CloseableHttpResponse response = executeRequest(httpDelete, customHeaders);

return createBunqResponseRaw(response);
} catch (IOException exception) {
} catch (IOException | URISyntaxException exception) {
throw new UncaughtExceptionError(exception);
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/bunq/sdk/http/BunqResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ public class BunqResponse<T> {

private T value;
private Map<String, String> headers;
private Pagination pagination;

public BunqResponse(T value, Map<String, String> headers) {
this(value, headers, null);
}

public BunqResponse(T value, Map<String, String> headers, Pagination pagination) {
this.value = value;
this.headers = headers;
this.pagination = pagination;
}

public T getValue() {
Expand All @@ -20,4 +26,8 @@ public Map<String, String> getHeaders() {
return headers;
}

public Pagination getPagination() {
return pagination;
}

}
119 changes: 119 additions & 0 deletions src/main/java/com/bunq/sdk/http/Pagination.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package com.bunq.sdk.http;

import com.bunq.sdk.exception.BunqException;
import java.util.HashMap;
import java.util.Map;

public class Pagination {

/**
* Error constants.
*/
private static final String ERROR_NO_PREVIOUS_PAGE =
"Could not generate previous page URL params: there is no previous page.";

/**
* URL Param constants.
*/
public static final String PARAM_OLDER_ID = "older_id";
public static final String PARAM_NEWER_ID = "newer_id";
public static final String PARAM_FUTURE_ID = "future_id";
public static final String PARAM_COUNT = "count";

/**
* Field constants.
*/
private static final String FIELD_OLDER_URL = "older_url";
private static final String FIELD_NEWER_URL = "newer_url";
private static final String FIELD_FUTURE_URL = "future_url";

private Integer olderId;
private Integer newerId;
private Integer futureId;
private Integer count;

/**
* Get the URL params required to request the next page of the listing.
*/
public Map<String, String> getUrlParamsNextPage() {
Map<String, String> params = new HashMap<>();
params.put(PARAM_NEWER_ID, Integer.toString(getNextId()));
addCountToParamsIfNeeded(params);

return params;
}

private Integer getNextId() {
if (hasNextItemAssured()) {
return newerId;
} else {
return futureId;
}
}

public boolean hasNextItemAssured() {
return newerId != null;
}

private void addCountToParamsIfNeeded(Map<String, String> params) {
if (count != null) {
params.put(PARAM_COUNT, Integer.toString(count));
}
}

public Map<String, String> getUrlParamsPreviousPage() {
if (!hasPreviousItem()) {
throw new BunqException(ERROR_NO_PREVIOUS_PAGE);
}

Map<String, String> params = new HashMap<>();
params.put(PARAM_OLDER_ID, Integer.toString(olderId));
addCountToParamsIfNeeded(params);

return params;
}

public boolean hasPreviousItem() {
return this.olderId != null;
}

public Map<String, String> getUrlParamsCountOnly() {
Map<String, String> params = new HashMap<>();
addCountToParamsIfNeeded(params);

return params;
}

public Integer getOlderId() {
return olderId;
}

public void setOlderId(Integer olderId) {
this.olderId = olderId;
}

public Integer getNewerId() {
return newerId;
}

public void setNewerId(Integer newerId) {
this.newerId = newerId;
}

public Integer getFutureId() {
return futureId;
}

public void setFutureId(Integer futureId) {
this.futureId = futureId;
}

public Integer getCount() {
return count;
}

public void setCount(Integer count) {
this.count = count;
}

}
99 changes: 99 additions & 0 deletions src/main/java/com/bunq/sdk/json/PaginationAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.bunq.sdk.json;

import com.bunq.sdk.http.Pagination;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import java.lang.reflect.Type;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;

/**
* Custom (de)serialization of SessionServer required due to the specific way we handle pagination.
*/
public class PaginationAdapter implements JsonDeserializer<Pagination> {

/**
* Field constants.
*/
private static final String FIELD_OLDER_URL = "older_url";
private static final String FIELD_NEWER_URL = "newer_url";
private static final String FIELD_FUTURE_URL = "future_url";

@Override
public Pagination deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
try {
JsonObject responseJson = json.getAsJsonObject();
Map<String, Integer> paginationBody = parsePaginationBody(responseJson);
Pagination pagination = new Pagination();
pagination.setOlderId(paginationBody.get(Pagination.PARAM_OLDER_ID));
pagination.setNewerId(paginationBody.get(Pagination.PARAM_NEWER_ID));
pagination.setFutureId(paginationBody.get(Pagination.PARAM_FUTURE_ID));
pagination.setCount(paginationBody.get(Pagination.PARAM_COUNT));

return pagination;
} catch (URISyntaxException exception) {
throw new JsonParseException(exception);
}
}

private Map<String, Integer> parsePaginationBody(JsonObject responseJson)
throws URISyntaxException {
Map<String, Integer> paginationBody = new HashMap<>();
updatePaginationBodyFromResponseField(
paginationBody,
Pagination.PARAM_OLDER_ID,
responseJson,
FIELD_OLDER_URL,
Pagination.PARAM_OLDER_ID
);
updatePaginationBodyFromResponseField(
paginationBody,
Pagination.PARAM_NEWER_ID,
responseJson,
FIELD_NEWER_URL,
Pagination.PARAM_NEWER_ID
);
updatePaginationBodyFromResponseField(
paginationBody,
Pagination.PARAM_FUTURE_ID,
responseJson,
FIELD_FUTURE_URL,
Pagination.PARAM_NEWER_ID
);

return paginationBody;
}

private void updatePaginationBodyFromResponseField(
Map<String, Integer> paginationBody,
String idField,
JsonObject responseJson,
String responseField,
String responseParam
) throws URISyntaxException {
if (responseJson.has(responseField)) {
URI uri = new URI(responseJson.get(responseField).getAsString());
List<NameValuePair> params = URLEncodedUtils.parse(uri, Charset.defaultCharset());

for (NameValuePair param : params) {
if (responseParam.equals(param.getName())) {
paginationBody.put(idField, Integer.parseInt(param.getValue()));
} else if (Pagination.PARAM_COUNT.equals(param.getName()) &&
paginationBody.get(Pagination.PARAM_COUNT) != null) {
paginationBody.put(Pagination.PARAM_COUNT, Integer.parseInt(param.getValue()));
}
}
}
}

}
9 changes: 5 additions & 4 deletions src/main/java/com/bunq/sdk/json/SessionServerAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,10 @@ public class SessionServerAdapter implements JsonDeserializer<SessionServer> {
private static final String FIELD_USER_COMPANY = "UserCompany";
private static final String FIELD_USER_PERSON = "UserPerson";

private static JsonElement getByIndexAndFieldName(JsonArray values, int index, String fieldName) {
return values.get(index).getAsJsonObject().get(fieldName);
}

@Override
public SessionServer deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
System.out.println(json);
JsonArray values = json.getAsJsonArray();

Id id = context.deserialize(
Expand Down Expand Up @@ -65,4 +62,8 @@ public SessionServer deserialize(JsonElement json, Type typeOfT,
}
}

private static JsonElement getByIndexAndFieldName(JsonArray values, int index, String fieldName) {
return values.get(index).getAsJsonObject().get(fieldName);
}

}
Loading

0 comments on commit 73d2985

Please sign in to comment.