Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Thomson committed May 19, 2015
2 parents 96cce4b + 6a8a0e8 commit 98ae756
Show file tree
Hide file tree
Showing 25 changed files with 268 additions and 133 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ With Maven:
<dependency>
<groupId>com.gocardless</groupId>
<artifactId>gocardless-pro</artifactId>
<version>0.2.2</version>
<version>0.2.3</version>
</dependency>
```

With Gradle:

```
compile 'com.gocardless:gocardless-pro:0.2.2'
compile 'com.gocardless:gocardless-pro:0.2.3'
```

## Initializing the client
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ apply plugin: 'signing'

sourceCompatibility = 1.7
group = ' com.gocardless'
version = '0.2.2'
version = '0.2.3'

repositories {
mavenCentral()
Expand Down
37 changes: 31 additions & 6 deletions src/main/java/com/gocardless/http/GetRequest.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package com.gocardless.http;

import java.io.Reader;
import java.io.InputStream;

/**
* Base class for GET requests that return a single item.
*
* @param <T> the type of the item returned by this request.
*/
public abstract class GetRequest<T> extends HttpRequest<T> {
protected GetRequest(HttpClient httpClient) {
public abstract class GetRequest<S, T> extends HttpRequest<T> {
private final GetRequestExecutor<S, T> executor;

protected GetRequest(HttpClient httpClient, GetRequestExecutor<S, T> executor) {
super(httpClient);
this.executor = executor;
}

/**
Expand All @@ -19,12 +22,12 @@ protected GetRequest(HttpClient httpClient) {
*
* @throws com.gocardless.GoCardlessException
*/
public T execute() {
return getHttpClient().execute(this);
public S execute() {
return executor.execute(this, getHttpClient());
}

@Override
protected T parseResponse(Reader stream, ResponseParser responseParser) {
protected T parseResponse(InputStream stream, ResponseParser responseParser) {
return responseParser.parseSingle(stream, getEnvelope(), getResponseClass());
}

Expand All @@ -39,4 +42,26 @@ protected final boolean hasBody() {
}

protected abstract Class<T> getResponseClass();

public interface GetRequestExecutor<S, T> {
S execute(GetRequest<S, T> request, HttpClient client);
}

public static <T> GetRequestExecutor<T, T> jsonExecutor() {
return new GetRequestExecutor<T, T>() {
@Override
public T execute(GetRequest<T, T> request, HttpClient client) {
return client.execute(request);
}
};
}

public static <T> GetRequestExecutor<InputStream, T> downloadExecutor(final String contentType) {
return new GetRequestExecutor<InputStream, T>() {
@Override
public InputStream execute(GetRequest<InputStream, T> request, HttpClient client) {
return client.rawExecute(request, contentType);
}
};
}
}
37 changes: 20 additions & 17 deletions src/main/java/com/gocardless/http/HttpClient.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.gocardless.http;

import java.io.IOException;
import java.io.Reader;
import java.io.InputStream;
import java.net.URL;
import java.util.Map;

Expand All @@ -19,7 +19,7 @@
* Users of this library should not need to access this class directly.
*/
public class HttpClient {
private static final String USER_AGENT = String.format("gocardless-pro/0.2.2 %s/%s %s/%s",
private static final String USER_AGENT = String.format("gocardless-pro/0.2.3 %s/%s %s/%s",
replaceSpaces(System.getProperty("os.name")),
replaceSpaces(System.getProperty("os.version")),
replaceSpaces(System.getProperty("java.vm.name")),
Expand Down Expand Up @@ -52,21 +52,32 @@ public HttpClient(String accessToken, String baseUrl) {
this.credentials = String.format("Bearer %s", accessToken);
}

<T> T execute(HttpRequest<T> request) {
InputStream rawExecute(HttpRequest<?> request, String accept) {
URL url = request.getUrl(urlFormatter);
Request.Builder httpRequest =
new Request.Builder().url(url).header("Authorization", credentials)
.header("User-Agent", USER_AGENT)
new Request.Builder().url(url).header("Accept", accept)
.header("Authorization", credentials).header("User-Agent", USER_AGENT)
.method(request.getMethod(), getBody(request));
for (Map.Entry<String, String> entry : HEADERS.entrySet()) {
httpRequest = httpRequest.header(entry.getKey(), entry.getValue());
}
Response response = execute(httpRequest.build());
if (response.isSuccessful()) {
return handleSuccessfulResponse(request, response);
} else {
if (!response.isSuccessful()) {
throw handleErrorResponse(response);
}
try {
return response.body().byteStream();
} catch (IOException e) {
throw new GoCardlessNetworkException("Failed to read response body", e);
}
}

<T> T execute(HttpRequest<T> request) {
try (InputStream stream = rawExecute(request, MEDIA_TYPE.toString())) {
return request.parseResponse(stream, responseParser);
} catch (IOException e) {
throw new GoCardlessNetworkException("Failed to read response body", e);
}
}

private <T> RequestBody getBody(HttpRequest<T> request) {
Expand All @@ -85,16 +96,8 @@ private Response execute(Request request) {
}
}

private <T> T handleSuccessfulResponse(HttpRequest<T> request, Response response) {
try (Reader stream = response.body().charStream()) {
return request.parseResponse(stream, responseParser);
} catch (IOException e) {
throw new GoCardlessNetworkException("Failed to read response body", e);
}
}

private GoCardlessException handleErrorResponse(Response response) {
try (Reader stream = response.body().charStream()) {
try (InputStream stream = response.body().byteStream()) {
return responseParser.parseError(stream);
} catch (IOException e) {
throw new GoCardlessNetworkException("Failed to read response body", e);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/gocardless/http/HttpRequest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.gocardless.http;

import java.io.Reader;
import java.io.InputStream;
import java.net.URL;
import java.util.Map;

Expand Down Expand Up @@ -46,5 +46,5 @@ protected String getRequestEnvelope() {

protected abstract boolean hasBody();

protected abstract T parseResponse(Reader stream, ResponseParser responseParser);
protected abstract T parseResponse(InputStream stream, ResponseParser responseParser);
}
4 changes: 2 additions & 2 deletions src/main/java/com/gocardless/http/ListRequest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.gocardless.http;

import java.io.Reader;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -35,7 +35,7 @@ public S execute() {
}

@Override
protected ListResponse<T> parseResponse(Reader stream, ResponseParser responseParser) {
protected ListResponse<T> parseResponse(InputStream stream, ResponseParser responseParser) {
return responseParser.parsePage(stream, getEnvelope(), getTypeToken());
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/gocardless/http/PostRequest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.gocardless.http;

import java.io.Reader;
import java.io.InputStream;

/**
* Base class for POST requests.
Expand All @@ -24,7 +24,7 @@ public T execute() {
}

@Override
protected T parseResponse(Reader stream, ResponseParser responseParser) {
protected T parseResponse(InputStream stream, ResponseParser responseParser) {
return responseParser.parseSingle(stream, getEnvelope(), getResponseClass());
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/gocardless/http/PutRequest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.gocardless.http;

import java.io.Reader;
import java.io.InputStream;

/**
* Base class for PUT requests.
Expand All @@ -24,7 +24,7 @@ public T execute() {
}

@Override
protected T parseResponse(Reader stream, ResponseParser responseParser) {
protected T parseResponse(InputStream stream, ResponseParser responseParser) {
return responseParser.parseSingle(stream, getEnvelope(), getResponseClass());
}

Expand Down
14 changes: 9 additions & 5 deletions src/main/java/com/gocardless/http/ResponseParser.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.gocardless.http;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.List;

Expand All @@ -18,22 +20,24 @@ final class ResponseParser {
this.gson = gson;
}

<T> T parseSingle(Reader stream, String envelope, Class<T> clazz) {
JsonElement json = new JsonParser().parse(stream);
<T> T parseSingle(InputStream stream, String envelope, Class<T> clazz) {
Reader reader = new InputStreamReader(stream);
JsonElement json = new JsonParser().parse(reader);
JsonObject object = json.getAsJsonObject().getAsJsonObject(envelope);
return gson.fromJson(object, clazz);
}

<T> ListResponse<T> parsePage(Reader stream, String envelope, TypeToken<List<T>> clazz) {
JsonObject json = new JsonParser().parse(stream).getAsJsonObject();
<T> ListResponse<T> parsePage(InputStream stream, String envelope, TypeToken<List<T>> clazz) {
Reader reader = new InputStreamReader(stream);
JsonObject json = new JsonParser().parse(reader).getAsJsonObject();
JsonArray array = json.getAsJsonArray(envelope);
List<T> items = gson.fromJson(array, clazz.getType());
JsonObject metaJson = json.getAsJsonObject("meta");
ListResponse.Meta meta = gson.fromJson(metaJson, ListResponse.Meta.class);
return new ListResponse<>(ImmutableList.copyOf(items), meta);
}

GoCardlessApiException parseError(Reader stream) {
GoCardlessApiException parseError(InputStream stream) {
ApiErrorResponse error = parseSingle(stream, "error", ApiErrorResponse.class);
return GoCardlessErrorMapper.toException(error);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ public CreditorBankAccountListRequest<Iterable<CreditorBankAccount>> all() {
/**
* Retrieves the details of an existing creditor bank account.
*/
public CreditorBankAccountGetRequest get(String identity) {
return new CreditorBankAccountGetRequest(httpClient, identity);
public CreditorBankAccountGetRequest<CreditorBankAccount> get(String identity) {
return new CreditorBankAccountGetRequest<>(httpClient,
GetRequest.<CreditorBankAccount>jsonExecutor(), identity);
}

/**
Expand Down Expand Up @@ -350,12 +351,14 @@ public String toString() {
*
* Retrieves the details of an existing creditor bank account.
*/
public static final class CreditorBankAccountGetRequest extends GetRequest<CreditorBankAccount> {
public static final class CreditorBankAccountGetRequest<S> extends
GetRequest<S, CreditorBankAccount> {
@PathParam
private final String identity;

private CreditorBankAccountGetRequest(HttpClient httpClient, String identity) {
super(httpClient);
private CreditorBankAccountGetRequest(HttpClient httpClient,
GetRequestExecutor<S, CreditorBankAccount> executor, String identity) {
super(httpClient, executor);
this.identity = identity;
}

Expand Down
11 changes: 6 additions & 5 deletions src/main/java/com/gocardless/services/CreditorService.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public CreditorListRequest<Iterable<Creditor>> all() {
/**
* Retrieves the details of an existing creditor.
*/
public CreditorGetRequest get(String identity) {
return new CreditorGetRequest(httpClient, identity);
public CreditorGetRequest<Creditor> get(String identity) {
return new CreditorGetRequest<>(httpClient, GetRequest.<Creditor>jsonExecutor(), identity);
}

/**
Expand Down Expand Up @@ -264,12 +264,13 @@ protected TypeToken<List<Creditor>> getTypeToken() {
*
* Retrieves the details of an existing creditor.
*/
public static final class CreditorGetRequest extends GetRequest<Creditor> {
public static final class CreditorGetRequest<S> extends GetRequest<S, Creditor> {
@PathParam
private final String identity;

private CreditorGetRequest(HttpClient httpClient, String identity) {
super(httpClient);
private CreditorGetRequest(HttpClient httpClient, GetRequestExecutor<S, Creditor> executor,
String identity) {
super(httpClient, executor);
this.identity = identity;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ public CustomerBankAccountListRequest<Iterable<CustomerBankAccount>> all() {
/**
* Retrieves the details of an existing bank account.
*/
public CustomerBankAccountGetRequest get(String identity) {
return new CustomerBankAccountGetRequest(httpClient, identity);
public CustomerBankAccountGetRequest<CustomerBankAccount> get(String identity) {
return new CustomerBankAccountGetRequest<>(httpClient,
GetRequest.<CustomerBankAccount>jsonExecutor(), identity);
}

/**
Expand Down Expand Up @@ -398,12 +399,14 @@ public String toString() {
*
* Retrieves the details of an existing bank account.
*/
public static final class CustomerBankAccountGetRequest extends GetRequest<CustomerBankAccount> {
public static final class CustomerBankAccountGetRequest<S> extends
GetRequest<S, CustomerBankAccount> {
@PathParam
private final String identity;

private CustomerBankAccountGetRequest(HttpClient httpClient, String identity) {
super(httpClient);
private CustomerBankAccountGetRequest(HttpClient httpClient,
GetRequestExecutor<S, CustomerBankAccount> executor, String identity) {
super(httpClient, executor);
this.identity = identity;
}

Expand Down
11 changes: 6 additions & 5 deletions src/main/java/com/gocardless/services/CustomerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public CustomerListRequest<Iterable<Customer>> all() {
/**
* Retrieves the details of an existing customer.
*/
public CustomerGetRequest get(String identity) {
return new CustomerGetRequest(httpClient, identity);
public CustomerGetRequest<Customer> get(String identity) {
return new CustomerGetRequest<>(httpClient, GetRequest.<Customer>jsonExecutor(), identity);
}

/**
Expand Down Expand Up @@ -384,12 +384,13 @@ public Map<String, Object> getQueryParams() {
*
* Retrieves the details of an existing customer.
*/
public static final class CustomerGetRequest extends GetRequest<Customer> {
public static final class CustomerGetRequest<S> extends GetRequest<S, Customer> {
@PathParam
private final String identity;

private CustomerGetRequest(HttpClient httpClient, String identity) {
super(httpClient);
private CustomerGetRequest(HttpClient httpClient, GetRequestExecutor<S, Customer> executor,
String identity) {
super(httpClient, executor);
this.identity = identity;
}

Expand Down
Loading

0 comments on commit 98ae756

Please sign in to comment.