This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
481 additions
and
7 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
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,22 +1,71 @@ | ||
package br.com.moip.api; | ||
|
||
import br.com.moip.Client; | ||
import br.com.moip.api.filter.Filters; | ||
import br.com.moip.api.filter.Pagination; | ||
import br.com.moip.request.OrderRequest; | ||
import br.com.moip.resource.Order; | ||
import br.com.moip.response.OrderListResponse; | ||
import br.com.moip.util.QueryStringFactory; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class OrderAPI { | ||
|
||
private final Client client; | ||
|
||
private static final String PATH = "/v2/orders"; | ||
|
||
public OrderAPI(final Client client) { | ||
this.client = client; | ||
} | ||
|
||
public Order create(final OrderRequest order) { | ||
return client.post("/v2/orders", order, Order.class); | ||
return client.post(PATH, order, Order.class); | ||
} | ||
|
||
public Order get(final String id) { | ||
return client.get("/v2/orders/" + id, Order.class); | ||
return client.get(String.format("%s/%s", PATH, id), Order.class); | ||
} | ||
|
||
public OrderListResponse list() { | ||
return client.get(PATH, OrderListResponse.class); | ||
} | ||
|
||
public OrderListResponse list(final Pagination pagination) { | ||
String path = new QueryStringFactory(PATH, pagination, null, null).generate(); | ||
|
||
return client.get(path, OrderListResponse.class); | ||
} | ||
|
||
public OrderListResponse list(final Filters filters) { | ||
return client.get(new QueryStringFactory(PATH, null, filters, null).generate(), OrderListResponse.class); | ||
} | ||
|
||
public OrderListResponse list(final String queryParam) { | ||
return client.get(new QueryStringFactory(PATH, null, null, hashParams(queryParam)).generate(), OrderListResponse.class); | ||
} | ||
|
||
public OrderListResponse list(final Pagination pagination, final Filters filters) { | ||
return client.get(new QueryStringFactory(PATH, pagination, filters, null).generate(), OrderListResponse.class); | ||
} | ||
|
||
public OrderListResponse list(final Pagination pagination, final String queryParam) { | ||
return client.get(new QueryStringFactory(PATH, pagination, null, hashParams(queryParam)).generate(), OrderListResponse.class); | ||
} | ||
|
||
public OrderListResponse list(final Filters filters, final String queryParam) { | ||
return client.get(new QueryStringFactory(PATH, null, filters, hashParams(queryParam)).generate(), OrderListResponse.class); | ||
} | ||
|
||
public OrderListResponse list(final Pagination pagination, final Filters filters, final String qParam) { | ||
return client.get(new QueryStringFactory(PATH, pagination, filters, hashParams(qParam)).generate(), OrderListResponse.class); | ||
} | ||
|
||
private Map<String, String> hashParams(final String queryParam) { | ||
return new HashMap<String, String>() {{ | ||
put("q", queryParam); | ||
}}; | ||
} | ||
} |
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,47 @@ | ||
package br.com.moip.api.filter; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Filters { | ||
|
||
private List<String> filters = new ArrayList<>(); | ||
|
||
public Filters greaterThan(String field, String value) { | ||
filters.add(String.format("%s::gt(%s)", field, value)); | ||
|
||
return this; | ||
} | ||
|
||
public Filters greaterThanOrEqual(String field, String value) { | ||
filters.add(String.format("%s::ge(%s)", field, value)); | ||
|
||
return this; | ||
} | ||
|
||
public Filters lessThan(String field, String value) { | ||
filters.add(String.format("%s::lt(%s)", field, value)); | ||
|
||
return this; | ||
} | ||
|
||
public Filters between(String field, String value1, String value2) { | ||
filters.add(String.format("%s::bt(%s,%s)", field, value1, value2)); | ||
|
||
return this; | ||
} | ||
|
||
public Filters in (String field, List<String> values) { | ||
filters.add(String.format("%s::in(%s)", field, StringUtils.join(values, ","))); | ||
|
||
return this; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return StringUtils.join(filters, "|"); | ||
} | ||
|
||
} |
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,45 @@ | ||
package br.com.moip.response; | ||
|
||
import br.com.moip.resource.Links; | ||
import br.com.moip.resource.Order; | ||
import br.com.moip.resource.invoice.Summary; | ||
|
||
import java.util.List; | ||
|
||
public class OrderListResponse { | ||
|
||
public static final String NEXT = "next"; | ||
public static final String PREVIOUS = "previous"; | ||
|
||
private List<Order> orders; | ||
private Summary summary; | ||
private Links links; | ||
|
||
public String next() { | ||
return links.getLinks().get(NEXT).toString(); | ||
} | ||
|
||
public String previous() { | ||
return links.getLinks().get(PREVIOUS).toString(); | ||
} | ||
|
||
public List<Order> getOrders() { | ||
return orders; | ||
} | ||
|
||
public Summary getSummary() { | ||
return summary; | ||
} | ||
|
||
public void setSummary(Summary summary) { | ||
this.summary = summary; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new StringBuilder("OrderListResponse{") | ||
.append("summary=").append(summary) | ||
.append(", orders=").append(orders) | ||
.append('}').toString(); | ||
} | ||
} |
Oops, something went wrong.