Skip to content

Commit

Permalink
Added new method getContact()
Browse files Browse the repository at this point in the history
  • Loading branch information
benedwards44 committed Jun 21, 2016
1 parent d207ffa commit 8928cfa
Show file tree
Hide file tree
Showing 14 changed files with 95 additions and 31 deletions.
27 changes: 27 additions & 0 deletions src/classes/XeroAccountingApi.cls
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,33 @@ public with sharing class XeroAccountingApi {
return xeroContacts;
}

/**
* @author Ben Edwards ([email protected])
* @description Method to return ALL contacts for your Xero environment
* @return List of Xero Contact wrapper classes
**/
public static XeroContact getContact (String xeroContactId) {

XeroContact xeroContact = new XeroContact();

// Execute the callout to the Contacts resource
HttpResponse response = XeroCalloutUtility.executeCallout('GET', 'Contacts/' + xeroContactId, null);

// If successful response
if (response.getStatusCode() == 200) {

// Use the parser to convert the response into Xero objects
xeroContact = XeroCalloutResponseParser.parseContacts(response.getBody())[0];
}
else {

// Raise error
throw new XeroAccountingApiException(response.getStatusCode() + ': ' + response.getBody());
}

return xeroContact;
}

/**
* @author Ben Edwards ([email protected])
* @description Method to create a Contact in Xero.
Expand Down
2 changes: 1 addition & 1 deletion src/classes/XeroAccountingApi.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>35.0</apiVersion>
<apiVersion>36.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion src/classes/XeroAccountingApiTest.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>35.0</apiVersion>
<apiVersion>36.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion src/classes/XeroAddress.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>35.0</apiVersion>
<apiVersion>36.0</apiVersion>
<status>Active</status>
</ApexClass>
39 changes: 23 additions & 16 deletions src/classes/XeroCalloutResponseParser.cls
Original file line number Diff line number Diff line change
@@ -1,42 +1,49 @@
/**
* @author Ben Edwards (ben@benedwards.co.nz)
* @author Ben Edwards (ben@edwards.nz)
* @description Class to parse responses from Xero callouts
**/
public with sharing class XeroCalloutResponseParser {

/**
* @author Ben Edwards (ben@benedwards.co.nz)
* @author Ben Edwards (ben@edwards.nz)
* @description Method to convert JSON responses into Xero objects
* @return Returns a Xero wrapper class object from a given response body
**/
public static List<XeroContact> parseContacts(String jsonBody) {

// Parse the JSON response
Map<String, Object> jsonResponseMap = (Map<String, Object>) JSON.deserializeUntyped(jsonBody);

// Re-serialize just the Contacts portion back to JSOn
String jsonSerialized = JSON.serialize((List<Object>) jsonResponseMap.get('Contacts'));

String jsonSerialized = generateJsonStringForParsing (jsonBody, 'Contacts');
// And finally parse the now re-serialized contact list back into Contact objects
return (List<XeroContact>) JSON.deserialize(jsonSerialized, List<XeroContact>.class);
return (List<XeroContact>) JSON.deserializeStrict(jsonSerialized, List<XeroContact>.class);
}


/**
* @author Ben Edwards (ben@benedwards.co.nz)
* @author Ben Edwards (ben@edwards.nz)
* @description Method to convert JSON responses into Xero objects
* @return Returns a Xero wrapper class object from a given response body
**/
public static List<XeroInvoice> parseInvoices(String jsonBody) {

// Parse the JSON response
Map<String, Object> jsonResponseMap = (Map<String, Object>) JSON.deserializeUntyped(jsonBody);

// Re-serialize just the Contacts portion back to JSOn
String jsonSerialized = JSON.serialize((List<Object>) jsonResponseMap.get('Invoices'));
String jsonSerialized = generateJsonStringForParsing (jsonBody, 'Invoices');

// And finally parse the now re-serialized contact list back into Contact objects
return (List<XeroInvoice>) JSON.deserialize(jsonSerialized, List<XeroInvoice>.class);
}
return (List<XeroInvoice>) JSON.deserializeStrict(jsonSerialized, List<XeroInvoice>.class);
}

/**
* @author Ben Edwards ([email protected])
* @description Method to take the full JSON response and just return the object portion. Eg. The Contact and Invoice portion only
* @return JSON String with only specific object data
**/
private static String generateJsonStringForParsing (String jsonBody, String objectName) {

// Parse the JSON response
Map<String, Object> jsonResponseMap = (Map<String, Object>) JSON.deserializeUntyped(jsonBody);

// Re-serialize just the object portion back to JSON
return JSON.serialize((List<Object>) jsonResponseMap.get(objectName));
}

}
2 changes: 1 addition & 1 deletion src/classes/XeroCalloutResponseParser.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>35.0</apiVersion>
<apiVersion>36.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion src/classes/XeroCalloutUtility.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>35.0</apiVersion>
<apiVersion>36.0</apiVersion>
<status>Active</status>
</ApexClass>
36 changes: 34 additions & 2 deletions src/classes/XeroContact.cls
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,53 @@ public class XeroContact {
public String LastName;
public String EmailAddress;
public String BankAccountDetails;
public String SkypeUserName;
public XeroAddress[] Addresses;
public XeroPhone[] Phones;
public String UpdatedDateUTC;
public ContactGroups[] ContactGroups;
public Boolean IsSupplier;
public Boolean IsCustomer;
public String DefaultCurrency;
public Balances Balances;
public ContactPersons[] ContactPersons;
public Boolean HasAttachments;
public Attachments[] Attachments;
public Boolean HasValidationErrors;

class ContactGroups {
public class ContactGroups {
}

class ContactPersons {
public class ContactPersons {

public String FirstName;
public String LastName;
public String EmailAddress;
public Boolean IncludeInEmails;
}

public class Balances {

public AccountsPayable AccountsPayable;
public AccountsReceivable AccountsReceivable;
}

public class AccountsPayable {
public Decimal Overdue; //0
public Decimal Outstanding; //0
}

public class AccountsReceivable {
public Decimal Overdue; //2625
public Decimal Outstanding; //2625
}

public class Attachments {

public String AttachmentID;
public String FileName;
public String Url;
public String MimeType;
public Integer ContentLength;
}
}
2 changes: 1 addition & 1 deletion src/classes/XeroContact.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>35.0</apiVersion>
<apiVersion>36.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion src/classes/XeroInvoice.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>35.0</apiVersion>
<apiVersion>36.0</apiVersion>
<status>Active</status>
</ApexClass>
4 changes: 1 addition & 3 deletions src/classes/XeroOAuthUtility.cls
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ public with sharing class XeroOAuthUtility {
// Add each paramaeter to the header string
header = header + key + '="' +parameters.get(key)+ '", ';
}

system.debug('### ' + signature);


// Add the OAuth signature and return.
// Note: The signature needs to be URL encoded, as if it includes / and + these should be encoded correctly
return header + 'oauth_signature="' + EncodingUtil.urlEncode(signature, 'UTF-8') + '"';
Expand Down
2 changes: 1 addition & 1 deletion src/classes/XeroOAuthUtility.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>35.0</apiVersion>
<apiVersion>36.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion src/classes/XeroPhone.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>35.0</apiVersion>
<apiVersion>36.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion src/classes/XeroXmlUtility.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>35.0</apiVersion>
<apiVersion>36.0</apiVersion>
<status>Active</status>
</ApexClass>

0 comments on commit 8928cfa

Please sign in to comment.