private async Task <HttpResponseMessage> Post(string uri, string json, bool signatureRequired = false) { try { var bodyContent = new StringContent(UnicodeToAscii(json)); _httpClient.DefaultRequestHeaders.Clear(); _httpClient.DefaultRequestHeaders.Add("x-accept-version", BitpayApiVersion); _httpClient.DefaultRequestHeaders.Add("x-bitpay-plugin-info", BitpayPluginInfo); bodyContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); if (signatureRequired) { var signature = KeyUtils.Sign(_ecKey, _baseUrl + uri + json); _httpClient.DefaultRequestHeaders.Add("x-signature", signature); _httpClient.DefaultRequestHeaders.Add("x-identity", _ecKey?.PublicKeyHexBytes); } var result = await _httpClient.PostAsync(uri, bodyContent).ConfigureAwait(false); return(result); } catch (Exception ex) { throw new BitPayApiCommunicationException(ex); } }
/// <summary> /// Make a DELETE request /// </summary> /// <param name="uri">The URI to request</param> /// <param name="parameters">The parameters of the request</param> /// <returns>The HttpResponseMessage of the request</returns> private async Task <HttpResponseMessage> Delete(string uri, Dictionary <string, string> parameters = null) { try { var fullUrl = _baseUrl + uri; _httpClient.DefaultRequestHeaders.Clear(); _httpClient.DefaultRequestHeaders.Add("x-accept-version", BitpayApiVersion); _httpClient.DefaultRequestHeaders.Add("x-bitpay-plugin-info", BitpayPluginInfo); if (parameters != null) { fullUrl += "?"; foreach (var entry in parameters) { fullUrl += entry.Key + "=" + entry.Value + "&"; } fullUrl = fullUrl.Substring(0, fullUrl.Length - 1); var signature = KeyUtils.Sign(_ecKey, fullUrl); _httpClient.DefaultRequestHeaders.Add("x-signature", signature); _httpClient.DefaultRequestHeaders.Add("x-identity", KeyUtils.BytesToHex(_ecKey.PublicKey)); } var result = await _httpClient.DeleteAsync(fullUrl); return(result); } catch (Exception ex) { throw new BitPayApiCommunicationException(ex); } }