Пример #1
0
 public T Get <T>(RestSharpParams settings)
 {
     if (settings.IsDeserializeCamelCaseFormat)
     {
         return(JsonConvert.DeserializeObject <T>(Request(settings), JsonSerializerSettings));
     }
     return(JsonConvert.DeserializeObject <T>(Request(settings)));
 }
Пример #2
0
        private string Request(RestSharpParams settings)
        {
            var response = GetRestResponse(settings);

            if (response.ErrorException != null)
            {
                throw new Exception(
                          $"Error while fetching the data, Endpoint : {settings.ApiEndPoint}, ErrorException : {response.ErrorException}");
            }
            return(!response.IsSuccessful ? string.Empty : response.Content);
        }
Пример #3
0
        private IRestResponse GetRestResponse(RestSharpParams settings)
        {
            var endpointUri = $"{settings.BaseUrl}{settings.ApiEndPoint}";
            var client      = new RestClient(settings.BaseUrl);
            var request     = new RestRequest(settings.ApiEndPoint, settings.MethodType)
            {
                RequestFormat = settings.DataFormat,
            };

            // Add headers
            if (settings.Headers != null && settings.Headers.Any())
            {
                foreach (var header in settings.Headers)
                {
                    request.AddHeader(header.Key, header.Value);
                }
            }

            // Add parameters
            if (settings.Parameters != null && settings.Parameters.Any())
            {
                foreach (var parameter in settings.Parameters)
                {
                    request.AddParameter(parameter.Key, parameter.Value);
                }
            }

            // Add parameters using object
            if (settings.Parameter != null)
            {
                request.AddJsonBody(settings.Parameter);
            }

            var result = client.ExecuteTaskAsync(request).Result;

            return(result);
        }
Пример #4
0
 public void Delete(RestSharpParams settings) => Request(settings);
Пример #5
0
 public string Post(RestSharpParams settings) => Request(settings);