From a762de029c9c482d67b1e6775c72abcdf422560e Mon Sep 17 00:00:00 2001 From: darkmatterpool <113581282+darkmatterpool@users.noreply.github.com> Date: Mon, 28 Nov 2022 13:13:15 +0200 Subject: [PATCH] feat: list available connectors configs (#62) Signed-off-by: Lawrence Zawila <113581282+darkmatterpool@users.noreply.github.com> --- internal/app/api/connectorconfigs.go | 40 +++++++++++++++++++ internal/app/api/module.go | 2 + internal/pkg/configtemplate/template.go | 35 ++++++++++++++++ internal/pkg/configtemplate/types.go | 9 +++++ .../pkg/connectors/bankingcircle/config.go | 13 ++++++ .../pkg/connectors/currencycloud/config.go | 13 ++++++ internal/pkg/connectors/dummypay/config.go | 24 ++++++++--- internal/pkg/connectors/modulr/config.go | 12 ++++++ internal/pkg/connectors/stripe/config.go | 12 ++++++ internal/pkg/connectors/wise/config.go | 10 +++++ swagger.yml | 16 +++++++- 11 files changed, 178 insertions(+), 8 deletions(-) create mode 100644 internal/app/api/connectorconfigs.go create mode 100644 internal/pkg/configtemplate/template.go create mode 100644 internal/pkg/configtemplate/types.go diff --git a/internal/app/api/connectorconfigs.go b/internal/app/api/connectorconfigs.go new file mode 100644 index 00000000..59f63c96 --- /dev/null +++ b/internal/app/api/connectorconfigs.go @@ -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 + } + } +} diff --git a/internal/app/api/module.go b/internal/app/api/module.go index d0b266aa..0891793b 100644 --- a/internal/app/api/module.go +++ b/internal/app/api/module.go @@ -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), diff --git a/internal/pkg/configtemplate/template.go b/internal/pkg/configtemplate/template.go new file mode 100644 index 00000000..2158be9d --- /dev/null +++ b/internal/pkg/configtemplate/template.go @@ -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, + } +} diff --git a/internal/pkg/configtemplate/types.go b/internal/pkg/configtemplate/types.go new file mode 100644 index 00000000..a44fed1b --- /dev/null +++ b/internal/pkg/configtemplate/types.go @@ -0,0 +1,9 @@ +package configtemplate + +type Type string + +const ( + TypeString Type = "string" + TypeDurationNs Type = "duration ns" + TypeDurationUnsignedInteger Type = "unsigned integer" +) diff --git a/internal/pkg/connectors/bankingcircle/config.go b/internal/pkg/connectors/bankingcircle/config.go index 06a23457..ccbb551c 100644 --- a/internal/pkg/connectors/bankingcircle/config.go +++ b/internal/pkg/connectors/bankingcircle/config.go @@ -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"` @@ -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 +} diff --git a/internal/pkg/connectors/currencycloud/config.go b/internal/pkg/connectors/currencycloud/config.go index 759fe971..15b056b7 100644 --- a/internal/pkg/connectors/currencycloud/config.go +++ b/internal/pkg/connectors/currencycloud/config.go @@ -3,6 +3,8 @@ package currencycloud import ( "encoding/json" "time" + + "github.com/formancehq/payments/internal/pkg/configtemplate" ) type Config struct { @@ -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 +} diff --git a/internal/pkg/connectors/dummypay/config.go b/internal/pkg/connectors/dummypay/config.go index 9a4394c2..316b59f0 100644 --- a/internal/pkg/connectors/dummypay/config.go +++ b/internal/pkg/connectors/dummypay/config.go @@ -3,6 +3,8 @@ package dummypay import ( "fmt" + "github.com/formancehq/payments/internal/pkg/configtemplate" + "github.com/formancehq/payments/internal/pkg/connectors" ) @@ -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 +} diff --git a/internal/pkg/connectors/modulr/config.go b/internal/pkg/connectors/modulr/config.go index 9be5ccae..9e111366 100644 --- a/internal/pkg/connectors/modulr/config.go +++ b/internal/pkg/connectors/modulr/config.go @@ -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"` @@ -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 +} diff --git a/internal/pkg/connectors/stripe/config.go b/internal/pkg/connectors/stripe/config.go index 055bc06d..c707858f 100644 --- a/internal/pkg/connectors/stripe/config.go +++ b/internal/pkg/connectors/stripe/config.go @@ -4,6 +4,8 @@ import ( "errors" "fmt" + "github.com/formancehq/payments/internal/pkg/configtemplate" + "github.com/formancehq/payments/internal/pkg/connectors" ) @@ -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 +} diff --git a/internal/pkg/connectors/wise/config.go b/internal/pkg/connectors/wise/config.go index 731fa583..c003666e 100644 --- a/internal/pkg/connectors/wise/config.go +++ b/internal/pkg/connectors/wise/config.go @@ -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"` } @@ -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 +} diff --git a/swagger.yml b/swagger.yml index bc6221ae..6588fad4 100644 --- a/swagger.yml +++ b/swagger.yml @@ -58,7 +58,6 @@ paths: application/json: schema: $ref: '#/components/schemas/Payment' - /connectors: get: summary: Get all installed connectors @@ -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