Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timestamp is being decoded as int64 instead of string #29

Closed
yepher opened this issue Feb 11, 2016 · 4 comments
Closed

Timestamp is being decoded as int64 instead of string #29

yepher opened this issue Feb 11, 2016 · 4 comments

Comments

@yepher
Copy link
Contributor

yepher commented Feb 11, 2016

events/events.go implementation is based on an error in SparkPost documentation. The Timestamp field is being decoded as int64 instead of String.

@yepher
Copy link
Contributor Author

yepher commented Feb 11, 2016

Added a fix here:

Commit: 85d31e2 [85d31e2]
Parents: fe82e99
Author: Chris Wilson github@yepher.com
Date: February 11, 2016 at 9:23:22 AM CST
Labels: HEAD master

• Fixes problem where Timestamp was getting decoded as int64 instead of String.
• Changes imports to be relative so it is easier to test local changes or changes on a branch.

@yepher yepher mentioned this issue Feb 11, 2016
@VojtechVitek
Copy link
Contributor

#35 parses timestamps into native golang time.Time values via custom JSON Unmarshaller. I noticed that Webhook Event API returns Unix timestamps (int64) and Event Samples API returns string of RFC 3339-like format.

Snippet:

type Timestamp time.Time

func (t *Timestamp) MarshalJSON() ([]byte, error) {
    return []byte(fmt.Sprint(time.Time(*t).Unix())), nil
}

func (t *Timestamp) UnmarshalJSON(data []byte) error {
    // Timestamps coming from Webhook Events are Unix timestamps.
    unix, err := strconv.ParseInt(string(data), 10, 64)
    if err == nil {
        *t = Timestamp(time.Unix(unix, 0))
        return nil
    }

    // Timestamps coming from Event Samples are in this RFC 3339-like format.
    customTime, err := time.Parse("\"2006-01-02T15:04:05.000-07:00\"", string(data))
    if err != nil {
        return err
    }

    *t = Timestamp(customTime)
    return nil
}

@VojtechVitek
Copy link
Contributor

Seems like the API changed or something in the last few days -- and instead of pure Unix timestamp integers, the Webhook API now returns strings. I had to add another commit https://github.com/pressly/gosparkpost/commit/c848d16a4f77aec77fb6e89d5fffd94441632a1a to trim the quotes.

@VojtechVitek
Copy link
Contributor

@yepher this can be closed, since Timestamps are now time.Time values

@yepher yepher closed this as completed Mar 15, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants