Skip to content

Commit

Permalink
Optionize push provider factory with way to customize HTTP client
Browse files Browse the repository at this point in the history
  • Loading branch information
jessepeterson committed Aug 11, 2021
1 parent 7a3c035 commit 65fb96a
Showing 1 changed file with 45 additions and 5 deletions.
50 changes: 45 additions & 5 deletions push/buford/buford.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,69 @@ package buford
import (
"crypto/tls"
"errors"
"net/http"
"time"

bufordpush "github.com/RobotsAndPencils/buford/push"
"github.com/micromdm/nanomdm/mdm"
"github.com/micromdm/nanomdm/push"
)

// NewClient describes a callback for setting up an HTTP client for Push notifications.
type NewClient func(*tls.Certificate) (*http.Client, error)

// bufordFactory instantiates new buford Services to satisfy the PushProviderFactory interface.
type bufordFactory struct {
workers uint
expiration time.Time
workers uint
expiration time.Time
newClientCallback NewClient
}

type Option func(*bufordFactory)

// WithWorkers sets how many worker goroutines to use when sending
// multiple push notifications.
func WithWorkers(workers uint) Option {
return func(f *bufordFactory) {
f.workers = workers
}
}

// WithExpiration sets the APNs expiration time for the push notifications.
func WithExpiration(expiration time.Time) Option {
return func(f *bufordFactory) {
f.expiration = expiration
}
}

// WithNewClient sets a callback to setup an HTTP client for each
// new Push provider.
func WithNewClient(newClientCallback NewClient) Option {
return func(f *bufordFactory) {
f.newClientCallback = newClientCallback
}
}

// NewPushProviderFactory creates a new instance that can spawn buford Services
func NewPushProviderFactory() *bufordFactory {
return &bufordFactory{
func NewPushProviderFactory(opts ...Option) *bufordFactory {
factory := &bufordFactory{
workers: 5,
}
for _, opt := range opts {
opt(factory)
}
return factory
}

// NewPushProvider generates a new PushProvider given a tls keypair
func (f *bufordFactory) NewPushProvider(cert *tls.Certificate) (push.PushProvider, error) {
client, err := bufordpush.NewClient(*cert)
var client *http.Client
var err error
if f.newClientCallback == nil {
client, err = bufordpush.NewClient(*cert)
} else {
client, err = f.newClientCallback(cert)
}
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 65fb96a

Please sign in to comment.