예제 #1
0
        private async Task <TelesignResponse> ExecuteAsync(string resource, HttpMethod method, Dictionary <string, string> parameters)
        {
            if (parameters == null)
            {
                parameters = new Dictionary <string, string>();
            }

            string resourceUri = string.Format("{0}{1}", this.restEndpoint, resource);

            FormUrlEncodedContent formBody = new FormUrlEncodedContent(parameters);
            string urlEncodedFields        = await formBody.ReadAsStringAsync().ConfigureAwait(false);

            HttpRequestMessage request;

            if (method == HttpMethod.Post || method == HttpMethod.Put)
            {
                request         = new HttpRequestMessage(method, resourceUri);
                request.Content = formBody;
            }
            else
            {
                UriBuilder resourceUriWithQuery = new UriBuilder(resourceUri);
                resourceUriWithQuery.Query = urlEncodedFields;
                request = new HttpRequestMessage(method, resourceUriWithQuery.ToString());
            }

            Dictionary <string, string> headers = GenerateTelesignHeaders(this.customerId,
                                                                          this.apiKey,
                                                                          method.ToString().ToUpper(),
                                                                          resource,
                                                                          urlEncodedFields,
                                                                          null,
                                                                          null,
                                                                          RestClient.UserAgent);

            foreach (KeyValuePair <string, string> header in headers)
            {
                if (header.Key == "Content-Type")
                {
                    // skip Content-Type, otherwise HttpClient will complain
                    continue;
                }

                request.Headers.Add(header.Key, header.Value);
            }

            HttpResponseMessage response = await this.httpClient.SendAsync(request).ConfigureAwait(false);

            TelesignResponse tsResponse = new TelesignResponse(response, isAsync: true);
            await tsResponse.Initialize();

            return(tsResponse);
        }
예제 #2
0
        protected async Task <TelesignResponse> ExecuteAsync(string resource, HttpMethod method, Dictionary <string, object> parameters)
        {
            string             contentType = "application/json";
            HttpRequestMessage request;
            string             fieldsToSign = null;

            if (parameters == null)
            {
                parameters = new Dictionary <string, object>();
            }

            string resourceUri = string.Format("{0}{1}", this.restEndpoint, resource);

            fieldsToSign    = JsonConvert.SerializeObject(parameters);
            request         = new HttpRequestMessage(method, resourceUri);
            request.Content = new StringContent(fieldsToSign);
            request.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);

            Dictionary <string, string> headers = RestClient.GenerateTelesignHeaders(
                this.customerId,
                this.apiKey,
                method.ToString().ToUpper(),
                resource,
                fieldsToSign,
                null,
                null,
                RestClient.UserAgent,
                contentType);

            foreach (KeyValuePair <string, string> header in headers)
            {
                // .NET considers content-type header to be part of the content, not the request
                // when content-type is set as request header, exception is raised
                // proper way to set content-type is through content property of the request object
                if (header.Key == "Content-Type")
                {
                    continue;
                }
                request.Headers.Add(header.Key, header.Value);
            }

            HttpResponseMessage response = await this.httpClient.SendAsync(request).ConfigureAwait(false);

            TelesignResponse tsResponse = new TelesignResponse(response, isAsync: true);
            await tsResponse.Initialize();

            return(tsResponse);
        }