| | 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 | |
|
| 44 | 19 | | internal KeyVaultPage(Func<T> itemFactory) |
| | 20 | | { |
| 44 | 21 | | _itemFactory = itemFactory; |
| 44 | 22 | | } |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// Gets the content items. |
| | 26 | | /// </summary> |
| 44 | 27 | | public ReadOnlySpan<T> Items { get => _items.AsSpan(); } |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// Gets the link to the next page. |
| | 31 | | /// </summary> |
| 72 | 32 | | public Uri NextLink { get; private set; } |
| | 33 | |
|
| | 34 | | void IJsonDeserializable.ReadProperties(JsonElement json) |
| | 35 | | { |
| 264 | 36 | | foreach (JsonProperty prop in json.EnumerateObject()) |
| | 37 | | { |
| 88 | 38 | | switch (prop.Name) |
| | 39 | | { |
| | 40 | | case "value": |
| 44 | 41 | | JsonElement value = prop.Value; |
| 44 | 42 | | if (value.ValueKind != JsonValueKind.Null) |
| | 43 | | { |
| 44 | 44 | | _items = new T[value.GetArrayLength()]; |
| | 45 | |
|
| 44 | 46 | | int i = 0; |
| | 47 | |
|
| 1320 | 48 | | foreach (JsonElement elem in value.EnumerateArray()) |
| | 49 | | { |
| 616 | 50 | | _items[i] = _itemFactory(); |
| | 51 | |
|
| 616 | 52 | | _items[i].ReadProperties(elem); |
| | 53 | |
|
| 616 | 54 | | i++; |
| | 55 | | } |
| | 56 | | } |
| | 57 | | break; |
| | 58 | |
|
| | 59 | | case "nextLink": |
| 44 | 60 | | var nextLinkUrl = prop.Value.GetString(); |
| 44 | 61 | | if (!string.IsNullOrEmpty(nextLinkUrl)) |
| | 62 | | { |
| 28 | 63 | | NextLink = new Uri(nextLinkUrl); |
| | 64 | | } |
| | 65 | | break; |
| | 66 | | } |
| | 67 | | } |
| 44 | 68 | | } |
| | 69 | | } |
| | 70 | | } |