예제 #1
0
        /// <summary>
        /// Creates a new instance of an UnexpectedStatusCodeError.
        /// </summary>
        /// <param name="response">The HttpResponseMessage to convert to an error.</param>
        /// <param name="target">The target of the error.</param>
        /// <returns>The new UnexpectedStatusCodeError.</returns>
        public static async Task <UnexpectedStatusCodeError> CreateAsync(HttpResponseMessage response, string target)
        {
            if (response is null)
            {
                throw new ArgumentNullException(nameof(response));
            }

            var error = new UnexpectedStatusCodeError();

            error.Code    = "UnexpectedStatusCode";
            error.Target  = target;
            error.Message = $"The HTTP status code of the response was not expected ({(int)response.StatusCode}).";

            if (response.Content != null)
            {
                var stringContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                error.Content = stringContent;
            }

            error.ReasonPhrase = response.ReasonPhrase;
            error.StatusCode   = response.StatusCode;

            return(error);
        }
예제 #2
0
        /// <summary>Create Tenant.</summary>
        /// <param name="iamClient">The IamClient to use.</param>
        /// <param name="request">The request body.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        /// <returns>The raw HttpResponseMessage.</returns>
        public static async Task <Result <Tenant> > CreateTenantResultAsync(this IIamClient iamClient, CreateTenantRequest request, System.Threading.CancellationToken cancellationToken = default)
        {
            if (iamClient is null)
            {
                throw new ArgumentNullException(nameof(iamClient));
            }

            HttpResponseMessage response = await iamClient.CreateTenantHttpResponseAsync(request, cancellationToken).ConfigureAwait(false);

            using (response)
            {
                switch (response.StatusCode)
                {
                case HttpStatusCode.OK:
                    return(await response.DeserializeJsonContentAsync <Tenant>().ConfigureAwait(false));

                case HttpStatusCode.NoContent:
                    return(default);

                case HttpStatusCode.BadRequest:
                case HttpStatusCode.InternalServerError:
                {
                    ErrorResponse errorResponse = await response.DeserializeJsonContentAsync <ErrorResponse>().ConfigureAwait(false);

                    return(ErrorResult <Tenant>(errorResponse.Error));
                }

                default:
                {
                    UnexpectedStatusCodeError error = await UnexpectedStatusCodeError.CreateAsync(response, $"{nameof(IIamClient)}.{nameof(CreateTenantResultAsync)}").ConfigureAwait(false);

                    return(ErrorResult <Tenant>(error));
                }
                }
            }
        }