Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
New with method for paymail server options
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Apr 5, 2022
1 parent 1ae6e70 commit 4a6fa73
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 38 deletions.
7 changes: 4 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ type (

// PaymailServerOptions is the options for the Paymail server
PaymailServerOptions struct {
*server.Configuration // Server configuration if Paymail is enabled
DefaultFromPaymail string // IE: from@domain.com
DefaultNote string // IE: some note for address resolution
*server.Configuration // Server configuration if Paymail is enabled
options []server.ConfigOps // Options for the paymail server
DefaultFromPaymail string // IE: from@domain.com
DefaultNote string // IE: some note for address resolution
}

// taskManagerOptions holds the configuration for taskmanager
Expand Down
25 changes: 21 additions & 4 deletions client_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,29 @@ func (c *Client) registerAllTasks() error {

// loadDefaultPaymailConfig will load the default paymail server configuration
func (c *Client) loadDefaultPaymailConfig() (err error) {
c.options.paymail.serverConfig.DefaultFromPaymail = defaultSenderPaymail
c.options.paymail.serverConfig.DefaultNote = defaultAddressResolutionPurpose

// Default FROM paymail
if len(c.options.paymail.serverConfig.DefaultFromPaymail) == 0 {
c.options.paymail.serverConfig.DefaultFromPaymail = defaultSenderPaymail
}

// Default note for address resolution
if len(c.options.paymail.serverConfig.DefaultNote) == 0 {
c.options.paymail.serverConfig.DefaultNote = defaultAddressResolutionPurpose
}

// Set default options if none are found
if len(c.options.paymail.serverConfig.options) == 0 {
c.options.paymail.serverConfig.options = append(c.options.paymail.serverConfig.options,
server.WithGenericCapabilities(),
server.WithDomainValidationDisabled(),
)
}

// Create the paymail configuration using the client and default service provider
c.options.paymail.serverConfig.Configuration, err = server.NewConfig(
&PaymailServiceProvider{client: c},
server.WithGenericCapabilities(),
server.WithDomainValidationDisabled(),
c.options.paymail.serverConfig.options...,
)
return
}
38 changes: 37 additions & 1 deletion client_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func defaultClientOptions() *clientOptions {
client: nil,
serverConfig: &PaymailServerOptions{
Configuration: nil,
options: []server.ConfigOps{},
},
},

Expand Down Expand Up @@ -424,7 +425,42 @@ func WithPaymailClient(client paymail.ClientInterface) ClientOps {
}

// WithPaymailServer will set the server configuration for Paymail
func WithPaymailServer(config *server.Configuration, defaultFromPaymail, defaultNote string) ClientOps {
func WithPaymailServer(domains []string, defaultFromPaymail, defaultNote string,
domainValidation, senderValidation bool) ClientOps {
return func(c *clientOptions) {

// Add generic capabilities
c.paymail.serverConfig.options = append(c.paymail.serverConfig.options, server.WithGenericCapabilities())

// Add each domain
for _, domain := range domains {
c.paymail.serverConfig.options = append(c.paymail.serverConfig.options, server.WithDomain(domain))
}

// Set the sender validation
if senderValidation {
c.paymail.serverConfig.options = append(c.paymail.serverConfig.options, server.WithSenderValidation())
}

// Domain validation
if !domainValidation {
c.paymail.serverConfig.options = append(c.paymail.serverConfig.options, server.WithDomainValidationDisabled())
}

// Add default values
if len(defaultFromPaymail) > 0 {
c.paymail.serverConfig.DefaultFromPaymail = defaultFromPaymail
}
if len(defaultNote) > 0 {
c.paymail.serverConfig.DefaultNote = defaultNote
}
}
}

// WithPaymailServerConfig will set the custom server configuration for Paymail
//
// This will allow overriding the Configuration.actions (paymail service provider)
func WithPaymailServerConfig(config *server.Configuration, defaultFromPaymail, defaultNote string) ClientOps {
return func(c *clientOptions) {
if config != nil {
c.paymail.serverConfig.Configuration = config
Expand Down
48 changes: 24 additions & 24 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,12 @@ func TestClient_ModifyPaymailConfig(t *testing.T) {

t.Run("update from sender and note", func(t *testing.T) {
opts := DefaultClientOpts(false, true)
opts = append(opts, WithPaymailServer(&server.Configuration{
APIVersion: "v1",
BSVAliasVersion: paymail.DefaultBsvAliasVersion,
Port: paymail.DefaultPort,
ServiceName: paymail.DefaultServiceName,
}, defaultSenderPaymail, defaultAddressResolutionPurpose))
opts = append(opts, WithPaymailServer(
[]string{testDomain},
defaultSenderPaymail,
defaultAddressResolutionPurpose,
false, false,
))

tc, err := NewClient(context.Background(), opts...)
require.NoError(t, err)
Expand All @@ -208,12 +208,12 @@ func TestClient_ModifyPaymailConfig(t *testing.T) {

t.Run("update server config", func(t *testing.T) {
opts := DefaultClientOpts(false, true)
opts = append(opts, WithPaymailServer(&server.Configuration{
APIVersion: "v1",
BSVAliasVersion: paymail.DefaultBsvAliasVersion,
Port: paymail.DefaultPort,
ServiceName: paymail.DefaultServiceName,
}, defaultSenderPaymail, defaultAddressResolutionPurpose))
opts = append(opts, WithPaymailServer(
[]string{testDomain},
defaultSenderPaymail,
defaultAddressResolutionPurpose,
false, false,
))

tc, err := NewClient(context.Background(), opts...)
require.NoError(t, err)
Expand Down Expand Up @@ -252,12 +252,12 @@ func TestClient_PaymailServerConfig(t *testing.T) {

t.Run("valid paymail server config", func(t *testing.T) {
opts := DefaultClientOpts(false, true)
opts = append(opts, WithPaymailServer(&server.Configuration{
APIVersion: "v1",
BSVAliasVersion: paymail.DefaultBsvAliasVersion,
Port: paymail.DefaultPort,
ServiceName: paymail.DefaultServiceName,
}, defaultSenderPaymail, defaultAddressResolutionPurpose))
opts = append(opts, WithPaymailServer(
[]string{testDomain},
defaultSenderPaymail,
defaultAddressResolutionPurpose,
false, false,
))

tc, err := NewClient(context.Background(), opts...)
require.NoError(t, err)
Expand Down Expand Up @@ -345,12 +345,12 @@ func TestPaymailOptions_ServerConfig(t *testing.T) {

t.Run("valid server config", func(t *testing.T) {
opts := DefaultClientOpts(false, true)
opts = append(opts, WithPaymailServer(&server.Configuration{
APIVersion: "v1",
BSVAliasVersion: paymail.DefaultBsvAliasVersion,
Port: paymail.DefaultPort,
ServiceName: paymail.DefaultServiceName,
}, defaultSenderPaymail, defaultAddressResolutionPurpose))
opts = append(opts, WithPaymailServer(
[]string{testDomain},
defaultSenderPaymail,
defaultAddressResolutionPurpose,
false, false,
))

tc, err := NewClient(context.Background(), opts...)
require.NoError(t, err)
Expand Down
17 changes: 11 additions & 6 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,28 @@ type PaymailService interface {
metadata Metadata, opts ...ModelOps) (*PaymailAddress, error)
}

// ClientServices is the client related services
type ClientServices interface {
Cachestore() cachestore.ClientInterface
Chainstate() chainstate.ClientInterface
Datastore() datastore.ClientInterface
Logger() logger.Interface
PaymailClient() paymail.ClientInterface
Taskmanager() taskmanager.ClientInterface
}

// ClientInterface is the client (bux engine) interface comprised of all services
type ClientInterface interface {
AccessKeyService
ClientServices
DestinationService
PaymailService
TransactionService
UTXOService
XPubService
AddModels(ctx context.Context, autoMigrate bool, models ...interface{}) error
AuthenticateRequest(ctx context.Context, req *http.Request, adminXPubs []string, adminRequired, requireSigning, signingDisabled bool) (*http.Request, error)
Cachestore() cachestore.ClientInterface
Chainstate() chainstate.ClientInterface
Close(ctx context.Context) error
Datastore() datastore.ClientInterface
Debug(on bool)
DefaultModelOptions(opts ...ModelOps) []ModelOps
EnableNewRelic()
Expand All @@ -96,12 +104,9 @@ type ClientInterface interface {
IsITCEnabled() bool
IsIUCEnabled() bool
IsNewRelicEnabled() bool
Logger() logger.Interface
ModifyPaymailConfig(config *server.Configuration, defaultFromPaymail, defaultNote string)
ModifyTaskPeriod(name string, period time.Duration) error
PaymailClient() paymail.ClientInterface
PaymailServerConfig() *PaymailServerOptions
Taskmanager() taskmanager.ClientInterface
UserAgent() string
Version() string
}

0 comments on commit 4a6fa73

Please sign in to comment.