Skip to content

Instantly share code, notes, and snippets.

@tstachl
Created September 3, 2015 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tstachl/0ec283f35ba3c7cc1581 to your computer and use it in GitHub Desktop.
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.
// Use:
// Desk client = new Desk('support@example.com', '$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