Skip to content

Commit 1a8ea8d

Browse files
committedOct 27, 2017
Added support of Setting the Decimal Place
UnitDP default is 2, but you can pass in 4 when you get, create or update BankTransactions, Invoices or Items. You can set the DecimalPlace in the config.json -but this does not automatically apply to your calls, you will still need to pass it using config.getDecimalPlace() - I think a global 2 or 4 DP is not flexible for those who may want more granular control.
1 parent 68e1e04 commit 1a8ea8d

File tree

4 files changed

+195
-16
lines changed

4 files changed

+195
-16
lines changed
 

‎example/src/main/resources/config.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
"ConsumerSecret" : "——YOUR-CONSUMER-SECRET—",
77
"ApiBaseUrl" : "https://api.xero.com",
88
"ApiEndpointPath" : "/api.xro/2.0/",
9+
"FilesEndpointPath" : "/files.xro/1.0/",
910
"RequestTokenPath": "/oauth/RequestToken",
1011
"AuthenticateUrl" : "/oauth/Authorize",
1112
"AccessTokenPath" : "/oauth/AccessToken",
1213
"CallbackBaseUrl" : "https://localhost",
13-
"CallbackPath" : "/CallbackServlet"
14+
"CallbackPath" : "/CallbackServlet",
15+
"DecimalPlaces" : "2"
1416
}

‎src/main/java/com/xero/api/Config.java

+13-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public interface Config {
1414

1515
String getApiUrl();
1616

17+
String getFilesUrl();
18+
1719
String getRequestTokenUrl();
1820

1921
String getAuthorizeUrl();
@@ -27,18 +29,18 @@ public interface Config {
2729
String getRedirectUri();
2830

2931
String getProxyHost();
30-
32+
3133
long getProxyPort();
3234

3335
boolean getProxyHttpsEnabled();
3436

3537
int getConnectTimeout();
3638

3739
int getReadTimeout();
38-
39-
// in seconds
40-
void setConnectTimeout(int connectTimeout);
41-
void setReadTimeout(int readTimeout);
40+
41+
String getDecimalPlaces();
42+
43+
// SETTERS
4244

4345
void setConsumerKey(String consumerKey);
4446

@@ -48,4 +50,10 @@ public interface Config {
4850

4951
void setAuthCallBackUrl(String authCallbackUrl);
5052

53+
void setConnectTimeout(int connectTimeout);
54+
55+
void setReadTimeout(int readTimeout);
56+
57+
void setDecimalPlaces(String decimalPlaces);
58+
5159
}

‎src/main/java/com/xero/api/JsonConfig.java

+39-9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class JsonConfig implements Config {
1919
private String CONSUMER_SECRET;
2020
private String API_BASE_URL = "https://api.xero.com";
2121
private String API_ENDPOINT_URL = "https://api.xero.com/api.xro/2.0/";
22+
private String FILES_ENDPOINT_URL = "https://api.xero.com/files.xro/1.0/";
2223
private String REQUEST_TOKEN_URL = "https://api.xero.com/oauth/RequestToken";
2324
private String AUTHENTICATE_URL = "https://api.xero.com/oauth/Authorize";
2425
private String ACCESS_TOKEN_URL = "https://api.xero.com/oauth/AccessToken";
@@ -29,9 +30,10 @@ public class JsonConfig implements Config {
2930
private String PROXY_HOST;
3031
private long PROXY_PORT = 80;
3132
private boolean PROXY_HTTPS_ENABLED = false;
32-
3333
private int CONNECT_TIMEOUT = 60;
3434
private int READ_TIMEOUT = 60;
35+
private String DECIMAL_PLACES = null;
36+
3537
private String configFile;
3638

3739
private static Config instance = null;
@@ -79,6 +81,11 @@ public String getApiUrl() {
7981
return API_ENDPOINT_URL;
8082
}
8183

84+
@Override
85+
public String getFilesUrl() {
86+
return FILES_ENDPOINT_URL;
87+
}
88+
8289
@Override
8390
public String getRequestTokenUrl() {
8491
return REQUEST_TOKEN_URL;
@@ -137,16 +144,12 @@ public int getReadTimeout() {
137144
}
138145

139146
@Override
140-
public void setConnectTimeout(int connectTimeout) {
141-
// in seconds
142-
CONNECT_TIMEOUT = connectTimeout;
147+
public String getDecimalPlaces(){
148+
// 2 or 4
149+
return DECIMAL_PLACES;
143150
}
144151

145-
@Override
146-
public void setReadTimeout(int readTimeout) {
147-
// in seconds
148-
READ_TIMEOUT = readTimeout;
149-
}
152+
// SETTERS
150153

151154
@Override
152155
public void setConsumerKey(String consumerKey) {
@@ -168,6 +171,24 @@ public void setAuthCallBackUrl(String authCallbackUrl) {
168171
AUTH_CALLBACK_URL = authCallbackUrl;
169172
}
170173

174+
@Override
175+
public void setConnectTimeout(int connectTimeout) {
176+
// in seconds
177+
CONNECT_TIMEOUT = connectTimeout;
178+
}
179+
180+
@Override
181+
public void setReadTimeout(int readTimeout) {
182+
// in seconds
183+
READ_TIMEOUT = readTimeout;
184+
}
185+
186+
@Override
187+
public void setDecimalPlaces(String decimalPlaces) {
188+
// 2 or 4
189+
DECIMAL_PLACES = decimalPlaces;
190+
}
191+
171192
private void load() {
172193
InputStream inputStream = JsonConfig.class.getResourceAsStream("/" + configFile);
173194
if (inputStream == null) {
@@ -216,6 +237,11 @@ private void load() {
216237
API_ENDPOINT_URL = API_BASE_URL + endpointPath;
217238
}
218239

240+
if (jsonObject.containsKey("FilesEndpointPath")) {
241+
String filesEndpointPath = (String) jsonObject.get("FilesEndpointPath");
242+
FILES_ENDPOINT_URL = API_BASE_URL + filesEndpointPath;
243+
}
244+
219245
if (jsonObject.containsKey("RequestTokenPath")) {
220246
String requestPath = (String) jsonObject.get("RequestTokenPath");
221247
REQUEST_TOKEN_URL = API_BASE_URL + requestPath;
@@ -255,5 +281,9 @@ private void load() {
255281
PROXY_HTTPS_ENABLED = (boolean) jsonObject.get("ProxyHttpsEnabled");
256282
}
257283
}
284+
285+
if (jsonObject.containsKey("DecimalPlaces")) {
286+
DECIMAL_PLACES = (String) jsonObject.get("DecimalPlaces");
287+
}
258288
}
259289
}

‎src/main/java/com/xero/api/XeroClient.java

+140-1
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,20 @@ protected Response post(String endPoint, JAXBElement<?> object) throws IOExcepti
340340
throw convertException(ioe);
341341
}
342342
}
343+
344+
protected Response post(String endPoint, JAXBElement<?> object, Map<String, String> params) throws IOException {
345+
String contents = marshallRequest(object);
346+
OAuthRequestResource req = new OAuthRequestResource(config, signerFactory, endPoint, "POST", contents, params);
347+
req.setToken(token);
348+
req.setTokenSecret(tokenSecret);
349+
350+
try {
351+
HttpResponse resp = req.execute();
352+
return unmarshallResponse(resp.parseAsString(), Response.class);
353+
} catch (IOException ioe) {
354+
throw convertException(ioe);
355+
}
356+
}
343357

344358
protected Response delete(String endPoint) throws IOException {
345359
HttpResponse resp = null;
@@ -482,12 +496,39 @@ public List<BankTransaction> getBankTransactions(Date modifiedAfter, String wher
482496
return responseObj.getBankTransactions().getBankTransaction();
483497
}
484498
}
499+
500+
501+
public List<BankTransaction> getBankTransactions(Date modifiedAfter, String where, String order, String page, String unitdp)
502+
throws IOException {
503+
Map<String, String> params = new HashMap<>();
504+
addToMapIfNotNull(params, "Where", where);
505+
addToMapIfNotNull(params, "order", order);
506+
addToMapIfNotNull(params, "page", page);
507+
addToMapIfNotNull(params, "unitdp", unitdp);
508+
509+
Response responseObj = get("BankTransactions", modifiedAfter, params);
510+
if (responseObj.getBankTransactions() == null) {
511+
ArrayOfBankTransaction array = new ArrayOfBankTransaction();
512+
return array.getBankTransaction();
513+
} else {
514+
return responseObj.getBankTransactions().getBankTransaction();
515+
}
516+
}
485517

486518
public List<BankTransaction> createBankTransactions(List<BankTransaction> bankTransactions) throws IOException {
487519
ArrayOfBankTransaction array = new ArrayOfBankTransaction();
488520
array.getBankTransaction().addAll(bankTransactions);
489521
return put("BankTransactions", objFactory.createBankTransactions(array)).getBankTransactions().getBankTransaction();
490522
}
523+
524+
public List<BankTransaction> createBankTransactions(List<BankTransaction> bankTransactions,String unitdp) throws IOException {
525+
Map<String, String> params = new HashMap<>();
526+
addToMapIfNotNull(params, "unitdp", unitdp);
527+
528+
ArrayOfBankTransaction array = new ArrayOfBankTransaction();
529+
array.getBankTransaction().addAll(bankTransactions);
530+
return put("BankTransactions", objFactory.createBankTransactions(array),params).getBankTransactions().getBankTransaction();
531+
}
491532

492533
public List<BankTransaction> updateBankTransactions(List<BankTransaction> bankTransactions) throws IOException {
493534
ArrayOfBankTransaction array = new ArrayOfBankTransaction();
@@ -496,11 +537,28 @@ public List<BankTransaction> updateBankTransactions(List<BankTransaction> bankTr
496537
.getBankTransactions()
497538
.getBankTransaction();
498539
}
540+
541+
public List<BankTransaction> updateBankTransactions(List<BankTransaction> bankTransactions, String unitdp) throws IOException {
542+
Map<String, String> params = new HashMap<>();
543+
addToMapIfNotNull(params, "unitdp", unitdp);
544+
545+
ArrayOfBankTransaction array = new ArrayOfBankTransaction();
546+
array.getBankTransaction().addAll(bankTransactions);
547+
return post("BankTransactions", objFactory.createBankTransactions(array),params)
548+
.getBankTransactions()
549+
.getBankTransaction();
550+
}
499551

500552
public BankTransaction getBankTransaction(String id) throws IOException {
501553
return singleResult(get("BankTransactions/" + id).getBankTransactions().getBankTransaction());
502554
}
503-
555+
556+
public BankTransaction getBankTransaction(String id,String unitdp) throws IOException {
557+
Map<String, String> params = new HashMap<>();
558+
addToMapIfNotNull(params, "unitdp", unitdp);
559+
return singleResult(get("BankTransactions/" + id,null,params).getBankTransactions().getBankTransaction());
560+
}
561+
504562
//BANK TRANSFERS
505563
public List<BankTransfer> getBankTransfers() throws IOException {
506564
Response responseObj = get("BankTransfers");
@@ -875,22 +933,64 @@ public List<Invoice> getInvoices(Date modifiedAfter, String where, String order,
875933
return responseObj.getInvoices().getInvoice();
876934
}
877935
}
936+
937+
public List<Invoice> getInvoices(Date modifiedAfter, String where, String order, String page, String ids, String unitdp)
938+
throws IOException {
939+
Map<String, String> params = new HashMap<>();
940+
addToMapIfNotNull(params, "Where", where);
941+
addToMapIfNotNull(params, "order", order);
942+
addToMapIfNotNull(params, "page", page);
943+
addToMapIfNotNull(params, "Ids", ids);
944+
addToMapIfNotNull(params, "unitdp", unitdp);
945+
946+
Response responseObj = get("Invoices", modifiedAfter, params);
947+
if (responseObj.getInvoices() == null) {
948+
ArrayOfInvoice array = new ArrayOfInvoice();
949+
return array.getInvoice();
950+
} else {
951+
return responseObj.getInvoices().getInvoice();
952+
}
953+
}
878954

879955
public List<Invoice> createInvoices(List<Invoice> invoices) throws IOException {
880956
ArrayOfInvoice array = new ArrayOfInvoice();
881957
array.getInvoice().addAll(invoices);
882958
return put("Invoices", objFactory.createInvoices(array)).getInvoices().getInvoice();
883959
}
960+
961+
public List<Invoice> createInvoices(List<Invoice> invoices,String unitdp) throws IOException {
962+
Map<String, String> params = new HashMap<>();
963+
addToMapIfNotNull(params, "unitdp", unitdp);
964+
965+
ArrayOfInvoice array = new ArrayOfInvoice();
966+
array.getInvoice().addAll(invoices);
967+
return put("Invoices", objFactory.createInvoices(array),params).getInvoices().getInvoice();
968+
}
884969

885970
public List<Invoice> updateInvoice(List<Invoice> objects) throws IOException {
886971
ArrayOfInvoice array = new ArrayOfInvoice();
887972
array.getInvoice().addAll(objects);
888973
return post("Invoices", objFactory.createInvoices(array)).getInvoices().getInvoice();
889974
}
975+
976+
public List<Invoice> updateInvoice(List<Invoice> objects,String unitdp) throws IOException {
977+
Map<String, String> params = new HashMap<>();
978+
addToMapIfNotNull(params, "unitdp", unitdp);
979+
980+
ArrayOfInvoice array = new ArrayOfInvoice();
981+
array.getInvoice().addAll(objects);
982+
return post("Invoices", objFactory.createInvoices(array),params).getInvoices().getInvoice();
983+
}
890984

891985
public Invoice getInvoice(String id) throws IOException {
892986
return singleResult(get("Invoices/" + id).getInvoices().getInvoice());
893987
}
988+
989+
public Invoice getInvoice(String id, String unitdp) throws IOException {
990+
Map<String, String> params = new HashMap<>();
991+
addToMapIfNotNull(params, "unitdp", unitdp);
992+
return singleResult(get("Invoices/" + id, null,params).getInvoices().getInvoice());
993+
}
894994

895995
public String getInvoicePdf(String id, String dirPath) throws IOException {
896996
return getFile("Invoices/" + id, null, null, "application/pdf", dirPath);
@@ -935,23 +1035,62 @@ public List<Item> getItems(Date modifiedAfter, String where, String order) throw
9351035
return responseObj.getItems().getItem();
9361036
}
9371037
}
1038+
1039+
public List<Item> getItems(Date modifiedAfter, String where, String order, String unitdp) throws IOException {
1040+
Map<String, String> params = new HashMap<>();
1041+
addToMapIfNotNull(params, "Where", where);
1042+
addToMapIfNotNull(params, "order", order);
1043+
addToMapIfNotNull(params, "unitdp", unitdp);
1044+
1045+
Response responseObj = get("Items", modifiedAfter, params);
1046+
if (responseObj.getItems() == null) {
1047+
ArrayOfItem array = new ArrayOfItem();
1048+
return array.getItem();
1049+
} else {
1050+
return responseObj.getItems().getItem();
1051+
}
1052+
}
9381053

9391054
public List<Item> createItems(List<Item> objects) throws IOException {
9401055
ArrayOfItem array = new ArrayOfItem();
9411056
array.getItem().addAll(objects);
9421057
return put("Items", objFactory.createItems(array)).getItems().getItem();
9431058
}
1059+
1060+
public List<Item> createItems(List<Item> objects,String unitdp) throws IOException {
1061+
Map<String, String> params = new HashMap<>();
1062+
addToMapIfNotNull(params, "unitdp", unitdp);
1063+
1064+
ArrayOfItem array = new ArrayOfItem();
1065+
array.getItem().addAll(objects);
1066+
return put("Items", objFactory.createItems(array),params).getItems().getItem();
1067+
}
9441068

9451069
public List<Item> updateItem(List<Item> objects) throws IOException {
9461070
ArrayOfItem array = new ArrayOfItem();
9471071
array.getItem().addAll(objects);
9481072
return post("Items", objFactory.createItems(array)).getItems().getItem();
9491073
}
1074+
1075+
public List<Item> updateItem(List<Item> objects,String unitdp) throws IOException {
1076+
Map<String, String> params = new HashMap<>();
1077+
addToMapIfNotNull(params, "unitdp", unitdp);
1078+
1079+
ArrayOfItem array = new ArrayOfItem();
1080+
array.getItem().addAll(objects);
1081+
return post("Items", objFactory.createItems(array),params).getItems().getItem();
1082+
}
9501083

9511084
public Item getItem(String id) throws IOException {
9521085
return singleResult(get("Items/" + id).getItems().getItem());
9531086
}
9541087

1088+
public Item getItem(String id, String unitdp) throws IOException {
1089+
Map<String, String> params = new HashMap<>();
1090+
addToMapIfNotNull(params, "unitdp", unitdp);
1091+
return singleResult(get("Items/" + id, null, params).getItems().getItem());
1092+
}
1093+
9551094
public String deleteItem(String id) throws IOException {
9561095
return delete("Items/" + id).getStatus();
9571096
}

0 commit comments

Comments
 (0)
Please sign in to comment.