{nonsensecodes}
Published in

{nonsensecodes}

Not All APIs are Created Equal: Complete and Usable Responses vs. Chunked and Problematic Ones

Setting up a command making a GET request to a different API led to some problems, and some … creative solutions.

After having gotten the Mandalorian translation command working, I decided to create another API related command, this time from an API that would allegedly deliver Donald Trump’s worst quotes. Two problems arose as I developed the GET request:

First of all, Not all APIs are created equal. The code I used to get the Star Wars API calls to work did not work for this Tronald Dump API. For the Star Wars API call, I just needed a straightforward capture of the incoming data:

res.on('data', (d) => {
parsed = JSON.parse(d);
let display = Object.entries(parsed);
message.channel.send(display);
});

The ‘data’, d, all arrived in one shot as a JSON object which was easily parsed, converted to an array, and sent to Discord as a message. 3 lines of code, not including the variable assignment for “parsed.” Pretty pretty simple.

The incoming data from Tronald Dump, however, was not so easily managed. Of course it wouldn’t be. Trump is an idiot; why would anything having to do with him be any easier to deal with?

But I digress. Talking data, the incoming data from the server was not all one shot: it was chunked. I’ve set up a series of commands set to make and receive API calls, and this was the first time I’ve ever dealt with chunked data. That added a few lines:

res.on('data', d => {
body += d;
});
res.on('end', () => {

This does not include the code I needed to write to actually make the data accessible. In the Star Wars API example above, I received the data and was able to make use of it right away. The incoming Tronald data, however, came back as

undefined{all of the rest of the nonsense in an object}

Why, the fuck, was that undefined there?! I couldn’t parse the data because of that; couldn’t access the data because of that; I was fucking stuck. What I ended up doing was pushing the incoming data to an array, then parsing the joined data in order to access the object’s properties:

let body = [];...res.on('data', d => {
body.push(d);
});
res.on('end', () => {
let result = JSON.parse(body.join(''));
return message.channel.send(result.value);
});

As the chunked data, d, comes in, it gets pushed to the body array, which is then made into a string with which the JSON parser could work on. Once all that was done, I could deliver the value of the property I wanted from the resulting object.

What a pain in the ass.

But thank you to Jim Danz from this stackoverflow thread with the answer that worked. I wouldn’t have thought of using an array as a solution on my own.

Anyway, this leads me to the second issue with this API: Tronald appears to deliver random quotes pulled from Trump’s twitter account, which would be fine, except all of the quotes that were delivered as I tested it had aged well. These were generally quotes regarding his campaign against Hillary Clinton, and as Trump won that election, well, it appears that this API is less about verifying the dumbass that he is and more about trying to make him look like he actually knows what he’s doing. I’m keeping the code, because it was a pain in the ass to figure out, but I’m removing the command due to false advertising.

Or perhaps just dumb luck.

114/301

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
The Paladin of Nonsense

The Paladin of Nonsense

Reading & Writing. Music & Movement. Coffee & Code.