| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using Azure.Core; |
| | 5 | |
|
| | 6 | | namespace Azure.DigitalTwins.Core |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// A helper class for working with the query APIs for digital twins. |
| | 10 | | /// </summary> |
| | 11 | | public static class QueryChargeHelper |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// A constant that is used to as the query-charge header field in the query page response. |
| | 15 | | /// </summary> |
| | 16 | | private const string QueryChargeHeader = "query-charge"; |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// Extract the query-charge field from a page header. |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="page">The page that contains the query-charge header.</param> |
| | 22 | | /// <param name="queryCharge">The query charge extracted from the header.</param> |
| | 23 | | /// <returns>True if the header contains a query-charge field, otherwise false.</returns> |
| | 24 | | /// <remarks> |
| | 25 | | /// For more samples, see <see href="https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/digitaltwins/Azu |
| | 26 | | /// </remarks> |
| | 27 | | /// <code snippet="Snippet:DigitalTwinsSampleQueryTwinsWithQueryCharge"> |
| | 28 | | /// // This code snippet demonstrates how you could extract the query charges incurred when calling |
| | 29 | | /// // the query API. It iterates over the response pages first to access to the query-charge header, |
| | 30 | | /// // and then the digital twin results within each page. |
| | 31 | | /// |
| | 32 | | /// AsyncPageable<string> asyncPageableResponseWithCharge = client.QueryAsync("SELECT * FROM digitalt |
| | 33 | | /// int pageNum = 0; |
| | 34 | | /// |
| | 35 | | /// // The "await" keyword here is required as a call is made when fetching a new page. |
| | 36 | | /// await foreach (Page<string> page in asyncPageableResponseWithCharge.AsPages()) |
| | 37 | | /// { |
| | 38 | | /// Console.WriteLine($"Page {++pageNum} results:"); |
| | 39 | | /// |
| | 40 | | /// // Extract the query-charge header from the page |
| | 41 | | /// if (QueryChargeHelper.TryGetQueryCharge(page, out float queryCharge)) |
| | 42 | | /// { |
| | 43 | | /// Console.WriteLine($"Query charge was: {queryCharge}"); |
| | 44 | | /// } |
| | 45 | | /// |
| | 46 | | /// // Iterate over the twin instances. |
| | 47 | | /// // The "await" keyword is not required here as the paged response is local. |
| | 48 | | /// foreach (string response in page.Values) |
| | 49 | | /// { |
| | 50 | | /// BasicDigitalTwin twin = JsonSerializer.Deserialize<BasicDigitalTwin>(response); |
| | 51 | | /// Console.WriteLine($"Found digital twin '{twin.Id}'"); |
| | 52 | | /// } |
| | 53 | | /// } |
| | 54 | | /// </code> |
| | 55 | | public static bool TryGetQueryCharge(Page<string> page, out float queryCharge) |
| | 56 | | { |
| 0 | 57 | | Argument.AssertNotNull(page, nameof(page)); |
| | 58 | |
|
| 0 | 59 | | if (!page.GetRawResponse().Headers.TryGetValue(QueryChargeHeader, out string queryChargeHeaderValue)) |
| | 60 | | { |
| 0 | 61 | | queryCharge = 0f; |
| 0 | 62 | | return false; |
| | 63 | | } |
| | 64 | |
|
| 0 | 65 | | return float.TryParse(queryChargeHeaderValue, out queryCharge); |
| | 66 | | } |
| | 67 | | } |
| | 68 | | } |