示例#1
0
 internal TranslationStatus(string id, DateTimeOffset createdOn, DateTimeOffset lastModified, DocumentTranslationStatus status, DocumentTranslationError error, StatusSummary summary)
 {
     Id           = id;
     CreatedOn    = createdOn;
     LastModified = lastModified;
     Status       = status;
     Error        = error;
     Summary      = summary;
 }
示例#2
0
 private static IDictionary <string, string> CreateAdditionalInformation(DocumentTranslationError error)
 {
     if (string.IsNullOrEmpty(error.Target))
     {
         return(null);
     }
     return(new Dictionary <string, string> {
         { "Target", error.Target }
     });
 }
 internal DocumentStatusResult(Uri translatedDocumentUri, Uri sourceDocumentUri, DateTimeOffset createdOn, DateTimeOffset lastModified, TranslationStatus status, string translateTo, DocumentTranslationError error, float progress, string documentId, long charactersCharged)
 {
     TranslatedDocumentUri = translatedDocumentUri;
     SourceDocumentUri     = sourceDocumentUri;
     CreatedOn             = createdOn;
     LastModified          = lastModified;
     Status            = status;
     TranslateTo       = translateTo;
     Error             = error;
     Progress          = progress;
     DocumentId        = documentId;
     CharactersCharged = charactersCharged;
 }
示例#4
0
        /// <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
                {
                    var update = async
                        ? await _serviceClient.GetTranslationStatusAsync(new Guid(Id), cancellationToken).ConfigureAwait(false)
                        : _serviceClient.GetTranslationStatus(new Guid(Id), cancellationToken);

                    _response = update.GetRawResponse();
                    _retryAfterHeaderValue = update.Headers.RetryAfter;

                    _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;
                    _documentsCanceled   = update.Value.DocumentsCanceled;

                    if (update.Value.Status == DocumentTranslationStatus.Succeeded ||
                        update.Value.Status == DocumentTranslationStatus.Canceled ||
                        update.Value.Status == DocumentTranslationStatus.Failed)
                    {
                        _hasCompleted = true;
                        _hasValue     = true;
                    }
                    else if (update.Value.Status == DocumentTranslationStatus.ValidationFailed)
                    {
                        DocumentTranslationError error = (DocumentTranslationError)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 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));
        }
示例#6
0
 public static DocumentStatus DocumentStatus(Uri translatedDocumentUri = default, Uri sourceDocumentUri = default, DateTimeOffset createdOn = default, DateTimeOffset lastModified = default, DocumentTranslationStatus status = default, string translatedTo = default, DocumentTranslationError error = default, float progress = default, string id = default, long charactersCharged = default)
 {
     return(new DocumentStatus(translatedDocumentUri, sourceDocumentUri, createdOn, lastModified, status, translatedTo, error, progress, id, charactersCharged));
 }
        internal static DocumentStatusResult DeserializeDocumentStatusResult(JsonElement element)
        {
            Optional <Uri>                      path                  = default;
            Uri                                 sourcePath            = default;
            DateTimeOffset                      createdDateTimeUtc    = default;
            DateTimeOffset                      lastActionDateTimeUtc = default;
            DocumentTranslationStatus           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"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        property.ThrowNonNullablePropertyIsNull();
                        continue;
                    }
                    path = new Uri(property.Value.GetString());
                    continue;
                }
                if (property.NameEquals("sourcePath"))
                {
                    sourcePath = 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 DocumentTranslationStatus(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.Value, sourcePath, createdDateTimeUtc, lastActionDateTimeUtc, status, to, error, progress, id, characterCharged));
        }