Skip to content

Commit

Permalink
Merge pull request #9 from mattevans/client-opts
Browse files Browse the repository at this point in the history
feat(client): new client signature now takes opts
  • Loading branch information
mattevans committed Nov 21, 2023
2 parents 94af12d + 4948852 commit 58097da
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 48 deletions.
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ postmark-go is a [Go](http://golang.org) client library for accessing the Postma
This is an unofficial library that is not affiliated with [Postmark](http://postmarkapp.com). Official libraries are available
[here](http://developer.postmarkapp.com/developer-official-libs.html).

v1.0 Breaking Changes
---------------------
The signature of `NewClient` has changed. It now accepts options, one of which can be a custom HTTP client. Please pin to an older version if required.

Installation
-----------------

Expand All @@ -20,13 +24,14 @@ Setup
You'll need to pass an `SERVER_API_TOKEN` when initializing the client. This token can be
found under the 'Credentials' tab of your Postmark server. More info [here](http://developer.postmarkapp.com/developer-api-overview.html#authentication).

Authentication
-------------
Client + Authentication
-----------------------
```go
auth := &http.Client{
Transport: &postmark.AuthTransport{Token: "SERVER_API_TOKEN"},
}
client := postmark.NewClient(auth)
client := postmark.NewClient(
postmark.WithClient(&http.Client{
Transport: &postmark.AuthTransport{Token: "SERVER_API_TOKEN"},
}),
)
```

Example usage (with Template)
Expand Down
13 changes: 7 additions & 6 deletions examples/batch-emails/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import (
"fmt"
"net/http"

postmark "github.com/mattevans/postmark-go"
"github.com/mattevans/postmark-go"
)

func main() {
// Authenticate.
auth := &http.Client{
Transport: &postmark.AuthTransport{Token: "SERVER_API_TOKEN"},
}
client := postmark.NewClient(auth)
// Init client with round tripper adding auth fields.
client := postmark.NewClient(
postmark.WithClient(&http.Client{
Transport: &postmark.AuthTransport{Token: "SERVER_API_TOKEN"},
}),
)

// Slice of recievers
receivers := []string{
Expand Down
13 changes: 7 additions & 6 deletions examples/bounce/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import (
"fmt"
"net/http"

postmark "github.com/mattevans/postmark-go"
"github.com/mattevans/postmark-go"
)

func main() {
// Authenticate.
auth := &http.Client{
Transport: &postmark.AuthTransport{Token: "SERVER_API_TOKEN"},
}
client := postmark.NewClient(auth)
// Init client with round tripper adding auth fields.
client := postmark.NewClient(
postmark.WithClient(&http.Client{
Transport: &postmark.AuthTransport{Token: "SERVER_API_TOKEN"},
}),
)

// Get delivery stats.
stats, response, err := client.Bounce.GetDeliveryStats()
Expand Down
13 changes: 7 additions & 6 deletions examples/send-email-attachment/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import (
"net/http"
"path/filepath"

postmark "github.com/mattevans/postmark-go"
"github.com/mattevans/postmark-go"
)

func main() {
// Authenticate.
auth := &http.Client{
Transport: &postmark.AuthTransport{Token: "SERVER_API_TOKEN"},
}
client := postmark.NewClient(auth)
// Init client with round tripper adding auth fields.
client := postmark.NewClient(
postmark.WithClient(&http.Client{
Transport: &postmark.AuthTransport{Token: "SERVER_API_TOKEN"},
}),
)

// Build the email.
emailReq := &postmark.Email{
Expand Down
11 changes: 6 additions & 5 deletions examples/send-email/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (
)

func main() {
// Authenticate.
auth := &http.Client{
Transport: &postmark.AuthTransport{Token: "SERVER_API_TOKEN"},
}
client := postmark.NewClient(auth)
// Init client with round tripper adding auth fields.
client := postmark.NewClient(
postmark.WithClient(&http.Client{
Transport: &postmark.AuthTransport{Token: "SERVER_API_TOKEN"},
}),
)

// Build the email.
emailReq := &postmark.Email{
Expand Down
49 changes: 49 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package postmark

import "net/http"

// Option to set an optional client value.
type Option func(o *Options)

// Options for our client.
type Options struct {
Client *http.Client
BackendURL string
UserAgent string
}

// NewOptions returns a new Options with defaults and supplied overrides.
func NewOptions(opts ...Option) *Options {
out := Options{
Client: http.DefaultClient,
BackendURL: backendURL,
UserAgent: userAgent,
}

for _, o := range opts {
o(&out)
}

return &out
}

// WithClient allows you to set a custom http client.
func WithClient(v *http.Client) Option {
return func(o *Options) {
o.Client = v
}
}

// WithBackendURL allows you to set a custom backend URL.
func WithBackendURL(v string) Option {
return func(o *Options) {
o.BackendURL = v
}
}

// WithUserAgent allows you to set a custom user agent.
func WithUserAgent(v string) Option {
return func(o *Options) {
o.UserAgent = v
}
}
35 changes: 16 additions & 19 deletions postmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,17 @@ import (
)

const (
packageVersion = "0.1.6"
packageVersion = "1.0.0"
backendURL = "https://api.postmarkapp.com"
userAgent = "postmark-go/" + packageVersion
)

// Client holds a connection to the Postmark API.
type Client struct {
client *http.Client
Token string
ConnectionType string
UserAgent string
BackendURL *url.URL
client *http.Client
Token string
UserAgent string
BackendURL *url.URL

// Services used for communicating with the API.
Email *EmailService
Expand Down Expand Up @@ -54,22 +53,21 @@ func (r *ErrorResponse) Error() string {

// NewClient creates a new Client with the appropriate connection details and
// services used for communicating with the API.
func NewClient(httpClient *http.Client) *Client {
if httpClient == nil {
httpClient = http.DefaultClient
}

baseURL, _ := url.Parse(backendURL)

c := &Client{
client: httpClient,
BackendURL: baseURL,
UserAgent: userAgent,
}
func NewClient(opts ...Option) *Client {
var (
options = NewOptions(opts...)
baseURL, _ = url.Parse(options.BackendURL)
c = &Client{
client: options.Client,
UserAgent: options.UserAgent,
BackendURL: baseURL,
}
)

c.Email = &EmailService{client: c}
c.Bounce = &BounceService{client: c}
c.Template = &TemplateService{client: c}

return c
}

Expand All @@ -80,7 +78,6 @@ func (c *Client) NewRequest(method, urlPath string, body interface{}) (*http.Req
if err != nil {
return nil, err
}

u := c.BackendURL.ResolveReference(rel)

buf := new(bytes.Buffer)
Expand Down

0 comments on commit 58097da

Please sign in to comment.