Skip to content

Commit

Permalink
feat: list available connectors configs (#62)
Browse files Browse the repository at this point in the history
Signed-off-by: Lawrence Zawila <[email protected]>
  • Loading branch information
darkmatterpool authored Nov 28, 2022
1 parent cf71535 commit a762de0
Show file tree
Hide file tree
Showing 11 changed files with 178 additions and 8 deletions.
40 changes: 40 additions & 0 deletions internal/app/api/connectorconfigs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package api

import (
"encoding/json"
"net/http"

"github.com/formancehq/payments/internal/pkg/connectors/currencycloud"
"github.com/formancehq/payments/internal/pkg/connectors/dummypay"
"github.com/formancehq/payments/internal/pkg/connectors/modulr"
"github.com/formancehq/payments/internal/pkg/connectors/stripe"
"github.com/formancehq/payments/internal/pkg/connectors/wise"

"github.com/formancehq/payments/internal/pkg/configtemplate"
"github.com/formancehq/payments/internal/pkg/connectors/bankingcircle"
)

func connectorConfigsHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// TODO: It's not ideal to re-identify available connectors
// Refactor it when refactoring the HTTP lib.

configs := configtemplate.BuildConfigs(
bankingcircle.Config{},
currencycloud.Config{},
dummypay.Config{},
modulr.Config{},
stripe.Config{},
wise.Config{},
)

w.Header().Set("Content-Type", "application/json")

err := json.NewEncoder(w).Encode(configs)
if err != nil {
handleServerError(w, r, err)

return
}
}
}
2 changes: 2 additions & 0 deletions internal/app/api/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ func httpRouter(db *mongo.Database, client *mongo.Client, handlers []connectorHa
authGroup.HandleFunc("/connectors", readConnectorsHandler(db))
connectorGroup := authGroup.PathPrefix("/connectors").Subrouter()

connectorGroup.Path("/configs").Handler(connectorConfigsHandler())

for _, h := range handlers {
connectorGroup.PathPrefix("/" + h.Name).Handler(
http.StripPrefix("/connectors", h.Handler),
Expand Down
35 changes: 35 additions & 0 deletions internal/pkg/configtemplate/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package configtemplate

type Configs map[string]Config

type Config map[string]Parameter

type Parameter struct {
DataType Type `json:"dataType"`
Required bool `json:"required"`
}

type TemplateBuilder interface {
BuildTemplate() (string, Config)
}

func BuildConfigs(builders ...TemplateBuilder) Configs {
configs := make(map[string]Config)
for _, builder := range builders {
name, config := builder.BuildTemplate()
configs[name] = config
}

return configs
}

func NewConfig() Config {
return make(map[string]Parameter)
}

func (c *Config) AddParameter(name string, dataType Type, required bool) {
(*c)[name] = Parameter{
DataType: dataType,
Required: required,
}
}
9 changes: 9 additions & 0 deletions internal/pkg/configtemplate/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package configtemplate

type Type string

const (
TypeString Type = "string"
TypeDurationNs Type = "duration ns"
TypeDurationUnsignedInteger Type = "unsigned integer"
)
13 changes: 13 additions & 0 deletions internal/pkg/connectors/bankingcircle/config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package bankingcircle

import "github.com/formancehq/payments/internal/pkg/configtemplate"

type Config struct {
Username string `json:"username" yaml:"username" bson:"username"`
Password string `json:"password" yaml:"password" bson:"password"`
Expand All @@ -26,3 +28,14 @@ func (c Config) Validate() error {

return nil
}

func (c Config) BuildTemplate() (string, configtemplate.Config) {
cfg := configtemplate.NewConfig()

cfg.AddParameter("username", configtemplate.TypeString, true)
cfg.AddParameter("password", configtemplate.TypeString, true)
cfg.AddParameter("endpoint", configtemplate.TypeString, true)
cfg.AddParameter("authorizationEndpoint", configtemplate.TypeString, true)

return connectorName, cfg
}
13 changes: 13 additions & 0 deletions internal/pkg/connectors/currencycloud/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package currencycloud
import (
"encoding/json"
"time"

"github.com/formancehq/payments/internal/pkg/configtemplate"
)

type Config struct {
Expand Down Expand Up @@ -67,3 +69,14 @@ func (d *Duration) UnmarshalJSON(b []byte) error {
return ErrDurationInvalid
}
}

func (c Config) BuildTemplate() (string, configtemplate.Config) {
cfg := configtemplate.NewConfig()

cfg.AddParameter("loginID", configtemplate.TypeString, true)
cfg.AddParameter("apiKey", configtemplate.TypeString, true)
cfg.AddParameter("endpoint", configtemplate.TypeString, false)
cfg.AddParameter("pollingPeriod", configtemplate.TypeDurationNs, true)

return connectorName, cfg
}
24 changes: 18 additions & 6 deletions internal/pkg/connectors/dummypay/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package dummypay
import (
"fmt"

"github.com/formancehq/payments/internal/pkg/configtemplate"

"github.com/formancehq/payments/internal/pkg/connectors"
)

Expand All @@ -19,29 +21,39 @@ type Config struct {
}

// String returns a string representation of the configuration.
func (cfg Config) String() string {
func (c Config) String() string {
return fmt.Sprintf("directory: %s, filePollingPeriod: %s, fileGenerationPeriod: %s",
cfg.Directory, cfg.FilePollingPeriod.String(), cfg.FileGenerationPeriod.String())
c.Directory, c.FilePollingPeriod.String(), c.FileGenerationPeriod.String())
}

// Validate validates the configuration.
func (cfg Config) Validate() error {
func (c Config) Validate() error {
// require directory path to be present
if cfg.Directory == "" {
if c.Directory == "" {
return ErrMissingDirectory
}

// check if file polling period is set properly
if cfg.FilePollingPeriod.Duration <= 0 {
if c.FilePollingPeriod.Duration <= 0 {
return fmt.Errorf("filePollingPeriod must be greater than 0: %w",
ErrFilePollingPeriodInvalid)
}

// check if file generation period is set properly
if cfg.FileGenerationPeriod.Duration <= 0 {
if c.FileGenerationPeriod.Duration <= 0 {
return fmt.Errorf("fileGenerationPeriod must be greater than 0: %w",
ErrFileGenerationPeriodInvalid)
}

return nil
}

func (c Config) BuildTemplate() (string, configtemplate.Config) {
cfg := configtemplate.NewConfig()

cfg.AddParameter("directory", configtemplate.TypeString, true)
cfg.AddParameter("filePollingPeriod", configtemplate.TypeDurationNs, true)
cfg.AddParameter("fileGenerationPeriod", configtemplate.TypeDurationNs, false)

return connectorName, cfg
}
12 changes: 12 additions & 0 deletions internal/pkg/connectors/modulr/config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package modulr

import "github.com/formancehq/payments/internal/pkg/configtemplate"

type Config struct {
APIKey string `json:"apiKey" bson:"apiKey"`
APISecret string `json:"apiSecret" bson:"apiSecret"`
Expand All @@ -17,3 +19,13 @@ func (c Config) Validate() error {

return nil
}

func (c Config) BuildTemplate() (string, configtemplate.Config) {
cfg := configtemplate.NewConfig()

cfg.AddParameter("apiKey", configtemplate.TypeString, true)
cfg.AddParameter("apiSecret", configtemplate.TypeString, true)
cfg.AddParameter("endpoint", configtemplate.TypeString, false)

return connectorName, cfg
}
12 changes: 12 additions & 0 deletions internal/pkg/connectors/stripe/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"errors"
"fmt"

"github.com/formancehq/payments/internal/pkg/configtemplate"

"github.com/formancehq/payments/internal/pkg/connectors"
)

Expand All @@ -28,3 +30,13 @@ func (c Config) Validate() error {
type TimelineConfig struct {
PageSize uint64 `json:"pageSize" yaml:"pageSize" bson:"pageSize"`
}

func (c Config) BuildTemplate() (string, configtemplate.Config) {
cfg := configtemplate.NewConfig()

cfg.AddParameter("apiKey", configtemplate.TypeString, true)
cfg.AddParameter("pollingPeriod", configtemplate.TypeDurationNs, false)
cfg.AddParameter("pageSize", configtemplate.TypeDurationUnsignedInteger, false)

return connectorName, cfg
}
10 changes: 10 additions & 0 deletions internal/pkg/connectors/wise/config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package wise

import "github.com/formancehq/payments/internal/pkg/configtemplate"

type Config struct {
APIKey string `json:"apiKey" yaml:"apiKey" bson:"apiKey"`
}
Expand All @@ -11,3 +13,11 @@ func (c Config) Validate() error {

return nil
}

func (c Config) BuildTemplate() (string, configtemplate.Config) {
cfg := configtemplate.NewConfig()

cfg.AddParameter("apiKey", configtemplate.TypeString, true)

return connectorName, cfg
}
16 changes: 14 additions & 2 deletions swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Payment'

/connectors:
get:
summary: Get all installed connectors
Expand All @@ -73,7 +72,20 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/ListConnectorsResponse'

/connectors/configs:
get:
summary: Get all available connectors configs
tags:
- Payments
operationId: getAllConnectorsConfigs
description: Get all available connectors configs
responses:
200:
description: List of available connectors configs
content:
application/json:
schema:
$ref: '#/components/schemas/ListConnectorsConfigsResponse'
/connectors/{connector}:
post:
summary: Install connector
Expand Down

0 comments on commit a762de0

Please sign in to comment.