| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Text.Json; |
| | | 6 | | |
| | | 7 | | namespace Azure.Security.KeyVault |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Defines a page in Azure responses. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <typeparam name="T">Type of the page content items</typeparam> |
| | | 13 | | internal class KeyVaultPage<T> : IJsonDeserializable |
| | | 14 | | where T : IJsonDeserializable |
| | | 15 | | { |
| | | 16 | | private T[] _items; |
| | | 17 | | private readonly Func<T> _itemFactory; |
| | | 18 | | |
| | 20 | 19 | | internal KeyVaultPage(Func<T> itemFactory) |
| | | 20 | | { |
| | 20 | 21 | | _itemFactory = itemFactory; |
| | 20 | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Gets the content items. |
| | | 26 | | /// </summary> |
| | 20 | 27 | | public ReadOnlySpan<T> Items { get => _items.AsSpan(); } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Gets the link to the next page. |
| | | 31 | | /// </summary> |
| | 0 | 32 | | public Uri NextLink { get; private set; } |
| | | 33 | | |
| | | 34 | | void IJsonDeserializable.ReadProperties(JsonElement json) |
| | | 35 | | { |
| | 112 | 36 | | foreach (JsonProperty prop in json.EnumerateObject()) |
| | | 37 | | { |
| | 36 | 38 | | switch (prop.Name) |
| | | 39 | | { |
| | | 40 | | case "value": |
| | 20 | 41 | | JsonElement value = prop.Value; |
| | 20 | 42 | | if (value.ValueKind != JsonValueKind.Null) |
| | | 43 | | { |
| | 20 | 44 | | _items = new T[value.GetArrayLength()]; |
| | | 45 | | |
| | 20 | 46 | | int i = 0; |
| | | 47 | | |
| | 152 | 48 | | foreach (JsonElement elem in value.EnumerateArray()) |
| | | 49 | | { |
| | 56 | 50 | | _items[i] = _itemFactory(); |
| | | 51 | | |
| | 56 | 52 | | _items[i].ReadProperties(elem); |
| | | 53 | | |
| | 56 | 54 | | i++; |
| | | 55 | | } |
| | | 56 | | } |
| | | 57 | | break; |
| | | 58 | | |
| | | 59 | | case "nextLink": |
| | 16 | 60 | | var nextLinkUrl = prop.Value.GetString(); |
| | 16 | 61 | | if (!string.IsNullOrEmpty(nextLinkUrl)) |
| | | 62 | | { |
| | 0 | 63 | | NextLink = new Uri(nextLinkUrl); |
| | | 64 | | } |
| | | 65 | | break; |
| | | 66 | | } |
| | | 67 | | } |
| | 20 | 68 | | } |
| | | 69 | | } |
| | | 70 | | } |