-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: clean connectors + add polling for connectors (#417)
- Loading branch information
1 parent
f1f8c99
commit 52a869a
Showing
76 changed files
with
1,376 additions
and
664 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
...od-latest/results/migrations-stack.formance.com-v1beta3/payments-v0.7.0-post-upgrade.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
apiVersion: stack.formance.com/v1beta3 | ||
kind: Migration | ||
metadata: | ||
annotations: | ||
reloader.stakater.com/auto: "true" | ||
generation: 1 | ||
labels: | ||
stack: "true" | ||
name: payments-v0.7.0-post-upgrade | ||
namespace: monopod-latest | ||
spec: | ||
configuration: monopod-latest | ||
module: payments | ||
postUpgrade: true | ||
targetedVersion: v0.7.0 | ||
version: monopod-latest | ||
status: | ||
terminated: true |
18 changes: 18 additions & 0 deletions
18
...pod-latest/results/migrations-stack.formance.com-v1beta3/payments-v0.7.0-pre-upgrade.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
apiVersion: stack.formance.com/v1beta3 | ||
kind: Migration | ||
metadata: | ||
annotations: | ||
reloader.stakater.com/auto: "true" | ||
generation: 1 | ||
labels: | ||
stack: "true" | ||
name: payments-v0.7.0-pre-upgrade | ||
namespace: monopod-latest | ||
spec: | ||
configuration: monopod-latest | ||
module: payments | ||
postUpgrade: false | ||
targetedVersion: v0.7.0 | ||
version: monopod-latest | ||
status: | ||
terminated: true |
18 changes: 18 additions & 0 deletions
18
...-ledgerv1/results/migrations-stack.formance.com-v1beta3/payments-v0.7.0-post-upgrade.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
apiVersion: stack.formance.com/v1beta3 | ||
kind: Migration | ||
metadata: | ||
annotations: | ||
reloader.stakater.com/auto: "true" | ||
generation: 1 | ||
labels: | ||
stack: "true" | ||
name: payments-v0.7.0-post-upgrade | ||
namespace: monopod-ledgerv1 | ||
spec: | ||
configuration: monopod-ledgerv1 | ||
module: payments | ||
postUpgrade: true | ||
targetedVersion: v0.7.0 | ||
version: monopod-ledgerv1 | ||
status: | ||
terminated: true |
18 changes: 18 additions & 0 deletions
18
...d-ledgerv1/results/migrations-stack.formance.com-v1beta3/payments-v0.7.0-pre-upgrade.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
apiVersion: stack.formance.com/v1beta3 | ||
kind: Migration | ||
metadata: | ||
annotations: | ||
reloader.stakater.com/auto: "true" | ||
generation: 1 | ||
labels: | ||
stack: "true" | ||
name: payments-v0.7.0-pre-upgrade | ||
namespace: monopod-ledgerv1 | ||
spec: | ||
configuration: monopod-ledgerv1 | ||
module: payments | ||
postUpgrade: false | ||
targetedVersion: v0.7.0 | ||
version: monopod-ledgerv1 | ||
status: | ||
terminated: true |
18 changes: 18 additions & 0 deletions
18
...re-v0.7.0/results/migrations-stack.formance.com-v1beta3/payments-v0.7.0-post-upgrade.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
apiVersion: stack.formance.com/v1beta3 | ||
kind: Migration | ||
metadata: | ||
annotations: | ||
reloader.stakater.com/auto: "true" | ||
generation: 1 | ||
labels: | ||
stack: "true" | ||
name: payments-v0.7.0-post-upgrade | ||
namespace: monopod-search-before-v0-7-0 | ||
spec: | ||
configuration: monopod-search-before-v0-7-0 | ||
module: payments | ||
postUpgrade: true | ||
targetedVersion: v0.7.0 | ||
version: monopod-search-before-v0-7-0 | ||
status: | ||
terminated: true |
18 changes: 18 additions & 0 deletions
18
...ore-v0.7.0/results/migrations-stack.formance.com-v1beta3/payments-v0.7.0-pre-upgrade.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
apiVersion: stack.formance.com/v1beta3 | ||
kind: Migration | ||
metadata: | ||
annotations: | ||
reloader.stakater.com/auto: "true" | ||
generation: 1 | ||
labels: | ||
stack: "true" | ||
name: payments-v0.7.0-pre-upgrade | ||
namespace: monopod-search-before-v0-7-0 | ||
spec: | ||
configuration: monopod-search-before-v0-7-0 | ||
module: payments | ||
postUpgrade: false | ||
targetedVersion: v0.7.0 | ||
version: monopod-search-before-v0-7-0 | ||
status: | ||
terminated: true |
18 changes: 18 additions & 0 deletions
18
...od-latest/results/migrations-stack.formance.com-v1beta3/payments-v0.7.0-post-upgrade.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
apiVersion: stack.formance.com/v1beta3 | ||
kind: Migration | ||
metadata: | ||
annotations: | ||
reloader.stakater.com/auto: "true" | ||
generation: 1 | ||
labels: | ||
stack: "true" | ||
name: payments-v0.7.0-post-upgrade | ||
namespace: multipod-latest | ||
spec: | ||
configuration: multipod-latest | ||
module: payments | ||
postUpgrade: true | ||
targetedVersion: v0.7.0 | ||
version: multipod-latest | ||
status: | ||
terminated: true |
18 changes: 18 additions & 0 deletions
18
...pod-latest/results/migrations-stack.formance.com-v1beta3/payments-v0.7.0-pre-upgrade.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
apiVersion: stack.formance.com/v1beta3 | ||
kind: Migration | ||
metadata: | ||
annotations: | ||
reloader.stakater.com/auto: "true" | ||
generation: 1 | ||
labels: | ||
stack: "true" | ||
name: payments-v0.7.0-pre-upgrade | ||
namespace: multipod-latest | ||
spec: | ||
configuration: multipod-latest | ||
module: payments | ||
postUpgrade: false | ||
targetedVersion: v0.7.0 | ||
version: multipod-latest | ||
status: | ||
terminated: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
components/payments/internal/app/connectors/bankingcircle/client/auth.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
func (c *Client) login(ctx context.Context) error { | ||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, | ||
c.authorizationEndpoint+"/api/v1/authorizations/authorize", http.NoBody) | ||
if err != nil { | ||
return fmt.Errorf("failed to create login request: %w", err) | ||
} | ||
|
||
req.SetBasicAuth(c.username, c.password) | ||
|
||
resp, err := c.httpClient.Do(req) | ||
if err != nil { | ||
return fmt.Errorf("failed to login: %w", err) | ||
} | ||
|
||
defer func() { | ||
err = resp.Body.Close() | ||
if err != nil { | ||
c.logger.Error(err) | ||
} | ||
}() | ||
|
||
responseBody, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return fmt.Errorf("failed to read login response body: %w", err) | ||
} | ||
|
||
//nolint:tagliatelle // allow for client-side structures | ||
type response struct { | ||
AccessToken string `json:"access_token"` | ||
ExpiresIn string `json:"expires_in"` | ||
} | ||
|
||
var res response | ||
|
||
if err = json.Unmarshal(responseBody, &res); err != nil { | ||
return fmt.Errorf("failed to unmarshal login response: %w", err) | ||
} | ||
|
||
c.accessToken = res.AccessToken | ||
|
||
expiresIn, err := strconv.Atoi(res.ExpiresIn) | ||
if err != nil { | ||
return fmt.Errorf("failed to convert expires_in to int: %w", err) | ||
} | ||
|
||
c.accessTokenExpiresAt = time.Now().Add(time.Duration(expiresIn) * time.Second) | ||
|
||
return nil | ||
} | ||
|
||
func (c *Client) ensureAccessTokenIsValid(ctx context.Context) error { | ||
if c.accessTokenExpiresAt.After(time.Now().Add(5 * time.Second)) { | ||
return nil | ||
} | ||
|
||
return c.login(ctx) | ||
} |
71 changes: 71 additions & 0 deletions
71
components/payments/internal/app/connectors/bankingcircle/client/client.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/formancehq/stack/libs/go-libs/logging" | ||
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" | ||
) | ||
|
||
type Client struct { | ||
httpClient *http.Client | ||
|
||
username string | ||
password string | ||
|
||
endpoint string | ||
authorizationEndpoint string | ||
|
||
logger logging.Logger | ||
|
||
accessToken string | ||
accessTokenExpiresAt time.Time | ||
} | ||
|
||
func newHTTPClient(userCertificate, userCertificateKey string) (*http.Client, error) { | ||
cert, err := tls.X509KeyPair([]byte(userCertificate), []byte(userCertificateKey)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
tr := http.DefaultTransport.(*http.Transport).Clone() | ||
tr.TLSClientConfig = &tls.Config{ | ||
Certificates: []tls.Certificate{cert}, | ||
} | ||
|
||
return &http.Client{ | ||
Timeout: 10 * time.Second, | ||
Transport: otelhttp.NewTransport(tr), | ||
}, nil | ||
} | ||
|
||
func NewClient( | ||
username, password, | ||
endpoint, authorizationEndpoint, | ||
uCertificate, uCertificateKey string, | ||
logger logging.Logger) (*Client, error) { | ||
httpClient, err := newHTTPClient(uCertificate, uCertificateKey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
c := &Client{ | ||
httpClient: httpClient, | ||
|
||
username: username, | ||
password: password, | ||
endpoint: endpoint, | ||
authorizationEndpoint: authorizationEndpoint, | ||
|
||
logger: logger, | ||
} | ||
|
||
if err := c.login(context.TODO()); err != nil { | ||
return nil, err | ||
} | ||
|
||
return c, nil | ||
} |
Oops, something went wrong.