| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | #nullable enable |
| | 5 | |
|
| | 6 | | using System; |
| | 7 | | using System.Globalization; |
| | 8 | | using System.Xml; |
| | 9 | |
|
| | 10 | | namespace Azure.Core |
| | 11 | | { |
| | 12 | | internal static class ResponseHeadersExtensions |
| | 13 | | { |
| | 14 | | public static bool TryGetValue(this ResponseHeaders headers, string name, out byte[]? value) |
| | 15 | | { |
| 0 | 16 | | if (headers.TryGetValue(name, out string? stringValue)) |
| | 17 | | { |
| 0 | 18 | | value = Convert.FromBase64String(stringValue); |
| 0 | 19 | | return true; |
| | 20 | | } |
| | 21 | |
|
| 0 | 22 | | value = null; |
| 0 | 23 | | return false; |
| | 24 | | } |
| | 25 | |
|
| | 26 | | public static bool TryGetValue(this ResponseHeaders headers, string name, out TimeSpan? value) |
| | 27 | | { |
| 0 | 28 | | if (headers.TryGetValue(name, out string? stringValue)) |
| | 29 | | { |
| 0 | 30 | | value = XmlConvert.ToTimeSpan(stringValue); |
| 0 | 31 | | return true; |
| | 32 | | } |
| | 33 | |
|
| 0 | 34 | | value = null; |
| 0 | 35 | | return false; |
| | 36 | | } |
| | 37 | |
|
| | 38 | | public static bool TryGetValue(this ResponseHeaders headers, string name, out DateTimeOffset? value) |
| | 39 | | { |
| 0 | 40 | | if (headers.TryGetValue(name, out string? stringValue)) |
| | 41 | | { |
| 0 | 42 | | value = DateTimeOffset.Parse(stringValue, CultureInfo.InvariantCulture); |
| 0 | 43 | | return true; |
| | 44 | | } |
| | 45 | |
|
| 0 | 46 | | value = null; |
| 0 | 47 | | return false; |
| | 48 | | } |
| | 49 | |
|
| | 50 | | public static bool TryGetValue<T>(this ResponseHeaders headers, string name, out T? value) where T : struct |
| | 51 | | { |
| 8 | 52 | | if (headers.TryGetValue(name, out string? stringValue)) |
| | 53 | | { |
| 8 | 54 | | value = (T)Convert.ChangeType(stringValue, typeof(T), CultureInfo.InvariantCulture); |
| 8 | 55 | | return true; |
| | 56 | | } |
| | 57 | |
|
| 0 | 58 | | value = null; |
| 0 | 59 | | return false; |
| | 60 | | } |
| | 61 | |
|
| | 62 | | public static bool TryGetValue<T>(this ResponseHeaders headers, string name, out T? value) where T : class |
| | 63 | | { |
| 0 | 64 | | if (headers.TryGetValue(name, out string? stringValue)) |
| | 65 | | { |
| 0 | 66 | | value = (T)Convert.ChangeType(stringValue, typeof(T), CultureInfo.InvariantCulture); |
| 0 | 67 | | return true; |
| | 68 | | } |
| | 69 | |
|
| 0 | 70 | | value = null; |
| 0 | 71 | | return false; |
| | 72 | | } |
| | 73 | | } |
| | 74 | | } |