Beispiel #1
0
        private void Constructor(string apiKey)
        {
            APIKey = apiKey;

            Contacts           = new ContactsClient(client, apiKey);
            Forms              = new FormsClient(client, apiKey);
            TransactionalEmail = new TransactionalEmailClient(client, apiKey);

            client.BaseAddress = new Uri("https://api.hubapi.com");
        }
        /// <summary>
        /// Deletes a specific webhook for a form.
        /// </summary>
        /// <param name="formsClient">The IFormsClient to use.</param>
        /// <param name="formId">The ID of the requested form.</param>
        /// <param name="webhookId">The ID of the requested webhook.</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>Returns nothing.</returns>
        public static async Task DeleteWebhookAsync(this IFormsClient formsClient, Guid formId, Guid webhookId, Guid tenantId, CancellationToken cancellationToken = default)
        {
            if (formsClient is null)
            {
                throw new ArgumentNullException(nameof(formsClient));
            }

            Result result = await formsClient.DeleteWebhookResultAsync(formId, webhookId, tenantId, cancellationToken).ConfigureAwait(false);

            if (result.IsFail)
            {
                throw ApiException.Create(result.Error);
            }
        }
        /// <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);
        }
        /// <summary>
        /// Submits a new form to VendorHub.
        /// </summary>
        /// <param name="formsClient">The IFormsClient to use.</param>
        /// <param name="formId">The ID of the requested form.</param>
        /// <param name="fields">The text fields to submit.</param>
        /// <param name="attachments">The files to attach.</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 submission.</returns>
        public static async Task <FormSubmission> SubmitFormAsync(this IFormsClient formsClient, Guid formId, IEnumerable <KeyValuePair <string, string> > fields, IEnumerable <KeyValuePair <string, HttpFile> > attachments, Guid tenantId, CancellationToken cancellationToken = default)
        {
            if (formsClient is null)
            {
                throw new ArgumentNullException(nameof(formsClient));
            }

            Result <FormSubmission> result = await formsClient.SubmitFormResultAsync(formId, fields, attachments, tenantId, cancellationToken).ConfigureAwait(false);

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

            throw ApiException.Create(result.Error);
        }
        /// <summary>
        /// Lists all forms for a particular tenant.
        /// </summary>
        /// <param name="formsClient">The IFormsClient to use.</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 list of forms.</returns>
        public static async Task <ICollection <FormDefinition> > ListFormsAsync(this IFormsClient formsClient, Guid tenantId, CancellationToken cancellationToken = default)
        {
            if (formsClient is null)
            {
                throw new ArgumentNullException(nameof(formsClient));
            }

            Result <ICollection <FormDefinition> > result = await formsClient.ListFormsResultAsync(tenantId, cancellationToken).ConfigureAwait(false);

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

            throw ApiException.Create(result.Error);
        }
        /// <summary>
        /// Gets a particular form submission.
        /// </summary>
        /// <param name="formsClient">The IFormsClient to use.</param>
        /// <param name="formId">The ID of the requested form.</param>
        /// <param name="submissionId">The ID of the requested submission.</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 form submission.</returns>
        public static async Task <FormSubmission> GetSubmissionAsync(this IFormsClient formsClient, Guid formId, Guid submissionId, Guid tenantId, CancellationToken cancellationToken = default)
        {
            if (formsClient is null)
            {
                throw new ArgumentNullException(nameof(formsClient));
            }

            Result <FormSubmission> result = await formsClient.GetSubmissionResultAsync(formId, submissionId, tenantId, cancellationToken).ConfigureAwait(false);

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

            throw ApiException.Create(result.Error);
        }
Beispiel #7
0
    public FormsClientIntegrationTests(ITestOutputHelper logger)
    {
        this.logger = logger;

        var serviceCollection = new ServiceCollection();

        serviceCollection.AddFormsClient(new FormsClientOptions
        {
            FormsApiUri        = new Uri(ApiEndpoint),
            TokenClientOptions = new ClientCredentialsTokenClientOptions
            {
                ClientId     = ClientId,
                ClientSecret = ClientSecret,
                Scope        = Scope,
                Authority    = Authority,
            },
        });
        ServiceProvider services = serviceCollection.BuildServiceProvider();

        this.client = services.GetRequiredService <IFormsClient>();
    }
        /// <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>());
                }
                }
            }
        }
        /// <summary>
        /// Submits a new form to VendorHub.
        /// </summary>
        /// <param name="formsClient">The IFormsClient to use.</param>
        /// <param name="formId">The ID of the requested form.</param>
        /// <param name="fields">The text fields to submit.</param>
        /// <param name="attachments">The files to attach.</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 <FormSubmission> > SubmitFormResultAsync(this IFormsClient formsClient, Guid formId, IEnumerable <KeyValuePair <string, string> > fields, IEnumerable <KeyValuePair <string, HttpFile> > attachments, Guid tenantId, CancellationToken cancellationToken = default)
        {
            if (formsClient is null)
            {
                throw new ArgumentNullException(nameof(formsClient));
            }

            HttpResponseMessage response = await formsClient.SubmitFormHttpResponseAsync(formId, fields, attachments, tenantId, cancellationToken).ConfigureAwait(false);

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

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

                    return(error.ToResult <FormSubmission>());
                }
                }
            }
        }
        /// <summary>
        /// Gets the submission statistics for all forms.
        /// </summary>
        /// <param name="formsClient">The IFormsClient to use.</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 form submission statistics.</returns>
        public static async Task <IReadOnlyDictionary <DateTimeOffset, int> > GetAllFormStatisticsAsync(this IFormsClient formsClient, Guid tenantId, CancellationToken cancellationToken = default)
        {
            if (formsClient is null)
            {
                throw new ArgumentNullException(nameof(formsClient));
            }

            Result <IReadOnlyDictionary <DateTimeOffset, int> > result = await formsClient.GetAllFormStatisticsResultAsync(tenantId, cancellationToken).ConfigureAwait(false);

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

            throw ApiException.Create(result.Error);
        }
        /// <summary>
        /// Gets a particular form submission attachment.
        /// </summary>
        /// <param name="formsClient">The IFormsClient to use.</param>
        /// <param name="formId">The ID of the requested form.</param>
        /// <param name="submissionId">The ID of the requested submission.</param>
        /// <param name="attachmentName">The name of the attachment.</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 <HttpFile> > GetSubmissionAttachmentResultAsync(this IFormsClient formsClient, Guid formId, Guid submissionId, string attachmentName, Guid tenantId, CancellationToken cancellationToken = default)
        {
            if (formsClient is null)
            {
                throw new ArgumentNullException(nameof(formsClient));
            }

            HttpResponseMessage response = await formsClient.GetSubmissionAttachmentHttpResponseAsync(formId, submissionId, attachmentName, tenantId, cancellationToken).ConfigureAwait(false);

            using (response)
            {
                switch (response.StatusCode)
                {
                case HttpStatusCode.OK:
                    return(new HttpFile
                    {
                        Data = await response.Content.ReadAsStreamAsync().ConfigureAwait(false),
                        ContentType = response.Content?.Headers?.ContentType?.MediaType,
                        FileName = response.Content?.Headers?.ContentDisposition?.FileName,
                    });

                case HttpStatusCode.NoContent:
                    return(default);

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

                    return(errorResponse.Error.ToResult <HttpFile>());
                }

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

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