Esempio n. 1
0
        internal async Task <Response> UpdateRelationshipAsync(
            string id,
            string relationshipId,
            string patchDocument,
            UpdateRelationshipOptions digitalTwinsUpdateRelationshipOptions = null,
            CancellationToken cancellationToken = default)
        {
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }
            if (relationshipId == null)
            {
                throw new ArgumentNullException(nameof(relationshipId));
            }

            using DiagnosticScope scope = _clientDiagnostics.CreateScope("DigitalTwinsClient.UpdateRelationship");
            scope.Start();
            try
            {
                using HttpMessage message = CreateUpdateRelationshipRequest(id, relationshipId, patchDocument, digitalTwinsUpdateRelationshipOptions);
                await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false);

                return(message.Response.Status switch
                {
                    204 => message.Response,
                    _ => throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false),
                });
            }
        internal HttpMessage CreateUpdateRelationshipRequest(string id, string relationshipId, IEnumerable <object> patchDocument, UpdateRelationshipOptions digitalTwinsUpdateRelationshipOptions)
        {
            var message = _pipeline.CreateMessage();
            var request = message.Request;

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

            uri.Reset(endpoint);
            uri.AppendPath("/digitaltwins/", false);
            uri.AppendPath(id, true);
            uri.AppendPath("/relationships/", false);
            uri.AppendPath(relationshipId, true);
            uri.AppendQuery("api-version", apiVersion, true);
            request.Uri = uri;
            if (digitalTwinsUpdateRelationshipOptions?.IfMatch != null)
            {
                request.Headers.Add("If-Match", digitalTwinsUpdateRelationshipOptions.IfMatch);
            }
            request.Headers.Add("Accept", "application/json");
            request.Headers.Add("Content-Type", "application/json-patch+json");
            var content = new Utf8JsonRequestContent();

            content.JsonWriter.WriteStartArray();
            foreach (var item in patchDocument)
            {
                content.JsonWriter.WriteObjectValue(item);
            }
            content.JsonWriter.WriteEndArray();
            request.Content = content;
            return(message);
        }