mirror of
https://github.com/hanzo-fi/stack.git
synced 2026-10-30 20:34:43 +00:00
Co-authored-by: Maxence Maireaux <[email protected]> Co-authored-by: David Ragot <[email protected]>
619 lines
21 KiB
Go
619 lines
21 KiB
Go
// Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT.
|
|
|
|
package formancesdkgo
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"github.com/formancehq/formance-sdk-go/pkg/models/operations"
|
|
"github.com/formancehq/formance-sdk-go/pkg/models/sdkerrors"
|
|
"github.com/formancehq/formance-sdk-go/pkg/models/shared"
|
|
"github.com/formancehq/formance-sdk-go/pkg/utils"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
type Webhooks struct {
|
|
sdkConfiguration sdkConfiguration
|
|
}
|
|
|
|
func newWebhooks(sdkConfig sdkConfiguration) *Webhooks {
|
|
return &Webhooks{
|
|
sdkConfiguration: sdkConfig,
|
|
}
|
|
}
|
|
|
|
// ActivateConfig - Activate one config
|
|
// Activate a webhooks config by ID, to start receiving webhooks to its endpoint.
|
|
func (s *Webhooks) ActivateConfig(ctx context.Context, request operations.ActivateConfigRequest) (*operations.ActivateConfigResponse, error) {
|
|
baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
url, err := utils.GenerateURL(ctx, baseURL, "/api/webhooks/configs/{id}/activate", request, nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error generating URL: %w", err)
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "PUT", url, nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error creating request: %w", err)
|
|
}
|
|
req.Header.Set("Accept", "application/json")
|
|
req.Header.Set("user-agent", s.sdkConfiguration.UserAgent)
|
|
|
|
client := s.sdkConfiguration.SecurityClient
|
|
|
|
httpRes, err := client.Do(req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error sending request: %w", err)
|
|
}
|
|
if httpRes == nil {
|
|
return nil, fmt.Errorf("error sending request: no response")
|
|
}
|
|
|
|
contentType := httpRes.Header.Get("Content-Type")
|
|
|
|
res := &operations.ActivateConfigResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: contentType,
|
|
RawResponse: httpRes,
|
|
}
|
|
|
|
rawBody, err := io.ReadAll(httpRes.Body)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error reading response body: %w", err)
|
|
}
|
|
httpRes.Body.Close()
|
|
httpRes.Body = io.NopCloser(bytes.NewBuffer(rawBody))
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
switch {
|
|
case utils.MatchContentType(contentType, `application/json`):
|
|
var out shared.ConfigResponse
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.ConfigResponse = &out
|
|
default:
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
default:
|
|
switch {
|
|
case utils.MatchContentType(contentType, `application/json`):
|
|
var out sdkerrors.WebhooksErrorResponse
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.WebhooksErrorResponse = &out
|
|
default:
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
// ChangeConfigSecret - Change the signing secret of a config
|
|
// Change the signing secret of the endpoint of a webhooks config.
|
|
//
|
|
// If not passed or empty, a secret is automatically generated.
|
|
// The format is a random string of bytes of size 24, base64 encoded. (larger size after encoding)
|
|
func (s *Webhooks) ChangeConfigSecret(ctx context.Context, request operations.ChangeConfigSecretRequest) (*operations.ChangeConfigSecretResponse, error) {
|
|
baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
url, err := utils.GenerateURL(ctx, baseURL, "/api/webhooks/configs/{id}/secret/change", request, nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error generating URL: %w", err)
|
|
}
|
|
|
|
bodyReader, reqContentType, err := utils.SerializeRequestBody(ctx, request, false, true, "ConfigChangeSecret", "json", `request:"mediaType=application/json"`)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error serializing request body: %w", err)
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "PUT", url, bodyReader)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error creating request: %w", err)
|
|
}
|
|
req.Header.Set("Accept", "application/json")
|
|
req.Header.Set("user-agent", s.sdkConfiguration.UserAgent)
|
|
|
|
req.Header.Set("Content-Type", reqContentType)
|
|
|
|
client := s.sdkConfiguration.SecurityClient
|
|
|
|
httpRes, err := client.Do(req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error sending request: %w", err)
|
|
}
|
|
if httpRes == nil {
|
|
return nil, fmt.Errorf("error sending request: no response")
|
|
}
|
|
|
|
contentType := httpRes.Header.Get("Content-Type")
|
|
|
|
res := &operations.ChangeConfigSecretResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: contentType,
|
|
RawResponse: httpRes,
|
|
}
|
|
|
|
rawBody, err := io.ReadAll(httpRes.Body)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error reading response body: %w", err)
|
|
}
|
|
httpRes.Body.Close()
|
|
httpRes.Body = io.NopCloser(bytes.NewBuffer(rawBody))
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
switch {
|
|
case utils.MatchContentType(contentType, `application/json`):
|
|
var out shared.ConfigResponse
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.ConfigResponse = &out
|
|
default:
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode == 400:
|
|
switch {
|
|
case utils.MatchContentType(contentType, `application/json`):
|
|
var out sdkerrors.WebhooksErrorResponse
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, &out
|
|
default:
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
default:
|
|
switch {
|
|
case utils.MatchContentType(contentType, `application/json`):
|
|
var out sdkerrors.WebhooksErrorResponse
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.WebhooksErrorResponse = &out
|
|
default:
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
// DeactivateConfig - Deactivate one config
|
|
// Deactivate a webhooks config by ID, to stop receiving webhooks to its endpoint.
|
|
func (s *Webhooks) DeactivateConfig(ctx context.Context, request operations.DeactivateConfigRequest) (*operations.DeactivateConfigResponse, error) {
|
|
baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
url, err := utils.GenerateURL(ctx, baseURL, "/api/webhooks/configs/{id}/deactivate", request, nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error generating URL: %w", err)
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "PUT", url, nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error creating request: %w", err)
|
|
}
|
|
req.Header.Set("Accept", "application/json")
|
|
req.Header.Set("user-agent", s.sdkConfiguration.UserAgent)
|
|
|
|
client := s.sdkConfiguration.SecurityClient
|
|
|
|
httpRes, err := client.Do(req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error sending request: %w", err)
|
|
}
|
|
if httpRes == nil {
|
|
return nil, fmt.Errorf("error sending request: no response")
|
|
}
|
|
|
|
contentType := httpRes.Header.Get("Content-Type")
|
|
|
|
res := &operations.DeactivateConfigResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: contentType,
|
|
RawResponse: httpRes,
|
|
}
|
|
|
|
rawBody, err := io.ReadAll(httpRes.Body)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error reading response body: %w", err)
|
|
}
|
|
httpRes.Body.Close()
|
|
httpRes.Body = io.NopCloser(bytes.NewBuffer(rawBody))
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
switch {
|
|
case utils.MatchContentType(contentType, `application/json`):
|
|
var out shared.ConfigResponse
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.ConfigResponse = &out
|
|
default:
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode == 404:
|
|
switch {
|
|
case utils.MatchContentType(contentType, `application/json`):
|
|
var out sdkerrors.WebhooksErrorResponse
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, &out
|
|
default:
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
default:
|
|
switch {
|
|
case utils.MatchContentType(contentType, `application/json`):
|
|
var out sdkerrors.WebhooksErrorResponse
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.WebhooksErrorResponse = &out
|
|
default:
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
// DeleteConfig - Delete one config
|
|
// Delete a webhooks config by ID.
|
|
func (s *Webhooks) DeleteConfig(ctx context.Context, request operations.DeleteConfigRequest) (*operations.DeleteConfigResponse, error) {
|
|
baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
url, err := utils.GenerateURL(ctx, baseURL, "/api/webhooks/configs/{id}", request, nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error generating URL: %w", err)
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "DELETE", url, nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error creating request: %w", err)
|
|
}
|
|
req.Header.Set("Accept", "application/json")
|
|
req.Header.Set("user-agent", s.sdkConfiguration.UserAgent)
|
|
|
|
client := s.sdkConfiguration.SecurityClient
|
|
|
|
httpRes, err := client.Do(req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error sending request: %w", err)
|
|
}
|
|
if httpRes == nil {
|
|
return nil, fmt.Errorf("error sending request: no response")
|
|
}
|
|
|
|
contentType := httpRes.Header.Get("Content-Type")
|
|
|
|
res := &operations.DeleteConfigResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: contentType,
|
|
RawResponse: httpRes,
|
|
}
|
|
|
|
rawBody, err := io.ReadAll(httpRes.Body)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error reading response body: %w", err)
|
|
}
|
|
httpRes.Body.Close()
|
|
httpRes.Body = io.NopCloser(bytes.NewBuffer(rawBody))
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
case httpRes.StatusCode == 400:
|
|
fallthrough
|
|
case httpRes.StatusCode == 404:
|
|
switch {
|
|
case utils.MatchContentType(contentType, `application/json`):
|
|
var out sdkerrors.WebhooksErrorResponse
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, &out
|
|
default:
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
default:
|
|
switch {
|
|
case utils.MatchContentType(contentType, `application/json`):
|
|
var out sdkerrors.WebhooksErrorResponse
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.WebhooksErrorResponse = &out
|
|
default:
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
// GetManyConfigs - Get many configs
|
|
// Sorted by updated date descending
|
|
func (s *Webhooks) GetManyConfigs(ctx context.Context, request operations.GetManyConfigsRequest) (*operations.GetManyConfigsResponse, error) {
|
|
baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
url := strings.TrimSuffix(baseURL, "/") + "/api/webhooks/configs"
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error creating request: %w", err)
|
|
}
|
|
req.Header.Set("Accept", "application/json")
|
|
req.Header.Set("user-agent", s.sdkConfiguration.UserAgent)
|
|
|
|
if err := utils.PopulateQueryParams(ctx, req, request, nil); err != nil {
|
|
return nil, fmt.Errorf("error populating query params: %w", err)
|
|
}
|
|
|
|
client := s.sdkConfiguration.SecurityClient
|
|
|
|
httpRes, err := client.Do(req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error sending request: %w", err)
|
|
}
|
|
if httpRes == nil {
|
|
return nil, fmt.Errorf("error sending request: no response")
|
|
}
|
|
|
|
contentType := httpRes.Header.Get("Content-Type")
|
|
|
|
res := &operations.GetManyConfigsResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: contentType,
|
|
RawResponse: httpRes,
|
|
}
|
|
|
|
rawBody, err := io.ReadAll(httpRes.Body)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error reading response body: %w", err)
|
|
}
|
|
httpRes.Body.Close()
|
|
httpRes.Body = io.NopCloser(bytes.NewBuffer(rawBody))
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
switch {
|
|
case utils.MatchContentType(contentType, `application/json`):
|
|
var out shared.ConfigsResponse
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.ConfigsResponse = &out
|
|
default:
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
default:
|
|
switch {
|
|
case utils.MatchContentType(contentType, `application/json`):
|
|
var out sdkerrors.WebhooksErrorResponse
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.WebhooksErrorResponse = &out
|
|
default:
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
// InsertConfig - Insert a new config
|
|
// Insert a new webhooks config.
|
|
//
|
|
// The endpoint should be a valid https URL and be unique.
|
|
//
|
|
// The secret is the endpoint's verification secret.
|
|
// If not passed or empty, a secret is automatically generated.
|
|
// The format is a random string of bytes of size 24, base64 encoded. (larger size after encoding)
|
|
//
|
|
// All eventTypes are converted to lower-case when inserted.
|
|
func (s *Webhooks) InsertConfig(ctx context.Context, request shared.ConfigUser) (*operations.InsertConfigResponse, error) {
|
|
baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
url := strings.TrimSuffix(baseURL, "/") + "/api/webhooks/configs"
|
|
|
|
bodyReader, reqContentType, err := utils.SerializeRequestBody(ctx, request, false, false, "Request", "json", `request:"mediaType=application/json"`)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error serializing request body: %w", err)
|
|
}
|
|
if bodyReader == nil {
|
|
return nil, fmt.Errorf("request body is required")
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "POST", url, bodyReader)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error creating request: %w", err)
|
|
}
|
|
req.Header.Set("Accept", "application/json")
|
|
req.Header.Set("user-agent", s.sdkConfiguration.UserAgent)
|
|
|
|
req.Header.Set("Content-Type", reqContentType)
|
|
|
|
client := s.sdkConfiguration.SecurityClient
|
|
|
|
httpRes, err := client.Do(req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error sending request: %w", err)
|
|
}
|
|
if httpRes == nil {
|
|
return nil, fmt.Errorf("error sending request: no response")
|
|
}
|
|
|
|
contentType := httpRes.Header.Get("Content-Type")
|
|
|
|
res := &operations.InsertConfigResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: contentType,
|
|
RawResponse: httpRes,
|
|
}
|
|
|
|
rawBody, err := io.ReadAll(httpRes.Body)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error reading response body: %w", err)
|
|
}
|
|
httpRes.Body.Close()
|
|
httpRes.Body = io.NopCloser(bytes.NewBuffer(rawBody))
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
switch {
|
|
case utils.MatchContentType(contentType, `application/json`):
|
|
var out shared.ConfigResponse
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.ConfigResponse = &out
|
|
default:
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode == 400:
|
|
switch {
|
|
case utils.MatchContentType(contentType, `application/json`):
|
|
var out sdkerrors.WebhooksErrorResponse
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, &out
|
|
default:
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
default:
|
|
switch {
|
|
case utils.MatchContentType(contentType, `application/json`):
|
|
var out sdkerrors.WebhooksErrorResponse
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.WebhooksErrorResponse = &out
|
|
default:
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
// TestConfig - Test one config
|
|
// Test a config by sending a webhook to its endpoint.
|
|
func (s *Webhooks) TestConfig(ctx context.Context, request operations.TestConfigRequest) (*operations.TestConfigResponse, error) {
|
|
baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails())
|
|
url, err := utils.GenerateURL(ctx, baseURL, "/api/webhooks/configs/{id}/test", request, nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error generating URL: %w", err)
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error creating request: %w", err)
|
|
}
|
|
req.Header.Set("Accept", "application/json")
|
|
req.Header.Set("user-agent", s.sdkConfiguration.UserAgent)
|
|
|
|
client := s.sdkConfiguration.SecurityClient
|
|
|
|
httpRes, err := client.Do(req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error sending request: %w", err)
|
|
}
|
|
if httpRes == nil {
|
|
return nil, fmt.Errorf("error sending request: no response")
|
|
}
|
|
|
|
contentType := httpRes.Header.Get("Content-Type")
|
|
|
|
res := &operations.TestConfigResponse{
|
|
StatusCode: httpRes.StatusCode,
|
|
ContentType: contentType,
|
|
RawResponse: httpRes,
|
|
}
|
|
|
|
rawBody, err := io.ReadAll(httpRes.Body)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error reading response body: %w", err)
|
|
}
|
|
httpRes.Body.Close()
|
|
httpRes.Body = io.NopCloser(bytes.NewBuffer(rawBody))
|
|
switch {
|
|
case httpRes.StatusCode == 200:
|
|
switch {
|
|
case utils.MatchContentType(contentType, `application/json`):
|
|
var out shared.AttemptResponse
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.AttemptResponse = &out
|
|
default:
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode == 400:
|
|
fallthrough
|
|
case httpRes.StatusCode == 404:
|
|
switch {
|
|
case utils.MatchContentType(contentType, `application/json`):
|
|
var out sdkerrors.WebhooksErrorResponse
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, &out
|
|
default:
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
case httpRes.StatusCode >= 400 && httpRes.StatusCode < 500:
|
|
fallthrough
|
|
case httpRes.StatusCode >= 500 && httpRes.StatusCode < 600:
|
|
return nil, sdkerrors.NewSDKError("API error occurred", httpRes.StatusCode, string(rawBody), httpRes)
|
|
default:
|
|
switch {
|
|
case utils.MatchContentType(contentType, `application/json`):
|
|
var out sdkerrors.WebhooksErrorResponse
|
|
if err := utils.UnmarshalJsonFromResponseBody(bytes.NewBuffer(rawBody), &out, ""); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.WebhooksErrorResponse = &out
|
|
default:
|
|
return nil, sdkerrors.NewSDKError(fmt.Sprintf("unknown content-type received: %s", contentType), httpRes.StatusCode, string(rawBody), httpRes)
|
|
}
|
|
}
|
|
|
|
return res, nil
|
|
}
|