0

Im playing around with Googles official js client for accessing Google APIs and in this case Google Glass Mirror API.

I need some guidance on how to use the update or even better patch methods (see https://developers.google.com/glass/v1/reference/timeline) to update timeline card that I previously inserted into the timeline.

Inserting and listing was fairly easy and works successfully. I have used this tutorial to getting started: https://github.com/emil10001/glass-mirror-nodejs-auth-demo/blob/master/app.js

I have tried following:

card = {
        "kind": "mirror#timelineItem",
        "id": "74b88eb3-a6d7-4c13-8b0e-bfdecf71c913",
        "created": "2014-05-22T20:26:56.253Z",
        "updated": "2014-05-22T20:27:18.961Z",
        "etag": "1400790438961",
        "text": "This item3 auto-resizes according to the text length",
        "notification": {
            "level": "DEFAULT"
        }
    }

client
    .mirror.timeline.update({"id": card.id, resource: card})
    .withAuthClient(oauth2Client)
    .execute(function (err, data) {
        if (!!err)
            errorCallback(err);
        else
            successCallback(data);
    });

and I get successfull response with following payload:

{ 
  kind: 'mirror#timelineItem',
  id: '74b88eb3-a6d7-4c13-8b0e-bfdecf71c913',
  selfLink: 'https://www.googleapis.com/mirror/v1/timeline/74b88eb3-a6d7-4c13-8b0e-bfdecf71c913',
  created: '2014-05-22T20:26:56.253Z',
  updated: '2014-05-22T20:32:11.862Z',
  etag: '1400790731862' 
}

What I end up with is card with empty content. I suspect that I'm not using update method correctly and the the second parameter resource is not correctly named?

Ismar Slomic
  • 5,315
  • 6
  • 44
  • 63

1 Answers1

0

The Google APIs generally use two different ways to specify values for things:

  • Parameters. These are generally short values that control which resource you're working with or how that resource is represented back to you. (If you're looking at API documentation, these are generally part of the URL.)
  • Resources. These are the values themselves that you care about. (If you're looking at API documentation, this is sent as the body of the message.)

In the case of timeline.insert, you're only dealing with a resource, since you're just adding it. With timeline.patch and timeline.update, you're dealing with both a parameter (the id of the resource you're editing) and the changes to the resource. So you need to specify two parameters to the function call.

Using your example above, the call might look something like this:

client
.mirror.timeline.update(card.id, card)
.withAuthClient(oauth2Client)
.execute(function (err, data) {
    if (!!err)
        errorCallback(err);
    else
        successCallback(data);
});

You also asked about the difference between timeline.patch and timeline.update.

Update is used to replace the timeline item with everything (writable) that you provide in your card. So using your example above, the text and notification fields would probably be updated from what they were before. The other fields would be ignored - they're not writable and Glass will set/change them as appropriate.

As you surmised, timeline.update is most useful when you have a card (either because you stored it when it was sent to you or because you got it from a timeline.list or something) and you need to change a few fields in it.

Patch is used when you may not have the entire card, but when you might still want to update some fields in it. In this case, the resource only specifies the fields that you want to change - the others are left alone.

So the following two are equivalent:

var update = {
  text: "new text",
  notification: {
    level: "DEFAULT"
  }
};
client.mirror.timeline.update( card.id, update )
.withAuthClient(oauth2Client)
.execute(function(err,data){
  console.log(err,data);
});

var patch = {
  text: "new text",
};
client.mirror.timeline.patch( card.id, patch )
.withAuthClient(oauth2Client)
.execute(function(err,data){
  console.log(err,data);
});

Note that if you want to remove a field (such as notification, above) with patch, you need to explicitly set the field to null.

Prisoner
  • 49,922
  • 7
  • 53
  • 105