Skip to content

Commit

Permalink
feat: Add GET /connectors route to list all installed connectors (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine Gelloz authored Oct 25, 2022
1 parent b969979 commit cddea5e
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 2 deletions.
17 changes: 17 additions & 0 deletions internal/app/api/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,23 @@ func readTask[Config payments.ConnectorConfigObject,
}
}

func findAll[Config payments.ConnectorConfigObject,
Descriptor payments.TaskDescriptor](connectorManager *integration.ConnectorManager[Config, Descriptor],
) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
res, err := connectorManager.FindAll(context.Background())
if err != nil {
handleError(w, r, err)

return
}

if err = json.NewEncoder(w).Encode(res); err != nil {
panic(err)
}
}
}

func uninstall[Config payments.ConnectorConfigObject,
Descriptor payments.TaskDescriptor](connectorManager *integration.ConnectorManager[Config, Descriptor],
) http.HandlerFunc {
Expand Down
2 changes: 2 additions & 0 deletions internal/app/api/connectormodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ func connectorRouter[Config payments.ConnectorConfigObject, Descriptor payments.
) *mux.Router {
r := mux.NewRouter()

r.Path("/").Methods(http.MethodGet).Handler(findAll(manager))

r.Path("/" + name).Methods(http.MethodPost).Handler(install(manager))

r.Path("/" + name + "/reset").Methods(http.MethodPost).Handler(reset(manager))
Expand Down
4 changes: 4 additions & 0 deletions internal/pkg/integration/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ func (l *ConnectorManager[ConnectorConfig, TaskDescriptor]) IsEnabled(ctx contex
return l.store.IsEnabled(ctx, l.loader.Name())
}

func (l *ConnectorManager[ConnectorConfig, TaskDescriptor]) FindAll(ctx context.Context) ([]payments.ConnectorBaseInfo, error) {
return l.store.FindAll(ctx)
}

func (l *ConnectorManager[ConnectorConfig, TaskDescriptor]) IsInstalled(ctx context.Context) (bool, error) {
return l.store.IsInstalled(ctx, l.loader.Name())
}
Expand Down
19 changes: 19 additions & 0 deletions internal/pkg/integration/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
)

type ConnectorStore interface {
FindAll(ctx context.Context) ([]payments.ConnectorBaseInfo, error)
IsInstalled(ctx context.Context, name string) (bool, error)
Install(ctx context.Context, name string, config any) error
Uninstall(ctx context.Context, name string) error
Expand All @@ -37,6 +38,10 @@ func (i *InMemoryConnectorStore) Uninstall(ctx context.Context, name string) err
return nil
}

func (i *InMemoryConnectorStore) FindAll(_ context.Context) ([]payments.ConnectorBaseInfo, error) {
return []payments.ConnectorBaseInfo{}, nil
}

func (i *InMemoryConnectorStore) IsInstalled(ctx context.Context, name string) (bool, error) {
return i.installed[name], nil
}
Expand Down Expand Up @@ -132,6 +137,20 @@ func (m *MongodbConnectorStore) Uninstall(ctx context.Context, name string) erro
})
}

func (m *MongodbConnectorStore) FindAll(ctx context.Context) ([]payments.ConnectorBaseInfo, error) {
cursor, err := m.db.Collection(payments.ConnectorsCollection).Find(ctx, map[string]any{})
if err != nil {
return []payments.ConnectorBaseInfo{}, errors.Wrap(err, "find all connectors")
}

var res []payments.ConnectorBaseInfo
if err = cursor.All(context.TODO(), &res); err != nil {
return []payments.ConnectorBaseInfo{}, errors.Wrap(err, "decoding all connectors")
}

return res, err
}

func (m *MongodbConnectorStore) IsInstalled(ctx context.Context, name string) (bool, error) {
ret := m.db.Collection(payments.ConnectorsCollection).FindOne(ctx, map[string]any{
"provider": name,
Expand Down
8 changes: 6 additions & 2 deletions internal/pkg/payments/connector.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package payments

type Connector[T ConnectorConfigObject] struct {
type ConnectorBaseInfo struct {
Provider string `json:"provider" bson:"provider"`
Disabled bool `json:"disabled" bson:"disabled"`
Config T `json:"config" bson:"config"`
}

type Connector[T ConnectorConfigObject] struct {
ConnectorBaseInfo
Config T `json:"config" bson:"config"`
}
34 changes: 34 additions & 0 deletions swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Payment'

'/connectors':
get:
summary: Get all installed connectors
operationId: getAllConnectors
description: Get all installed connectors
responses:
200:
description: List of installed connectors
content:
application/json:
schema:
$ref: '#/components/schemas/ListConnectorsResponse'

'/connectors/{connector}':
post:
summary: Install connector
Expand Down Expand Up @@ -348,3 +362,23 @@ components:
format: date-time
raw:
nullable: true

ListConnectorsResponse:
type: object
required:
- data
properties:
data:
type: array
items:
$ref: '#/components/schemas/ConnectorBaseInfo'

ConnectorBaseInfo:
type: object
properties:
provider:
type: string
example: stripe
disabled:
type: boolean
example: false

0 comments on commit cddea5e

Please sign in to comment.