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); var nonce = GetNonce(); var signature = GetSignatureBinary(apiCommand, nonce, bodyAsBytes); using (var request = new HttpRequestMessage( HttpMethod.Post, new Uri(_client.BaseAddress, apiCommand) )) { request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); request.Headers.Add("Apiauth-Key", _accessKey); request.Headers.Add("Apiauth-Nonce", nonce); request.Headers.Add("Apiauth-Signature", signature); 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); LocalBitcoinsException.ThrowException(apiCommand, json); } { var resultAsString = await response.Content.ReadAsStringAsync().ConfigureAwait(false); var json = JsonConvert.DeserializeObject <dynamic>(resultAsString); return(json); } } } }
public async Task <dynamic> CallApiAsync(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 nonce = GetNonce(); var signature = GetSignature(apiCommand, nonce, args); 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.Headers.Add("Apiauth-Nonce", nonce); request.Headers.Add("Apiauth-Signature", signature); 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); LocalBitcoinsException.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 <dynamic>(resultAsString); return(json); } } } finally { httpContent?.Dispose(); } }