Skip to content

Commit 118e766

Browse files
committed
Add Assets API and ContentType to API calls
1 parent 625ef79 commit 118e766

11 files changed

Lines changed: 2087 additions & 8 deletions

src/main/java/com/xero/api/OAuthRequestResource.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ public OAuthRequestResource(Config config, SignerFactory signerFactory, String
8383
this(config, signerFactory, resource, method, params);
8484
this.body = body;
8585
}
86+
87+
public OAuthRequestResource(Config config, SignerFactory signerFactory, String resource, String method, String body, Map<? extends String, ?> params, String accept, String contentType) {
88+
this(config, signerFactory, resource, method, params);
89+
this.accept = accept;
90+
this.contentType = contentType;
91+
this.body = body;
92+
}
8693

8794
public OAuthRequestResource(Config config, SignerFactory signerFactory, String resource, String method, String body, Map<? extends String, ?> params, String accept) {
8895
this(config, signerFactory, resource, method, params);
@@ -190,6 +197,7 @@ public String getFileName()
190197

191198
public final Map<String, String> execute() throws IOException,XeroApiException {
192199
CloseableHttpClient httpclient =null;
200+
193201
httpclient = new XeroHttpContext(config,this.accept,this.contentType,this.ifModifiedSince).getHttpClient();
194202

195203
if(this.resource.indexOf("https") ==-1) {
Lines changed: 361 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,361 @@
1+
package com.xero.api.client;
2+
3+
import com.xero.api.ApiClient;
4+
5+
import com.xero.models.assets.Asset;
6+
import com.xero.models.assets.AssetType;
7+
import com.xero.models.assets.Assets;
8+
import com.xero.models.assets.Setting;
9+
import java.util.UUID;
10+
11+
import com.fasterxml.jackson.core.type.TypeReference;
12+
import com.xero.api.exception.XeroExceptionHandler;
13+
import com.xero.model.*;
14+
import com.xero.api.*;
15+
16+
import org.apache.logging.log4j.LogManager;
17+
import org.apache.logging.log4j.Logger;
18+
19+
import java.io.IOException;
20+
import java.text.DateFormat;
21+
import java.text.SimpleDateFormat;
22+
import java.util.Date;
23+
import java.util.HashMap;
24+
import java.util.List;
25+
import java.util.Map;
26+
import java.util.TimeZone;
27+
import java.util.regex.Pattern;
28+
29+
import javax.ws.rs.core.UriBuilder;
30+
31+
public class AssetApi {
32+
private ApiClient apiClient;
33+
private XeroExceptionHandler xeroExceptionHandler;
34+
private Config config;
35+
private SignerFactory signerFactory;
36+
private String token = null;
37+
private String tokenSecret = null;
38+
final static Logger logger = LogManager.getLogger(XeroClient.class);
39+
protected static final DateFormat utcFormatter;
40+
41+
static {
42+
utcFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
43+
utcFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
44+
}
45+
46+
protected static final Pattern MESSAGE_PATTERN = Pattern.compile("<Message>(.*)</Message>");
47+
protected final ObjectFactory objFactory = new ObjectFactory();
48+
49+
public AssetApi() {
50+
this(JsonConfig.getInstance());
51+
this.xeroExceptionHandler = new XeroExceptionHandler();
52+
}
53+
54+
public AssetApi(Config config) {
55+
this(config, new ConfigBasedSignerFactory(config));
56+
this.xeroExceptionHandler = new XeroExceptionHandler();
57+
}
58+
59+
public AssetApi(Config config, SignerFactory signerFactory) {
60+
this.config = config;
61+
this.signerFactory = signerFactory;
62+
this.xeroExceptionHandler = new XeroExceptionHandler();
63+
}
64+
65+
public AssetApi(ApiClient apiClient) {
66+
this(JsonConfig.getInstance());
67+
this.xeroExceptionHandler = new XeroExceptionHandler();
68+
this.apiClient = apiClient;
69+
}
70+
71+
public ApiClient getApiClient() {
72+
return apiClient;
73+
}
74+
75+
public void setApiClient(ApiClient apiClient) {
76+
this.apiClient = apiClient;
77+
}
78+
79+
public void setOAuthToken(String token, String tokenSecret) {
80+
this.token = token;
81+
this.tokenSecret = tokenSecret;
82+
}
83+
84+
protected String POST(String url, String body, Map<String, String> params, Date modifiedAfter) throws IOException {
85+
86+
OAuthRequestResource req = new OAuthRequestResource(
87+
config,
88+
signerFactory,
89+
url,
90+
"POST",
91+
body,
92+
params,
93+
"application/json",
94+
"application/json");
95+
96+
req.setToken(token);
97+
req.setTokenSecret(tokenSecret);
98+
99+
try {
100+
Map<String, String> resp = req.execute();
101+
Object r = resp.get("content");
102+
return r.toString();
103+
} catch (IOException ioe) {
104+
throw xeroExceptionHandler.convertException(ioe);
105+
}
106+
}
107+
108+
protected String PUT(String url, String body, Map<String, String> params, Date modifiedAfter) throws IOException {
109+
110+
OAuthRequestResource req = new OAuthRequestResource(
111+
config,
112+
signerFactory,
113+
url,
114+
"PUT",
115+
body,
116+
params,
117+
"application/json",
118+
"application/json");
119+
120+
req.setToken(token);
121+
req.setTokenSecret(tokenSecret);
122+
123+
try {
124+
Map<String, String> resp = req.execute();
125+
Object r = resp.get("content");
126+
return r.toString();
127+
} catch (IOException ioe) {
128+
throw xeroExceptionHandler.convertException(ioe);
129+
}
130+
}
131+
132+
protected String DELETE(String url, String body, Map<String, String> params, Date modifiedAfter) throws IOException {
133+
134+
OAuthRequestResource req = new OAuthRequestResource(
135+
config,
136+
signerFactory,
137+
url,
138+
"DELETE",
139+
body,
140+
params,
141+
"application/json",
142+
"application/json");
143+
144+
req.setToken(token);
145+
req.setTokenSecret(tokenSecret);
146+
147+
try {
148+
Map<String, String> resp = req.execute();
149+
Object r = resp.get("content");
150+
return r.toString();
151+
} catch (IOException ioe) {
152+
throw xeroExceptionHandler.convertException(ioe);
153+
}
154+
}
155+
156+
protected String GET(String url, String body, Map<String, String> params, Date modifiedAfter) throws IOException {
157+
158+
OAuthRequestResource req = new OAuthRequestResource(
159+
config,
160+
signerFactory,
161+
url,
162+
"GET",
163+
null,
164+
params,
165+
"application/json",
166+
"application/json");
167+
168+
req.setToken(token);
169+
req.setTokenSecret(tokenSecret);
170+
171+
if (modifiedAfter != null) {
172+
req.setIfModifiedSince(modifiedAfter);
173+
}
174+
175+
try {
176+
Map<String, String> resp = req.execute();
177+
Object r = resp.get("content");
178+
return r.toString();
179+
} catch (IOException ioe) {
180+
throw xeroExceptionHandler.convertException(ioe);
181+
}
182+
}
183+
184+
/**
185+
* adds a fixed asset
186+
* Adds an asset to the system
187+
* <p><b>200</b> - search results matching criteria
188+
* <p><b>400</b> - invalid input, object invalid
189+
* @param asset Fixed asset to add
190+
* @return Asset
191+
* @throws IOException if an error occurs while attempting to invoke the API
192+
**/
193+
public Asset createAsset(Asset asset, Map<String, String> params) throws IOException {
194+
195+
try {
196+
UriBuilder uriBuilder = UriBuilder.fromUri(apiClient.getBasePath() + "/Assets");
197+
String url = uriBuilder.build().toString();
198+
199+
String body = null;
200+
Date modifiedAfter = null;
201+
202+
ApiClient apiClient = new ApiClient();
203+
body = apiClient.getObjectMapper().writeValueAsString(asset);
204+
String response = this.POST(url, body, params, modifiedAfter);
205+
206+
TypeReference<Asset> typeRef = new TypeReference<Asset>() {};
207+
return apiClient.getObjectMapper().readValue(response, typeRef);
208+
209+
} catch (IOException e) {
210+
throw xeroExceptionHandler.convertException(e);
211+
}
212+
}
213+
/**
214+
* adds a fixed asset type
215+
* Adds an fixed asset type to the system
216+
* <p><b>200</b> - search results matching criteria
217+
* <p><b>400</b> - invalid input, object invalid
218+
* <p><b>409</b> - an existing type already exists
219+
* @param assetType Asset type to add
220+
* @return AssetType
221+
* @throws IOException if an error occurs while attempting to invoke the API
222+
**/
223+
public AssetType createAssetType(AssetType assetType, Map<String, String> params) throws IOException {
224+
225+
try {
226+
UriBuilder uriBuilder = UriBuilder.fromUri(apiClient.getBasePath() + "/AssetTypes");
227+
String url = uriBuilder.build().toString();
228+
229+
String body = null;
230+
Date modifiedAfter = null;
231+
232+
ApiClient apiClient = new ApiClient();
233+
body = apiClient.getObjectMapper().writeValueAsString(assetType);
234+
String response = this.POST(url, body, params, modifiedAfter);
235+
236+
TypeReference<AssetType> typeRef = new TypeReference<AssetType>() {};
237+
return apiClient.getObjectMapper().readValue(response, typeRef);
238+
239+
} catch (IOException e) {
240+
throw xeroExceptionHandler.convertException(e);
241+
}
242+
}
243+
/**
244+
* retrieves fixed asset by id
245+
* By passing in the appropriate asset id, you can search for a specific fixed asset in the system
246+
* <p><b>200</b> - search results matching criteria
247+
* <p><b>400</b> - bad input parameter
248+
* @param id fixed asset id for single object
249+
* @return Asset
250+
* @throws IOException if an error occurs while attempting to invoke the API
251+
**/
252+
public Asset getAssetById(UUID id, Map<String, String> params) throws IOException {
253+
254+
try {
255+
// create a map of path variables
256+
final Map<String, String> uriVariables = new HashMap<String, String>();
257+
uriVariables.put("id", id.toString());
258+
UriBuilder uriBuilder = UriBuilder.fromUri(apiClient.getBasePath() + "/Assets/{id}");
259+
String url = uriBuilder.buildFromMap(uriVariables).toString();
260+
261+
String body = null;
262+
Date modifiedAfter = null;
263+
264+
ApiClient apiClient = new ApiClient();
265+
String response = this.GET(url, body, params, modifiedAfter);
266+
267+
TypeReference<Asset> typeRef = new TypeReference<Asset>() {};
268+
return apiClient.getObjectMapper().readValue(response, typeRef);
269+
270+
} catch (IOException e) {
271+
throw xeroExceptionHandler.convertException(e);
272+
}
273+
}
274+
/**
275+
* searches fixed asset settings
276+
* By passing in the appropriate options, you can search for available fixed asset types in the system
277+
* <p><b>200</b> - search results matching criteria
278+
* <p><b>400</b> - bad input parameter
279+
* @return Setting
280+
* @throws IOException if an error occurs while attempting to invoke the API
281+
**/
282+
public Setting getAssetSettings(Map<String, String> params) throws IOException {
283+
284+
try {
285+
UriBuilder uriBuilder = UriBuilder.fromUri(apiClient.getBasePath() + "/Settings");
286+
String url = uriBuilder.build().toString();
287+
288+
String body = null;
289+
Date modifiedAfter = null;
290+
291+
ApiClient apiClient = new ApiClient();
292+
String response = this.GET(url, body, params, modifiedAfter);
293+
294+
TypeReference<Setting> typeRef = new TypeReference<Setting>() {};
295+
return apiClient.getObjectMapper().readValue(response, typeRef);
296+
297+
} catch (IOException e) {
298+
throw xeroExceptionHandler.convertException(e);
299+
}
300+
}
301+
/**
302+
* searches fixed asset types
303+
* By passing in the appropriate options, you can search for available fixed asset types in the system
304+
* <p><b>200</b> - search results matching criteria
305+
* <p><b>400</b> - bad input parameter
306+
* @return List&lt;AssetType&gt;
307+
* @throws IOException if an error occurs while attempting to invoke the API
308+
**/
309+
public List<AssetType> getAssetTypes(Map<String, String> params) throws IOException {
310+
311+
try {
312+
UriBuilder uriBuilder = UriBuilder.fromUri(apiClient.getBasePath() + "/AssetTypes");
313+
String url = uriBuilder.build().toString();
314+
315+
String body = null;
316+
Date modifiedAfter = null;
317+
318+
ApiClient apiClient = new ApiClient();
319+
String response = this.GET(url, body, params, modifiedAfter);
320+
321+
TypeReference<List<AssetType>> typeRef = new TypeReference<List<AssetType>>() {};
322+
return apiClient.getObjectMapper().readValue(response, typeRef);
323+
324+
} catch (IOException e) {
325+
throw xeroExceptionHandler.convertException(e);
326+
}
327+
}
328+
/**
329+
* searches fixed asset
330+
* By passing in the appropriate options, you can search for available fixed asset in the system
331+
* <p><b>200</b> - search results matching criteria
332+
* <p><b>400</b> - bad input parameter
333+
* @param status Required when retrieving a collection of assets. See Asset Status Codes
334+
* @param page Results are paged. This specifies which page of the results to return. The default page is 1.
335+
* @param pageSize The number of records returned per page. By default the number of records returned is 10.
336+
* @param orderBy Requests can be ordered by AssetType, AssetName, AssetNumber, PurchaseDate and PurchasePrice. If the asset status is DISPOSED it also allows DisposalDate and DisposalPrice.
337+
* @param sortDirection ASC or DESC
338+
* @param filterBy A string that can be used to filter the list to only return assets containing the text. Checks it against the AssetName, AssetNumber, Description and AssetTypeName fields.
339+
* @return Assets
340+
* @throws IOException if an error occurs while attempting to invoke the API
341+
**/
342+
public Assets getAssets(Map<String, String> params) throws IOException {
343+
344+
try {
345+
UriBuilder uriBuilder = UriBuilder.fromUri(apiClient.getBasePath() + "/Assets");
346+
String url = uriBuilder.build().toString();
347+
348+
String body = null;
349+
Date modifiedAfter = null;
350+
351+
ApiClient apiClient = new ApiClient();
352+
String response = this.GET(url, body, params, modifiedAfter);
353+
354+
TypeReference<Assets> typeRef = new TypeReference<Assets>() {};
355+
return apiClient.getObjectMapper().readValue(response, typeRef);
356+
357+
} catch (IOException e) {
358+
throw xeroExceptionHandler.convertException(e);
359+
}
360+
}
361+
}

0 commit comments

Comments
 (0)