-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathTrackerService.java
More file actions
131 lines (112 loc) · 4.63 KB
/
TrackerService.java
File metadata and controls
131 lines (112 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package com.easypost.service;
import com.easypost.exception.EasyPostException;
import com.easypost.exception.General.EndOfPaginationError;
import com.easypost.http.Requestor;
import com.easypost.http.Requestor.RequestMethod;
import com.easypost.model.TrackerCollection;
import com.easypost.model.Tracker;
import com.easypost.utils.InternalUtilities;
import lombok.SneakyThrows;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
public class TrackerService {
private final EasyPostClient client;
/**
* TrackerService constructor.
*
* @param client The client object.
*/
TrackerService(EasyPostClient client) {
this.client = client;
}
/**
* Create a new Tracker object using a map of parameters.
*
* @param params Map of parameters used to create the Tracker.
* @return Tracker object.
* @throws EasyPostException when the request fails.
*/
public Tracker create(final Map<String, Object> params) throws EasyPostException {
Map<String, Object> wrappedParams = new HashMap<String, Object>();
wrappedParams.put("tracker", params);
String endpoint = "trackers";
return Requestor.request(RequestMethod.POST, endpoint, wrappedParams, Tracker.class, client);
}
/**
* Retrieve a Tracker object from the API.
*
* @param id ID of the Tracker to retrieve.
* @return Tracker object.
* @throws EasyPostException when the request fails.
*/
public Tracker retrieve(final String id) throws EasyPostException {
String endpoint = "trackers/" + id;
return Requestor.request(RequestMethod.GET, endpoint, null, Tracker.class, client);
}
/**
* Get a list of all Tracker objects.
*
* @param params Map of parameters used to filter the list of Trackers.
* @return TrackerCollection object.
* @throws EasyPostException when the request fails.
*/
public TrackerCollection all(final Map<String, Object> params) throws EasyPostException {
String endpoint = "trackers";
TrackerCollection trackerCollection =
Requestor.request(RequestMethod.GET, endpoint, params, TrackerCollection.class, client);
// we store the params in the collection so that we can use them to get the next page
trackerCollection.setTrackingCode(InternalUtilities.getOrDefault(params, "tracking_code", null));
trackerCollection.setCarrier(InternalUtilities.getOrDefault(params, "carrier", null));
return trackerCollection;
}
/**
* Get the next page of an TrackerCollection.
*
* @param collection TrackerCollection to get next page of.
* @return TrackerCollection object.
* @throws EndOfPaginationError when there are no more pages to retrieve.
*/
public TrackerCollection getNextPage(TrackerCollection collection) throws EndOfPaginationError {
return getNextPage(collection, null);
}
/**
* Get the next page of an TrackerCollection.
*
* @param collection TrackerCollection to get next page of.
* @param pageSize The number of results to return on the next page.
* @return TrackerCollection object.
* @throws EndOfPaginationError when there are no more pages to retrieve.
*/
public TrackerCollection getNextPage(TrackerCollection collection, Integer pageSize) throws EndOfPaginationError {
return collection.getNextPage(new Function<Map<String, Object>, TrackerCollection>() {
@Override @SneakyThrows
public TrackerCollection apply(Map<String, Object> parameters) {
return all(parameters);
}
}, collection.getTrackers(), pageSize);
}
/**
* Retrieve a batch of Tracker objects.
*
* @param params Map of parameters used to filter the list of Trackers.
* @return TrackerCollection object.
* @throws EasyPostException when the request fails.
*/
public TrackerCollection retrieveBatch(final Map<String, Object> params) throws EasyPostException {
String endpoint = "trackers/batch";
TrackerCollection trackerCollection =
Requestor.request(RequestMethod.POST, endpoint, params, TrackerCollection.class, client);
return trackerCollection;
}
/**
* Delete a Tracker object.
*
* @param id ID of the Tracker to delete.
* @throws EasyPostException when the request fails.
*/
public void delete(final String id) throws EasyPostException {
String endpoint = String.format("trackers/%s", id);
Requestor.request(RequestMethod.DELETE, endpoint, null, Tracker.class, client);
}
}