Skip to content

Commit

Permalink
Merge branch 'main' into feat/secrets-encryption
Browse files Browse the repository at this point in the history
  • Loading branch information
darkmatterpool authored Feb 2, 2023
2 parents 71c2a33 + e767e68 commit a0fd7f7
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ COPY --from=builder /go/src/github.com/formancehq/payments/bin/payments /usr/loc
EXPOSE 8080

ENTRYPOINT ["payments"]

ENV OTEL_SERVICE_NAME=payments
CMD ["server"]
52 changes: 38 additions & 14 deletions internal/app/api/connector.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package api

import (
"context"
"encoding/json"
"errors"
"net/http"
"time"

Expand Down Expand Up @@ -51,6 +49,10 @@ func handleError(w http.ResponseWriter, r *http.Request, err error) {
func readConfig[Config models.ConnectorConfigObject](connectorManager *integration.ConnectorManager[Config],
) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if connectorNotInstalled(connectorManager, w, r) {
return
}

config, err := connectorManager.ReadConfig(r.Context())
if err != nil {
handleError(w, r, err)
Expand Down Expand Up @@ -81,6 +83,10 @@ type listTasksResponseElement struct {
func listTasks[Config models.ConnectorConfigObject](connectorManager *integration.ConnectorManager[Config],
) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if connectorNotInstalled(connectorManager, w, r) {
return
}

pageSize, err := pageSizeQueryParam(r)
if err != nil {
handleValidationError(w, r, err)
Expand Down Expand Up @@ -134,6 +140,10 @@ func listTasks[Config models.ConnectorConfigObject](connectorManager *integratio
func readTask[Config models.ConnectorConfigObject](connectorManager *integration.ConnectorManager[Config],
) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if connectorNotInstalled(connectorManager, w, r) {
return
}

taskID, err := uuid.Parse(mux.Vars(r)["taskID"])
if err != nil {
handleErrorBadRequest(w, r, err)
Expand Down Expand Up @@ -171,6 +181,10 @@ func readTask[Config models.ConnectorConfigObject](connectorManager *integration
func uninstall[Config models.ConnectorConfigObject](connectorManager *integration.ConnectorManager[Config],
) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if connectorNotInstalled(connectorManager, w, r) {
return
}

err := connectorManager.Uninstall(r.Context())
if err != nil {
handleError(w, r, err)
Expand All @@ -185,7 +199,7 @@ func uninstall[Config models.ConnectorConfigObject](connectorManager *integratio
func install[Config models.ConnectorConfigObject](connectorManager *integration.ConnectorManager[Config],
) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
installed, err := connectorManager.IsInstalled(context.Background())
installed, err := connectorManager.IsInstalled(r.Context())
if err != nil {
handleError(w, r, err)

Expand Down Expand Up @@ -222,20 +236,11 @@ func install[Config models.ConnectorConfigObject](connectorManager *integration.
func reset[Config models.ConnectorConfigObject](connectorManager *integration.ConnectorManager[Config],
) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
installed, err := connectorManager.IsInstalled(context.Background())
if err != nil {
handleError(w, r, err)

if connectorNotInstalled(connectorManager, w, r) {
return
}

if !installed {
handleError(w, r, errors.New("connector not installed"))

return
}

err = connectorManager.Reset(r.Context())
err := connectorManager.Reset(r.Context())
if err != nil {
handleError(w, r, err)

Expand All @@ -245,3 +250,22 @@ func reset[Config models.ConnectorConfigObject](connectorManager *integration.Co
w.WriteHeader(http.StatusNoContent)
}
}

func connectorNotInstalled[Config models.ConnectorConfigObject](connectorManager *integration.ConnectorManager[Config],
w http.ResponseWriter, r *http.Request,
) bool {
installed, err := connectorManager.IsInstalled(r.Context())
if err != nil {
handleError(w, r, err)

return true
}

if !installed {
handleErrorBadRequest(w, r, integration.ErrNotInstalled)

return true
}

return false
}
20 changes: 18 additions & 2 deletions internal/app/api/stripe.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"encoding/json"
"net/http"

"github.com/formancehq/payments/internal/app/integration"

"github.com/formancehq/go-libs/api"

"github.com/formancehq/payments/internal/app/models"
Expand Down Expand Up @@ -49,13 +51,27 @@ func (req *stripeTransferRequest) validate() error {

type stripeTransfersRepository interface {
GetConfig(ctx context.Context, connectorName models.ConnectorProvider, cfg any) error
IsInstalled(ctx context.Context, provider models.ConnectorProvider) (bool, error)
}

func handleStripeTransfers(repo stripeTransfersRepository) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
installed, err := repo.IsInstalled(r.Context(), stripeConnector.Name)
if err != nil {
handleError(w, r, err)

return
}

if !installed {
handleErrorBadRequest(w, r, integration.ErrNotInstalled)

return
}

var cfg stripeConnector.Config

if err := repo.GetConfig(r.Context(), stripeConnector.Name, &cfg); err != nil {
if err = repo.GetConfig(r.Context(), stripeConnector.Name, &cfg); err != nil {
handleError(w, r, err)

return
Expand All @@ -65,7 +81,7 @@ func handleStripeTransfers(repo stripeTransfersRepository) http.HandlerFunc {

var transferRequest stripeTransferRequest

err := json.NewDecoder(r.Body).Decode(&transferRequest)
err = json.NewDecoder(r.Body).Decode(&transferRequest)
if err != nil {
handleError(w, r, err)

Expand Down
5 changes: 3 additions & 2 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ paths:
tags:
- Payments
description: |
Reset a connector by its name.
Reset a connector by its name.
It will remove the connector and ALL PAYMENTS generated with it.
parameters:
- $ref: '#/components/parameters/Connector'
Expand Down Expand Up @@ -160,7 +160,7 @@ paths:
responses:
'200':
$ref: '#/components/responses/Task'
/connectors/stripe/transfer:
/connectors/stripe/transfers:
post:
summary: Transfer funds between Stripe accounts
tags:
Expand Down Expand Up @@ -869,6 +869,7 @@ components:
- ACTIVE
- TERMINATED
- FAILED
- SUCCEEDED
ServerInfo:
type: object
required:
Expand Down

0 comments on commit a0fd7f7

Please sign in to comment.