internal TranslationStatusDetail(string translationId, DateTimeOffset createdOn, DateTimeOffset lastModified, TranslationStatus status, DocumentTranslationError error, StatusSummary summary)
 {
     TranslationId = translationId;
     CreatedOn     = createdOn;
     LastModified  = lastModified;
     Status        = status;
     Error         = error;
     Summary       = summary;
 }
 private static IDictionary <string, string> CreateAdditionalInformation(DocumentTranslationError error)
 {
     if (string.IsNullOrEmpty(error.Target))
     {
         return(null);
     }
     return(new Dictionary <string, string> {
         { "Target", error.Target }
     });
 }
 internal DocumentStatusDetail(Uri locationUri, DateTimeOffset createdOn, DateTimeOffset lastModified, TranslationStatus status, string translateTo, DocumentTranslationError error, float translationProgressPercentage, string documentId, long?characterCharged)
 {
     LocationUri  = locationUri;
     CreatedOn    = createdOn;
     LastModified = lastModified;
     Status       = status;
     TranslateTo  = translateTo;
     Error        = error;
     TranslationProgressPercentage = translationProgressPercentage;
     DocumentId       = documentId;
     CharacterCharged = characterCharged;
 }
Exemplo n.º 4
0
 internal DocumentStatusResult(Uri translatedDocumentUri, DateTimeOffset createdOn, DateTimeOffset lastModified, TranslationStatus status, string translateTo, DocumentTranslationError error, float progress, string documentId, long charactersCharged)
 {
     TranslatedDocumentUri = translatedDocumentUri;
     CreatedOn             = createdOn;
     LastModified          = lastModified;
     Status            = status;
     TranslateTo       = translateTo;
     Error             = error;
     Progress          = progress;
     DocumentId        = documentId;
     CharactersCharged = charactersCharged;
 }
        internal static TranslationStatusResult DeserializeTranslationStatusResult(JsonElement element)
        {
            string            id = default;
            DateTimeOffset    createdDateTimeUtc    = default;
            DateTimeOffset    lastActionDateTimeUtc = default;
            TranslationStatus status = default;
            Optional <DocumentTranslationError> error = default;
            StatusSummary summary = default;

            foreach (var property in element.EnumerateObject())
            {
                if (property.NameEquals("id"))
                {
                    id = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("createdDateTimeUtc"))
                {
                    createdDateTimeUtc = property.Value.GetDateTimeOffset("O");
                    continue;
                }
                if (property.NameEquals("lastActionDateTimeUtc"))
                {
                    lastActionDateTimeUtc = property.Value.GetDateTimeOffset("O");
                    continue;
                }
                if (property.NameEquals("status"))
                {
                    status = new TranslationStatus(property.Value.GetString());
                    continue;
                }
                if (property.NameEquals("error"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        property.ThrowNonNullablePropertyIsNull();
                        continue;
                    }
                    error = DocumentTranslationError.DeserializeDocumentTranslationError(property.Value);
                    continue;
                }
                if (property.NameEquals("summary"))
                {
                    summary = StatusSummary.DeserializeStatusSummary(property.Value);
                    continue;
                }
            }
            return(new TranslationStatusResult(id, createdDateTimeUtc, lastActionDateTimeUtc, status, error.Value, summary));
        }
        /// <summary>
        /// Calls the server to get updated status of the long-running operation.
        /// </summary>
        /// <param name="async">When <c>true</c>, the method will be executed asynchronously; otherwise, it will execute synchronously.</param>
        /// <param name="cancellationToken">A <see cref="CancellationToken"/> used for the service call.</param>
        /// <returns>The HTTP response received from the server.</returns>
        private async ValueTask <Response> UpdateStatusAsync(bool async, CancellationToken cancellationToken)
        {
            if (!_hasCompleted)
            {
                using DiagnosticScope scope = _diagnostics.CreateScope($"{nameof(DocumentTranslationOperation)}.{nameof(UpdateStatus)}");
                scope.Start();

                try
                {
                    Response <TranslationStatusResult> update = async
                        ? await _serviceClient.GetOperationStatusAsync(new Guid(Id), cancellationToken).ConfigureAwait(false)
                        : _serviceClient.GetOperationStatus(new Guid(Id), cancellationToken);

                    _response = update.GetRawResponse();

                    _createdOn           = update.Value.CreatedOn;
                    _lastModified        = update.Value.LastModified;
                    _status              = update.Value.Status;
                    _documentsTotal      = update.Value.DocumentsTotal;
                    _documentsFailed     = update.Value.DocumentsFailed;
                    _documentsInProgress = update.Value.DocumentsInProgress;
                    _documentsSucceeded  = update.Value.DocumentsSucceeded;
                    _documentsNotStarted = update.Value.DocumentsNotStarted;
                    _documentsCancelled  = update.Value.DocumentsCancelled;

                    if (update.Value.Status == TranslationStatus.Succeeded ||
                        update.Value.Status == TranslationStatus.Cancelled ||
                        update.Value.Status == TranslationStatus.Failed)
                    {
                        _hasCompleted = true;
                    }
                    else if (update.Value.Status == TranslationStatus.ValidationFailed)
                    {
                        DocumentTranslationError error = update.Value.Error;
                        _requestFailedException = _diagnostics.CreateRequestFailedException(_response, error.Message, error.ErrorCode.ToString(), CreateAdditionalInformation(error));
                        _hasCompleted           = true;
                        throw _requestFailedException;
                    }
                }
                catch (Exception e)
                {
                    scope.Failed(e);
                    throw;
                }
            }

            return(GetRawResponse());
        }
        internal static DocumentStatusResult DeserializeDocumentStatusResult(JsonElement element)
        {
            Uri               path = default;
            DateTimeOffset    createdDateTimeUtc    = default;
            DateTimeOffset    lastActionDateTimeUtc = default;
            TranslationStatus status = default;
            string            to     = default;
            Optional <DocumentTranslationError> error = default;
            float           progress         = default;
            string          id               = default;
            Optional <long> characterCharged = default;

            foreach (var property in element.EnumerateObject())
            {
                if (property.NameEquals("path"))
                {
                    path = new Uri(property.Value.GetString());
                    continue;
                }
                if (property.NameEquals("createdDateTimeUtc"))
                {
                    createdDateTimeUtc = property.Value.GetDateTimeOffset("O");
                    continue;
                }
                if (property.NameEquals("lastActionDateTimeUtc"))
                {
                    lastActionDateTimeUtc = property.Value.GetDateTimeOffset("O");
                    continue;
                }
                if (property.NameEquals("status"))
                {
                    status = new TranslationStatus(property.Value.GetString());
                    continue;
                }
                if (property.NameEquals("to"))
                {
                    to = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("error"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        property.ThrowNonNullablePropertyIsNull();
                        continue;
                    }
                    error = DocumentTranslationError.DeserializeDocumentTranslationError(property.Value);
                    continue;
                }
                if (property.NameEquals("progress"))
                {
                    progress = property.Value.GetSingle();
                    continue;
                }
                if (property.NameEquals("id"))
                {
                    id = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("characterCharged"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        property.ThrowNonNullablePropertyIsNull();
                        continue;
                    }
                    characterCharged = property.Value.GetInt64();
                    continue;
                }
            }
            return(new DocumentStatusResult(path, createdDateTimeUtc, lastActionDateTimeUtc, status, to, error.Value, progress, id, characterCharged));
        }