private static MissingInputException CreateMissingInputException(IHypertextControl hypertextControl, IEnumerable <string> sendKeys, string[] missing) { string message; if (sendKeys.Any()) { message = $"Unable to prepare a request for the relation '{hypertextControl.Rel}'. These inputs were " + $"supplied, '{String.Join(", ", sendKeys)}'. One or more of these inputs were missing, '{String.Join(", ", missing)}'."; } else { message = $"Unable to prepare a request for the relation '{hypertextControl.Rel}'. No inputs were " + $"supplied. The control has these inputs, '{String.Join(", ", missing)}'."; } var missingInputException = new MissingInputException(message); foreach (string m in missing) { missingInputException.Data.Add(m, "missing"); } return(missingInputException); }
private string PrepareUri(IDictionary <string, string> sendPairs, IHypertextControl hypertextControl) { var pairs = new Dictionary <string, string>(sendPairs); var uriTemplate = new UriTemplate.Core.UriTemplate(hypertextControl.HRef); var uri = uriTemplate.BindByName(pairs); var bindings = uriTemplate.Match(uri, pairs.Keys).Bindings; foreach (string bound in bindings.Keys) { pairs.Remove(bound); } if (pairs.Count > 0) { string queryString = String.Join("&", pairs.Select(v => $"{v.Key}={v.Value}")); string questionMark = uri.OriginalString.Contains("?") ? String.Empty : "?"; return(Flurl.Url.Combine(uri.OriginalString, questionMark, queryString)); } else { return(uri.OriginalString); } }
public static HttpMethod DetermineHttpMethod(this IHypertextControl hypertextControl) { var firstMethodValue = hypertextControl.ControlData.FirstOrDefault(cd => cd.Key == HttpControlData.MethodControlName).Value?.ToLowerInvariant(); if (firstMethodValue == null) { return(HttpMethod.Get); } else { return(new HttpMethod(firstMethodValue)); } }
public static bool SupportsRequestBody(this IHypertextControl hypertextControl) { var method = hypertextControl.DetermineHttpMethod(); if (method == HttpMethod.Post) { return(true); } else if (method == HttpMethod.Put) { return(true); } else { return(false); } }
private void CheckInputs(IDictionary <string, string> sendPairs, IHypertextControl hypertextControl) { string[] overlapping = this.GetOverlapping(sendPairs.Keys, hypertextControl.Inputs); if (overlapping.Length == 0) { string[] allInputs = hypertextControl.Inputs.Select(i => i.Name).ToArray(); throw CreateMissingInputException(hypertextControl, sendPairs.Keys, allInputs); } // string[] missingRequired = this.GetMissingRequired(sendPairs.Keys, hypertextControl.Inputs); if (missingRequired.Length > 0) { throw CreateMissingInputException(hypertextControl, sendPairs.Keys, missingRequired); } }
// public IHypertextControl GetControl(string rel) { if (rel == null) { throw new ArgumentNullException(nameof(rel)); } if (this.Count == 0) { throw new RelationNotFoundException($"Could not find a hyperlink with relation '{rel}'. There are no hyperlinks on the resource."); } else { IHypertextControl control = this.FirstOrDefault(c => rel.Equals(c.Rel, StringComparison.OrdinalIgnoreCase)); return(control ?? throw new RelationNotFoundException( $"Could not find a hyperlink with relation '{rel}'. The available relations are '{string.Join(", ", GetRelations())}'")); } }
// private void AppendHeaders(IHypertextControl hypertextControl, HttpRequestMessage httpRequest) { if (this.DefaultMediaType != null) { logger?.LogDebug($"Append header Accept: {this.DefaultMediaType}"); httpRequest.Headers.Accept.ParseAdd(this.DefaultMediaType); } if (this.DefaultCharSet != null) { logger?.LogDebug($"Append header Accept: {this.DefaultCharSet}"); httpRequest.Headers.AcceptCharset.ParseAdd(this.DefaultCharSet); } if (hypertextControl.TryParseIfMatch(out string ifMatch)) { logger?.LogDebug($"Append header Accept: {ifMatch}"); httpRequest.Headers.IfMatch.ParseAdd(ifMatch); } }
// internal override async Task <HttpResponseMessage> ExecuteStepRequestAsync(HttpStep previous, CancellationToken cancellationToken) { IHypertextControl control = previous.Resource.GetControl(this.Rel); return(await this.StepContext.HttpClient.GetAsync(control.HRef, cancellationToken)); }
public HttpRequestMessage BuildRequest(IDictionary <string, string> sendPairs, IHypertextControl hypertextControl) { if (sendPairs == null) { throw new ArgumentNullException(nameof(sendPairs)); } if (hypertextControl == null) { throw new ArgumentNullException(nameof(hypertextControl)); } if (hypertextControl.Inputs != null) { this.CheckInputs(sendPairs, hypertextControl); } var httpRequest = this.CreateRequestWithOptionalBody(sendPairs, hypertextControl); this.AppendHeaders(hypertextControl, httpRequest); return(httpRequest); }
private HttpContent PrepareHttpContent(IDictionary <string, string> sendPairs, IHypertextControl hypertextControl) { string jsonBodyString; if (hypertextControl.Inputs != null) { var explicitlyDefinedPairs = new Dictionary <string, string>(); foreach (var input in hypertextControl.Inputs) { if (sendPairs.TryGetValue(input.Name, out string sendValue)) { explicitlyDefinedPairs.Add(input.Name, sendValue); } else if (!input.IsOptional) { throw new InvalidOperationException( "Cannot prepare the HTTP content. A missing input was unexpected. Ensure " + "all required fields are present before calling this method."); } } jsonBodyString = Newtonsoft.Json.JsonConvert.SerializeObject(explicitlyDefinedPairs); } else { jsonBodyString = Newtonsoft.Json.JsonConvert.SerializeObject(sendPairs); } logger?.LogDebug($"HTTP request body has {jsonBodyString.Length} chars."); var httpContent = new StringContent(jsonBodyString); httpContent.Headers.ContentType.MediaType = this.DefaultMediaType; httpContent.Headers.ContentType.CharSet = this.DefaultCharSet; return(httpContent); }
private HttpRequestMessage CreateRequestWithOptionalBody(IDictionary <string, string> sendPairs, IHypertextControl hypertextControl) { logger?.LogDebug("Creating HTTP request."); HttpRequestMessage httpRequest; if (hypertextControl.SupportsRequestBody()) { httpRequest = new HttpRequestMessage(hypertextControl.DetermineHttpMethod(), hypertextControl.HRef) { Content = this.PrepareHttpContent(sendPairs, hypertextControl) }; } else { httpRequest = new HttpRequestMessage(hypertextControl.DetermineHttpMethod(), this.PrepareUri(sendPairs, hypertextControl)); } return(httpRequest); }
public static bool TryParseIfMatch(this IHypertextControl hypertextControl, out string ifMatch) { ifMatch = hypertextControl.ControlData.FirstOrDefault(cd => cd.Key == HttpControlData.IfMatchControlName).Value; return(ifMatch != null); }