// Get and parse JSON response public async Task <TResponse> GetJsonAsync <TResponse>(string url, Dictionary <string, string> parameters = null, Dictionary <string, string> headers = null, CancellationToken cancellationToken = default(CancellationToken)) { var response = await SendRequestAsync(url, HttpMethod.Get, null, headers, parameters, cancellationToken); var responseString = await response.Content.ReadAsStringAsync(); DebugFunc?.Invoke($"Response JSON: {responseString}"); return(JsonConvert.DeserializeObject <TResponse>(responseString)); }
// Post binary data and parse JSON response public async Task <TResponse> PostBytesAsync <TResponse>(string url, byte[] data, Dictionary <string, string> headers = null, Dictionary <string, string> parameters = null, CancellationToken cancellationToken = default(CancellationToken)) { var response = await PostBytesAsync(url, data, headers, parameters, cancellationToken); var responseString = await response.Content.ReadAsStringAsync(); DebugFunc?.Invoke($"Response JSON: {responseString}"); return(JsonConvert.DeserializeObject <TResponse>(responseString)); }
public static string DebugList <T>(List <T> array, string name = null, DebugFunc <T> debugFunc = null) { string debug = string.Empty; foreach (T bId in array) { if (debugFunc == null) { debug += bId.ToString() + " ; "; } else { debug += debugFunc(bId); } } return(string.IsNullOrEmpty(name) ? debug : name + " [" + debug + "](" + array.Count + ")"); }
public static void SetDebugLabel(string key, Func <object> func) { DebugLabelValues[key] = new DebugFunc(func); }
// Send http request public async Task <HttpResponseMessage> SendRequestAsync(string url, HttpMethod httpMethod, HttpContent content, Dictionary <string, string> headers = null, Dictionary <string, string> parameters = null, CancellationToken cancellationToken = default(CancellationToken)) { // Create request var request = new HttpRequestMessage( httpMethod ?? HttpMethod.Get, parameters == null ? url : $"{url}?{await new FormUrlEncodedContent(parameters).ReadAsStringAsync()}"); DebugFunc?.Invoke($"Method: {request.Method}"); DebugFunc?.Invoke($"URI: {request.RequestUri}"); // Set headers if (headers != null) { foreach (var h in headers) { request.Headers.Add(h.Key, h.Value); } } DebugFunc?.Invoke($"Request headers: {request.Headers}"); // Set content if (content != null) { request.Content = content; if (content is FormUrlEncodedContent || content is StringContent) { DebugFunc?.Invoke($"Content: {await request.Content?.ReadAsStringAsync()}"); } else { DebugFunc?.Invoke($"Content: byte[{(await request.Content.ReadAsByteArrayAsync()).Length}]"); } } // Inject user function just before sending request if (BeforeRequestFunc != null) { await BeforeRequestFunc(request); } // Send request var response = await httpClient.SendAsync(request, cancellationToken); // Inject user function just after receiving response if (AfterRequestFunc != null) { await AfterRequestFunc(response); } DebugFunc?.Invoke($"Status code: {(int)response.StatusCode}"); DebugFunc?.Invoke($"Response headers: {response.Headers}"); // Throw exception if the status code is not success if (!response.IsSuccessStatusCode) { if (response.Content != null) { DebugFunc?.Invoke($"Response content: {await response.Content.ReadAsStringAsync()}"); } response.EnsureSuccessStatusCode(); } return(response); }