-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: Support appendSeedData argument to be an array or array of arrays #18
base: master
Are you sure you want to change the base?
Conversation
Looks great! |
@@ -63,7 +63,19 @@ function api(token, endpoint, method, requestBody, callback) { | |||
if (responseBody.trim().length === 0) { | |||
responseBody = null; | |||
} else { | |||
responseBody = JSON.parse(responseBody); | |||
try { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a case where api-json.js
is being used for a request that doesn't get back JSON?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. CSV data for example when we appendSeedData or readSeedData
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I think that's a bug in SeedData.readSeedData()
. It should be using apiStream
instead of apiJson
.
There's no concern for SeedData.appendSeedData()
, because the response is always empty (204 No Content).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well okay so we are wrapping appendSeedData into the stream and take end
and the wrapper for the callback. That's just fine. Remember that you have 2 versions of append/read one stream one json. And one possibly doesn't work currently as it uses the api-json.js
instead of api-stream.js
.
Additionally using JSON.parse() and hoping to always retrieve JSON feels presumptious. What if the URL we are requesting this from is not an API endpoint, keep in mind the user may point it to another server not from us. What if the Server returns HTML because an Error happend internally and the Servlet was inaccessible? We should guard against these issues better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But then readSeedData and ReadSeedDataStream are redundant?
Am 22.09.2015 07:35 schrieb "Matt Solnit" [email protected]:
In lib/util/api-json.js
#18 (comment):@@ -63,7 +63,19 @@ function api(token, endpoint, method, requestBody, callback) {
if (responseBody.trim().length === 0) {
responseBody = null;
} else {
responseBody = JSON.parse(responseBody);
try {
Actually, I think that's a bug in SeedData.readSeedData(). It should be
using apiStream instead of apiJson.There's no concern for SeedData.appendSeedData(), because the response is
always empty (204 No Content).—
Reply to this email directly or view it on GitHub
https://github.com/SOASTA/repository.js/pull/18/files#r40054154.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like switching to a single readSeedData
that just reads a stream, instead of expecting it in JSON.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, so I have a few thoughts on this.
SeedData.readSeedData
definitely has a bug, and just as you found, if the response has multiple lines (which I never tested), you'll getSyntaxError: Unexpected string
. This is bad.- Removing this method, and requiring the caller to provide a
Stream.Writable
, is more "pure" but inconvenient IMO. - Having to deal with special edge cases in
api-json.js
is also bad. We should letJSON.parse()
do its thing without having to worry about "expected non-JSON" responses.
I propose that we keep SeedData.readSeedData
, but instead of using apiJson()
, it instead creates a temporary stream, calls apiStream()
, and then "un-wraps" the content, returning it to the caller as a regular string. In fact, it can even use this.readSeedDataStream()
with an in-between callback to do the un-wrapping.
Finally, we can add a note to the JSDoc, warning against doing this unless you know the response is small.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds fair.
Uses
csv-stringify
to render from one and two dimensional arrays tocsv
and can post that to the SeedData service.Also fixes a bug in
api-json.js
where theresponseBody
would come down bare CSV data and crashes the request asapi-json.js
expects JSON only and results in aSyntaxError
withUnexpected token ,
which is also fixed with the PR.please review: @msolnit @nicjansma