Skip to content

Commit bdaf629

Browse files
committed
Update example code
Include code to set custom Config and pass as arg to constructors for Files, Accounting, Asset, etc api client classes Update Journals sample calls to use correct properties Update AccountType to use new separate class in com.xero.models.accounting
1 parent 80bd265 commit bdaf629

2 files changed

Lines changed: 47 additions & 15 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<groupId>com.github.xeroapi</groupId>
55
<artifactId>xero-java</artifactId>
66
<packaging>jar</packaging>
7-
<version>2.2.4</version>
7+
<version>2.2.5</version>
88
<name>Xero-Java SDK</name>
99
<description>This is the official Java SDK for Xero API</description>
1010
<url>https://github.com/XeroAPI/Xero-Java</url>

src/main/java/com/xero/example/RequestResourceServlet.java

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.xero.api.XeroApiException;
3434
import com.xero.api.XeroClientException;
3535
import com.xero.models.accounting.Account;
36+
import com.xero.models.accounting.Account.SystemAccountEnum;
3637
import com.xero.models.accounting.Accounts;
3738
import com.xero.models.accounting.Allocation;
3839
import com.xero.models.accounting.Allocations;
@@ -65,6 +66,7 @@
6566
import com.xero.models.accounting.Invoices;
6667
import com.xero.models.accounting.Item;
6768
import com.xero.models.accounting.Items;
69+
import com.xero.models.accounting.Journal;
6870
import com.xero.models.accounting.JournalLine;
6971
import com.xero.models.accounting.Journals;
7072
import com.xero.models.accounting.LineAmountTypes;
@@ -80,6 +82,8 @@
8082
import com.xero.models.accounting.PaymentService;
8183
import com.xero.models.accounting.PaymentServices;
8284
import com.xero.models.accounting.Payments;
85+
import com.xero.models.accounting.Phone;
86+
import com.xero.models.accounting.Phone.PhoneTypeEnum;
8387
import com.xero.models.accounting.Prepayments;
8488
import com.xero.models.accounting.PurchaseOrder;
8589
import com.xero.models.accounting.PurchaseOrders;
@@ -121,7 +125,7 @@
121125
public class RequestResourceServlet extends HttpServlet
122126
{
123127
private static final long serialVersionUID = 1L;
124-
private Config config = JsonConfig.getInstance();
128+
private Config config = null;
125129
final static Logger logger = LogManager.getLogger(OAuthRequestResource.class);
126130

127131
private String htmlString = "<link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css\" integrity=\"sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7\" crossorigin=\"anonymous\">"
@@ -213,6 +217,15 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
213217
request.getRequestDispatcher("index.jsp").forward(request, response);
214218
}
215219

