internal Response <Stream> Add(string id, Stream twin, CreateDigitalTwinOptions digitalTwinsAddOptions = null, CancellationToken cancellationToken = default)
        {
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }
            if (twin == null)
            {
                throw new ArgumentNullException(nameof(twin));
            }

            using HttpMessage message = CreateAddRequest(id, twin, digitalTwinsAddOptions);
            _pipeline.Send(message, cancellationToken);
            switch (message.Response.Status)
            {
            case 200:
            {
                Stream value = message.ExtractResponseContent();
                return(Response.FromValue(value, message.Response));
            }

            case 202:
                return(Response.FromValue <Stream>(null, message.Response));

            default:
                throw _clientDiagnostics.CreateRequestFailedException(message.Response);
            }
        }
        internal HttpMessage CreateAddRequest(string id, Stream twin, CreateDigitalTwinOptions digitalTwinsAddOptions)
        {
            var message = _pipeline.CreateMessage();
            var request = message.Request;

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

            uri.Reset(endpoint);
            uri.AppendPath("/digitaltwins/", false);
            uri.AppendPath(id, true);
            uri.AppendQuery("api-version", apiVersion, true);
            request.Uri = uri;
            if (digitalTwinsAddOptions?.TraceParent != null)
            {
                request.Headers.Add("TraceParent", digitalTwinsAddOptions.TraceParent);
            }
            if (digitalTwinsAddOptions?.TraceState != null)
            {
                request.Headers.Add("TraceState", digitalTwinsAddOptions.TraceState);
            }
            request.Headers.Add("If-None-Match", "*");
            request.Headers.Add("Content-Type", "application/json");
            request.Headers.Add("Accept", "application/json");
            var content = RequestContent.Create(twin);

            request.Content = content;
            return(message);
        }
Пример #3
0
        internal async Task <Response <string> > AddAsync(string id, string twin, CreateDigitalTwinOptions digitalTwinsAddOptions = null, CancellationToken cancellationToken = default)
        {
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }
            if (twin == null)
            {
                throw new ArgumentNullException(nameof(twin));
            }

            using DiagnosticScope scope = _clientDiagnostics.CreateScope("DigitalTwinsClient.Add");
            scope.Start();
            try
            {
                using HttpMessage message = CreateAddRequest(id, twin, digitalTwinsAddOptions);
                await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false);

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

                    value = document.RootElement.GetRawText();
                    return(Response.FromValue(value, message.Response));
                }

                case 201:
                    return(Response.FromValue <string>(null, message.Response));
        internal async Task <Response <Stream> > AddAsync(
            string id,
            Stream twin,
            CreateDigitalTwinOptions digitalTwinsAddOptions = null,
            CancellationToken cancellationToken             = default)
        {
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }
            if (twin == null)
            {
                throw new ArgumentNullException(nameof(twin));
            }

            using DiagnosticScope scope = _clientDiagnostics.CreateScope("DigitalTwinsClient.Add");
            scope.Start();
            try
            {
                using HttpMessage message = CreateAddRequest(id, twin, digitalTwinsAddOptions);
                await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false);

                switch (message.Response.Status)
                {
                case 200:
                {
                    Stream value = message.ExtractResponseContent();
                    return(Response.FromValue(value, message.Response));
                }

                case 202:
                    return(Response.FromValue <Stream>(null, message.Response));

                default:
                    throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false);
                }
            }
            catch (Exception e)
            {
                scope.Failed(e);
                throw;
            }
        }