public async Task <dynamic> CallApiPostFileAsync(string apiCommand, Dictionary <string, string> args, string fileName) { using (var httpContent = new MultipartFormDataContent()) { if (args != null) { foreach (var keyValuePair in args) { httpContent.Add(new StringContent(keyValuePair.Value), string.Format(CultureInfo.InvariantCulture, "\"{0}\"", keyValuePair.Key)); } } if (fileName != null) { var fileBytes = File.ReadAllBytes(fileName); httpContent.Add(new ByteArrayContent(fileBytes), "\"document\"", "\"" + Path.GetFileName(fileName) + "\""); } var bodyAsBytes = await httpContent.ReadAsByteArrayAsync().ConfigureAwait(false); using (var request = new HttpRequestMessage( HttpMethod.Post, new Uri(_client.BaseAddress, apiCommand) )) { 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); } { var resultAsString = await response.Content.ReadAsStringAsync().ConfigureAwait(false); var json = JsonConvert.DeserializeObject <dynamic>(resultAsString); return(json); } } } }
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.Headers.Add("Apiauth-Key", _accessKey); 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(); } }