コード例 #1
0
        public BaseClient(string baseUrl, ISerializer dataSerializer, IRequestDataWriter requestDataWriter, IResponseDataReader responseDataReader, int?requestTimeoutInSeconds = null)
        {
            if (!RestUriBuilder.IsValidUri(baseUrl))
            {
                throw new ArgumentException($"{nameof(baseUrl)} must be set to a valid Url");
            }

            if (dataSerializer == null)
            {
                throw new ArgumentException($"{nameof(dataSerializer)} can not be Null");
            }

            if (requestDataWriter == null)
            {
                throw new ArgumentException($"{nameof(requestDataWriter)} can not be Null");
            }

            if (responseDataReader == null)
            {
                throw new ArgumentException($"{nameof(responseDataReader)} can not be Null");
            }

            BaseUrl        = baseUrl;
            Serializer     = dataSerializer;
            RequestWriter  = requestDataWriter;
            ResponseReader = responseDataReader;

            Headers       = new Dictionary <HttpRequestHeader, string>();
            CustomHeaders = new Dictionary <string, string>();

            if (requestTimeoutInSeconds.HasValue)
            {
                RequestTimeoutInMilliseconds = (int)Math.Truncate(TimeSpan.FromSeconds(requestTimeoutInSeconds.Value).TotalMilliseconds);
            }
        }
コード例 #2
0
        public async Task <T> ExecuteCall <T>(string method, HttpVerbs verb, object bodyParameter = null, IDictionary <string, string> queryStringParameters = null) where T : class
        {
            if (string.IsNullOrEmpty(method))
            {
                throw new ArgumentException($"{nameof(method)} must be set");
            }

            T result = null;

            var uri     = new RestUriBuilder(BaseUrl, method, queryStringParameters);
            var request = GetRequest(uri.ToString(), verb);

            string serializedObject = null;

            if (bodyParameter != null)
            {
                serializedObject = Serializer.Serialize(bodyParameter);

                await RequestWriter.WriteAsync(request, serializedObject);
            }

            string responseString = null;

            try
            {
                var response = (HttpWebResponse)await request.GetResponseAsync();

                using (response)
                {
                    responseString = await ResponseReader.ReadAsync(response);

                    if (!string.IsNullOrWhiteSpace(responseString))
                    {
                        result = Serializer.Deserialize <T>(responseString);
                    }
                }
            }
            catch (WebException we)
            {
                request?.Abort();

                var errorResponse = ((HttpWebResponse)we.Response);

                using (errorResponse)
                {
                    responseString = await ResponseReader.ReadAsync(errorResponse);

                    if (!string.IsNullOrWhiteSpace(responseString))
                    {
                        var errorResult = errorResponse.ContentType.EndsWith("json")
                                        ? Serializer.Deserialize <ErrorResponse>(responseString)
                                        : null;

                        log.Error($"RESTful '{method}' {verb.ToString().ToUpper()} failed with code: '{(int)errorResponse.StatusCode}', message: '{errorResult?.Error ?? errorResult?.Status ?? "NULL"}'", we);
                        throw;
                    }
                }
            }
            catch (Exception ex)
            {
                request?.Abort();

                log.Error(ex.Message, ex);
                throw;
            }

            return(result);
        }