Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2020 Robert Bosch GmbH |
| 3 | * Author: Constantin Ziesche (constantin.ziesche@bosch.com) |
| 4 | * |
| 5 | * This program and the accompanying materials are made available under the |
| 6 | * terms of the Eclipse Public License 2.0 which is available at |
| 7 | * http://www.eclipse.org/legal/epl-2.0 |
| 8 | * |
| 9 | * SPDX-License-Identifier: EPL-2.0 |
| 10 | *******************************************************************************/ |
| 11 | using BaSyx.Utils.ResultHandling; |
| 12 | using BaSyx.Utils.Settings.Sections; |
| 13 | using Newtonsoft.Json; |
| 14 | using Newtonsoft.Json.Linq; |
| 15 | using System; |
Constantin Ziesche | 7b6d479 | 2020-08-18 17:15:11 +0200 | [diff] [blame] | 16 | using System.Collections; |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 17 | using System.Collections.Generic; |
| 18 | using System.Net; |
| 19 | using System.Net.Http; |
| 20 | using System.Text; |
| 21 | using System.Threading.Tasks; |
| 22 | |
| 23 | namespace BaSyx.Utils.Client.Http |
| 24 | { |
| 25 | public abstract class SimpleHttpClient |
| 26 | { |
| 27 | public HttpClient HttpClient { get; } |
| 28 | public HttpClientHandler HttpClientHandler { get; } |
| 29 | public JsonSerializerSettings JsonSerializerSettings { get; protected set; } |
| 30 | |
Constantin Ziesche | 7b6d479 | 2020-08-18 17:15:11 +0200 | [diff] [blame] | 31 | public const int DEFAULT_REQUEST_TIMEOUT = 20000; |
| 32 | |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 33 | protected SimpleHttpClient() |
| 34 | { |
| 35 | HttpClientHandler = new HttpClientHandler() { MaxConnectionsPerServer = 100, UseProxy = false }; |
| 36 | HttpClient = new HttpClient(HttpClientHandler); |
| 37 | |
Constantin Ziesche | 7b6d479 | 2020-08-18 17:15:11 +0200 | [diff] [blame] | 38 | JsonSerializerSettings = new JsonSerializerSettings() |
| 39 | { |
| 40 | NullValueHandling = NullValueHandling.Ignore, |
| 41 | Formatting = Formatting.Indented, |
| 42 | DefaultValueHandling = DefaultValueHandling.Include, |
| 43 | MissingMemberHandling = MissingMemberHandling.Ignore |
| 44 | }; |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | protected virtual void LoadProxy(ProxyConfiguration proxyConfiguration) |
| 48 | { |
| 49 | if (proxyConfiguration == null) |
| 50 | return; |
| 51 | |
| 52 | if (proxyConfiguration.UseProxy && !string.IsNullOrEmpty(proxyConfiguration.ProxyAddress)) |
| 53 | { |
| 54 | HttpClientHandler.UseProxy = true; |
| 55 | if (!string.IsNullOrEmpty(proxyConfiguration.UserName) && !string.IsNullOrEmpty(proxyConfiguration.Password)) |
| 56 | { |
| 57 | NetworkCredential credential; |
| 58 | if (!string.IsNullOrEmpty(proxyConfiguration.Domain)) |
| 59 | credential = new NetworkCredential(proxyConfiguration.UserName, proxyConfiguration.Password, proxyConfiguration.Domain); |
| 60 | else |
| 61 | credential = new NetworkCredential(proxyConfiguration.UserName, proxyConfiguration.Password); |
| 62 | |
| 63 | HttpClientHandler.Proxy = new WebProxy(proxyConfiguration.ProxyAddress, false, null, credential); |
| 64 | } |
| 65 | else |
| 66 | HttpClientHandler.Proxy = new WebProxy(proxyConfiguration.ProxyAddress); |
| 67 | } |
| 68 | else |
| 69 | HttpClientHandler.UseProxy = false; |
| 70 | } |
| 71 | |
| 72 | protected virtual IResult<HttpResponseMessage> SendRequest(HttpRequestMessage message, int timeout) |
| 73 | { |
| 74 | try |
| 75 | { |
| 76 | var task = HttpClient.SendAsync(message); |
| 77 | if (Task.WhenAny(task, Task.Delay(timeout)).Result == task) |
| 78 | { |
| 79 | return new Result<HttpResponseMessage>(true, task.Result); |
| 80 | } |
| 81 | else |
| 82 | { |
| 83 | return new Result<HttpResponseMessage>(false, new List<IMessage> { new Message(MessageType.Error, "Error while sending the request: timeout") }); |
| 84 | } |
| 85 | } |
| 86 | catch (Exception e) |
| 87 | { |
| 88 | return new Result<HttpResponseMessage>(e); |
| 89 | } |
| 90 | } |
| 91 | |
Constantin Ziesche | 310d2aa | 2020-03-25 11:48:26 +0100 | [diff] [blame] | 92 | protected virtual async Task<IResult<HttpResponseMessage>> SendRequestAsync(HttpRequestMessage message) |
| 93 | { |
| 94 | try |
| 95 | { |
| 96 | using(HttpResponseMessage response = await HttpClient.SendAsync(message).ConfigureAwait(false)) |
| 97 | return new Result<HttpResponseMessage>(true, response); |
| 98 | } |
| 99 | catch (Exception e) |
| 100 | { |
| 101 | return new Result<HttpResponseMessage>(e); |
| 102 | } |
| 103 | } |
| 104 | |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 105 | protected virtual HttpRequestMessage CreateRequest(Uri uri, HttpMethod method) |
| 106 | { |
| 107 | return new HttpRequestMessage(method, uri); |
| 108 | } |
| 109 | |
| 110 | protected virtual HttpRequestMessage CreateRequest(Uri uri, HttpMethod method, HttpContent content) |
| 111 | { |
| 112 | var message = CreateRequest(uri, method); |
| 113 | if (content != null) |
| 114 | message.Content = content; |
| 115 | |
| 116 | return message; |
| 117 | } |
| 118 | |
| 119 | protected virtual HttpRequestMessage CreateJsonContentRequest(Uri uri, HttpMethod method, object content) |
| 120 | { |
| 121 | var message = CreateRequest(uri, method, () => |
| 122 | { |
| 123 | var serialized = JsonConvert.SerializeObject(content, JsonSerializerSettings); |
| 124 | return new StringContent(serialized, Encoding.UTF8, "application/json"); |
| 125 | }); |
| 126 | return message; |
| 127 | } |
| 128 | |
| 129 | protected virtual HttpRequestMessage CreateRequest(Uri uri, HttpMethod method, Func<HttpContent> content) |
| 130 | { |
| 131 | var message = CreateRequest(uri, method); |
| 132 | if (content != null) |
| 133 | message.Content = content.Invoke(); |
| 134 | |
| 135 | return message; |
| 136 | } |
| 137 | |
| 138 | protected virtual IResult EvaluateResponse(IResult result, HttpResponseMessage response) |
| 139 | { |
| 140 | var messageList = new List<IMessage>(); |
| 141 | messageList.AddRange(result.Messages); |
| 142 | |
| 143 | if (response != null) |
| 144 | { |
| 145 | var responseString = response.Content.ReadAsStringAsync().Result; |
| 146 | if (response.IsSuccessStatusCode) |
| 147 | { |
| 148 | messageList.Add(new Message(MessageType.Information, response.ReasonPhrase, ((int)response.StatusCode).ToString())); |
| 149 | return new Result(true, messageList); |
| 150 | } |
| 151 | else |
| 152 | { |
Constantin Ziesche | 310d2aa | 2020-03-25 11:48:26 +0100 | [diff] [blame] | 153 | messageList.Add(new Message(MessageType.Error, response.ReasonPhrase + " | " + responseString, ((int)response.StatusCode).ToString())); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 154 | return new Result(false, messageList); |
| 155 | } |
| 156 | } |
| 157 | messageList.Add(new Message(MessageType.Error, "Evaluation of response failed - Response from host is null", null)); |
| 158 | return new Result(false, messageList); |
| 159 | } |
| 160 | |
| 161 | protected virtual IResult<T> EvaluateResponse<T>(IResult result, HttpResponseMessage response) |
| 162 | { |
| 163 | var messageList = new List<IMessage>(); |
| 164 | messageList.AddRange(result.Messages); |
| 165 | |
| 166 | if (response != null) |
| 167 | { |
| 168 | var responseString = response.Content.ReadAsStringAsync().Result; |
| 169 | if (response.IsSuccessStatusCode) |
| 170 | { |
| 171 | try |
| 172 | { |
Constantin Ziesche | 7b6d479 | 2020-08-18 17:15:11 +0200 | [diff] [blame] | 173 | T requestResult = JsonConvert.DeserializeObject<T>(responseString, JsonSerializerSettings); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 174 | |
| 175 | messageList.Add(new Message(MessageType.Information, response.ReasonPhrase, ((int)response.StatusCode).ToString())); |
| 176 | return new Result<T>(true, requestResult, messageList); |
| 177 | } |
| 178 | catch (Exception e) |
| 179 | { |
Constantin Ziesche | 7b6d479 | 2020-08-18 17:15:11 +0200 | [diff] [blame] | 180 | messageList.Add(new ExceptionMessage(e)); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 181 | return new Result<T>(false, messageList); |
| 182 | } |
| 183 | } |
| 184 | else |
| 185 | { |
Constantin Ziesche | 310d2aa | 2020-03-25 11:48:26 +0100 | [diff] [blame] | 186 | messageList.Add(new Message(MessageType.Error, response.ReasonPhrase + " | " + responseString, ((int)response.StatusCode).ToString())); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 187 | return new Result<T>(false, messageList); |
| 188 | } |
| 189 | } |
| 190 | messageList.Add(new Message(MessageType.Error, "Evaluation of response failed - Response from host is null", null)); |
| 191 | return new Result<T>(false, messageList); |
| 192 | } |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 193 | } |
| 194 | } |