Exemplo n.º 1
0
        /// <inheritdoc/>
        public async Task <HttpResponseMessage> CreateWebhookHttpResponseAsync(Guid formId, CreateWebhookRequest request, Guid tenantId, CancellationToken cancellationToken = default)
        {
            IHttpRequestMessageBuilder requestBuilder = UrlBuilder
                                                        .Create("tenants/{tenantId}/forms/{formId}/webhooks")
                                                        .ReplaceToken("{tenantId}", tenantId)
                                                        .ReplaceToken("{formId}", formId)
                                                        .ToRequest()
                                                        .WithHttpMethod().Post()
                                                        .WithAcceptApplicationJson();

            requestBuilder = await this.PreviewCreateWebhookAsync(requestBuilder, cancellationToken).ConfigureAwait(false);

            HttpResponseMessage response = await this.SendRequestWithPolicy(requestBuilder, this.CreateWebhookPolicy, cancellationToken).ConfigureAwait(false);

            return(response);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a new form specific webhook.
        /// </summary>
        /// <param name="formsClient">The IFormsClient to use.</param>
        /// <param name="formId">The ID of the requested form.</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 webhook.</returns>
        public static async Task <Webhook> CreateWebhookAsync(this IFormsClient formsClient, Guid formId, CreateWebhookRequest request, Guid tenantId, CancellationToken cancellationToken = default)
        {
            if (formsClient is null)
            {
                throw new ArgumentNullException(nameof(formsClient));
            }

            Result <Webhook> result = await formsClient.CreateWebhookResultAsync(formId, request, tenantId, cancellationToken).ConfigureAwait(false);

            if (result.IsSuccess)
            {
                return(result.Value);
            }

            throw ApiException.Create(result.Error);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Creates a new form specific webhook.
        /// </summary>
        /// <param name="formsClient">The IFormsClient to use.</param>
        /// <param name="formId">The ID of the requested form.</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 <Webhook> > CreateWebhookResultAsync(this IFormsClient formsClient, Guid formId, CreateWebhookRequest request, Guid tenantId, CancellationToken cancellationToken = default)
        {
            if (formsClient is null)
            {
                throw new ArgumentNullException(nameof(formsClient));
            }

            HttpResponseMessage response = await formsClient.CreateWebhookHttpResponseAsync(formId, request, tenantId, cancellationToken).ConfigureAwait(false);

            using (response)
            {
                switch (response.StatusCode)
                {
                case HttpStatusCode.OK:
                    return(Result.Create(await response.DeserializeJsonContentAsync <Webhook>().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 <Webhook>());
                }

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

                    return(error.ToResult <Webhook>());
                }
                }
            }
        }