mozilla / ping-centre

INACTIVE - http://mzl.la/ghe-archive
Mozilla Public License 2.0
8 stars 10 forks source link

Update example.js? #49

Closed pdehaan closed 4 months ago

pdehaan commented 7 years ago

Here's the current behavior if I try running ./example.js locally (with a couple minor tweaks):

Input:

// example.js

const topic = process.env.PING_CENTRE_TOPIC;
const PingCentre = require("./src/ping-centre");
const pingClient = new PingCentre(topic, "clientID", "https://onyx_tiles.stage.mozaws.net/v3/links/activity-stream");

pingClient.validate({
  event_type: "search",
  value: 3
});

Output:

$ PING_CENTRE_TOPIC=commonSchema node example

(node:1492) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Invalid payload {"event_type":"search","value":3}.

$ node --version # v6.9.5
$ npm --version # 3.10.10

Not super helpful failure message, but if I sprinkle some .then().catch() magic, things get a bit better:

Input:

const topic = process.env.PING_CENTRE_TOPIC;
const PingCentre = require("./src/ping-centre");
const pingClient = new PingCentre(topic, "clientID", "https://onyx_tiles.stage.mozaws.net/v3/links/activity-stream");

pingClient.validate({
  event_type: "search",
  value: 3
})
.then(data => console.log(data))
.catch(err => console.error(err));

Output:

$ PING_CENTRE_TOPIC=commonSchema node example

{ Error: Invalid payload {"event_type":"search","value":3}.
    at Joi.validate (/Users/pdehaan/dev/github/mozilla/ping-centre/src/ping-centre.js:28:25)
    at _class._validateWithOptions (/Users/pdehaan/dev/github/mozilla/ping-centre/node_modules/joi-browser/dist/joi-browser.js:5163:21)
    at _class.root.validate (/Users/pdehaan/dev/github/mozilla/ping-centre/node_modules/joi-browser/dist/joi-browser.js:197:24)
    at Promise (/Users/pdehaan/dev/github/mozilla/ping-centre/src/ping-centre.js:26:11)
    at PingCentre.validate (/Users/pdehaan/dev/github/mozilla/ping-centre/src/ping-centre.js:25:12)
    at Object.<anonymous> (/Users/pdehaan/dev/github/mozilla/ping-centre/example2.js:5:12)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
  details: [ '"client_id" is required', '"topic" is required' ],
  payload: { event_type: 'search', value: 3 } }

So it looks like I still need to pass a client_id and topic to .validate(), even though I specified them in the constructor.

Adding those two missing props seems to work and validates as expected:

Input

const topic = process.env.PING_CENTRE_TOPIC;
const PingCentre = require("./src/ping-centre");
const pingClient = new PingCentre(topic, "clientID", "https://onyx_tiles.stage.mozaws.net/v3/links/activity-stream");

pingClient.validate({
  event_type: "search",
  value: 3,
  client_id: 'clientID',
  topic
})
.then(data => console.log(data))
.catch(err => console.error(err));

Output:

$ PING_CENTRE_TOPIC=commonSchema node example

{ event_type: 'search',
  value: 3,
  client_id: 'clientID',
  topic: 'commonSchema' }

SUCCESS! Now that my example ping validates, if I change the .validate() to .sendPing(), it implodes with another error:

Input:

const topic = process.env.PING_CENTRE_TOPIC;
const PingCentre = require("./src/ping-centre");
const pingClient = new PingCentre(topic, "clientID", "https://onyx_tiles.stage.mozaws.net/v3/links/ping-centre");

pingClient.sendPing({
  event_type: "search",
  value: 3,
  client_id: 'clientID',
  topic
})
.then(data => console.log(data))
.catch(err => console.error(err));

Output:

$ PING_CENTRE_TOPIC=commonSchema node example

Ping failure with response code: 400
Error: Ping failure with response code: 400
    at fetch.then.response (/Users/pdehaan/dev/github/mozilla/ping-centre/src/ping-centre.js:53:17)
    at process._tickCallback (internal/process/next_tick.js:103:7)
pdehaan commented 7 years ago

So I guess the questions are:

  1. Should something that passes .validate() be able to sent via .sendPing()?
  2. Should the endpoint in the example.js PingCentre() constructor be to ".../activity-stream" [like it is in example.js], or should it be to ".../ping-centre" [like it is in src/config.js]
  3. Should the PingCentre#validate() method inject the this._topic and this._clientID values, like we do in PingCentre#sendPing()?