Esempio n. 1
0
        /// <summary>
        /// Prepare for async call to the API server
        /// </summary>
        /// <param name="method">HTTP verb</param>
        /// <param name="requestBody">JSON formatted string</param>
        /// <param name="queryParams">JSON formatted query paramaters</param>
        /// <param name="urlPath">The path to the API endpoint.</param>
        /// <param name="cancellationToken">Cancel the asynchronous call.</param>
        /// <returns>Response object</returns>
        /// <exception cref="Exception">The method will NOT catch and swallow exceptions generated by sending a request
        /// through the internal http client. Any underlying exception will pass right through.
        /// In particular, this means that you may expect
        /// a TimeoutException if you are not connected to the internet.</exception>
        public async Task <Response> RequestAsync(
            SendGridClient.Method method,
            string requestBody = null,
            string queryParams = null,
            string urlPath     = null,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            var endpoint = this.client.BaseAddress + this.BuildUrl(urlPath, queryParams);

            // Build the request body
            StringContent content = null;

            if (requestBody != null)
            {
                content = new StringContent(requestBody, Encoding.UTF8, this.MediaType);
            }

            // Build the final request
            var request = new HttpRequestMessage
            {
                Method     = new HttpMethod(method.ToString()),
                RequestUri = new Uri(endpoint),
                Content    = content
            };

            return(await this.MakeRequest(request, cancellationToken).ConfigureAwait(false));
        }
Esempio n. 2
0
        /// <summary>
        /// Prepare for async call to the API server
        /// </summary>
        /// <param name="method">HTTP verb</param>
        /// <param name="requestBody">JSON formatted string</param>
        /// <param name="queryParams">JSON formatted query paramaters</param>
        /// <param name="urlPath">The path to the API endpoint.</param>
        /// <param name="cancellationToken">Cancel the asynchronous call.</param>
        /// <returns>Response object</returns>
        public async Task <Response> RequestAsync(
            SendGridClient.Method method,
            string requestBody = null,
            string queryParams = null,
            string urlPath     = null,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            try
            {
                var endpoint = client.BaseAddress + BuildUrl(urlPath, queryParams);

                // Build the request body
                StringContent content = null;
                if (requestBody != null)
                {
                    content = new StringContent(requestBody, Encoding.UTF8, this.MediaType);
                }

                // Build the final request
                var request = new HttpRequestMessage
                {
                    Method     = new HttpMethod(method.ToString()),
                    RequestUri = new Uri(endpoint),
                    Content    = content
                };
                return(await MakeRequest(request, cancellationToken).ConfigureAwait(false));
            }
            catch (Exception ex)
            {
                var response = new HttpResponseMessage();
                var message  = (ex is HttpRequestException) ? ".NET HttpRequestException" : ".NET Exception";
                response.Content = new StringContent(message + ", raw message: \n\n" + ex.Message);
                return(new Response(response.StatusCode, response.Content, response.Headers));
            }
        }
 public CheckBounceRequest(SendGridClient.Method method, string urlPath)
 {
     this.method  = method;
     this.urlPath = urlPath;
 }
Esempio n. 4
0
 public DeleteAllBouncesRequest(SendGridClient.Method method, string urlPath, string requestBody)
 {
     this.method      = method;
     this.urlPath     = urlPath;
     this.requestBody = requestBody;
 }
Esempio n. 5
0
 public DeleteBounceRequest(SendGridClient.Method method, string urlPath, string queryParams)
 {
     this.method      = method;
     this.urlPath     = urlPath;
     this.queryParams = queryParams;
 }
 public CreateKeyRequest(SendGridClient.Method method, string urlPath, string requestBody)
 {
     this.method      = method;
     this.urlPath     = urlPath;
     this.requestBody = requestBody;
 }
Esempio n. 7
0
        /// <summary>
        /// Prepare for async call to the API server
        /// </summary>
        /// <param name="method">HTTP verb</param>
        /// <param name="requestBody">JSON formatted string</param>
        /// <param name="requestHeaders">Custom request headers.</param>
        /// <param name="queryParams">JSON formatted query paramaters</param>
        /// <param name="urlPath">The path to the API endpoint.</param>
        /// <param name="cancellationToken">Cancel the asynchronous call.</param>
        /// <returns>Response object</returns>
        public async Task <Response> RequestAsync(
            SendGridClient.Method method,
            string requestBody = null,
            Dictionary <string, string> requestHeaders = null,
            string queryParams = null,
            string urlPath     = null,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            using (var client = this.BuildHttpClient())
            {
                try
                {
                    // Build the URL
                    client.BaseAddress = new Uri(this.Host);
                    if (urlPath != null)
                    {
                        this.SetUrlPath(urlPath);
                    }

                    string endpoint = this.BuildUrl(queryParams);

                    // Build the request headers
                    if (requestHeaders != null)
                    {
                        this.AddRequestHeader(requestHeaders);
                    }

                    client.DefaultRequestHeaders.Accept.Clear();
                    if (this.RequestHeaders != null)
                    {
                        foreach (KeyValuePair <string, string> header in this.RequestHeaders)
                        {
                            if (header.Key == "Authorization")
                            {
                                client.DefaultRequestHeaders.Authorization = this.AddAuthorization(header);
                            }
                            else if (header.Key == "Content-Type")
                            {
                                this.MediaType = header.Value;
                                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(this.MediaType));
                            }
                            else
                            {
                                client.DefaultRequestHeaders.Add(header.Key, header.Value);
                            }
                        }
                    }

                    // Build the request body
                    StringContent content = null;
                    if (requestBody != null)
                    {
                        content = new StringContent(requestBody, Encoding.UTF8, this.MediaType);
                    }

                    // Build the final request
                    HttpRequestMessage request = new HttpRequestMessage
                    {
                        Method     = new HttpMethod(method.ToString()),
                        RequestUri = new Uri(endpoint),
                        Content    = content
                    };
                    return(await this.MakeRequest(client, request, cancellationToken).ConfigureAwait(false));
                }
                catch (Exception ex)
                {
                    HttpResponseMessage response = new HttpResponseMessage();
                    string message;
                    message          = (ex is HttpRequestException) ? ".NET HttpRequestException" : ".NET Exception";
                    message          = message + ", raw message: \n\n";
                    response.Content = new StringContent(message + ex.Message);
                    return(new Response(response.StatusCode, response.Content, response.Headers));
                }
            }
        }