Пример #1
0
        internal HttpMessage CreateAddRequest(IEnumerable <object> models, DigitalTwinModelsAddOptions digitalTwinModelsAddOptions)
        {
            var message = _pipeline.CreateMessage();
            var request = message.Request;

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

            uri.Reset(endpoint);
            uri.AppendPath("/models", false);
            uri.AppendQuery("api-version", apiVersion, true);
            request.Uri = uri;
            if (digitalTwinModelsAddOptions?.Traceparent != null)
            {
                request.Headers.Add("traceparent", digitalTwinModelsAddOptions.Traceparent);
            }
            if (digitalTwinModelsAddOptions?.Tracestate != null)
            {
                request.Headers.Add("tracestate", digitalTwinModelsAddOptions.Tracestate);
            }
            request.Headers.Add("Content-Type", "application/json");
            request.Headers.Add("Accept", "application/json");
            if (models != null)
            {
                var content = new Utf8JsonRequestContent();
                content.JsonWriter.WriteStartArray();
                foreach (var item in models)
                {
                    content.JsonWriter.WriteObjectValue(item);
                }
                content.JsonWriter.WriteEndArray();
                request.Content = content;
            }
            return(message);
        }
Пример #2
0
        public async Task <Response <IReadOnlyList <DigitalTwinsModelData> > > AddAsync(IEnumerable <object> models = null, DigitalTwinModelsAddOptions digitalTwinModelsAddOptions = null, CancellationToken cancellationToken = default)
        {
            using var message = CreateAddRequest(models, digitalTwinModelsAddOptions);
            await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false);

            switch (message.Response.Status)
            {
            case 201:
            {
                IReadOnlyList <DigitalTwinsModelData> value = default;
                using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, cancellationToken).ConfigureAwait(false);

                List <DigitalTwinsModelData> array = new List <DigitalTwinsModelData>();
                foreach (var item in document.RootElement.EnumerateArray())
                {
                    array.Add(DigitalTwinsModelData.DeserializeDigitalTwinsModelData(item));
                }
                value = array;
                return(Response.FromValue(value, message.Response));
            }
Пример #3
0
        // The modelUpdates parameter needs to be changed from IEnumerable<object> to IEnumerable<string>
        // and not parsed like a json object.
        internal async Task <Response <IReadOnlyList <DigitalTwinsModelData> > > AddAsync(IEnumerable <string> models = null, DigitalTwinModelsAddOptions digitalTwinModelsAddOptions = null, CancellationToken cancellationToken = default)
        {
            using DiagnosticScope scope = _clientDiagnostics.CreateScope("DigitalTwinModelsClient.Add");
            scope.Start();
            try
            {
                using HttpMessage message = CreateAddRequest(models, digitalTwinModelsAddOptions);
                await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false);

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

                    List <DigitalTwinsModelData> array = new List <DigitalTwinsModelData>(document.RootElement.GetArrayLength());
                    foreach (JsonElement item in document.RootElement.EnumerateArray())
                    {
                        array.Add(DigitalTwinsModelData.DeserializeDigitalTwinsModelData(item));
                    }
                    value = array;
                    return(Response.FromValue(value, message.Response));
                }