Skip to content

Commit

Permalink
extracted repetitive code for List/Get/Create/Update/Delete into inte…
Browse files Browse the repository at this point in the history
…rfaces
  • Loading branch information
tubbynl committed Nov 15, 2016
1 parent d36eaf4 commit 9eddc7d
Show file tree
Hide file tree
Showing 16 changed files with 177 additions and 277 deletions.
18 changes: 12 additions & 6 deletions src/main/java/nl/stil4m/mollie/concepts/AbstractConcept.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
import nl.stil4m.mollie.ResponseOrError;
import nl.stil4m.mollie.domain.Page;

public abstract class AbstractConcept<T extends Object> {
public abstract class AbstractConcept<T extends Object> implements Concept<T> {
private static final Collector<CharSequence, ?, String> URL_JOINER = Collectors.joining("/");
private static final TypeReference<Void> VOID_TYPE_REFERENCE = new TypeReference<Void>(){};

private final TypeReference<Page<T>> pageTypeReference;
private final TypeReference<T> singleTypeReference;
private final String apiKey;
Expand All @@ -48,26 +49,31 @@ private static String trimToNull(String value) {
return value!=null && !value.trim().isEmpty()?value.trim():null;
}

protected String url(String... elements) {
@Override
public String url(String... elements) {
if(elements==null || elements.length==0) {
return this.endpoint;
}
return joinUrl(this.endpoint,joinUrl(elements));
}

protected ResponseOrError<Page<T>> requestPage(HttpUriRequest httpRequest) throws IOException {
@Override
public ResponseOrError<Page<T>> requestPage(HttpUriRequest httpRequest) throws IOException {
return requestExecutor.execute(apiKey, httpRequest, pageTypeReference);
}

protected ResponseOrError<T> requestSingle(HttpUriRequest httpRequest) throws IOException {
@Override
public ResponseOrError<T> requestSingle(HttpUriRequest httpRequest) throws IOException {
return requestExecutor.execute(apiKey, httpRequest, singleTypeReference);
}

protected ResponseOrError<Void> requestVoid(HttpUriRequest httpRequest) throws IOException {
@Override
public ResponseOrError<Void> requestVoid(HttpUriRequest httpRequest) throws IOException {
return requestExecutor.execute(apiKey, httpRequest, VOID_TYPE_REFERENCE);
}

protected HttpEntity buildHttpEntity(Object value) throws UnsupportedCharsetException, JsonProcessingException {
@Override
public HttpEntity buildHttpEntity(Object value) throws UnsupportedCharsetException, JsonProcessingException {
return new StringEntity(requestExecutor.serialize(value), ContentType.APPLICATION_JSON);
}
}
24 changes: 24 additions & 0 deletions src/main/java/nl/stil4m/mollie/concepts/Concept.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package nl.stil4m.mollie.concepts;

import java.io.IOException;
import java.nio.charset.UnsupportedCharsetException;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpUriRequest;

import com.fasterxml.jackson.core.JsonProcessingException;

import nl.stil4m.mollie.ResponseOrError;
import nl.stil4m.mollie.domain.Page;

public interface Concept<T extends Object> {
String url(String... elements);

ResponseOrError<Page<T>> requestPage(HttpUriRequest get) throws IOException;

ResponseOrError<T> requestSingle(HttpUriRequest get) throws IOException;

ResponseOrError<Void> requestVoid(HttpUriRequest get) throws IOException;

HttpEntity buildHttpEntity(Object value) throws UnsupportedCharsetException, JsonProcessingException;
}
16 changes: 16 additions & 0 deletions src/main/java/nl/stil4m/mollie/concepts/Create.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package nl.stil4m.mollie.concepts;

import java.io.IOException;

import org.apache.http.client.methods.HttpPost;

import nl.stil4m.mollie.ResponseOrError;

public interface Create<R extends Object,O extends Object> extends Concept<R> {

default ResponseOrError<R> create(O create) throws IOException {
HttpPost httpPost = new HttpPost(url());
httpPost.setEntity(buildHttpEntity(create));
return requestSingle(httpPost);
}
}
33 changes: 2 additions & 31 deletions src/main/java/nl/stil4m/mollie/concepts/CustomerPayments.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,12 @@
package nl.stil4m.mollie.concepts;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Optional;

import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;

import com.fasterxml.jackson.core.type.TypeReference;

import nl.stil4m.mollie.RequestExecutor;
import nl.stil4m.mollie.ResponseOrError;
import nl.stil4m.mollie.domain.CustomerPayment;
import nl.stil4m.mollie.domain.Page;
import nl.stil4m.mollie.domain.Payment;

public class CustomerPayments extends AbstractConcept<Payment> {
private static final TypeReference<Page<Payment>> PAGE_TYPE = new TypeReference<Page<Payment>>() {};
private static final TypeReference<Payment> SINGLE_TYPE = new TypeReference<Payment>() {};
public class CustomerPayments extends AbstractConcept<Payment> implements ListAll<Payment>,Create<Payment,CustomerPayment> {

public CustomerPayments(String apiKey, String endpoint, RequestExecutor requestExecutor, String customerId) {
super(apiKey,requestExecutor,SINGLE_TYPE,PAGE_TYPE,endpoint,"customers",customerId,"payments");
}

public ResponseOrError<Page<Payment>> all(Optional<Object> count, Optional<Object> offset) throws URISyntaxException, IOException {
URIBuilder builder = new URIBuilder(url())
.setParameter("count", String.valueOf(count.orElse(10)))
.setParameter("offset", String.valueOf(offset.orElse(0)));

HttpGet httpGet = new HttpGet(builder.build());
return requestPage(httpGet);
}

public ResponseOrError<Payment> create(CustomerPayment customerPayment) throws IOException {
HttpPost httpPost = new HttpPost(url());
httpPost.setEntity(buildHttpEntity(customerPayment));
return requestSingle(httpPost);
super(apiKey,requestExecutor,Payments.SINGLE_TYPE,Payments.PAGE_TYPE,endpoint,"customers",customerId,"payments");
}
}
37 changes: 1 addition & 36 deletions src/main/java/nl/stil4m/mollie/concepts/Customers.java
Original file line number Diff line number Diff line change
@@ -1,53 +1,18 @@
package nl.stil4m.mollie.concepts;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Optional;

import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;

import com.fasterxml.jackson.core.type.TypeReference;

import nl.stil4m.mollie.RequestExecutor;
import nl.stil4m.mollie.ResponseOrError;
import nl.stil4m.mollie.domain.CreateCustomer;
import nl.stil4m.mollie.domain.Customer;
import nl.stil4m.mollie.domain.Page;
import nl.stil4m.mollie.domain.UpdateCustomer;

public class Customers extends AbstractConcept<Customer> {
public class Customers extends AbstractConcept<Customer> implements ListAll<Customer>,GetById<Customer>,Create<Customer,CreateCustomer>,Update<Customer,UpdateCustomer> {
private static final TypeReference<Page<Customer>> PAGE_TYPE = new TypeReference<Page<Customer>>() {};
private static final TypeReference<Customer> SINGLE_TYPE = new TypeReference<Customer>() {};

public Customers(String apiKey, String endpoint, RequestExecutor requestExecutor) {
super(apiKey,requestExecutor,SINGLE_TYPE,PAGE_TYPE,endpoint,"customers");
}

public ResponseOrError<Customer> create(CreateCustomer createCustomer) throws IOException {
HttpPost httpPost = new HttpPost(url());
httpPost.setEntity(buildHttpEntity(createCustomer));
return requestSingle(httpPost);
}

public ResponseOrError<Customer> update(String customerId, UpdateCustomer updateCustomer) throws IOException {
HttpPost httpPost = new HttpPost(url(customerId));
httpPost.setEntity(buildHttpEntity(updateCustomer));
return requestSingle(httpPost);
}

public ResponseOrError<Page<Customer>> all(Optional<Integer> count, Optional<Integer> offset) throws IOException, URISyntaxException {
URIBuilder builder = new URIBuilder(url())
.setParameter("count", String.valueOf(count.orElse(10)))
.setParameter("offset", String.valueOf(offset.orElse(0)));

HttpGet httpGet = new HttpGet(builder.build());
return requestPage(httpGet);
}

public ResponseOrError<Customer> get(String id) throws IOException {
HttpGet httpGet = new HttpGet(url(id));
return requestSingle(httpGet);
}
}
15 changes: 15 additions & 0 deletions src/main/java/nl/stil4m/mollie/concepts/Delete.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package nl.stil4m.mollie.concepts;

import java.io.IOException;

import org.apache.http.client.methods.HttpDelete;

import nl.stil4m.mollie.ResponseOrError;

public interface Delete<T extends Object> extends Concept<T> {

default ResponseOrError<Void> delete(String id) throws IOException {
HttpDelete httpDelete = new HttpDelete(url(id));
return requestVoid(httpDelete);
}
}
15 changes: 15 additions & 0 deletions src/main/java/nl/stil4m/mollie/concepts/GetById.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package nl.stil4m.mollie.concepts;

import java.io.IOException;

import org.apache.http.client.methods.HttpGet;

import nl.stil4m.mollie.ResponseOrError;

public interface GetById<T extends Object> extends Concept<T> {

default ResponseOrError<T> get(String id) throws IOException {
HttpGet httpGet = new HttpGet(url(id));
return requestSingle(httpGet);
}
}
27 changes: 2 additions & 25 deletions src/main/java/nl/stil4m/mollie/concepts/Issuers.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,16 @@
package nl.stil4m.mollie.concepts;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Optional;

import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;

import com.fasterxml.jackson.core.type.TypeReference;

import nl.stil4m.mollie.RequestExecutor;
import nl.stil4m.mollie.ResponseOrError;
import nl.stil4m.mollie.domain.Issuer;
import nl.stil4m.mollie.domain.Page;

public class Issuers extends AbstractConcept<Issuer> {
public class Issuers extends AbstractConcept<Issuer> implements ListAll<Issuer>,GetById<Issuer> {
private static final TypeReference<Page<Issuer>> PAGE_TYPE = new TypeReference<Page<Issuer>>() {};
private static final TypeReference<Issuer> SINGLE_TYPE = new TypeReference<Issuer>() {};


public Issuers(String apiKey, String endpoint, RequestExecutor requestExecutor) {
super(apiKey,requestExecutor,SINGLE_TYPE,PAGE_TYPE,endpoint,"issuers");
}

public ResponseOrError<Page<Issuer>> all(Optional<Integer> count, Optional<Integer> offset) throws IOException, URISyntaxException {
URIBuilder builder = new URIBuilder(url())
.setParameter("count", String.valueOf(count.orElse(10)))
.setParameter("offset", String.valueOf(offset.orElse(0)));

HttpGet httpGet = new HttpGet(builder.build());
return requestPage(httpGet);
}

public ResponseOrError<Issuer> get(String id) throws IOException {
HttpGet httpGet = new HttpGet(url(id));
return requestSingle(httpGet);
}
}
}
44 changes: 44 additions & 0 deletions src/main/java/nl/stil4m/mollie/concepts/ListAll.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package nl.stil4m.mollie.concepts;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Optional;

import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;

import nl.stil4m.mollie.ResponseOrError;
import nl.stil4m.mollie.domain.Page;
import nl.stil4m.mollie.domain.Payment;

public interface ListAll<T extends Object> extends Concept<T> {

default ResponseOrError<Page<T>> all(Optional<Integer> count, Optional<Integer> offset) throws IOException, URISyntaxException {
return list(count,offset);
}

default ResponseOrError<Page<T>> list(Optional<Integer> count, Optional<Integer> offset) throws IOException, URISyntaxException {
URIBuilder builder = new URIBuilder(url())
.setParameter("count", String.valueOf(count.orElse(10)))
.setParameter("offset", String.valueOf(offset.orElse(0)));

HttpGet httpGet = new HttpGet(builder.build());
return requestPage(httpGet);
}

default ResponseOrError<Page<T>> next(Page<Payment> page) throws IOException {
if (!page.getLinks().getNext().isPresent()) {
throw new IllegalArgumentException("Page does not have next");
}
HttpGet httpGet = new HttpGet(page.getLinks().getNext().get());
return requestPage(httpGet);
}

default ResponseOrError<Page<T>> previous(Page<Payment> page) throws IOException {
if (!page.getLinks().getPrevious().isPresent()) {
throw new IllegalArgumentException("Page does not have next");
}
HttpGet httpGet = new HttpGet(page.getLinks().getPrevious().get());
return requestPage(httpGet);
}
}
Loading

0 comments on commit 9eddc7d

Please sign in to comment.