예제 #1
0
        /// <exception cref="WebException"></exception>
        /// <exception cref="InvalidDataException"></exception>
        /// <exception cref="TinyTimeoutException">Tiny client timeout</exception>
        public IHttpResponse Send(HttpClientRequest request)
        {
            if (!request.Timeout.HasValue && Timeout.HasValue)
            {
                request.SetTimeout(Timeout.Value);
            }

            if (request.KeepAlive == KeepAliveMode.UpToClient)
            {
                request.SetKeepAlive(_keepAlive);
            }

            if (_requestPreprocessor != null)
            {
                request = _requestPreprocessor(request);
            }

            var response = _sender.Send(request);

            if (_responsePreprocessor != null)
            {
                response = _responsePreprocessor(response);
            }

            return(response);
        }
예제 #2
0
        public static HttpClientRequest SetMultipartContent(
            this HttpClientRequest request,
            string boundary,
            params HttpClientRequest[] requests)
        {
            var content = new MultipartContent(requests, boundary);

            request.SetContent(content);

            var serializers = requests.Select(r => new TextResponseDeserialaizer()).ToArray();

            request.SetDeserializer(new MultipartRequestDeserializer(serializers));

            return(request);
        }
예제 #3
0
        public HttpClientRequest GetCopyFor(string subQuery)
        {
            var copy = new HttpClientRequest(this.Method, subQuery)
            {
                Content      = this.Content,
                Deserializer = this.Deserializer,
                Encoder      = this.Encoder,
                KeepAlive    = this.KeepAlive,
                Timeout      = this.Timeout,
            };

            foreach (var header in headers)
            {
                copy.AddCustomHeader(header.Key, header.Value);
            }

            foreach (var queryParam in queryParams)
            {
                copy.AddUriParam(queryParam.Key, queryParam.Value);
            }

            return(copy);
        }
예제 #4
0
 public static HttpClientRequest SetJsonContent(
     this HttpClientRequest request,
     object serializeableObject)
 => request.SetContent(new JsonContent(serializeableObject));
예제 #5
0
 public static HttpClientRequest SetMultipartContent(
     this HttpClientRequest request,
     params HttpClientRequest[] requests)
 => SetMultipartContent(request, Guid.NewGuid().ToString(), requests);
 /// <exception cref="WebException">Channel error</exception>
 /// <exception cref="TinyHttpException">Server side error</exception>
 /// <exception cref="InvalidDataException">Data format error</exception>
 /// <exception cref="TinyTimeoutException">Tiny client timeout</exception>
 public static HttpResponse <string> PutAndReceiveText(this HttpClient client, string query,
                                                       object jsonSerializeableContent, bool throwIfFailed = true) => client.SendAndReceiveText(HttpClientRequest.CreateJsonPut(query, jsonSerializeableContent), throwIfFailed);
 /// <exception cref="WebException">Channel error</exception>
 /// <exception cref="TinyHttpException">Server side error</exception>
 /// <exception cref="InvalidDataException">Data format error</exception>
 /// <exception cref="TinyTimeoutException">Tiny client timeout</exception>
 public static HttpResponse <string> GetAndReceiveText(this HttpClient client, string query, bool throwIfFailed = true)
 => client.SendAndReceiveText(HttpClientRequest.CreateGet(query), throwIfFailed);
        /// <exception cref="WebException">Channel error</exception>
        /// <exception cref="TinyHttpException">Server side error</exception>
        /// <exception cref="InvalidDataException">Data format error</exception>
        /// <exception cref="TinyTimeoutException">Tiny client timeout</exception>
        public static TJsonObject SendAndReceiveJsonObject <TJsonObject>(this HttpClient client, HttpClientRequest request, bool throwIfFailed = true)
        {
            var response = client.Send(request);

            if (throwIfFailed)
            {
                response.ThrowIfFailed();
            }

            var stringResponse = response as HttpResponse <string>;

            if (stringResponse == null)
            {
                throw new InvalidDataException("Unexpected response format");
            }
            try
            {
                return(JsonHelper.Deserialize <TJsonObject>(stringResponse.Content));
            }
            catch (JsonException e)
            {
                throw new InvalidDataException("Json deserialization exception: " + e.Message, e);
            }
        }
 /// <exception cref="WebException">Channel error</exception>
 /// <exception cref="TinyHttpException">Server side error</exception>
 /// <exception cref="InvalidDataException">Data format error</exception>
 /// <exception cref="TinyTimeoutException">Tiny client timeout</exception>
 public static TResponseJsonObject PostAndReceiveJson <TResponseJsonObject>(this HttpClient client, string query, object jsonSerializeableContent, bool throwIfFailed = true)
 => client.SendAndReceiveJsonObject <TResponseJsonObject>(HttpClientRequest.CreateJsonPost(query, jsonSerializeableContent), throwIfFailed);
 /// <exception cref="WebException">Channel error</exception>
 /// <exception cref="TinyHttpException">Server side error</exception>
 /// <exception cref="InvalidDataException">Data format error</exception>
 /// <exception cref="TinyTimeoutException">Tiny client timeout</exception>
 public static TResponseJsonObject GetAndReceiveJson <TResponseJsonObject>(this HttpClient client, string query, bool throwIfFailed = true)
 => client.SendAndReceiveJsonObject <TResponseJsonObject>(HttpClientRequest.CreateGet(query), throwIfFailed);
 /// <exception cref="WebException">Channel error</exception>
 /// <exception cref="InvalidDataException">Data format error</exception>
 /// <exception cref="TinyTimeoutException">Tiny client timeout</exception>
 public static IHttpResponse SendJsonPut(this HttpClient client, string query,
                                         object jsonSerializeableObject)
 => client.Send(HttpClientRequest.CreateJsonPut(query, jsonSerializeableObject));
 /// <exception cref="WebException">Channel error</exception>
 /// <exception cref="TinyTimeoutException">Tiny client timeout</exception>
 /// <exception cref="InvalidDataException">Data format error</exception>
 public static IHttpResponse SendGet(this HttpClient client, string query)
 => client.Send(HttpClientRequest.CreateGet(query));
        /// <exception cref="WebException">Channel error</exception>
        /// <exception cref="TinyHttpException">Server side error</exception>
        /// <exception cref="InvalidDataException">Data format error</exception>
        /// <exception cref="TinyTimeoutException">Tiny client timeout</exception>
        public static HttpResponse <string> SendAndReceiveText(this HttpClient client, HttpClientRequest request, bool throwIfFailed = true)
        {
            var response = client.Send(request);

            if (throwIfFailed)
            {
                response.ThrowIfFailed();
            }

            var stringResponse = response as HttpResponse <String>;

            if (stringResponse == null)
            {
                throw new InvalidDataException("Unexpected response format");
            }
            return(stringResponse);
        }