-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d207ffa
commit 8928cfa
Showing
14 changed files
with
95 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |