public static void ThrowException(string callerMethod, dynamic json) { var ex = new WeatherException(callerMethod, FormatMessage(callerMethod, json)) { DataJson = json }; throw ex; }
public async Task <dynamic> CallApiAsync <T>(string apiCommand, RequestType requestType, Dictionary <string, string> args, bool getAsBinary = false) { HttpContent httpContent = null; if (requestType == RequestType.Post) { if (args != null && args.Any()) { httpContent = new FormUrlEncodedContent(args); } } try { var relativeUrl = apiCommand; if (requestType == RequestType.Get) { if (args != null && args.Any()) { relativeUrl += "?" + UrlEncodeParams(args); } } using (var request = new HttpRequestMessage( requestType == RequestType.Get ? HttpMethod.Get : HttpMethod.Post, new Uri(_client.BaseAddress, relativeUrl) )) { request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); request.Content = httpContent; var response = await _client.SendAsync(request).ConfigureAwait(false); if (!response.IsSuccessStatusCode) { var resultAsString = await response.Content.ReadAsStringAsync().ConfigureAwait(false); var json = JsonConvert.DeserializeObject <dynamic>(resultAsString); WeatherException.ThrowException(apiCommand, json); } if (getAsBinary) { var resultAsByteArray = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false); return(resultAsByteArray); } else { var resultAsString = await response.Content.ReadAsStringAsync().ConfigureAwait(false); var json = JsonConvert.DeserializeObject <T>(resultAsString); return(json); } } } finally { httpContent?.Dispose(); } }