| | 1 | | namespace Songhay.Extensions; |
| | 2 | |
|
| | 3 | | /// <summary> |
| | 4 | | /// Extensions of <see cref="RestApiMetadata"/>. |
| | 5 | | /// </summary> |
| | 6 | | public static class RestApiMetadataExtensions |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// To the URI. |
| | 10 | | /// </summary> |
| | 11 | | /// <param name="meta">The meta.</param> |
| | 12 | | /// <param name="uriTemplateKey">The URI template key.</param> |
| | 13 | | /// <param name="bindByPositionValues">The bind by position values.</param> |
| | 14 | | public static Uri? ToUri(this RestApiMetadata? meta, string? uriTemplateKey, params string?[] bindByPositionValues) |
| 1 | 15 | | { |
| 1 | 16 | | if (meta == null || meta.ApiBase == null) return null; |
| | 17 | |
|
| 1 | 18 | | bindByPositionValues.ThrowWhenNullOrEmpty(); |
| | 19 | |
|
| 2 | 20 | | if (meta.UriTemplates.Keys.All(i => i != uriTemplateKey)) |
| 0 | 21 | | throw new FormatException("The expected REST API metadata URI template key is not here."); |
| | 22 | |
|
| | 23 | | const string forwardSlash = "/"; |
| 1 | 24 | | var uriBase = meta.ApiBase.OriginalString.EndsWith(forwardSlash) |
| 1 | 25 | | ? string.Concat(meta.ApiBase.OriginalString, meta.UriTemplates[uriTemplateKey!]) |
| 1 | 26 | | : string.Concat(meta.ApiBase.OriginalString, forwardSlash, meta.UriTemplates[uriTemplateKey!]); |
| | 27 | |
|
| 1 | 28 | | var uriTemplate = new UriTemplate(uriBase); |
| 1 | 29 | | var uri = uriTemplate.BindByPosition(bindByPositionValues!); |
| | 30 | |
|
| 1 | 31 | | return uri; |
| 1 | 32 | | } |
| | 33 | | } |