Exemplo n.º 1
0
 private static void SetRequestBody(IRestRequest request, RestClientParameters restClientParameters)
 {
     if (restClientParameters.RequestBody != null)
     {
         request.AddBody(restClientParameters.RequestBody);
     }
 }
Exemplo n.º 2
0
 private static void SetRequestSerializer(IRestRequest request, RestClientParameters restClientParameters)
 {
     if (restClientParameters.RequestDataFormat == DataFormat.Json &&
         restClientParameters.RequestDataSerializer != default)
     {
         request.JsonSerializer = restClientParameters.RequestDataSerializer;
     }
 }
Exemplo n.º 3
0
        private void AddResponseHandlers(IRestClient client, RestClientParameters restClientParameters)
        {
            var contentType = DataFormatHelper.ToContentType(restClientParameters.ResponseDataFormat);

            if (contentType != null)
            {
                client.AddHandler(contentType, () => restClientParameters.ResponseDataDeserializer);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Returns an initialized client.
        /// </summary>
        /// <param name="restClientParameters">Parameters.</param>
        /// <returns>Rest client.</returns>
        public IRestClient GetRestClient(RestClientParameters restClientParameters)
        {
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            var client = new RestClient(restClientParameters.BaseUrl)
            {
                Authenticator = GetAuthenticator(),
            };

            AddResponseHandlers(client, restClientParameters);
            return(client);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Returns an initialized request.
        /// </summary>
        /// <param name="restClientParameters">Parameters.</param>
        /// <returns>Rest request.</returns>
        public IRestRequest GetRestRequest(RestClientParameters restClientParameters)
        {
            var request = new RestRequest(restClientParameters.RequestUrl)
            {
                Method        = restClientParameters.RequestMethod.Map <RequestMethod, Method>(),
                RequestFormat = restClientParameters.RequestDataFormat.Map <DataFormat, RestSharp.DataFormat>(),
            };

            SetRequestParameters(request, restClientParameters.RequestHeaders, ParameterType.HttpHeader);
            SetRequestParameters(request, restClientParameters.RequestUrlSegments, ParameterType.UrlSegment);
            SetRequestParameters(request, restClientParameters.RequestQueryParameters, ParameterType.QueryString);
            SetRequestBody(request, restClientParameters);
            SetRequestSerializer(request, restClientParameters);
            return(request);
        }