【问题标题】:How to query with parameters using Purest module (node.js)如何使用 Purest 模块(node.js)查询参数
【发布时间】:2016-01-05 08:54:03
【问题描述】:

我正在尝试通过我的 API 请求传递一些参数,以便获取已过滤的数据。

我正在使用一个名为 Purest 的模块,它基本上是一个 REST API 客户端库。 它支持根据Purest Query API进行表达查询调用

而且,我的 API 提供者是 Pocket,他们的文档说以下 more info here

内容类型

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

排序

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

现在,我想获取视频数据并按最新排序。 但我不知道如何在我的查询中添加这些参数。

以下是我尝试过的,但我得到了400 Bad Request。因为我不知道这个数据库的表名,所以也不确定要在 select 中放什么。

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);
  })

【问题讨论】:

    标签: node.js api http


    【解决方案1】:

    Pocket 的 API 只接受 POST 请求,并希望您发送 JSON 编码的请求正文:

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

    对于这个提供程序,查询 API 看起来有点奇怪,因为端点被称为 get,而您正在向它发出 POST 请求。

    Purest 建立在request 之上,并且完全兼容它。以下代码将产生与上面完全相同的结果:

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

    您也可以改用request

    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))
    })
    

    【讨论】:

    • 再次,这就像一个魅力。我很幸运能从这些模块的创建者那里得到帮助。最后,您介意在我的下一条评论中解决来自 Pocket API 网站的这个建议吗?不确定我是否完全理解这样做。似乎很重要,因为我想避免再次请求相同的数据
    • 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.
    • 无论何时处理 REST API,您都必须将结果数据缓存在您自己的数据库中,并将该数据显示给您的用户。这与rate limits 有关。上面的内容确切地表明:如果您想保留所有历史更新的完整列表,则必须将其保存在自己的数据库中,而不是每次都从 Pocket 的 REST API 请求完整列表。
    • 所以我的想法是建立我自己的数据库?有没有其他选择,比如使用浏览器本地存储或缓存响应的东西?我认为像我这样的简单网站不需要自己的数据库,因为数据不会被修改或从中生成。
    • 我实际上收到了无效的 JSON 响应,即属性名称不是字符串。请看这个stackoverflow.com/questions/33039120/…这个答案中的请求调用有什么关系吗?
    猜你喜欢
    • 2013-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    • 2022-11-06
    相关资源
    最近更新 更多