예제 #1
0
        protected async Task <RestResponse <TResponseModel> > ExecuteWithRequestBodyAsync <TResponseModel, TRequestModel>(
            Func <Uri, StringContent, CancellationToken, Task <HttpResponseMessage> > action, Uri uri,
            TRequestModel requestModel, CancellationToken cancellationToken = default)
            where TResponseModel : class
            where TRequestModel : class
        {
            var requestBody = new StringContent(_format.Serialize(requestModel), RequestEncoding, _format.MediaType);
            var message     = await action(uri, requestBody, cancellationToken);

            var responseBody = await message.Content.ReadAsStringAsync();

            return(new RestResponse <TResponseModel>(responseBody, _format, message));
        }
예제 #2
0
        public void WriteOutputContent(MediaType mediaType, IFormat format)
        {
            if (mediaType == null)
            {
                throw new ArgumentNullException("mediaType", "mediaType cannot be null.");
            }

            if (format == null)
            {
                throw new ArgumentNullException("format", "format cannot be null.");
            }

            object resp = this.ResponseObject;
            this.Headers["Content-Type"] = format.ContentType(mediaType);

            if (resp != null)
            {
                format.Serialize(mediaType, resp, this.OutputStream);
            }
        }
예제 #3
0
        public void WriteOutputContent(MediaType mediaType, IFormat format)
        {
            if (mediaType == null)
            {
                throw new ArgumentNullException("mediaType", "mediaType cannot be null.");
            }

            if (format == null)
            {
                throw new ArgumentNullException("format", "format cannot be null.");
            }

            string contentType = format.ContentType(mediaType);

            if (contentType.Contains("*"))
            {
                throw new InvalidOperationException(
                    string.Format(
                        CultureInfo.InvariantCulture,
                        "Format {0} returned a Content-Type of {1} for the chosen media type of {2}. Writing a wildcard Content-Type to the response is not supported.",
                        format.GetType(),
                        contentType,
                        mediaType));
            }

            object resp = this.ResponseObject;
            this.httpResponse.ContentType = contentType;

            if (resp != null)
            {
                format.Serialize(mediaType, resp, this.httpResponse.OutputStream);
            }
        }