alexwhitman / node-pushbullet-api

PushBullet API module for Node.js
165 stars 36 forks source link

pusher.me() returns rubbish #48

Open davidnewcomb opened 1 year ago

davidnewcomb commented 1 year ago

Following your documentation. I am using version 3.0.0. What am I doing wrong? How can I get the useful information using your module?

import PushBullet from 'pushbullet'
import { API_KEY } from './config.js'

const pusher = new PushBullet(API_KEY)
const me = await pusher.me()
console.log('Me:', me)

Gives:

Me: Response {
  size: 0,
  [Symbol(Body internals)]: {
    body: PassThrough {
      _readableState: [ReadableState],
      _events: [Object: null prototype],
      _eventsCount: 3,
      _maxListeners: undefined,
      _writableState: [WritableState],
      allowHalfOpen: true,
      [Symbol(kCapture)]: false,
      [Symbol(kCallback)]: null
    },
    stream: PassThrough {
      _readableState: [ReadableState],
      _events: [Object: null prototype],
      _eventsCount: 3,
      _maxListeners: undefined,
      _writableState: [WritableState],
      allowHalfOpen: true,
      [Symbol(kCapture)]: false,
      [Symbol(kCallback)]: null
    },
    boundary: null,
    disturbed: false,
    error: null
  },
  [Symbol(Response internals)]: {
    type: 'default',
    url: 'https://api.pushbullet.com/v2/users/me',
    status: 200,
    statusText: '',
    headers: {
      'alt-svc': 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000',
      connection: 'close',
      'content-length': '353',
      'content-type': 'application/json; charset=utf-8',
      date: 'Mon, 31 Jul 2023 21:08:44 GMT',
      via: '1.1 google',
      'x-ratelimit-limit': '16384',
      'x-ratelimit-remaining': '16369',
      'x-ratelimit-reset': '1690840927'
    },
    counter: 0,
    highWaterMark: 16384
  }
}

But when I use curl with the same access token I get:

curl --header 'Access-Token: <access_token>' "https://api.pushbullet.com/v2/users/me" 
{"active":true,"iden":"....}
davidnewcomb commented 1 year ago

https://www.npmjs.com/package/pushbullet#user-content-asyncawait

davidnewcomb commented 1 year ago

I've reopened this to ask why I'm supposed to await .json(). Surely that bit should live inside the library as it's part of the session layer. When I call me() I should just get back the me object and not your internal object where I have to do the last little bit - each time.

This is what I had to do to get version 3.0.0 working. Is this right? It's not at all what the docs say.

const pusher = new PushBullet(API_KEY)
const pbObjectPromise = await pusher.devices()
const pbObject = await pbObjectPromise.json()
const devices = pbObject.devices
const device = devices.find((d) => d.nickname === DEVICE_NICKNAME)
alexwhitman commented 1 year ago

The response that is returned is the response object from fetch() which is used internally. Earlier versions did return the JSON response but there were requests to return the full response as the PushBullet API returns rate limiting information in the response headers (https://docs.pushbullet.com/#ratelimiting) which can be useful to not exceed quota limits.