/// <summary>Call the CloudPOS API.</summary> /// <param name="uri">The uri to call.</param> /// <param name="action">The action to perform.</param> /// <param name="jsonToSend">The JSON to send.</param> /// <returns>The CallResults.</returns> public static CallResults CallAPI(string uri, enumRESTVerb action, string jsonToSend = "") { CallResults results = new CallResults(); string appendOrQueryString = uri.Contains("?") ? "&": "?"; string pagedUrl = string.Format("{0}{1}PageNumber=", uri, appendOrQueryString); string nextUrl = uri; do { using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(Constants.API_KEY); StringContent content = new StringContent(jsonToSend, Encoding.UTF8, "application/json"); HttpResponseMessage response = GetResponseFromAPI(client, nextUrl, action, content); string data = response.Content.ReadAsStringAsync().Result; if (response.IsSuccessStatusCode == true) { nextUrl = HandlePaging(response, pagedUrl); results.JsonBuilder.Append(data); } else { results.ErrorMessage = data; HandleBadResponse(response, ref nextUrl, ref results); } } }while (nextUrl != string.Empty); results.JsonBuilder.Replace("][", ","); return(results); }
/// <summary>Gets a response from the API.</summary> /// <param name="client">The client.</param> /// <param name="uri">The uri to call.</param> /// <param name="action">The action to perform.</param> /// <param name="content">The content to post.</param> /// <returns>The response.</returns> private static HttpResponseMessage GetResponseFromAPI(HttpClient client, string uri, enumRESTVerb action, StringContent content) { HttpResponseMessage response = default(HttpResponseMessage); switch (action) { case enumRESTVerb.GET: response = client.GetAsync(uri).Result; break; case enumRESTVerb.POST: response = client.PostAsync(uri, content).Result; break; case enumRESTVerb.PUT: response = client.PutAsync(uri, content).Result; break; } return(response); }