Skip to content

Commit 58097da

Browse files
authored
Merge pull request #9 from mattevans/client-opts
feat(client): new client signature now takes opts
2 parents 94af12d + 4948852 commit 58097da

File tree

7 files changed

+103
-48
lines changed

7 files changed

+103
-48
lines changed

README.md

+11-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ postmark-go is a [Go](http://golang.org) client library for accessing the Postma
99
This is an unofficial library that is not affiliated with [Postmark](http://postmarkapp.com). Official libraries are available
1010
[here](http://developer.postmarkapp.com/developer-official-libs.html).
1111

12+
v1.0 Breaking Changes
13+
---------------------
14+
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.
15+
1216
Installation
1317
-----------------
1418

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

23-
Authentication
24-
-------------
27+
Client + Authentication
28+
-----------------------
2529
```go
26-
auth := &http.Client{
27-
Transport: &postmark.AuthTransport{Token: "SERVER_API_TOKEN"},
28-
}
29-
client := postmark.NewClient(auth)
30+
client := postmark.NewClient(
31+
postmark.WithClient(&http.Client{
32+
Transport: &postmark.AuthTransport{Token: "SERVER_API_TOKEN"},
33+
}),
34+
)
3035
```
3136

3237
Example usage (with Template)

examples/batch-emails/main.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ import (
44
"fmt"
55
"net/http"
66

7-
postmark "github.com/mattevans/postmark-go"
7+
"github.com/mattevans/postmark-go"
88
)
99

1010
func main() {
11-
// Authenticate.
12-
auth := &http.Client{
13-
Transport: &postmark.AuthTransport{Token: "SERVER_API_TOKEN"},
14-
}
15-
client := postmark.NewClient(auth)
11+
// Init client with round tripper adding auth fields.
12+
client := postmark.NewClient(
13+
postmark.WithClient(&http.Client{
14+
Transport: &postmark.AuthTransport{Token: "SERVER_API_TOKEN"},
15+
}),
16+
)
1617

1718
// Slice of recievers
1819
receivers := []string{

examples/bounce/main.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ import (
44
"fmt"
55
"net/http"
66

7-
postmark "github.com/mattevans/postmark-go"
7+
"github.com/mattevans/postmark-go"
88
)
99

1010
func main() {
11-
// Authenticate.
12-
auth := &http.Client{
13-
Transport: &postmark.AuthTransport{Token: "SERVER_API_TOKEN"},
14-
}
15-
client := postmark.NewClient(auth)
11+
// Init client with round tripper adding auth fields.
12+
client := postmark.NewClient(
13+
postmark.WithClient(&http.Client{
14+
Transport: &postmark.AuthTransport{Token: "SERVER_API_TOKEN"},
15+
}),
16+
)
1617

1718
// Get delivery stats.
1819
stats, response, err := client.Bounce.GetDeliveryStats()

examples/send-email-attachment/main.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ import (
66
"net/http"
77
"path/filepath"
88

9-
postmark "github.com/mattevans/postmark-go"
9+
"github.com/mattevans/postmark-go"
1010
)
1111

1212
func main() {
13-
// Authenticate.
14-
auth := &http.Client{
15-
Transport: &postmark.AuthTransport{Token: "SERVER_API_TOKEN"},
16-
}
17-
client := postmark.NewClient(auth)
13+
// Init client with round tripper adding auth fields.
14+
client := postmark.NewClient(
15+
postmark.WithClient(&http.Client{
16+
Transport: &postmark.AuthTransport{Token: "SERVER_API_TOKEN"},
17+
}),
18+
)
1819

1920
// Build the email.
2021
emailReq := &postmark.Email{

examples/send-email/main.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ import (
88
)
99

1010
func main() {
11-
// Authenticate.
12-
auth := &http.Client{
13-
Transport: &postmark.AuthTransport{Token: "SERVER_API_TOKEN"},
14-
}
15-
client := postmark.NewClient(auth)
11+
// Init client with round tripper adding auth fields.
12+
client := postmark.NewClient(
13+
postmark.WithClient(&http.Client{
14+
Transport: &postmark.AuthTransport{Token: "SERVER_API_TOKEN"},
15+
}),
16+
)
1617

1718
// Build the email.
1819
emailReq := &postmark.Email{

options.go

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package postmark
2+
3+
import "net/http"
4+
5+
// Option to set an optional client value.
6+
type Option func(o *Options)
7+
8+
// Options for our client.
9+
type Options struct {
10+
Client *http.Client
11+
BackendURL string
12+
UserAgent string
13+
}
14+
15+
// NewOptions returns a new Options with defaults and supplied overrides.
16+
func NewOptions(opts ...Option) *Options {
17+
out := Options{
18+
Client: http.DefaultClient,
19+
BackendURL: backendURL,
20+
UserAgent: userAgent,
21+
}
22+
23+
for _, o := range opts {
24+
o(&out)
25+
}
26+
27+
return &out
28+
}
29+
30+
// WithClient allows you to set a custom http client.
31+
func WithClient(v *http.Client) Option {
32+
return func(o *Options) {
33+
o.Client = v
34+
}
35+
}
36+
37+
// WithBackendURL allows you to set a custom backend URL.
38+
func WithBackendURL(v string) Option {
39+
return func(o *Options) {
40+
o.BackendURL = v
41+
}
42+
}
43+
44+
// WithUserAgent allows you to set a custom user agent.
45+
func WithUserAgent(v string) Option {
46+
return func(o *Options) {
47+
o.UserAgent = v
48+
}
49+
}

postmark.go

+16-19
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,17 @@ import (
1010
)
1111

1212
const (
13-
packageVersion = "0.1.6"
13+
packageVersion = "1.0.0"
1414
backendURL = "https://api.postmarkapp.com"
1515
userAgent = "postmark-go/" + packageVersion
1616
)
1717

1818
// Client holds a connection to the Postmark API.
1919
type Client struct {
20-
client *http.Client
21-
Token string
22-
ConnectionType string
23-
UserAgent string
24-
BackendURL *url.URL
20+
client *http.Client
21+
Token string
22+
UserAgent string
23+
BackendURL *url.URL
2524

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

5554
// NewClient creates a new Client with the appropriate connection details and
5655
// services used for communicating with the API.
57-
func NewClient(httpClient *http.Client) *Client {
58-
if httpClient == nil {
59-
httpClient = http.DefaultClient
60-
}
61-
62-
baseURL, _ := url.Parse(backendURL)
63-
64-
c := &Client{
65-
client: httpClient,
66-
BackendURL: baseURL,
67-
UserAgent: userAgent,
68-
}
56+
func NewClient(opts ...Option) *Client {
57+
var (
58+
options = NewOptions(opts...)
59+
baseURL, _ = url.Parse(options.BackendURL)
60+
c = &Client{
61+
client: options.Client,
62+
UserAgent: options.UserAgent,
63+
BackendURL: baseURL,
64+
}
65+
)
6966

7067
c.Email = &EmailService{client: c}
7168
c.Bounce = &BounceService{client: c}
7269
c.Template = &TemplateService{client: c}
70+
7371
return c
7472
}
7573

@@ -80,7 +78,6 @@ func (c *Client) NewRequest(method, urlPath string, body interface{}) (*http.Req
8078
if err != nil {
8179
return nil, err
8280
}
83-
8481
u := c.BackendURL.ResolveReference(rel)
8582

8683
buf := new(bytes.Buffer)

0 commit comments

Comments
 (0)