Created
September 3, 2015 17:57
-
-
Save tstachl/0ec283f35ba3c7cc1581 to your computer and use it in GitHub Desktop.
This is an untested example of how you could use the Desk.com API to fetch all customers in java.
This file contains hidden or 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
// Use: | |
// Desk client = new Desk('[email protected]', '$upp0rt!', 'https://example.desk.com'); | |
// JSONObject[] customers = client.getAllCustomers(); | |
class Desk { | |
public Desk(String username, String password, String endpoint) { | |
this.username = username; | |
this.password = password; | |
this.endpoint = endpoint; | |
} | |
public HttpGet getRequest(String path) { | |
String auth = this.username + ":" + this.password; | |
byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("US-ASCII"))); | |
String authHeader = "Basic " + new String(encodedAuth); | |
HttpGet request = new HttpGet(this.endpoint + this.path); | |
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/json"); | |
request.setHeader(HttpHeaders.AUTHORIZATION, authHeader); | |
return request; | |
} | |
public JSONObject[] getAllCustomers() { | |
JSONObject[] customers; | |
String href = this.endpoint + "/api/v2/customers"; | |
while (href !== null) { | |
HttpClient client = new DefaultHttpClient(); | |
JSONObject response = new JSONObject(new BasicResponseHandler().handleResponse(client.execute())); | |
JSONArray customersArr = response.getJSONObject("_embedded").getJSONArray("entries"); | |
for (int i = 0; i < customersArr.length(); i++) { | |
customers.push(customersArr[i]); | |
} | |
JSONObject next = response.getJSONObject("_links").getJSONObject("next"); | |
if (next !== null) { | |
href = next.getString("href"); | |
} else { | |
href = null; | |
} | |
} | |
return customers; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment