forked from stil4m/mollie-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extracted repetitive code for List/Get/Create/Update/Delete into inte…
…rfaces
- Loading branch information
Showing
16 changed files
with
177 additions
and
277 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
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; | ||
} |
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,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
33
src/main/java/nl/stil4m/mollie/concepts/CustomerPayments.java
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 |
---|---|---|
@@ -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"); | ||
} | ||
} |
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 |
---|---|---|
@@ -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); | ||
} | ||
} |
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,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); | ||
} | ||
} |
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,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); | ||
} | ||
} |
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 |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
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,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); | ||
} | ||
} |
Oops, something went wrong.