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

Timeline API #62

Merged
merged 38 commits into from
Sep 30, 2016
Merged

Timeline API #62

merged 38 commits into from
Sep 30, 2016

Conversation

lukevers
Copy link
Contributor

This PR adds support for the Timeline API!

🎉

Also fixed one @see statement to include the correct URL.
This is needed for the Timeline API.
Although it's not in the docs on the API page, I found in the timeline
overview page that it's a required field in order to create an event
type.

Weird since we already do add the application id in the URL, but that's
what it wants.

http://developers.hubspot.com/docs/methods/timeline/timeline-overview
Also added the missing parameters for updateEventType
@lukevers
Copy link
Contributor Author

I've noticed some very weird things with the timeline API. Everything in here works, but some of the things that make some of it work are weird (not the code, just the way some of the API endpoints work).

For example:

When updating a property for a timeline event (docs), if you do not provide the original name (which cannot be changed anyways...) in the JSON body request, a 400 Bad Request is returned with the message "Cannot rename a property.". Also, if you do not include propertyType (even if you're not changing it...) you'll get a 500 Internal Server Error.

There are some other things that I've noticed adding support for the Timeline API, and if you're curious about them I can let you know, but I'm contacting someone at HubSpot to discuss the odd things I've found.

@lukevers lukevers mentioned this pull request Sep 28, 2016
if (isset($timestamp)) {
// Times must be in milliseconds epoch time.
$data['json']['timestamp'] = $timestamp->getTimestamp() * 1000;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I like the DateTime typehinting. If I see the variable name I am going to expect to send an int, but then it's a DateTime. Also, what if i am working with data I got from hubspot and it is already milliseconds?

How about something like:

if (is_numeric($timestamp) && strlen((string)$num) == 10) {
    $timestamp = $timestamp * 1000;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, good point.

I kept sending seconds before I realized in the docs it said to use milliseconds instead of seconds, and that's what made me decide to go with a DateTime so other people wouldn't make that mistake.

Your solution is nice for any dates before Sat, 20 Nov 2286 17:46:39 GMT (9999999999), but after that date we'll have a problem. (Not sure if anyone will be using this PHP library in the year 2286...haha)

Maybe something more like this:

if ($timestamp instanceof \DateTime) {
    $timestamp = $timestamp->getTimestamp() * 1000;
}

if (isset($timestamp)) {
    $data['json']['timestamp'] = $timestamp;
}

That way the function can support both a DateTime and maybe in the PHPDoc we just mention that it either has to be a DateTime or an already converted to milliseconds timestamp. Could even rename it to be more like this:

/**
 * @param mixed $timestampMilliseconds - Timestamp in milliseconds format or DateTime
 */

Thoughts?

Copy link
Collaborator

@ryanwinchester ryanwinchester Sep 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think with a variable name of $timestamp people should know that we are expecting an integer timestamp and we don't need to muddy up the code with a bunch of checks.

If it really is a concern, maybe add a timestamp method like this in the Resource base class:

public function timestamp($timestamp)
{
    switch (true) {
        case: $timestamp instanceof \DateTime:
            return $timestamp->getTimestamp() * 1000;
        case: is_numeric($timestamp) && strlen((string)$timestamp) == 10:
            return $timestamp * 1000;
        default:
            return $timestamp;
        }
    }
}

then you can just do:

$data['json']['timestamp'] = $this->timestamp($timestamp);

or maybe call the method millisecondTimestamp()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this solution. It could potentially be useful elsewhere too.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I don't expect we'll be using a hubspot php library a couple of centuries in the future 😂

@ryanwinchester ryanwinchester merged commit d9631f0 into HubSpot:master Sep 30, 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

Successfully merging this pull request may close these issues.

None yet

2 participants