|
| 1 | +package com.easypost.service; |
| 2 | + |
| 3 | +import java.util.Map; |
| 4 | +import java.util.function.Function; |
| 5 | + |
| 6 | +import com.easypost.exception.EasyPostException; |
| 7 | +import com.easypost.exception.General.EndOfPaginationError; |
| 8 | +import com.easypost.http.Requestor; |
| 9 | +import com.easypost.http.Requestor.RequestMethod; |
| 10 | +import com.easypost.model.Claim; |
| 11 | +import com.easypost.model.ClaimCollection; |
| 12 | + |
| 13 | +import lombok.SneakyThrows; |
| 14 | + |
| 15 | +public class ClaimService { |
| 16 | + private final EasyPostClient client; |
| 17 | + |
| 18 | + /** |
| 19 | + * ClaimService constructor. |
| 20 | + * |
| 21 | + * @param client The client object. |
| 22 | + */ |
| 23 | + ClaimService(EasyPostClient client) { |
| 24 | + this.client = client; |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Create a new claim object from a map of parameters. |
| 29 | + * |
| 30 | + * @param params Map of parameters. |
| 31 | + * @return Claim object |
| 32 | + * @throws EasyPostException when the request fails. |
| 33 | + */ |
| 34 | + public Claim create(final Map<String, Object> params) throws EasyPostException { |
| 35 | + String endpoint = "claims"; |
| 36 | + |
| 37 | + return Requestor.request(RequestMethod.POST, endpoint, params, Claim.class, client); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Retrieve an Claim from the API. |
| 42 | + * |
| 43 | + * @param id The ID of the Claim to retrieve. |
| 44 | + * @return Claim object |
| 45 | + * @throws EasyPostException when the request fails. |
| 46 | + */ |
| 47 | + public Claim retrieve(final String id) throws EasyPostException { |
| 48 | + String endpoint = "claims/" + id; |
| 49 | + |
| 50 | + return Requestor.request(RequestMethod.GET, endpoint, null, Claim.class, client); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Get a list of Claims. |
| 55 | + * |
| 56 | + * @param params a map of parameters |
| 57 | + * @return ClaimCollection object |
| 58 | + * @throws EasyPostException when the request fails. |
| 59 | + */ |
| 60 | + public ClaimCollection all(final Map<String, Object> params) throws EasyPostException { |
| 61 | + String type = (String) params.get("type"); |
| 62 | + String status = (String) params.get("status"); |
| 63 | + params.remove(type); |
| 64 | + params.remove(status); |
| 65 | + String endpoint = "claims"; |
| 66 | + |
| 67 | + ClaimCollection claimCollection = |
| 68 | + Requestor.request(RequestMethod.GET, endpoint, params, ClaimCollection.class, client); |
| 69 | + claimCollection.setType(type); |
| 70 | + claimCollection.setType(status); |
| 71 | + |
| 72 | + return claimCollection; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Cancel an Claim from the API. |
| 77 | + * |
| 78 | + * @param id The ID of the Claim to cancel. |
| 79 | + * @return Claim object |
| 80 | + * @throws EasyPostException when the request fails. |
| 81 | + */ |
| 82 | + public Claim cancel(final String id) throws EasyPostException { |
| 83 | + String endpoint = String.format("claims/%s/cancel", id); |
| 84 | + |
| 85 | + return Requestor.request(RequestMethod.POST, endpoint, null, Claim.class, client); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Get the next page of an ClaimCollection. |
| 90 | + * |
| 91 | + * @param collection ClaimCollection to get next page of. |
| 92 | + * @return ClaimCollection object. |
| 93 | + * @throws EndOfPaginationError when there are no more pages to retrieve. |
| 94 | + */ |
| 95 | + public ClaimCollection getNextPage(ClaimCollection collection) throws EndOfPaginationError { |
| 96 | + return getNextPage(collection, null); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Get the next page of an ClaimCollection. |
| 101 | + * |
| 102 | + * @param collection ClaimCollection to get next page of. |
| 103 | + * @param pageSize The number of results to return on the next page. |
| 104 | + * @return ClaimCollection object. |
| 105 | + * @throws EndOfPaginationError when there are no more pages to retrieve. |
| 106 | + */ |
| 107 | + public ClaimCollection getNextPage( |
| 108 | + ClaimCollection collection, Integer pageSize) throws EndOfPaginationError { |
| 109 | + return collection.getNextPage(new Function<Map<String, Object>, ClaimCollection>() { |
| 110 | + @Override @SneakyThrows |
| 111 | + public ClaimCollection apply(Map<String, Object> parameters) { |
| 112 | + return all(parameters); |
| 113 | + } |
| 114 | + }, collection.getClaims(), pageSize); |
| 115 | + } |
| 116 | +} |
0 commit comments