-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Lawrence Zawila <[email protected]>
- Loading branch information
1 parent
b7edb34
commit d0075d8
Showing
10 changed files
with
200 additions
and
141 deletions.
There are no files selected for viewing
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
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,13 @@ | ||
package wise | ||
|
||
type Config struct { | ||
APIKey string `json:"apiKey" yaml:"apiKey" bson:"apiKey"` | ||
} | ||
|
||
func (c Config) Validate() error { | ||
if c.APIKey == "" { | ||
return ErrMissingAPIKey | ||
} | ||
|
||
return nil | ||
} |
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,11 @@ | ||
package wise | ||
|
||
import "github.com/pkg/errors" | ||
|
||
var ( | ||
// ErrMissingTask is returned when the task is missing. | ||
ErrMissingTask = errors.New("task is not implemented") | ||
|
||
// ErrMissingAPIKey is returned when the api key is missing from config. | ||
ErrMissingAPIKey = errors.New("missing apiKey from config") | ||
) |
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,25 @@ | ||
package wise | ||
|
||
import ( | ||
"github.com/numary/go-libs/sharedlogging" | ||
"github.com/numary/payments/pkg/bridge/integration" | ||
"github.com/numary/payments/pkg/bridge/task" | ||
) | ||
|
||
// NewLoader creates a new loader. | ||
func NewLoader() integration.Loader[Config, TaskDefinition] { | ||
loader := integration.NewLoaderBuilder[Config, TaskDefinition]("wise"). | ||
WithLoad(func(logger sharedlogging.Logger, config Config) integration.Connector[TaskDefinition] { | ||
return integration.NewConnectorBuilder[TaskDefinition](). | ||
WithInstall(func(ctx task.ConnectorContext[TaskDefinition]) error { | ||
return ctx.Scheduler(). | ||
Schedule( | ||
TaskDefinition{Name: taskNameFetchProfiles}, | ||
false) | ||
}). | ||
WithResolve(resolveTasks(logger, config)). | ||
Build() | ||
}).Build() | ||
|
||
return loader | ||
} |
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,39 @@ | ||
package wise | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/numary/go-libs/sharedlogging" | ||
"github.com/numary/payments/pkg/bridge/task" | ||
) | ||
|
||
func taskFetchProfiles(logger sharedlogging.Logger, config Config) task.Task { | ||
return func( | ||
ctx context.Context, | ||
scheduler task.Scheduler[TaskDefinition], | ||
) error { | ||
client := newClient(config.APIKey) | ||
|
||
profiles, err := client.getProfiles() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, profile := range profiles { | ||
logger.Infof(fmt.Sprintf("scheduling fetch-transfers: %d", profile.ID)) | ||
|
||
def := TaskDefinition{ | ||
Name: taskNameFetchTransfers, | ||
ProfileID: profile.ID, | ||
} | ||
|
||
err = scheduler.Schedule(def, false) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
} |
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,50 @@ | ||
package wise | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/numary/go-libs/sharedlogging" | ||
payments "github.com/numary/payments/pkg" | ||
"github.com/numary/payments/pkg/bridge/ingestion" | ||
"github.com/numary/payments/pkg/bridge/task" | ||
) | ||
|
||
func taskFetchTransfers(logger sharedlogging.Logger, config Config, profileID uint64) task.Task { | ||
return func( | ||
ctx context.Context, | ||
scheduler task.Scheduler[TaskDefinition], | ||
ingester ingestion.Ingester, | ||
) error { | ||
client := newClient(config.APIKey) | ||
|
||
transfers, err := client.getTransfers(&profile{ | ||
ID: profileID, | ||
}) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
batch := ingestion.Batch{} | ||
|
||
for _, transfer := range transfers { | ||
logger.Info(transfer) | ||
batch = append(batch, ingestion.BatchElement{ | ||
Referenced: payments.Referenced{ | ||
Reference: fmt.Sprintf("%d", transfer.ID), | ||
Type: "transfer", | ||
}, | ||
Payment: &payments.Data{ | ||
Status: payments.StatusSucceeded, | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
Scheme: payments.SchemeOther, | ||
InitialAmount: int64(transfer.TargetValue * 100), | ||
Asset: fmt.Sprintf("%s/2", transfer.TargetCurrency), | ||
Raw: transfer, | ||
}, | ||
}) | ||
} | ||
|
||
return ingester.Ingest(ctx, batch, struct{}{}) | ||
} | ||
} |
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,35 @@ | ||
package wise | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/numary/go-libs/sharedlogging" | ||
"github.com/numary/payments/pkg/bridge/task" | ||
) | ||
|
||
const ( | ||
taskNameFetchTransfers = "fetch-transfers" | ||
taskNameFetchProfiles = "fetch-profiles" | ||
) | ||
|
||
// TaskDefinition is the definition of a task. | ||
type TaskDefinition struct { | ||
Name string `json:"name" yaml:"name" bson:"name"` | ||
ProfileID uint64 `json:"profileID" yaml:"profileID" bson:"profileID"` | ||
} | ||
|
||
func resolveTasks(logger sharedlogging.Logger, config Config) func(taskDefinition TaskDefinition) task.Task { | ||
return func(taskDefinition TaskDefinition) task.Task { | ||
switch taskDefinition.Name { | ||
case taskNameFetchProfiles: | ||
return taskFetchProfiles(logger, config) | ||
case taskNameFetchTransfers: | ||
return taskFetchTransfers(logger, config, taskDefinition.ProfileID) | ||
} | ||
|
||
// This should never happen. | ||
return func() error { | ||
return fmt.Errorf("key '%s': %w", taskDefinition.Name, ErrMissingTask) | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Everything LGTM, aside from this, we may have some opportunities to map it to other, failure related statuses. Wdyt?