Exemple #1
0
        public Client(ApiConfiguration payConfiguration)
        {
            apiUrlBuilder = new ApiUrlBuilder(payConfiguration);

            this.payConfiguration = payConfiguration;
            canonicalBuilder      = new CanonicalBuilder();
            signatureHelper       = new SignatureHelper(payConfiguration, canonicalBuilder);
        }
Exemple #2
0
 public SignatureHelper(ApiConfiguration payConfiguration, CanonicalBuilder canonicalBuilder)
 {
     this.payConfiguration = payConfiguration;
     this.canonicalBuilder = canonicalBuilder;
 }
        /// <summary>
        /// Helper method to execute the request
        /// </summary>
        protected virtual HttpWebResponse SendRequest(ApiRequest apiRequest, Dictionary <string, string> postSignedHeaders)
        {
            string path = apiRequest.Path.ToString();

            // add the query parameters to the URL, if there are any
            if (apiRequest.QueryParameters != null && apiRequest.QueryParameters.Count > 0)
            {
                var canonicalBuilder = new CanonicalBuilder();
                path += "?" + canonicalBuilder.GetCanonicalizedQueryString(apiRequest.QueryParameters);
            }

            // TODO: move setting of SecurityProtocol into constructor

            // ensure the right minimum TLS version is being used
            if (Util.IsObsoleteSecurityProtocol(ServicePointManager.SecurityProtocol))
            {
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            }

            // TODO: consider switching to HttpClient class for web requests

            // create the web request
            HttpWebRequest request = WebRequest.Create(path) as HttpWebRequest;

            foreach (KeyValuePair <string, string> header in postSignedHeaders)
            {
                if (WebHeaderCollection.IsRestricted(header.Key))
                {
                    switch (header.Key)
                    {
                    case "accept":
                        request.Accept = header.Value;
                        break;

                    case "content-type":
                        request.ContentType = header.Value;
                        break;

                    case "user-agent":
                        request.UserAgent = header.Value;
                        break;

                    default:
                        throw new AmazonPayClientException("unknown header" + " " + header.Key);
                    }
                }
                else
                {
                    request.Headers.Add(header.Key, header.Value);
                }
            }
            request.Method = apiRequest.HttpMethod.ToString();

            if (apiRequest.HttpMethod != HttpMethod.GET)
            {
                using (var streamWriter = new StreamWriter(request.GetRequestStream()))
                {
                    streamWriter.Write(apiRequest.Body.ToJson());
                    streamWriter.Flush();
                }
            }

            HttpWebResponse httpResponse;

            try
            {
                httpResponse = request.GetResponse() as HttpWebResponse;
            }
            catch (WebException we)
            {
                httpResponse = (HttpWebResponse)we.Response as HttpWebResponse;

                if (httpResponse == null)
                {
                    throw new AmazonPayClientException("Http Response is empty " + we);
                }
            }

            return(httpResponse);
        }