Beispiel #1
0
        /// <summary>Creates and configures a new instance of the <see cref="UriBuilder"/> class.</summary>
        /// <param name="baseUri">The base URI.</param>
        /// <param name="resource">The resource name.</param>
        /// <param name="formData">The form data.</param>
        /// <returns>The <see cref="Uri"/>.</returns>
        /// <exception cref="FormatException">One or more query parameters violate the format for a valid URI as defined by RFC 2396.</exception>
        protected virtual Uri BuildRequestUri(Uri baseUri, string resource, UrlEncodedForm formData)
        {
            Debug.Assert(baseUri != null, "baseUri != null");
            Debug.Assert(formData != null, "formData != null");
            var uriBuilder = new UriBuilder(baseUri)
            {
                Path  = resource,
                Query = formData.GetQueryString()
            };

            return(uriBuilder.Uri);
        }
Beispiel #2
0
        /// <summary>Sends a request and returns the response.</summary>
        /// <param name="request">The service request.</param>
        /// <param name="cancellationToken">The <see cref="CancellationToken"/> that provides cancellation support.</param>
        /// <typeparam name="TResult">The type of the response content.</typeparam>
        /// <exception cref="ServiceException">The service responded with an error code.</exception>
        /// <returns>An instance of the specified type.</returns>
        public Task <IResponse <TResult> > SendAsync <TResult>(IRequest request, CancellationToken cancellationToken)
        {
            // Translate the request to form data
            var formData = new UrlEncodedForm();

            foreach (var parameter in request.GetParameters())
            {
                formData[parameter.Key] = parameter.Value;
            }

            // Build the resource URI
            Uri uri;

            try
            {
                uri = this.BuildRequestUri(this.baseUri, request.Resource, formData);
            }
            catch (FormatException formatException)
            {
                // Caught when the given parameters would result in an invalid URI
                // Wrap the FormatException in a ServiceException
                throw new ServiceException("An error occurred while formatting the request URI. See the inner exception for details.", formatException)
                      {
                          Request = request
                      };
            }

            // Handle the request
            var httpWebRequest = this.CreateHttpWebRequest(uri);

            return(this.GetHttpWebResponseAsync(httpWebRequest, cancellationToken).ContinueWith(
                       task =>
            {
                using (var response = task.Result)
                {
                    try
                    {
                        return this.OnResponse <TResult>(response);
                    }
                    catch (ServiceException exception)
                    {
                        // Set the cause of this exception
                        exception.Request = request;

                        // Rethrow
                        throw;
                    }
                }
            },
                       cancellationToken));
        }
Beispiel #3
0
        /// <summary>Sends a request and returns the response.</summary>
        /// <param name="request">The service request.</param>
        /// <typeparam name="TResult">The type of the response content.</typeparam>
        /// <exception cref="ServiceException">The service responded with an error code.</exception>
        /// <returns>An instance of the specified type.</returns>
        public IResponse <TResult> Send <TResult>(IRequest request)
        {
            // Translate the request to form data
            var formData = new UrlEncodedForm();

            foreach (var parameter in request.GetParameters())
            {
                formData[parameter.Key] = parameter.Value;
            }

            // Build the resource URI
            Uri uri;

            try
            {
                uri = this.BuildRequestUri(this.baseUri, request.Resource, formData);
            }
            catch (FormatException formatException)
            {
                // Caught when the given parameters would result in an invalid URI
                // Wrap the FormatException in a ServiceException
                throw new ServiceException("An error occurred while formatting the request URI. See the inner exception for details.", formatException)
                      {
                          Request = request
                      };
            }

            // Handle the request
            try
            {
                var httpWebRequest = this.CreateHttpWebRequest(uri);
                using (var response = this.GetHttpWebResponse(httpWebRequest))
                {
                    Debug.Assert(response != null, "response != null");
                    return(this.OnResponse <TResult>(response));
                }
            }
            catch (ServiceException serviceException)
            {
                // Set the cause of this ServiceException
                serviceException.Request = request;

                // Rethrow
                throw;
            }
        }