Skip to content

Commit

Permalink
Minor cleanup and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Aug 12, 2023
1 parent ce51a81 commit 5ed9b01
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions postmark.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Package postmark ...
// Package postmark encapsulates the Postmark API via Go
package postmark

import (
Expand Down Expand Up @@ -57,15 +57,15 @@ func (client *Client) doRequest(ctx context.Context, opts parameters, dst interf
url := fmt.Sprintf("%s/%s", client.BaseURL, opts.Path)

var req *http.Request
req, err = http.NewRequestWithContext(ctx, opts.Method, url, nil)
if err != nil {
if req, err = http.NewRequestWithContext(
ctx, opts.Method, url, nil,
); err != nil {
return
}

if opts.Payload != nil {
var payloadData []byte
payloadData, err = json.Marshal(opts.Payload)
if err != nil {
if payloadData, err = json.Marshal(opts.Payload); err != nil {
return
}
req.Body = io.NopCloser(bytes.NewBuffer(payloadData))
Expand All @@ -77,38 +77,33 @@ func (client *Client) doRequest(ctx context.Context, opts parameters, dst interf
switch opts.TokenType {
case accountToken:
req.Header.Add("X-Postmark-Account-Token", client.AccountToken)

default:
req.Header.Add("X-Postmark-Server-Token", client.ServerToken)
}

var res *http.Response
res, err = client.HTTPClient.Do(req)
if err != nil {
if res, err = client.HTTPClient.Do(req); err != nil {
return
}

defer func() {
_ = res.Body.Close()
}()
var body []byte
body, err = io.ReadAll(res.Body)
if err != nil {
if body, err = io.ReadAll(res.Body); err != nil {
return
}

if res.StatusCode >= 400 {
if res.StatusCode >= http.StatusBadRequest {
// If the status code is not a success, attempt to unmarshall the body into the APIError struct.
var apiErr APIError
err = json.Unmarshal(body, &apiErr)
if err != nil {
if err = json.Unmarshal(body, &apiErr); err != nil {
return
}
return apiErr
}

err = json.Unmarshal(body, dst)
return
return json.Unmarshal(body, dst)
}

// APIError represents errors returned by Postmark
Expand Down

0 comments on commit 5ed9b01

Please sign in to comment.