Skip to content

Commit

Permalink
feat: implement update webhook config endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
vitords committed Oct 24, 2023
1 parent bf57303 commit ff47c70
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
9 changes: 9 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ type Client interface {

// Webhooks
GetWebhookConfig(ctx context.Context) (*GetWebhookConfigResponse, error)
UpdateWebhookConfig(
ctx context.Context,
request *UpdateWebhookConfigRequest,
) (*UpdateWebhookConfigResponse, error)
}

// clientImpl to the Monta Partner API.
Expand Down Expand Up @@ -185,6 +189,11 @@ func doDelete(ctx context.Context, client *clientImpl, path string) error {
return err
}

// Template method to execute PUT requests towards monta.
func doPut[T any](ctx context.Context, client *clientImpl, path string, body io.Reader) (*T, error) {
return execute[T](ctx, client, http.MethodPut, path, nil, &http.Header{"content-type": {"application/json"}}, body)
}

// Template method to execute requests towards monta.
func execute[T any](
ctx context.Context,
Expand Down
35 changes: 35 additions & 0 deletions client_webhooks.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package monta

import (
"bytes"
"context"
"encoding/json"
"net/url"
)

Expand All @@ -21,3 +23,36 @@ func (c *clientImpl) GetWebhookConfig(ctx context.Context) (*GetWebhookConfigRes
query := url.Values{}
return doGet[GetWebhookConfigResponse](ctx, c, path, query)
}

// UpdateWebhookConfigRequest is the request input to the [Client.UpdateWebhookConfig] method.
type UpdateWebhookConfigRequest struct {
// A HTTPS URL to send the webhook payload to when an event occurs.
WebhookURL string `json:"webhookUrl"`
// A cryptoghrapic secret used to sign the webhook payload.
WebhookSecret string `json:"webhookSecret"`
// A list of event type to subscribe to. Use ["*"] to subscribe to all.
EventTypes []*WebhookEventType `json:"eventTypes"`
}

// UpdateWebhookConfigResponse is the response output from the [Client.UpdateWebhookConfig] method.
type UpdateWebhookConfigResponse struct {
// A HTTPS URL to send the webhook payload to when an event occurs.
WebhookURL string `json:"webhookUrl"`
// A cryptoghrapic secret used to sign the webhook payload.
WebhookSecret string `json:"webhookSecret"`
// A list of event type to subscribe to. Use of ["*"] means subscribe to all.
EventTypes []*string `json:"eventTypes"`
}

// UpdateWebhookConfig to update your webhook config.
func (c *clientImpl) UpdateWebhookConfig(
ctx context.Context,
request *UpdateWebhookConfigRequest,
) (*UpdateWebhookConfigResponse, error) {
path := "/v1/webhooks/config"
var requestBody bytes.Buffer
if err := json.NewEncoder(&requestBody).Encode(&request); err != nil {
return nil, err
}
return doPut[UpdateWebhookConfigResponse](ctx, c, path, &requestBody)
}

0 comments on commit ff47c70

Please sign in to comment.