/// <summary> /// Binds the <see cref="UriTemplate" /> /// to the specified <c>params</c> by position. /// </summary> /// <param name="template">The template.</param> /// <param name="baseUri">The base URI.</param> /// <param name="values">The values.</param> /// <returns></returns> /// <exception cref="ArgumentNullException">template - The expected URI template is not here.</exception> /// <exception cref="FormatException"></exception> public static Uri BindByPosition(this UriTemplate template, Uri baseUri, params string[] values) { if (template == null) { throw new ArgumentNullException("template", "The expected URI template is not here."); } var keys = template.GetParameterNames(); for (int i = 0; i < keys.Count(); i++) { template.AddParameter(keys.ElementAt(i), values.ElementAtOrDefault(i)); } var resolved = template.Resolve(); if (baseUri != null) { return(new UriBuilder(baseUri).WithPath(resolved).Uri); } else { var isAbsolute = Uri.IsWellFormedUriString(resolved, UriKind.Absolute); var isRelative = Uri.IsWellFormedUriString(resolved, UriKind.Relative); if (!isAbsolute && !isRelative) { throw new FormatException($"The resolved URI template, {resolved}, is in an unknown format."); } return(isAbsolute ? new Uri(resolved, UriKind.Absolute) : new Uri(resolved, UriKind.Relative)); } }
public static Uri GetResolvedTarget(this Uri resolvedTarget, Dictionary <string, object> linkParameters, bool addNonTemplatedParametersToQueryString) { if (resolvedTarget == null) { return(null); } var uriTemplate = new UriTemplate(resolvedTarget.OriginalString); if (addNonTemplatedParametersToQueryString) { var templateParameters = uriTemplate.GetParameterNames(); if (linkParameters.Any(p => !templateParameters.Contains(p.Key))) { resolvedTarget = resolvedTarget.AddParametersToQueryString(null, linkParameters.Keys.ToArray()); uriTemplate = new UriTemplate(resolvedTarget.OriginalString); } } if (resolvedTarget.OriginalString.Contains("{")) { ApplyParametersToTemplate(uriTemplate, linkParameters); resolvedTarget = new Uri(uriTemplate.Resolve(), UriKind.RelativeOrAbsolute); return(resolvedTarget); } return(resolvedTarget); }
static string ResolveLink(ILinked entity, string link, IDictionary <string, object> parameters = null) { Link linkItem; if (!entity.Links.TryGetValue(link, out linkItem)) { throw new NotSupportedException("The requested link isn't available."); } var expression = linkItem.GetUri(); var template = new UriTemplate(expression); if (parameters != null) { var missing = parameters.Select(p => p.Key).Except(template.GetParameterNames()).ToArray(); if (missing.Any()) { throw new ArgumentException("The URI template '" + expression + "' does not contain parameter: " + string.Join(",", missing)); } foreach (var parameter in parameters) { var value = parameter.Value is DateTime ? ((DateTime)parameter.Value).ToString("O") : parameter.Value; template.SetParameter(parameter.Key, value); } } return(template.Resolve()); }
public static string CreateUriQueryForTs(string uriText, ParameterDescriptionEx[] parameterDescriptions) { UriTemplate template = new UriTemplate(uriText); string[] parameterNames; try { parameterNames = template.GetParameterNames().ToArray(); } catch (ArgumentException ex) { throw new CodeGenException($"When CreateuriQuery, path {uriText} triggers error: {ex.Message}"); } if (parameterNames.Length == 0 && parameterDescriptions.Length == 0) { return(null); } string newUriText = uriText; foreach (ParameterDescriptionEx d in parameterDescriptions) { if (d.ParameterDescriptor.ParameterBinder == ParameterBinder.FromBody || d.ParameterDescriptor.ParameterBinder == ParameterBinder.FromForm) { continue; } newUriText = UriTemplateTransform.TransformForTs(newUriText, d); } return(newUriText); }
/// <summary> /// Parameterize the link to construct a URI. /// </summary> /// <param name="parameters">Parameters to substitute into the template, if any.</param> /// <returns>A constructed URI.</returns> /// <remarks>This method ensures that templates containing parameters cannot be accidentally /// used as URIs.</remarks> public string GetUri(IDictionary <string, object> parameters = null) { if (Template == null) { throw new InvalidOperationException("Attempted to process an empty URI template."); } var template = new UriTemplate(Template); if (parameters != null) { var missing = parameters.Select(p => p.Key).Except(template.GetParameterNames()); if (missing.Any()) { throw new ArgumentException($"The URI template `{Template}` does not contain parameter: `{string.Join("`, `", missing)}`."); } foreach (var parameter in parameters) { var value = parameter.Value is DateTime time ? time.ToString("O") : parameter.Value; template.SetParameter(parameter.Key, value); } } return(template.Resolve()); }
/// <summary> /// Resolves the URI Template defined in the Target URI using the assigned URI parameters /// </summary> /// <returns></returns> public Uri GetResolvedTarget() { if (Target != null) { var uriTemplate = new UriTemplate(Target.OriginalString); if (AddNonTemplatedParametersToQueryString) { var templateParameters = uriTemplate.GetParameterNames(); if (_Parameters.Any(p => !templateParameters.Contains(p.Key))) { AddParametersAsTemplate(); uriTemplate = new UriTemplate(Target.OriginalString); } } if (Target.OriginalString.Contains("{")) { ApplyParameters(uriTemplate); var resolvedTarget = new Uri(uriTemplate.Resolve(), UriKind.RelativeOrAbsolute); return(resolvedTarget); } } return(Target); }
public static string CreateUriQuery(string uriText, ParameterDescription[] parameterDescriptions) { var template = new UriTemplate(uriText); var parameterNames = template.GetParameterNames().ToArray(); if (parameterNames.Length == 0) { return(null); } string newUriText = uriText; Func <ParameterDescription, string> GetUriText = (d) => { if (d.ParameterDescriptor.ParameterType == typeofString) { return(newUriText.Replace($"{{{d.Name}}}", $"\"+Uri.EscapeDataString({d.Name})+\"")); } else if ((d.ParameterDescriptor.ParameterType == typeofDateTime) || (d.ParameterDescriptor.ParameterType == typeofDateTimeOffset)) { return(newUriText.Replace($"{{{d.Name}}}", $"\"+{d.Name}.ToUniversalTime().ToString(\"yyyy-MM-ddTHH:mm:ss.fffffffZ\")+\"")); } else { return(newUriText.Replace($"{{{d.Name}}}", $"\"+{d.Name}+\"")); } }; for (int i = 0; i < parameterNames.Length; i++) { var name = parameterNames[i]; //PathSegmentVariableNames[i] always give uppercase var d = parameterDescriptions.FirstOrDefault(r => r.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase)); Debug.Assert(d != null); newUriText = GetUriText(d); } //for (int i = 0; i < template.QueryValueVariableNames.Count; i++) //{ // var name = template.QueryValueVariableNames[i]; // var d = parameterDescriptions.FirstOrDefault(r => r.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase)); // Debug.Assert(d != null); // newUriText = GetUriText(d); //} return(newUriText); }
public static Uri BindByName(this UriTemplate template, Uri baseUri, IDictionary <string, string> parameters) { IDictionary <string, string> unusedParameters = new Dictionary <string, string>(parameters); foreach (var p in template.GetParameterNames()) { if (parameters.TryGetValue(p, out string v)) { template = template.AddParameter(p, v); unusedParameters.Remove(p); } } var url = new Uri(baseUri, template.Resolve()); url = url.AddQueryParameters(unusedParameters); return(url); }
protected static string CreateUriQuery(string uriText, ParameterDescription[] parameterDescriptions) { var template = new UriTemplate(uriText); var parameterNames = template.GetParameterNames().ToArray(); if (parameterNames.Length == 0) { return(null); } string newUriText = uriText; for (int i = 0; i < parameterNames.Length; i++) { var name = parameterNames[i];//PathSegmentVariableNames[i] always give uppercase var d = parameterDescriptions.FirstOrDefault(r => r.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase)); Debug.Assert(d != null); if (d.ParameterDescriptor.ParameterType == typeofString) { newUriText = newUriText.Replace($"{{{d.Name}}}", $"'+encodeURIComponent({d.Name})+'"); } else if (d.ParameterDescriptor.ParameterType == typeofDateTime || d.ParameterDescriptor.ParameterType == typeofDateTimeOffset) { newUriText = newUriText.Replace($"{{{d.Name}}}", $"'+{d.Name}.toISOString()+'"); } else { newUriText = newUriText.Replace($"{{{d.Name}}}", $"'+{d.Name}+'"); } } //for (int i = 0; i < template.QueryValueVariableNames.Count; i++) //{ // var name = template.QueryValueVariableNames[i]; // var d = parameterDescriptions.FirstOrDefault(r => r.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase)); // Debug.Assert(d != null); // newUriText = (d.ParameterDescriptor.ParameterType == typeofString) ? // newUriText.Replace($"{{{d.Name}}}", $"'+encodeURIComponent({d.Name})+'") // : newUriText.Replace($"{{{d.Name}}}", $"'+{d.Name}+'"); //} return(newUriText); }
public static string CreateUriQueryForTs(string uriText, ParameterDescription[] parameterDescriptions) { var template = new UriTemplate(uriText); var parameterNames = template.GetParameterNames().ToArray(); if (parameterNames.Length == 0) { return(null); } string newUriText = uriText; for (int i = 0; i < parameterNames.Length; i++) { var name = parameterNames[i]; //PathSegmentVariableNames[i] always give uppercase var d = parameterDescriptions.FirstOrDefault(r => r.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase)); Debug.Assert(d != null); newUriText = UriTemplateTransform.TransformForTs(newUriText, d); } return(newUriText); }
public static string CreateUriQueryForTs(string uriText, ParameterDescription[] parameterDescriptions) { var template = new UriTemplate(uriText); var parameterNames = template.GetParameterNames().ToArray(); if (parameterNames.Length == 0 && parameterDescriptions.Length == 0) { return(null); } string newUriText = uriText; foreach (var d in parameterDescriptions) { if (d.ParameterDescriptor.ParameterBinder == ParameterBinder.FromBody || d.ParameterDescriptor.ParameterBinder == ParameterBinder.FromForm) { continue; } newUriText = UriTemplateTransform.TransformForTs(newUriText, d); } return(newUriText); }
/// <summary> /// Returns list of URI Template parameters specified in the Target URI /// </summary> /// <returns></returns> public IEnumerable <string> GetParameterNames() { var uriTemplate = new UriTemplate(Target.OriginalString); return(uriTemplate.GetParameterNames()); }