Skip to content

Commit

Permalink
Merge pull request #1 from SparkPost/master
Browse files Browse the repository at this point in the history
up
  • Loading branch information
kakysha committed May 18, 2016
2 parents 65e3e9d + 379b8b5 commit 6651000
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 81 deletions.
39 changes: 7 additions & 32 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,12 @@ import (
"fmt"
"io/ioutil"
"net/http"
"reflect"
"regexp"
"strings"

certifi "github.com/certifi/gocertifi"
)

// TODO: define paths statically in each endpoint's file, move version to Config
// TODO: rename e.g. Transmissions.Create to Client.SendTransmission

// Config includes all information necessary to make an API request.
type Config struct {
BaseUrl string
Expand Down Expand Up @@ -199,14 +195,17 @@ func (c *Client) DoRequest(method, urlStr string, data []byte) (*Response, error
ares := &Response{HTTP: res}

if c.Config.Verbose {
fmt.Println("Server Response: ", ares.HTTP.Status)
bodyBytes, err := ares.ReadBody()
if err != nil {
fmt.Println("Error: ", err)
} else {
fmt.Println("Body: ", string(bodyBytes))
fmt.Println("Server Response: ", ares.HTTP.Status)
bodyBytes, err := ares.ReadBody()
if err != nil {
fmt.Println("Error: ", err)
} else {
fmt.Println("Body: ", string(bodyBytes))
}
}

}

return ares, err
Expand Down Expand Up @@ -249,30 +248,6 @@ func (r *Response) ParseResponse() error {
return nil
}

// AssertObject asserts that the provided variable is a map[string]something.
// The string parameter is used to customize the generated error message.
func AssertObject(obj interface{}, label string) error {
// List of handled types from here:
// http://golang.org/pkg/encoding/json/#Unmarshal
switch objVal := obj.(type) {
case map[string]interface{}:
// auto-parsed nested json object
case map[string]bool:
// user-provided json literal (convenience)
case map[string]float64:
// user-provided json literal (convenience)
case map[string]string:
// user-provided json literal (convenience)
case map[string][]interface{}:
// user-provided json literal (convenience)
case map[string]map[string]interface{}:
// user-provided json literal (convenience)
default:
return fmt.Errorf("expected key/val pairs for %s, got [%s]", label, reflect.TypeOf(objVal))
}
return nil
}

// AssertJson returns an error if the provided HTTP response isn't JSON.
func (r *Response) AssertJson() error {
if r.HTTP == nil {
Expand Down
11 changes: 1 addition & 10 deletions events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ type Event interface {
type Events []Event

var (
ErrWebhookValidation = errors.New("webhook validation request")
ErrNotImplemented = errors.New("not implemented")
ErrNotImplemented = errors.New("not implemented")
)

// ValidEventType returns true if the event name parameter is valid.
Expand Down Expand Up @@ -117,9 +116,6 @@ func (events *Events) UnmarshalJSON(data []byte) error {
// Parse raw events from Event Webhook ("msys"-wrapped array of events).
rawEvents, err := parseRawJSONEventsFromWebhook(data)
if err != nil {
if err == ErrWebhookValidation {
return err
}
// Parse raw events from Event Samples ("results" object with array of events).
rawEvents, err = parseRawJSONEventsFromSamples(data)
if err != nil {
Expand All @@ -145,11 +141,6 @@ func parseRawJSONEventsFromWebhook(data []byte) ([]json.RawMessage, error) {
return nil, err
}

// Empty "msys" wrapper is used for webhook validation.
if len(msysEventWrappers) == 1 && len(msysEventWrappers[0].MsysEventWrapper) == 0 {
return nil, ErrWebhookValidation
}

for _, wrapper := range msysEventWrappers {
for _, rawEvent := range wrapper.MsysEventWrapper {
rawEvents = append(rawEvents, rawEvent)
Expand Down
7 changes: 5 additions & 2 deletions events/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ func TestSampleWebhookValidationRequest(t *testing.T) {

var events Events
err = json.Unmarshal(payload, &events)
if err != ErrWebhookValidation {
t.Fatalf("expected ErrWebhookValidation error, got %v", err)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if len(events) != 0 {
t.Fatalf("expected zero events, got %d: %v", len(events), events)
}
}
17 changes: 0 additions & 17 deletions recipient_lists.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,23 +133,6 @@ func (r Recipient) Validate() error {
if err != nil {
return err
}

// Metadata must be an object, not an array or bool etc.
if r.Metadata != nil {
err := AssertObject(r.Metadata, "metadata")
if err != nil {
return err
}
}

// SubstitutionData must be an object, not an array or bool etc.
if r.SubstitutionData != nil {
err := AssertObject(r.SubstitutionData, "substitution_data")
if err != nil {
return err
}
}

return nil
}

Expand Down
33 changes: 13 additions & 20 deletions transmissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,24 @@ type Transmission struct {
NumInvalidRecipients *int `json:"num_invalid_recipients,omitempty"`
}

type RFC3339 time.Time

func (r *RFC3339) MarshalJSON() ([]byte, error) {
if r == nil {
return json.Marshal(nil)
}
return json.Marshal(time.Time(*r).Format(time.RFC3339))
}

// Options specifies settings to apply to this Transmission.
// If not specified, and present in TmplOptions, those values will be used.
type TxOptions struct {
TmplOptions

StartTime *time.Time `json:"start_time,omitempty"`
Sandbox string `json:"sandbox,omitempty"`
SkipSuppression string `json:"skip_suppression,omitempty"`
InlineCSS bool `json:"inline_css,omitempty"`
StartTime *RFC3339 `json:"start_time,omitempty"`
Sandbox string `json:"sandbox,omitempty"`
SkipSuppression string `json:"skip_suppression,omitempty"`
InlineCSS bool `json:"inline_css,omitempty"`
}

// ParseRecipients asserts that Transmission.Recipients is valid.
Expand Down Expand Up @@ -181,22 +190,6 @@ func (t *Transmission) Validate() error {
return err
}

// Metadata must be an object, not an array or bool etc.
if t.Metadata != nil {
err := AssertObject(t.Metadata, "metadata")
if err != nil {
return err
}
}

// SubstitutionData must be an object, not an array or bool etc.
if t.SubstitutionData != nil {
err := AssertObject(t.SubstitutionData, "substitution_data")
if err != nil {
return err
}
}

return nil
}

Expand Down

0 comments on commit 6651000

Please sign in to comment.