/// <summary>
 /// Creates a HttpResponse object from the incoming HttpResponseMessage and parses it's content for the specified type.
 /// </summary>
 /// <typeparam name="T">The type of the parsed content.</typeparam>
 /// <param name="serializer">The IHttpContentSerializer to be used.</param>
 /// <param name="responseMessage">The recieved HttpResponseMessage to be parsed.</param>
 /// <param name="cancellationToken">A CancellationToken that can be used to cancel this operation.</param>
 /// <returns>The HttpResponse instance.</returns>
 public virtual async Task <HttpResponse <T> > CreateResponseAsync <T>(IHttpContentSerializer serializer, HttpResponseMessage responseMessage, CancellationToken cancellationToken = default)
 {
     return(new HttpResponse <T>
            (
                responseMessage.StatusCode,
                await serializer.DeserializeResponseContentAsync <T>(responseMessage.Content, cancellationToken)
            ));
 }
Exemplo n.º 2
0
 /// <summary>
 /// Creates a new <see cref="RefitSettings"/> instance with the specified parameters
 /// </summary>
 /// <param name="contentSerializer">The <see cref="IHttpContentSerializer"/> instance to use</param>
 /// <param name="urlParameterFormatter">The <see cref="IUrlParameterFormatter"/> instance to use (defaults to <see cref="DefaultUrlParameterFormatter"/>)</param>
 /// <param name="formUrlEncodedParameterFormatter">The <see cref="IFormUrlEncodedParameterFormatter"/> instance to use (defaults to <see cref="DefaultFormUrlEncodedParameterFormatter"/>)</param>
 public RefitSettings(
     IHttpContentSerializer contentSerializer,
     IUrlParameterFormatter?urlParameterFormatter = null,
     IFormUrlEncodedParameterFormatter?formUrlEncodedParameterFormatter = null)
 {
     ContentSerializer                = contentSerializer ?? throw new ArgumentNullException(nameof(contentSerializer), "The content serializer can't be null");
     UrlParameterFormatter            = urlParameterFormatter ?? new DefaultUrlParameterFormatter();
     FormUrlEncodedParameterFormatter = formUrlEncodedParameterFormatter ?? new DefaultFormUrlEncodedParameterFormatter();
     ExceptionFactory = new DefaultApiExceptionFactory(this).CreateAsync;
 }
        /// <summary>
        /// Creates a new HttpRequestMessage instance for the outgoint request.
        /// </summary>
        /// <param name="serializer">The seralizer to be used for the requests content.</param>
        /// <param name="request">The requests parameteres.</param>
        /// <param name="cancellationToken">A CancellationToken that can be used to cancel this operation.</param>
        /// <returns>The HttpRequest instance to be sent.</returns>
        public virtual async Task <HttpRequestMessage> CreateHttpRequestMessageAsync(IHttpContentSerializer serializer, HttpRequestBase request, CancellationToken cancellationToken = default)
        {
            var requestMessage = new HttpRequestMessage(request.Method, await CreateRequestUriAsync(BaseUri, request.RequestUri, request.QueryParameters));

            if (request is HttpRequestWithContent requestWithContent && requestWithContent.Content != null)
            {
                requestMessage.Content = await serializer.SerializeRequestContentAsync(requestWithContent.Content, cancellationToken);
            }

            if (UseGzip)
            {
                requestMessage.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue(GzipEncoding));
                requestMessage.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue(DeflateEncoding));
            }

            foreach (var header in await serializer.CreateAcceptCharsetHeaderValuesAsync())
            {
                requestMessage.Headers.AcceptCharset.Add(header);
            }

            foreach (var header in await serializer.CreateAcceptHeaderValuesAsync())
            {
                requestMessage.Headers.Accept.Add(header);
            }

            if (request.Headers != null)
            {
                foreach (var header in request.Headers)
                {
                    var headerValue = header.Value.StringValue;

                    if (!string.IsNullOrEmpty(headerValue))
                    {
                        requestMessage.Headers.Add(header.Key, headerValue);
                    }
                }
            }

            return(requestMessage);
        }
Exemplo n.º 4
0
        public HttpSerializer(IServiceProvider provider, ClientConfiguration config)
        {
            _provider = provider;
            _config   = config;

            Serializer = (IHttpContentSerializer)_provider.GetService(_config.Serializer);

            Deserializers = new Dictionary <string, IHttpContentSerializer>();
            foreach (var serType in _config.Deserializers)
            {
                var ser = (IHttpContentSerializer)_provider.GetService(serType);
                foreach (var contentType in ser.ContentTypes ?? Enumerable.Empty <string>())
                {
                    if (Deserializers.ContainsKey(contentType))
                    {
                        Deserializers[contentType] = ser;
                    }
                    else
                    {
                        Deserializers.Add(contentType, ser);
                    }
                }
            }
        }
Exemplo n.º 5
0
 public CybtansContentSerializer(IHttpContentSerializer defaultSerializer) : this(BinarySerializer.DefaultEncoding, defaultSerializer)
 {
 }
Exemplo n.º 6
0
 public CybtansContentSerializer(Encoding encoding, IHttpContentSerializer defaultSerializer)
 {
     _encoding          = encoding;
     _mediaType         = $"{BinarySerializer.MEDIA_TYPE}; charset={_encoding.WebName}";
     _defaultSerializer = defaultSerializer;
 }
 /// <summary>
 /// Creates a HttpResponse object from the incoming HttpResponseMessage without parsing it's content.
 /// </summary>
 /// <param name="serializer">The IHttpContentSerializer to be used.</param>
 /// <param name="responseMessage">The recieved HttpResponseMessage to be parsed.</param>
 /// <param name="cancellationToken">A CancellationToken that can be used to cancel this operation.</param>
 /// <returns>The HttpResponse instance.</returns>
 public virtual Task <HttpResponse> CreateResponseAsync(IHttpContentSerializer serializer, HttpResponseMessage responseMessage, CancellationToken cancellationToken = default)
 {
     return(Task.FromResult(new HttpResponse(responseMessage.StatusCode)));
 }
 /// <summary>
 ///     Serializes the specified <paramref name="content"/> into a new
 ///     <see cref="HttpContent"/> instance.
 ///
 ///     This method passes the type of the generic type parameter <typeparamref name="T"/>
 ///     as the content type to the serializer.
 /// </summary>
 /// <typeparam name="T">
 ///     The target type of the object which is supposed to be serialized.
 /// </typeparam>
 /// <param name="serializer">The serializer.</param>
 /// <param name="content">
 ///     The object to be serialized into a new <see cref="HttpContent"/> instance.
 ///     This can be <see langword="null"/>.
 /// </param>
 /// <param name="encoding">
 ///     An optional encoding to be used by the serializer if it serializes the <paramref name="content"/>
 ///     to an <see cref="HttpContent"/> which requires one.
 ///     If <see langword="null"/>, a default encoding is used.
 /// </param>
 /// <returns>
 ///     A new <see cref="HttpContent"/> instance which holds the serialized <paramref name="content"/>
 ///     or <see langword="null"/>.
 /// </returns>
 /// <exception cref="ArgumentNullException">
 ///     * <paramref name="serializer"/>
 /// </exception>
 /// <exception cref="HttpContentSerializationException">
 ///     Serializing the <paramref name="content"/> failed.
 /// </exception>
 public static HttpContent?Serialize <T>(this IHttpContentSerializer serializer, T content, Encoding?encoding)
 {
     _ = serializer ?? throw new ArgumentNullException(nameof(serializer));
     return(serializer.Serialize(content, typeof(T), encoding));
 }