Skip to content

Commit

Permalink
Merge branch 'master' into ISSUE-78
Browse files Browse the repository at this point in the history
  • Loading branch information
yargevad committed Nov 15, 2016
2 parents 1166eb1 + 9d41de4 commit 2c0d038
Show file tree
Hide file tree
Showing 8 changed files with 264 additions and 44 deletions.
8 changes: 4 additions & 4 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Config struct {
Verbose bool
}

// Client contains connection and authentication information.
// Client contains connection, configuration, and authentication information.
// Specifying your own http.Client gives you lots of control over how connections are made.
type Client struct {
Config *Config
Expand Down Expand Up @@ -61,8 +61,8 @@ type Response struct {
HTTP *http.Response
Body []byte
Verbose map[string]string
Results map[string]interface{} `json:"results,omitempty"`
Errors []Error `json:"errors,omitempty"`
Results interface{} `json:"results,omitempty"`
Errors []Error `json:"errors,omitempty"`
}

// Error mirrors the error format returned by SparkPost APIs.
Expand Down Expand Up @@ -120,7 +120,7 @@ func (api *Client) Init(cfg *Config) error {
}

// SetHeader adds additional HTTP headers for every API request made from client.
// Usefull to set subaccount X-MSYS-SUBACCOUNT header and etc.
// Useful to set subaccount X-MSYS-SUBACCOUNT header and etc.
func (c *Client) SetHeader(header string, value string) {
c.headers[header] = value
}
Expand Down
8 changes: 6 additions & 2 deletions recipient_lists.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,13 @@ func (c *Client) RecipientListCreate(rl *RecipientList) (id string, res *Respons

if res.HTTP.StatusCode == 200 {
var ok bool
id, ok = res.Results["id"].(string)
var results map[string]interface{}
if results, ok = res.Results.(map[string]interface{}); !ok {
return id, res, fmt.Errorf("Unexpected response to Recipient List creation (results)")
}
id, ok = results["id"].(string)
if !ok {
err = fmt.Errorf("Unexpected response to Recipient List creation")
return id, res, fmt.Errorf("Unexpected response to Recipient List creation (id)")
}

} else if len(res.Errors) > 0 {
Expand Down
8 changes: 6 additions & 2 deletions subaccounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,16 @@ func (c *Client) SubaccountCreate(s *Subaccount) (res *Response, err error) {

if res.HTTP.StatusCode == 200 {
var ok bool
f, ok := res.Results["subaccount_id"].(float64)
var results map[string]interface{}
if results, ok = res.Results.(map[string]interface{}); !ok {
return res, fmt.Errorf("Unexpected response to Subaccount creation (results)")
}
f, ok := results["subaccount_id"].(float64)
if !ok {
err = fmt.Errorf("Unexpected response to Subaccount creation")
}
s.ID = int(f)
s.ShortKey, ok = res.Results["short_key"].(string)
s.ShortKey, ok = results["short_key"].(string)
if !ok {
err = fmt.Errorf("Unexpected response to Subaccount creation")
}
Expand Down
68 changes: 34 additions & 34 deletions suppression_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package gosparkpost
import (
"encoding/json"
"fmt"
URL "net/url"
"net/url"
)

// https://developers.sparkpost.com/api/#/reference/suppression-list
Expand All @@ -19,6 +19,7 @@ type SuppressionEntry struct {
Transactional bool `json:"transactional,omitempty"`
NonTransactional bool `json:"non_transactional,omitempty"`
Source string `json:"source,omitempty"`
Type string `json:type,omitempty`
Description string `json:"description,omitempty"`
Updated string `json:"updated,omitempty"`
Created string `json:"created,omitempty"`
Expand All @@ -29,36 +30,34 @@ type SuppressionListWrapper struct {
Recipients []SuppressionEntry `json:"recipients,omitempty"`
}

func (c *Client) SuppressionList() (*SuppressionListWrapper, error) {
func (c *Client) SuppressionList() (*SuppressionListWrapper, *Response, error) {
path := fmt.Sprintf(suppressionListsPathFormat, c.Config.ApiVersion)
finalUrl := fmt.Sprintf("%s%s", c.Config.BaseUrl, path)

return doSuppressionRequest(c, finalUrl)
return suppressionGet(c, c.Config.BaseUrl+path)
}

func (c *Client) SuppressionRetrieve(recipientEmail string) (*SuppressionListWrapper, error) {
func (c *Client) SuppressionRetrieve(recipientEmail string) (*SuppressionListWrapper, *Response, error) {
path := fmt.Sprintf(suppressionListsPathFormat, c.Config.ApiVersion)
finalUrl := fmt.Sprintf("%s%s/%s", c.Config.BaseUrl, path, recipientEmail)

return doSuppressionRequest(c, finalUrl)
return suppressionGet(c, finalUrl)
}

func (c *Client) SuppressionSearch(parameters map[string]string) (*SuppressionListWrapper, error) {
func (c *Client) SuppressionSearch(parameters map[string]string) (*SuppressionListWrapper, *Response, error) {
var finalUrl string
path := fmt.Sprintf(suppressionListsPathFormat, c.Config.ApiVersion)

if parameters == nil || len(parameters) == 0 {
finalUrl = fmt.Sprintf("%s%s", c.Config.BaseUrl, path)
} else {
params := URL.Values{}
params := url.Values{}
for k, v := range parameters {
params.Add(k, v)
}

finalUrl = fmt.Sprintf("%s%s?%s", c.Config.BaseUrl, path, params.Encode())
}

return doSuppressionRequest(c, finalUrl)
return suppressionGet(c, finalUrl)
}

func (c *Client) SuppressionDelete(recipientEmail string) (res *Response, err error) {
Expand All @@ -67,58 +66,54 @@ func (c *Client) SuppressionDelete(recipientEmail string) (res *Response, err er

res, err = c.HttpDelete(finalUrl)
if err != nil {
return nil, err
return res, err
}

if res.HTTP.StatusCode >= 200 && res.HTTP.StatusCode <= 299 {
return
return res, err

} else if len(res.Errors) > 0 {
// handle common errors
err = res.PrettyError("SuppressionEntry", "delete")
if err != nil {
return nil, err
return res, err
}

err = fmt.Errorf("%d: %s", res.HTTP.StatusCode, string(res.Body))
}

return
return res, err
}

func (c *Client) SuppressionInsertOrUpdate(entries []SuppressionEntry) (err error) {
func (c *Client) SuppressionInsertOrUpdate(entries []SuppressionEntry) (*Response, error) {
if entries == nil {
err = fmt.Errorf("send `entries` cannot be nil here")
return
return nil, fmt.Errorf("send `entries` cannot be nil here")
}

path := fmt.Sprintf(suppressionListsPathFormat, c.Config.ApiVersion)
finalUrl := fmt.Sprintf("%s%s", c.Config.BaseUrl, path)

list := SuppressionListWrapper{nil, entries}

return c.send(finalUrl, list)

return suppressionPut(c, c.Config.BaseUrl+path, list)
}

func (c *Client) send(finalUrl string, recipients SuppressionListWrapper) (err error) {
func suppressionPut(c *Client, finalUrl string, recipients SuppressionListWrapper) (*Response, error) {
jsonBytes, err := json.Marshal(recipients)
if err != nil {
return
return nil, err
}

res, err := c.HttpPut(finalUrl, jsonBytes)
if err != nil {
return
return res, err
}

if err = res.AssertJson(); err != nil {
return
return res, err
}

err = res.ParseResponse()
if err != nil {
return
return res, err
}

if res.HTTP.StatusCode == 200 {
Expand All @@ -127,40 +122,45 @@ func (c *Client) send(finalUrl string, recipients SuppressionListWrapper) (err e
// handle common errors
err = res.PrettyError("Transmission", "create")
if err != nil {
return
return res, err
}

err = fmt.Errorf("%d: %s", res.HTTP.StatusCode, string(res.Body))
}

return
return res, err
}

func doSuppressionRequest(c *Client, finalUrl string) (*SuppressionListWrapper, error) {
func suppressionGet(c *Client, finalUrl string) (*SuppressionListWrapper, *Response, error) {
// Send off our request
res, err := c.HttpGet(finalUrl)
if err != nil {
return nil, err
return nil, res, err
}

// Assert that we got a JSON Content-Type back
if err = res.AssertJson(); err != nil {
return nil, err
return nil, res, err
}

err = res.ParseResponse()
if err != nil {
return nil, res, err
}

// Get the Content
bodyBytes, err := res.ReadBody()
if err != nil {
return nil, err
return nil, res, err
}

// Parse expected response structure
var resMap SuppressionListWrapper
err = json.Unmarshal(bodyBytes, &resMap)

if err != nil {
return nil, err
return nil, res, err
}

return &resMap, err
return &resMap, res, err
}

0 comments on commit 2c0d038

Please sign in to comment.