예제 #1
0
        internal async Task <ResponseWithHeaders <QueryResult <T>, QueryQueryTwinsHeaders> > QueryTwinsAsync <T>(
            QuerySpecification querySpecification,
            QueryOptions queryTwinsOptions      = null,
            ObjectSerializer objectSerializer   = null,
            CancellationToken cancellationToken = default)
        {
            if (querySpecification == null)
            {
                throw new ArgumentNullException(nameof(querySpecification));
            }

            if (objectSerializer == null)
            {
                throw new ArgumentNullException(nameof(objectSerializer));
            }

            using HttpMessage message = CreateQueryTwinsRequest(querySpecification, queryTwinsOptions);
            await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false);

            var headers = new QueryQueryTwinsHeaders(message.Response);

            switch (message.Response.Status)
            {
            case 200:
            {
                using JsonDocument document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, cancellationToken).ConfigureAwait(false);

                QueryResult <T> value = QueryResult <T> .DeserializeQueryResult(document.RootElement, objectSerializer);

                return(ResponseWithHeaders.FromValue(value, headers, message.Response));
            }
예제 #2
0
        internal HttpMessage CreateQueryTwinsRequest(QuerySpecification querySpecification, QueryOptions queryTwinsOptions)
        {
            var message = _pipeline.CreateMessage();
            var request = message.Request;

            request.Method = RequestMethod.Post;
            var uri = new RawRequestUriBuilder();

            uri.Reset(endpoint);
            uri.AppendPath("/query", false);
            uri.AppendQuery("api-version", apiVersion, true);
            request.Uri = uri;
            if (queryTwinsOptions?.TraceParent != null)
            {
                request.Headers.Add("traceparent", queryTwinsOptions.TraceParent);
            }
            if (queryTwinsOptions?.TraceState != null)
            {
                request.Headers.Add("tracestate", queryTwinsOptions.TraceState);
            }
            if (queryTwinsOptions?.MaxItemsPerPage != null)
            {
                request.Headers.Add("max-items-per-page", queryTwinsOptions.MaxItemsPerPage.Value);
            }
            request.Headers.Add("Content-Type", "application/json");
            request.Headers.Add("Accept", "application/json");
            var content = new Utf8JsonRequestContent();

            content.JsonWriter.WriteObjectValue(querySpecification);
            request.Content = content;
            return(message);
        }
예제 #3
0
        internal HttpMessage CreateQueryTwinsRequest(QuerySpecification querySpecification)
        {
            var message = _pipeline.CreateMessage();
            var request = message.Request;

            request.Method = RequestMethod.Post;
            var uri = new RawRequestUriBuilder();

            uri.Reset(endpoint);
            uri.AppendPath("/query", false);
            uri.AppendQuery("api-version", apiVersion, true);
            request.Uri = uri;
            request.Headers.Add("Content-Type", "application/json");
            using var content = new Utf8JsonRequestContent();
            content.JsonWriter.WriteObjectValue(querySpecification);
            request.Content = content;
            return(message);
        }
예제 #4
0
        public async Task <ResponseWithHeaders <QueryResult, QueryQueryTwinsHeaders> > QueryTwinsAsync(QuerySpecification querySpecification, CancellationToken cancellationToken = default)
        {
            if (querySpecification == null)
            {
                throw new ArgumentNullException(nameof(querySpecification));
            }

            using var message = CreateQueryTwinsRequest(querySpecification);
            await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false);

            var headers = new QueryQueryTwinsHeaders(message.Response);

            switch (message.Response.Status)
            {
            case 200:
            {
                QueryResult value = default;
                using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, cancellationToken).ConfigureAwait(false);

                if (document.RootElement.ValueKind == JsonValueKind.Null)
                {
                    value = null;
                }
                else
                {
                    value = QueryResult.DeserializeQueryResult(document.RootElement);
                }
                return(ResponseWithHeaders.FromValue(value, headers, message.Response));
            }