-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pagination; cleanup; add missing endpoitns and fields [#4]
- Loading branch information
Showing
119 changed files
with
2,727 additions
and
1,123 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,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; | ||
} | ||
|
||
} |
This file contains 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,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())); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains 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
Oops, something went wrong.