|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Net; |
| 5 | +using System.Net.Http; |
| 6 | +using System.Text; |
| 7 | +using System.Threading; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Newtonsoft.Json; |
| 10 | + |
| 11 | +namespace RestSharp |
| 12 | +{ |
| 13 | + public class RestClient |
| 14 | + { |
| 15 | + public RestClientOptions Options { get; } |
| 16 | + private readonly HttpClient _httpClient; |
| 17 | + private static readonly HttpMethod PatchMethod = new HttpMethod("PATCH"); |
| 18 | + |
| 19 | + public RestClient(RestClientOptions options, Action<RestClient> configureSerialization = null) |
| 20 | + { |
| 21 | + Options = options; |
| 22 | + var handler = new HttpClientHandler(); |
| 23 | + if (options.Proxy != null) |
| 24 | + { |
| 25 | + handler.Proxy = options.Proxy; |
| 26 | + handler.UseProxy = true; |
| 27 | + } |
| 28 | + _httpClient = new HttpClient(handler) { BaseAddress = options.BaseUrl }; |
| 29 | + configureSerialization?.Invoke(this); |
| 30 | + } |
| 31 | + |
| 32 | + public RestClient(HttpClient httpClient, RestClientOptions options, Action<RestClient> configureSerialization = null) |
| 33 | + { |
| 34 | + Options = options; |
| 35 | + _httpClient = httpClient; |
| 36 | + if (Options.BaseUrl != null) |
| 37 | + _httpClient.BaseAddress = Options.BaseUrl; |
| 38 | + configureSerialization?.Invoke(this); |
| 39 | + } |
| 40 | + |
| 41 | + private HttpMethod ToHttpMethod(Method method) |
| 42 | + { |
| 43 | + return method switch |
| 44 | + { |
| 45 | + Method.Get => HttpMethod.Get, |
| 46 | + Method.Post => HttpMethod.Post, |
| 47 | + Method.Put => HttpMethod.Put, |
| 48 | + Method.Delete => HttpMethod.Delete, |
| 49 | + Method.Patch => PatchMethod, |
| 50 | + _ => HttpMethod.Get |
| 51 | + }; |
| 52 | + } |
| 53 | + |
| 54 | + private HttpRequestMessage BuildRequest(RestRequest request) |
| 55 | + { |
| 56 | + var uriBuilder = new UriBuilder(new Uri(Options.BaseUrl, request.Resource)); |
| 57 | + var queryParams = request.Parameters |
| 58 | + .Where(p => p.Type == ParameterType.QueryString) |
| 59 | + .Select(p => $"{Uri.EscapeDataString(p.Name)}={Uri.EscapeDataString(p.Value.ToString())}"); |
| 60 | + var query = string.Join("&", queryParams); |
| 61 | + if (!string.IsNullOrEmpty(query)) |
| 62 | + { |
| 63 | + if (string.IsNullOrEmpty(uriBuilder.Query)) |
| 64 | + uriBuilder.Query = query; |
| 65 | + else |
| 66 | + uriBuilder.Query = $"{uriBuilder.Query.TrimStart('?')}&{query}"; |
| 67 | + } |
| 68 | + var message = new HttpRequestMessage(ToHttpMethod(request.Method), uriBuilder.Uri); |
| 69 | + |
| 70 | + foreach (var header in request.Parameters.Where(p => p.Type == ParameterType.HttpHeader)) |
| 71 | + { |
| 72 | + message.Headers.TryAddWithoutValidation(header.Name, header.Value.ToString()); |
| 73 | + } |
| 74 | + |
| 75 | + if (request.JsonBody != null) |
| 76 | + { |
| 77 | + message.Content = new StringContent(request.JsonBody, Encoding.UTF8, "application/json"); |
| 78 | + } |
| 79 | + |
| 80 | + return message; |
| 81 | + } |
| 82 | + |
| 83 | + private RestResponse CreateRestResponse(HttpResponseMessage httpResponse, string content) |
| 84 | + { |
| 85 | + var response = new RestResponse |
| 86 | + { |
| 87 | + StatusCode = httpResponse.StatusCode, |
| 88 | + Content = content, |
| 89 | + ResponseStatus = ResponseStatus.Completed, |
| 90 | + ResponseUri = httpResponse.RequestMessage?.RequestUri |
| 91 | + }; |
| 92 | + |
| 93 | + foreach (var header in httpResponse.Headers) |
| 94 | + { |
| 95 | + foreach (var val in header.Value) |
| 96 | + { |
| 97 | + response.Headers.Add(new HeaderParameter { Name = header.Key, Value = val }); |
| 98 | + } |
| 99 | + } |
| 100 | + foreach (var header in httpResponse.Content.Headers) |
| 101 | + { |
| 102 | + foreach (var val in header.Value) |
| 103 | + { |
| 104 | + response.Headers.Add(new HeaderParameter { Name = header.Key, Value = val }); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return response; |
| 109 | + } |
| 110 | + |
| 111 | + public async Task<RestResponse> ExecuteAsync(RestRequest request, CancellationToken cancellationToken = default) |
| 112 | + { |
| 113 | + var message = BuildRequest(request); |
| 114 | + using var httpResponse = await _httpClient.SendAsync(message, cancellationToken).ConfigureAwait(false); |
| 115 | + var content = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 116 | + return CreateRestResponse(httpResponse, content); |
| 117 | + } |
| 118 | + |
| 119 | + public async Task<RestResponse<T>> ExecuteAsync<T>(RestRequest request, CancellationToken cancellationToken = default) |
| 120 | + { |
| 121 | + var baseResponse = await ExecuteAsync(request, cancellationToken).ConfigureAwait(false); |
| 122 | + var data = default(T); |
| 123 | + if (!string.IsNullOrEmpty(baseResponse.Content)) |
| 124 | + { |
| 125 | + try |
| 126 | + { |
| 127 | + data = JsonConvert.DeserializeObject<T>(baseResponse.Content); |
| 128 | + } |
| 129 | + catch |
| 130 | + { |
| 131 | + // ignore deserialization errors |
| 132 | + } |
| 133 | + } |
| 134 | + return new RestResponse<T> |
| 135 | + { |
| 136 | + StatusCode = baseResponse.StatusCode, |
| 137 | + Content = baseResponse.Content, |
| 138 | + ResponseStatus = baseResponse.ResponseStatus, |
| 139 | + ResponseUri = baseResponse.ResponseUri, |
| 140 | + Headers = baseResponse.Headers, |
| 141 | + ErrorMessage = baseResponse.ErrorMessage, |
| 142 | + Data = data |
| 143 | + }; |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments