|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Net.Http; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using CSF.Screenplay.Abilities; |
| 6 | +using Newtonsoft.Json; |
| 7 | + |
| 8 | +namespace CSF.Screenplay.JsonApis.Abilities |
| 9 | +{ |
| 10 | + public class ConsumeJsonWebServices : Ability |
| 11 | + { |
| 12 | + #region fields |
| 13 | + |
| 14 | + static readonly TimeSpan SystemDefaultTimeout = new TimeSpan(0, 0, 30); |
| 15 | + |
| 16 | + readonly TimeSpan defaultTimeout; |
| 17 | + readonly HttpClient httpClient; |
| 18 | + readonly JsonSerializer serializer; |
| 19 | + |
| 20 | + #endregion |
| 21 | + |
| 22 | + #region public API |
| 23 | + |
| 24 | + public virtual void Execute(IProvidesInvocationDetails invocationDetails) |
| 25 | + { |
| 26 | + GetResponse(invocationDetails); |
| 27 | + } |
| 28 | + |
| 29 | + public virtual T GetResult<T>(IProvidesInvocationDetails invocationDetails) |
| 30 | + { |
| 31 | + var response = GetResponse(invocationDetails); |
| 32 | + return ConvertResponse<T>(response); |
| 33 | + } |
| 34 | + |
| 35 | + #endregion |
| 36 | + |
| 37 | + #region private methods |
| 38 | + |
| 39 | + HttpResponseMessage GetResponse(IProvidesInvocationDetails invocationDetails) |
| 40 | + { |
| 41 | + if(invocationDetails == null) |
| 42 | + throw new ArgumentNullException(nameof(invocationDetails)); |
| 43 | + |
| 44 | + var requestMessage = invocationDetails.GetRequestMessage(); |
| 45 | + var timeout = GetTimeout(invocationDetails); |
| 46 | + |
| 47 | + var response = httpClient.SendAsync(requestMessage); |
| 48 | + |
| 49 | + var waitSuccess = response.Wait(timeout); |
| 50 | + if(!waitSuccess) |
| 51 | + throw new TimeoutException($"Timeout waiting for a response from a JSON API. Waited for {timeout.ToString("g")}"); |
| 52 | + |
| 53 | + AssertThatResultIsSuccess(response.Result, timeout); |
| 54 | + |
| 55 | + return response.Result; |
| 56 | + } |
| 57 | + |
| 58 | + void AssertThatResultIsSuccess(HttpResponseMessage result, TimeSpan timeout) |
| 59 | + { |
| 60 | + try |
| 61 | + { |
| 62 | + result.EnsureSuccessStatusCode(); |
| 63 | + } |
| 64 | + catch(HttpRequestException ex) |
| 65 | + { |
| 66 | + var response = GetResultString(result, timeout); |
| 67 | + throw new JsonApiException($@"The API request failed: HTTP {result.StatusCode.ToString()} |
| 68 | +{response}", ex); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + string GetResultString(HttpResponseMessage result, TimeSpan timeout) |
| 73 | + { |
| 74 | + Task<string> contentTask = null; |
| 75 | + |
| 76 | + try |
| 77 | + { |
| 78 | + contentTask = result.Content.ReadAsStringAsync(); |
| 79 | + } |
| 80 | + catch(Exception ex) |
| 81 | + { |
| 82 | + throw new JsonApiException($"The API request failed with no content: HTTP {result.StatusCode.ToString()}", ex); |
| 83 | + } |
| 84 | + |
| 85 | + contentTask.Wait(timeout); |
| 86 | + return contentTask.Result; |
| 87 | + } |
| 88 | + |
| 89 | + protected virtual T ConvertResponse<T>(HttpResponseMessage response) |
| 90 | + { |
| 91 | + using(var buffer = new MemoryStream()) |
| 92 | + { |
| 93 | + var copyTask = response.Content.CopyToAsync(buffer); |
| 94 | + copyTask.Wait(TimeSpan.FromSeconds(5)); |
| 95 | + |
| 96 | + buffer.Position = 0; |
| 97 | + |
| 98 | + using(var reader = new StreamReader(buffer)) |
| 99 | + { |
| 100 | + return (T) serializer.Deserialize(reader, typeof(T)); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + TimeSpan GetTimeout(IProvidesInvocationDetails invocationDetails) |
| 106 | + => invocationDetails.GetTimeout().GetValueOrDefault(defaultTimeout); |
| 107 | + |
| 108 | + #endregion |
| 109 | + |
| 110 | + #region boilerplate Ability overrides |
| 111 | + |
| 112 | + protected override string GetReport(Actors.INamed actor) |
| 113 | + => $"{actor.Name} can consume JSON web services."; |
| 114 | + |
| 115 | + protected override void Dispose(bool disposing) |
| 116 | + { |
| 117 | + base.Dispose(disposing); |
| 118 | + |
| 119 | + if(disposing) |
| 120 | + httpClient.Dispose(); |
| 121 | + } |
| 122 | + |
| 123 | + #endregion |
| 124 | + |
| 125 | + #region constructor |
| 126 | + |
| 127 | + public ConsumeJsonWebServices(string baseUriString, TimeSpan? defaultTimeout = null) |
| 128 | + : this(new Uri(baseUriString, UriKind.Absolute), defaultTimeout) {} |
| 129 | + |
| 130 | + public ConsumeJsonWebServices(Uri baseUri = null, TimeSpan? defaultTimeout = null) |
| 131 | + { |
| 132 | + httpClient = new HttpClient { BaseAddress = baseUri }; |
| 133 | + this.defaultTimeout = defaultTimeout.GetValueOrDefault(SystemDefaultTimeout); |
| 134 | + serializer = JsonSerializer.CreateDefault(); |
| 135 | + } |
| 136 | + |
| 137 | + #endregion |
| 138 | + } |
| 139 | +} |
0 commit comments