| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | // <auto-generated/> |
| | 5 | |
|
| | 6 | | #nullable disable |
| | 7 | |
|
| | 8 | | using System; |
| | 9 | | using System.Collections.Generic; |
| | 10 | | using System.Text.Json; |
| | 11 | | using System.Threading; |
| | 12 | | using System.Threading.Tasks; |
| | 13 | | using Azure.Core; |
| | 14 | | using Azure.Core.Pipeline; |
| | 15 | | using Azure.Iot.Hub.Service.Models; |
| | 16 | |
|
| | 17 | | namespace Azure.Iot.Hub.Service |
| | 18 | | { |
| | 19 | | internal partial class QueryRestClient |
| | 20 | | { |
| | 21 | | private Uri endpoint; |
| | 22 | | private string apiVersion; |
| | 23 | | private ClientDiagnostics _clientDiagnostics; |
| | 24 | | private HttpPipeline _pipeline; |
| | 25 | |
|
| | 26 | | /// <summary> Initializes a new instance of QueryRestClient. </summary> |
| | 27 | | /// <param name="clientDiagnostics"> The handler for diagnostic messaging in the client. </param> |
| | 28 | | /// <param name="pipeline"> The HTTP pipeline for sending and receiving REST requests and responses. </param> |
| | 29 | | /// <param name="endpoint"> server parameter. </param> |
| | 30 | | /// <param name="apiVersion"> Api Version. </param> |
| | 31 | | /// <exception cref="ArgumentNullException"> <paramref name="apiVersion"/> is null. </exception> |
| 76 | 32 | | public QueryRestClient(ClientDiagnostics clientDiagnostics, HttpPipeline pipeline, Uri endpoint = null, string a |
| | 33 | | { |
| 76 | 34 | | endpoint ??= new Uri("https://fully-qualified-iothubname.azure-devices.net"); |
| 76 | 35 | | if (apiVersion == null) |
| | 36 | | { |
| 0 | 37 | | throw new ArgumentNullException(nameof(apiVersion)); |
| | 38 | | } |
| | 39 | |
|
| 76 | 40 | | this.endpoint = endpoint; |
| 76 | 41 | | this.apiVersion = apiVersion; |
| 76 | 42 | | _clientDiagnostics = clientDiagnostics; |
| 76 | 43 | | _pipeline = pipeline; |
| 76 | 44 | | } |
| | 45 | |
|
| | 46 | | internal HttpMessage CreateGetTwinsRequest(QuerySpecification querySpecification, string xMsContinuation, string |
| | 47 | | { |
| 14 | 48 | | var message = _pipeline.CreateMessage(); |
| 14 | 49 | | var request = message.Request; |
| 14 | 50 | | request.Method = RequestMethod.Post; |
| 14 | 51 | | var uri = new RawRequestUriBuilder(); |
| 14 | 52 | | uri.Reset(endpoint); |
| 14 | 53 | | uri.AppendPath("/devices/query", false); |
| 14 | 54 | | uri.AppendQuery("api-version", apiVersion, true); |
| 14 | 55 | | request.Uri = uri; |
| 14 | 56 | | if (xMsContinuation != null) |
| | 57 | | { |
| 0 | 58 | | request.Headers.Add("x-ms-continuation", xMsContinuation); |
| | 59 | | } |
| 14 | 60 | | if (xMsMaxItemCount != null) |
| | 61 | | { |
| 0 | 62 | | request.Headers.Add("x-ms-max-item-count", xMsMaxItemCount); |
| | 63 | | } |
| 14 | 64 | | request.Headers.Add("Content-Type", "application/json"); |
| 14 | 65 | | var content = new Utf8JsonRequestContent(); |
| 14 | 66 | | content.JsonWriter.WriteObjectValue(querySpecification); |
| 14 | 67 | | request.Content = content; |
| 14 | 68 | | return message; |
| | 69 | | } |
| | 70 | |
|
| | 71 | | /// <summary> Query an IoT Hub to retrieve information regarding device twins using a SQL-like language. See htt |
| | 72 | | /// <param name="querySpecification"> The query string. See https://docs.microsoft.com/azure/iot-hub/iot-hub-dev |
| | 73 | | /// <param name="xMsContinuation"> The continuation token used to get the next page of results. </param> |
| | 74 | | /// <param name="xMsMaxItemCount"> The maximum number of items returned per page. The service may use a differen |
| | 75 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 76 | | /// <exception cref="ArgumentNullException"> <paramref name="querySpecification"/> is null. </exception> |
| | 77 | | public async Task<ResponseWithHeaders<IReadOnlyList<TwinData>, QueryGetTwinsHeaders>> GetTwinsAsync(QuerySpecifi |
| | 78 | | { |
| 6 | 79 | | if (querySpecification == null) |
| | 80 | | { |
| 0 | 81 | | throw new ArgumentNullException(nameof(querySpecification)); |
| | 82 | | } |
| | 83 | |
|
| 6 | 84 | | using var message = CreateGetTwinsRequest(querySpecification, xMsContinuation, xMsMaxItemCount); |
| 6 | 85 | | await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); |
| 6 | 86 | | var headers = new QueryGetTwinsHeaders(message.Response); |
| 6 | 87 | | switch (message.Response.Status) |
| | 88 | | { |
| | 89 | | case 200: |
| | 90 | | { |
| | 91 | | IReadOnlyList<TwinData> value = default; |
| 6 | 92 | | using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, canc |
| 6 | 93 | | List<TwinData> array = new List<TwinData>(); |
| 172 | 94 | | foreach (var item in document.RootElement.EnumerateArray()) |
| | 95 | | { |
| 80 | 96 | | array.Add(TwinData.DeserializeTwinData(item)); |
| | 97 | | } |
| 6 | 98 | | value = array; |
| 6 | 99 | | return ResponseWithHeaders.FromValue(value, headers, message.Response); |
| | 100 | | } |
| | 101 | | default: |
| 0 | 102 | | throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(fa |
| | 103 | | } |
| 6 | 104 | | } |
| | 105 | |
|
| | 106 | | /// <summary> Query an IoT Hub to retrieve information regarding device twins using a SQL-like language. See htt |
| | 107 | | /// <param name="querySpecification"> The query string. See https://docs.microsoft.com/azure/iot-hub/iot-hub-dev |
| | 108 | | /// <param name="xMsContinuation"> The continuation token used to get the next page of results. </param> |
| | 109 | | /// <param name="xMsMaxItemCount"> The maximum number of items returned per page. The service may use a differen |
| | 110 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| | 111 | | /// <exception cref="ArgumentNullException"> <paramref name="querySpecification"/> is null. </exception> |
| | 112 | | public ResponseWithHeaders<IReadOnlyList<TwinData>, QueryGetTwinsHeaders> GetTwins(QuerySpecification querySpeci |
| | 113 | | { |
| 8 | 114 | | if (querySpecification == null) |
| | 115 | | { |
| 0 | 116 | | throw new ArgumentNullException(nameof(querySpecification)); |
| | 117 | | } |
| | 118 | |
|
| 8 | 119 | | using var message = CreateGetTwinsRequest(querySpecification, xMsContinuation, xMsMaxItemCount); |
| 8 | 120 | | _pipeline.Send(message, cancellationToken); |
| 8 | 121 | | var headers = new QueryGetTwinsHeaders(message.Response); |
| 8 | 122 | | switch (message.Response.Status) |
| | 123 | | { |
| | 124 | | case 200: |
| | 125 | | { |
| | 126 | | IReadOnlyList<TwinData> value = default; |
| 8 | 127 | | using var document = JsonDocument.Parse(message.Response.ContentStream); |
| 8 | 128 | | List<TwinData> array = new List<TwinData>(); |
| 292 | 129 | | foreach (var item in document.RootElement.EnumerateArray()) |
| | 130 | | { |
| 138 | 131 | | array.Add(TwinData.DeserializeTwinData(item)); |
| | 132 | | } |
| 8 | 133 | | value = array; |
| 8 | 134 | | return ResponseWithHeaders.FromValue(value, headers, message.Response); |
| | 135 | | } |
| | 136 | | default: |
| 0 | 137 | | throw _clientDiagnostics.CreateRequestFailedException(message.Response); |
| | 138 | | } |
| 8 | 139 | | } |
| | 140 | | } |
| | 141 | | } |