Created
January 24, 2015 21:39
-
-
Save tstachl/bbdf5293bc7216c663d0 to your computer and use it in GitHub Desktop.
This is an example of how you can interact with the Desk.com API using C#. It shows the 4 basic requests "GET", "POST", "PATCH" and "DELETE". If you have any questions reach out to us at support@desk.com.
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
using System; | |
using System.Text; | |
using System.Net; | |
using System.Net.Http; | |
using System.Net.Http.Headers; | |
using Newtonsoft.Json.Linq; | |
namespace DeskApiExample | |
{ | |
class MainClass | |
{ | |
public const String USERNAME = "support@example.com"; | |
public const String PASSWORD = "My$ecre!Passwor6"; | |
public const String ENDPOINT = "https://example.desk.com"; | |
public static void Main(string[] args) | |
{ | |
Get(); | |
String self = Post(); | |
Patch(self); | |
Delete(self); | |
} | |
public static void Get() | |
{ | |
// Send a request and get HTTP response | |
HttpResponseMessage response = CreateClient().GetAsync(ENDPOINT + "/api/v2/labels").Result; | |
// Check that response was successful or throw exception | |
response.EnsureSuccessStatusCode(); | |
// Parse the json response | |
JObject deskResponse = JObject.Parse(response.Content.ReadAsStringAsync().Result); | |
// Walk through the label entries | |
foreach (JObject label in (JArray)deskResponse["_embedded"]["entries"]) { | |
// Output each label | |
Console.WriteLine("Label: {0}", label["name"]); | |
} | |
} | |
public static String Post() | |
{ | |
// Create the json body to send with the request | |
StringContent json = new StringContent(JObject.Parse(@"{ | |
name: 'Test Label', | |
description: 'This is to test the API', | |
types: ['case'], | |
enabled: true, | |
color: 'red' | |
}").ToString(), Encoding.UTF8, "application/json"); | |
// Send a request and get HTTP response | |
HttpResponseMessage response = CreateClient().PostAsync(ENDPOINT + "/api/v2/labels", json).Result; | |
// Check that response was successful or throw exception | |
response.EnsureSuccessStatusCode(); | |
// Parse the json response | |
JObject deskResponse = JObject.Parse(response.Content.ReadAsStringAsync().Result); | |
// Write out the complete response | |
Console.WriteLine(deskResponse.ToString()); | |
// return the self href | |
return (String)deskResponse["_links"]["self"]["href"]; | |
} | |
public static void Patch(String href) | |
{ | |
// Create the json body to send with the request | |
StringContent json = new StringContent(JObject.Parse(@"{ | |
name: 'Updated Test Label', | |
color: 'blue' | |
}").ToString(), Encoding.UTF8, "application/json"); | |
// Add X-HTTP-Method-Override header to client | |
HttpClient client = CreateClient(); | |
client.DefaultRequestHeaders.Add("X-HTTP-Method-Override", "PATCH"); | |
// Send a request and get HTTP response | |
HttpResponseMessage response = client.PostAsync(ENDPOINT + href, json).Result; | |
// Check that response was successful or throw exception | |
response.EnsureSuccessStatusCode(); | |
// Parse the json response | |
JObject deskResponse = JObject.Parse(response.Content.ReadAsStringAsync().Result); | |
// Write out the changes | |
Console.WriteLine("Name: {0}", deskResponse["name"]); | |
Console.WriteLine("Color: {0}", deskResponse["color"]); | |
} | |
public static void Delete(String href) | |
{ | |
// Send a request and get HTTP response | |
HttpResponseMessage response = CreateClient().DeleteAsync(ENDPOINT + href).Result; | |
// Check that response was successful or throw exception | |
response.EnsureSuccessStatusCode(); | |
// Output the status | |
Console.WriteLine("Delete Status Code: {0}", response.StatusCode); | |
// Was the label deleted? | |
if (response.StatusCode == HttpStatusCode.NoContent) { | |
Console.WriteLine("The Label was deleted."); | |
} else { | |
Console.WriteLine("Something went wrong."); | |
} | |
} | |
public static AuthenticationHeaderValue CreateBasicHeader(String username, String password) | |
{ | |
Byte[] byteArray = Encoding.UTF8.GetBytes(username + ":" + password); | |
return new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray)); | |
} | |
public static HttpClient CreateClient() | |
{ | |
// Create an HttpClient instance | |
HttpClient client = new HttpClient(); | |
// Authentication header | |
client.DefaultRequestHeaders.Authorization = CreateBasicHeader(USERNAME, PASSWORD); | |
// Accept header | |
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); | |
// Return the client | |
return client; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment