0

I'm trying to pass some parameters with my API request so I can get data that's already filtered.

I'm using a module called Purest and it's basically a REST API client library. It supports making expressive query call as per Purest Query API

And, my API provider is Pocket and their documentation says the following more info here.

contentType

article = only return articles
video = only return videos or articles with embedded videos
image = only return images

sort

newest = return items in order of newest to oldest
oldest = return items in order of oldest to newest
title = return items in order of title alphabetically
site = return items in order of url alphabetically

Now, I want to get video data and sort it by newest. But I'm short on clues how to add these parameters in my query.

Below is what I've tried but I'm getting 400 Bad Request. Not also sure what to put in select because I don't know the table name of this database.

var Purest = require('purest')
, getpocket = new Purest({provider:'getpocket'})


getpocket.query()
  .select('')
  .where({contentType:'video'},{sort:'newest'})
  .post('get')
  .auth('xxxxx', 'xxxxx')
  .request(function (err, res, body) {
    console.log(body);
  })
simo
  • 15,078
  • 7
  • 45
  • 59
Seong Lee
  • 10,314
  • 25
  • 68
  • 106

1 Answers1

2

Pocket's API accepts only POST requests, and expect you to send a JSON encoded request body:

getpocket.query()
  .post('get')
  .json({
    consumer_key:'...',
    access_token:'...',
    contentType:'article',
    sort:'title'
  })
  .request(function (err, res, body) {})

With this provider specifically the Query API looks a bit weird, because the endpoint is called get and you are making a POST request to it.

Purest is built on top of request and it's fully compatible with it. The following code will produce the exact same result as the above one:

getpocket.post('https://getpocket.com/v3/get', {
  json: {
    consumer_key:'...',
    access_token:'...',
    contentType:'article',
    sort:'title'
  }
}, function (err, res, body) {})

Alternatively you can use request instead:

var request = require('request')
request.post('https://getpocket.com/v3/get', {
  headers: {'content-type':'application/json'},
  body: JSON.stringify({
    consumer_key:'...',
    access_token:'...',
    contentType:'article',
    sort:'title'
  })
}, function (err, res, body) {
  console.log(JSON.parse(body))
})
simo
  • 15,078
  • 7
  • 45
  • 59
  • Again, this worked like a charm. I feel very lucky to get assistance from the creator of those modules. Lastly would you mind addressing this suggestion from Pocket API site in my next comment? Not sure if I understand it fully to do so. Seems important as I wanna avoid requesting the same data all over again – Seong Lee Oct 08 '15 at 07:29
  • `Best Practices Retrieving Full List: Whenever possible, you should use the since parameter, or count and and offset parameters when retrieving a user's list. After retrieving the list, you should store the current time (which is provided along with the list response) and pass that in the next request for the list. This way the server only needs to return a small set (changes since that time) instead of the user's entire list every time.` – Seong Lee Oct 08 '15 at 07:29
  • Whenever you deal with REST APIs you have to cache the result data in your own database, and show that data to your users. That's related to [rate limits](https://getpocket.com/developer/docs/rate-limits). The above suggests exactly that: if you want to keep a full list of all of your updates historically, you have to keep it in your own database, instead of requesting the full list each time from the Pocket's REST API. – simo Oct 08 '15 at 07:53
  • So the idea is to setup my own database? Is there any alternative like using browser local storage or something to cache the response? I thought a simple website like mine won't require own database because data is not modified or generated from it. – Seong Lee Oct 08 '15 at 08:23
  • I'm actually getting invalid JSON response that property names are not string. Please see this http://stackoverflow.com/questions/33039120/parsing-json-in-node-js-app?noredirect=1#comment53899595_33039120 Does it have anything to do with the request call in this answer? – Seong Lee Oct 09 '15 at 13:43
  • Please see my comment above about invalid JSON with properties not in string. – Seong Lee Oct 11 '15 at 15:00
  • It works on my end, see [this](https://github.com/simov/purest/blob/master/test/request/get.js#L1352-L1377). Getting parsed JSON response. – simo Oct 11 '15 at 16:35
  • Well, I tried both ways you suggested in this answer but I'm getting this '{ status: 1, complete: 1, list: { '890245271': { item_id: '890245271', resolved_id: '890245271', ... ' Here item_id, resolved_id and all the rest properties should be wrapped in `' '` to be valid JSON. – Seong Lee Oct 12 '15 at 00:09
  • I have no idea what you are doing wrong, but I just added one more example using the request module (see the last example in my answer above). All of the examples work. – simo Oct 12 '15 at 05:47