/// <inheritdoc/> public async Task <HttpResponseMessage> CreateFormHttpResponseAsync(CreateFormRequest request, Guid tenantId, CancellationToken cancellationToken = default) { IHttpRequestMessageBuilder requestBuilder = UrlBuilder .Create("tenants/{tenantId}/forms") .ReplaceToken("{tenantId}", tenantId) .ToRequest() .WithHttpMethod().Post() .WithContentJson(request) .WithAcceptApplicationJson(); requestBuilder = await this.PreviewCreateFormAsync(requestBuilder, cancellationToken).ConfigureAwait(false); HttpResponseMessage response = await this.SendRequestWithPolicy(requestBuilder, this.CreateFormPolicy, cancellationToken).ConfigureAwait(false); return(response); }
/// <summary> /// Creates a new forms. /// </summary> /// <param name="formsClient">The IFormsClient to use.</param> /// <param name="request">The request body.</param> /// <param name="tenantId">The ID of the requested tenant.</param> /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param> /// <returns>The new form definition.</returns> public static async Task <FormDefinition> CreateFormAsync(this IFormsClient formsClient, CreateFormRequest request, Guid tenantId, CancellationToken cancellationToken = default) { if (formsClient is null) { throw new ArgumentNullException(nameof(formsClient)); } Result <FormDefinition> result = await formsClient.CreateFormResultAsync(request, tenantId, cancellationToken).ConfigureAwait(false); if (result.IsSuccess) { return(result.Value); } throw ApiException.Create(result.Error); }
/// <summary> /// Creates a new forms. /// </summary> /// <param name="formsClient">The IFormsClient to use.</param> /// <param name="request">The request body.</param> /// <param name="tenantId">The ID of the requested tenant.</param> /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param> /// <returns>The result or an error.</returns> public static async Task <Result <FormDefinition> > CreateFormResultAsync(this IFormsClient formsClient, CreateFormRequest request, Guid tenantId, CancellationToken cancellationToken = default) { if (formsClient is null) { throw new ArgumentNullException(nameof(formsClient)); } HttpResponseMessage response = await formsClient.CreateFormHttpResponseAsync(request, tenantId, cancellationToken).ConfigureAwait(false); using (response) { switch (response.StatusCode) { case HttpStatusCode.OK: return(Result.Create(await response.DeserializeJsonContentAsync <FormDefinition>().ConfigureAwait(false))); case HttpStatusCode.NoContent: return(default); case HttpStatusCode.BadRequest: case HttpStatusCode.InternalServerError: { ErrorResponse errorResponse = await response.DeserializeJsonContentAsync <ErrorResponse>().ConfigureAwait(false); return(errorResponse.Error.ToResult <FormDefinition>()); } default: { UnexpectedStatusCodeError error = await UnexpectedStatusCodeError.CreateAsync(response, $"{nameof(IFormsClient)}.{nameof(CreateFormResultAsync)}").ConfigureAwait(false); return(error.ToResult <FormDefinition>()); } } } }