C#
Content Outline
Hello World with C#
Using Webex Experience Management.API NuGet Portable Class Library(PCL)
Download the Webex Experience Management.API NuGet Portable Class Library(PCL) / Source
// Simply add Webex Experience Management.API NuGet for drop-in API to your .NET Project.
public class ExampleWithNuGet
{
public async Task APIHelloWorld()
{
Webex Experience Management.APIClient client = new Webex Experience Management.APIClient("https://api.getcloudcherry.com", "yourusername", "yourpassword");
bool loginstatus = await client.Login();
if(loginstatus)
{
var analytics = await client.GetAnalyticsByLocation(new Webex Experience Management.FilterBy { afterdate = DateTime.Now.AddMonths(-1) });
foreach(var location in analytics)
{
Console.WriteLine(location.Key + ": " + location.Value.TotalResponses + " Responses");
Console.WriteLine("Delight Score: " + location.Value.MDM);
Console.WriteLine("NPS: " + location.Value.NetPromoter.NetPromoters);
Console.WriteLine("Liked/DisLiked: " + location.Value.Liked + "/" + location.Value.Disliked);
}
}
}
}
Using Source Code ( < .NET 4.5 )
public async void HelloWebex Experience ManagementAPI(string username, string password)
{
string apiendpoint = "https://api.getcloudcherry.com";
var client = new HttpClient(); // Create a Instance of Async HttpClient(System.Net.Http)
//Step 1 : Authenticate To Obtain API Bearer Token To Use With Every API Call
//Compose Authentication Request
HttpRequestMessage authrequest = new HttpRequestMessage(HttpMethod.Post, apiendpoint + "/api/LoginToken");
var postvalues = new[] {
new KeyValuePair< string, string >("grant_type", "password"),
new KeyValuePair< string, string >("username", username),
new KeyValuePair< string, string >("password", password)
};
authrequest.Content = new FormUrlEncodedContent(postvalues);
HttpResponseMessage authresponse = await client.SendAsync(authrequest);
string responseBodyAsText = await authresponse.Content.ReadAsStringAsync();
//Deserialize Into Object Using JSON.Net(Newtonsoft.Json)
var tokenStructure = new { access_token = string.Empty, expires_in = 0 };
var token = JsonConvert.DeserializeAnonymousType(responseBodyAsText, tokenStructure);
Console.WriteLine("Authenticated : " + !string.IsNullOrEmpty(token.access_token));
//Step 2: Get Filtered Responses Using API
var filterfor = new
{ // Filter Responses as below
//location = new List { "Downtown" }, // Only for location Downtown
afterdate = new DateTime(2014, 11, 20), // After Nov 20th 2014
beforedata = new DateTime(2014, 11, 26) // Before Nov 26th 2014
};
var json = JsonConvert.SerializeObject(filterfor);
//API Endpoint To Query For Responses(Refer WhitePaper)
string url = apiendpoint + "/api/answers";
HttpRequestMessage queryrequest = new HttpRequestMessage(HttpMethod.Post, url);
queryrequest.Content = new StringContent(json, Encoding.UTF8, "application/json");
//Add Bearer Token To Authenticate This Stateless Request
queryrequest.Headers.Add("Authorization", "Bearer " + token.access_token);
var queryresponse = await client.SendAsync(queryrequest);
var responseBody = await queryresponse.Content.ReadAsStringAsync();
//Deseralize
var answerStructure = new { LocationId = string.Empty, ResponseDateTime = new DateTime(), Responses = new List< Response >() };
var answers = JsonConvert.DeserializeObject< List< Answer > >(responseBody);
foreach(var a in answers)
{ // Iterate Through Each Response To Print Individual Answers To Each Question Answered
Console.WriteLine("\nResponse: " + a.LocationId + " " + a.ResponseDateTime);
foreach (var r in a.Responses)
Console.WriteLine(r.QuestionText + ": " + (!string.IsNullOrEmpty(r.TextInput) ? r.TextInput : r.NumberInput.ToString()));
}
//Step 3: More API Requests Here ..
//Ex. Create Questions, Create Multi-Channel Survey Token ...
//Last Step - Keep Session Clean - Logout
queryrequest = new HttpRequestMessage(HttpMethod.Get, apiendpoint + "/api/account/logout");
queryrequest.Headers.Add("Authorization", "Bearer " + token.access_token);
var logoutAck = await client.SendAsync(queryrequest);
if (logoutAck.IsSuccessStatusCode)
Console.WriteLine("Logged Out");
}
public class Answer
{
public string LocationId { get; set; } // Where
public DateTime ResponseDateTime { get; set; } // When
public List< Response > Responses { get; set; } //Answers
}
public class Response
{
public string QuestionId { get; set; }
public string QuestionText { get; set; }
public string TextInput { get; set; } // Text Answer
public int NumberInput { get; set; } // Numeric Answer
}