220+
try {
221+
//config = new CustomJsonConfig();
222+
config = JsonConfig.getInstance();
223+
System.out.println("Your user agent is: " + config.getUserAgent());
224+
} catch(Exception e) {
225+
System.out.println(e.getMessage());
226+
}
227+
228+
216229
OAuthAccessToken refreshToken = new OAuthAccessToken(config);
217230
String tokenTimestamp = storage.get(request, "tokenTimestamp");
218231
if(config.getAppType().equals("PARTNER") && refreshToken.isStale(tokenTimestamp)) {
@@ -240,19 +253,23 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
240253
System.out.println(tokenSecret);
241254

242255
ApiClient apiClientForBankFeeds = new ApiClient(config.getBankFeedsUrl(),null,null,null);
243-
BankFeedsApi bankFeedsApi = new BankFeedsApi(apiClientForBankFeeds);
256+
BankFeedsApi bankFeedsApi = new BankFeedsApi(config);
257+
bankFeedsApi.setApiClient(apiClientForBankFeeds);
244258
bankFeedsApi.setOAuthToken(token, tokenSecret);
245259

246260
ApiClient apiClientForAssets = new ApiClient(config.getAssetsUrl(),null,null,null);
247-
AssetApi assetApi = new AssetApi(apiClientForAssets);
261+
AssetApi assetApi = new AssetApi(config);
262+
assetApi.setApiClient(apiClientForAssets);
248263
assetApi.setOAuthToken(token, tokenSecret);
249264

250265
ApiClient apiClientForAccounting = new ApiClient(config.getApiUrl(),null,null,null);
251-
AccountingApi accountingApi = new AccountingApi(apiClientForAccounting);
266+
AccountingApi accountingApi = new AccountingApi(config);
267+
accountingApi.setApiClient(apiClientForAccounting);
252268
accountingApi.setOAuthToken(token, tokenSecret);
253269

254270
ApiClient apiClientForFiles = new ApiClient(config.getFilesUrl(),null,null,null);
255-
FilesApi filesApi = new FilesApi(apiClientForFiles);
271+
FilesApi filesApi = new FilesApi(config);
272+
filesApi.setApiClient(apiClientForFiles);
256273
filesApi.setOAuthToken(token, tokenSecret);
257274

258275
OffsetDateTime ifModifiedSince = null;
@@ -390,7 +407,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
390407
try {
391408
// GET all accounts
392409
Accounts accounts = accountingApi.getAccounts(null, null, null);
393-
messages.add("Get a all Accounts - total : " + accounts.getAccounts().size());
410+
messages.add("Get a all Accounts - total : " + accounts.getAccounts().size());
394411

395412
// GET one account
396413
Accounts oneAccount = accountingApi.getAccount(accounts.getAccounts().get(0).getAccountID());
@@ -401,25 +418,31 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
401418
acct.setName("Bye" + SampleData.loadRandomNum());
402419
acct.setCode("Hello" + SampleData.loadRandomNum());
403420
acct.setDescription("Foo boo");
404-
acct.setType(Account.TypeEnum.EXPENSE);
421+
acct.setType(com.xero.models.accounting.AccountType.EXPENSE);
405422
Accounts newAccount = accountingApi.createAccount(acct);
406-
messages.add("Create a new Account - Name : " + newAccount.getAccounts().get(0).getName() + " Description : " + newAccount.getAccounts().get(0).getDescription() + "");
423+
System.out.println("Create a new Account - Name : " + newAccount.getAccounts().get(0).getName() + " Description : " + newAccount.getAccounts().get(0).getDescription() + "");
407424
UUID accountID = newAccount.getAccounts().get(0).getAccountID();
408425

426+
System.out.println(newAccount.getAccounts().get(0).toString());
427+
System.out.println("Bank Account type: " + newAccount.getAccounts().get(0).getBankAccountType());
428+
409429
// CREATE Bank account
410430
Account bankAcct = new Account();
411431
bankAcct.setName("Checking " + SampleData.loadRandomNum());
412432
bankAcct.setCode("12" + SampleData.loadRandomNum());
413-
bankAcct.setType(Account.TypeEnum.BANK);
433+
bankAcct.setType(com.xero.models.accounting.AccountType.BANK);
414434
bankAcct.setBankAccountNumber("1234" + SampleData.loadRandomNum());
415435
Accounts newBankAccount = accountingApi.createAccount(bankAcct);
416436
messages.add("Create Bank Account - Name : " + newBankAccount.getAccounts().get(0).getName());
417-
437+
System.out.println(newBankAccount.getAccounts().get(0).toString());
438+
System.out.println("Bank Account type: " + newBankAccount.getAccounts().get(0).getBankAccountType());
439+
418440
// GET BANK account
419441
where = "Status==\"ACTIVE\"&&Type==\"BANK\"";
420442
Accounts accountsWhere = accountingApi.getAccounts(ifModifiedSince, where, order);
421443
messages.add("Get a all Accounts - total : " + accountsWhere.getAccounts().size());
422-
444+
445+
423446
// UDPATE Account
424447
newAccount.getAccounts().get(0).setDescription("Monsters Inc.");
425448
newAccount.getAccounts().get(0).setStatus(null);
@@ -439,7 +462,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
439462
UUID deleteAccountID = newAccount.getAccounts().get(0).getAccountID();
440463
Accounts deleteAccount = accountingApi.deleteAccount(deleteAccountID);
441464
messages.add("Delete account - Status? : " + deleteAccount.getAccounts().get(0).getStatus());
442-
465+
443466
} catch (Exception e) {
444467
System.out.println(e.toString());
445468
e.printStackTrace();
@@ -1290,6 +1313,13 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
12901313
Contact contact = new Contact();
12911314
contact.setName("Foo" + SampleData.loadRandomNum());
12921315
contact.setEmailAddress("sid" + SampleData.loadRandomNum() + "@blah.com");
1316+
List<Phone> phones = new ArrayList<Phone>();
1317+
Phone phone = new Phone();
1318+
phone.setPhoneType(PhoneTypeEnum.MOBILE);
1319+
phone.setPhoneNumber("555-1212");
1320+
phone.setPhoneAreaCode("415");
1321+
phones.add(phone);
1322+
contact.setPhones(phones);
12931323
Contacts newContact = accountingApi.createContact(contact);
12941324
messages.add("Create new Contact - Name : " + newContact.getContacts().get(0).getName());
12951325

@@ -1835,6 +1865,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
18351865
} else if (object.equals("Journals")) {
18361866
/* JOURNAL */
18371867
try {
1868+
18381869
boolean paymentsOnly = false;
18391870
// GET all Journals
18401871
Journals journals = accountingApi.getJournals(ifModifiedSince, null, paymentsOnly);
@@ -1848,6 +1879,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
18481879
UUID journalId = journals.getJournals().get(0).getJournalID();
18491880
Journals oneJournal = accountingApi.getJournal(journalId);
18501881
messages.add("Get one Journal - number : " + oneJournal.getJournals().get(0).getJournalNumber());
1882+
18511883
} catch (XeroApiException e) {
18521884
System.out.println(e.getMessage());
18531885
}
@@ -1997,13 +2029,13 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
19972029
JournalLine credit = new JournalLine();
19982030
credit.description("Hello there");
19992031
credit.setAccountCode(accountCode);
2000-
credit.setLineAmount("100");
2032+
credit.setNetAmount(100.00f);
20012033
manualJournal.addJournalLinesItem(credit);
20022034

20032035
JournalLine debit = new JournalLine();
20042036
debit.description("Goodbye");
20052037
debit.setAccountCode(accountCode);
2006-
debit.setLineAmount("-100");
2038+
debit.setNetAmount(-100.00f);
20072039
manualJournal.addJournalLinesItem(debit);
20082040
manualJournals.addManualJournalsItem(manualJournal);
20092041
ManualJournals createdManualJournals = accountingApi.createManualJournal(manualJournals);

0 commit comments

Comments
 (0)