mirror of
https://github.com/hanzo-fi/payments.git
synced 2026-10-30 20:31:20 +00:00
- go.mod module path + all self-imports: github.com/formancehq/payments -> github.com/hanzo-fi/payments (1590 .go files / 3152 refs; 20 modules: root, pkg/client, pkg/domain, 18 ce/plugins). External dep kept: formancehq/go-libs. - plugins: each ce/plugins/* consumed a *published* pkg/domain; added local 'replace .../pkg/domain => ../../../pkg/domain' (consistent with root) so every standalone plugin module builds in-monorepo. compile-plugin.sh MOD var rebranded too. - branding: README, LICENSE (dual MIT+EE; upstream copyright retained + Hanzo added), goreleaser (release images -> registry.hanzo.ai/hanzo-fi, OCI labels, homepage), proto go_package, renovate, speakeasy gen.yaml, connector-template *.gotpl. - CI: removed Numary/Formance workflows + composite action; added canonical hanzo.yml + cicd.yml (hanzoai/ci, gated HANZO_CI_ENABLED) + secretless ci.yml (go build/vet). - kept as data/compat keys: wire capability enums ALLOW_FORMANCE_*, generated SDK type client.Formance, domain field FormanceRedirectURL, OTEL_SERVICE_NAME, upstream base image. - verified: go build ./... (default + -tags ee) + all 20 nested modules green.
103 lines
2.8 KiB
Go
103 lines
2.8 KiB
Go
package testserver
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/hanzo-fi/payments/pkg/domain/httpwrapper"
|
|
"github.com/hanzo-fi/payments/pkg/domain/models"
|
|
formance "github.com/hanzo-fi/payments/pkg/client"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func NewStackClient(urlStr string, clientTimeout time.Duration, transport http.RoundTripper) (*formance.Formance, error) {
|
|
httpClient := &http.Client{
|
|
Timeout: clientTimeout,
|
|
Transport: transport,
|
|
}
|
|
|
|
return formance.New(
|
|
urlStr,
|
|
formance.WithClient(httpClient),
|
|
), nil
|
|
}
|
|
|
|
type Client struct {
|
|
baseUrl string
|
|
internalClient httpwrapper.Client
|
|
transport http.RoundTripper
|
|
}
|
|
|
|
func NewClient(urlStr string, clientTimeout time.Duration, transport http.RoundTripper) (*Client, error) {
|
|
config := &httpwrapper.Config{Timeout: clientTimeout}
|
|
internalClient := httpwrapper.NewClient(config)
|
|
return &Client{
|
|
baseUrl: urlStr,
|
|
transport: transport,
|
|
internalClient: internalClient,
|
|
}, nil
|
|
}
|
|
|
|
func (c *Client) wrapError(err error, method, path string, status int, errBody map[string]interface{}) error {
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
return fmt.Errorf("error with status %d for %s to '%s': %w, body: %+v", status, method, path, err, errBody)
|
|
}
|
|
|
|
func (c *Client) Get(ctx context.Context, path string, resBody any) error {
|
|
method := http.MethodGet
|
|
req, err := http.NewRequestWithContext(ctx, method, c.baseUrl+path, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var errBody map[string]interface{}
|
|
status, err := c.internalClient.Do(ctx, req, resBody, &errBody)
|
|
return c.wrapError(err, method, path, status, errBody)
|
|
}
|
|
|
|
func (c *Client) Do(ctx context.Context, method string, path string, body any, resBody any) error {
|
|
b, err := json.Marshal(body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, method, c.baseUrl+path, bytes.NewReader(b))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var errBody map[string]interface{}
|
|
status, err := c.internalClient.Do(ctx, req, resBody, &errBody)
|
|
return c.wrapError(err, method, path, status, errBody)
|
|
}
|
|
|
|
func (c *Client) PollTask(ctx context.Context, t T) func(id string) func() models.Task {
|
|
return func(id string) func() models.Task {
|
|
return func() models.Task {
|
|
path := "/v3/tasks/" + id
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.baseUrl+path, nil)
|
|
require.NoError(t, err)
|
|
|
|
httpClient := &http.Client{Timeout: defaultHttpClientTimeout}
|
|
res, err := httpClient.Do(req)
|
|
require.NoError(t, err)
|
|
defer res.Body.Close()
|
|
require.Equal(t, http.StatusOK, res.StatusCode)
|
|
|
|
var expectedBody struct{ Data models.Task }
|
|
rawBody, err := io.ReadAll(res.Body)
|
|
require.NoError(t, err)
|
|
err = json.Unmarshal(rawBody, &expectedBody)
|
|
require.NoError(t, err)
|
|
return expectedBody.Data
|
|
}
|
|
}
|
|
}
|