示例#1
0
 public Builder WithProcess(Process.ProcessType processType)
 {
     this.process = processType;
     return(this);
 }
        private static async Task PrintErrorAsync(HttpResponseMessage response, Uri apiEndpoint, Process.ProcessType type, CancellationToken cancellationToken)
        {
            await using var errorWriter = Console.Error;

            if (response.StatusCode == HttpStatusCode.InternalServerError || response.StatusCode == HttpStatusCode.BadRequest)
            {
                var jsonStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);

                var jsonOptions = new JsonSerializerOptions()
                {
                    PropertyNameCaseInsensitive = true,
                };

                var problemDetails = await JsonSerializer
                                     .DeserializeAsync <ProblemDetails>(jsonStream, jsonOptions, cancellationToken)
                                     .ConfigureAwait(false);

                await errorWriter.WriteLineAsync($"Title: {problemDetails.Title}").ConfigureAwait(false);

                await errorWriter.WriteLineAsync($"Status: {problemDetails.Status} ({(int)problemDetails.Status})").ConfigureAwait(false);

                await errorWriter.WriteLineAsync($"Detail: {problemDetails.Detail}").ConfigureAwait(false);

                return;
            }

            if (response.StatusCode == HttpStatusCode.NotFound)
            {
                await errorWriter.WriteLineAsync($"[{(int)response.StatusCode}]The given endpoint {apiEndpoint} was not found.").ConfigureAwait(false);

                return;
            }

            if (response.StatusCode == HttpStatusCode.Unauthorized)
            {
                await errorWriter.WriteLineAsync($"[{(int)response.StatusCode}]You are not authenticated for endpoint {apiEndpoint}.").ConfigureAwait(false);

                return;
            }

            if (response.StatusCode == HttpStatusCode.Forbidden)
            {
                if (type == Process.ProcessType.Download)
                {
                    await errorWriter.WriteLineAsync($"[{(int)response.StatusCode}]You are not allowed to download file from {apiEndpoint}.").ConfigureAwait(false);
                }

                if (type == Process.ProcessType.Upload)
                {
                    await errorWriter.WriteLineAsync($"[{(int)response.StatusCode}]You are not allowed to upload file from {apiEndpoint}.").ConfigureAwait(false);
                }

                return;
            }

            throw new ArgumentOutOfRangeException(nameof(response.StatusCode), $"[{(int)response.StatusCode}]Message: {response.ReasonPhrase}\nEndpoint: {apiEndpoint}.");
        }