Skip to content
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

For templates.find, return JSON parsed body #111

Closed
aydrian opened this issue Mar 18, 2016 · 10 comments
Closed

For templates.find, return JSON parsed body #111

aydrian opened this issue Mar 18, 2016 · 10 comments

Comments

@aydrian
Copy link
Contributor

aydrian commented Mar 18, 2016

User should not have to JSON.parse(res.body). It should be returned as JSON.

@aydrian aydrian added this to the 2.0.0 Release milestone Mar 18, 2016
@coldacid
Copy link
Contributor

coldacid commented Apr 7, 2016

This seems to go for all calls, not just templates.find.

@coldacid
Copy link
Contributor

coldacid commented Apr 7, 2016

It seems to be a misuse of the json property of the options object passed to the request module's request function. The request body should be passed in as options.body and options.json should be true (not just truthy, apparently).

@aydrian
Copy link
Contributor Author

aydrian commented Apr 7, 2016

You are correct and I have this fixed I the 2.0.0 wip because we determined it would be a breaking change for the 1.x line. We also determined it only happens for get requests.

@coldacid
Copy link
Contributor

coldacid commented Apr 7, 2016

When will 2.0.0 be out? I've fixed it myself (along with adding a line missing from my PR for subaccounts; forgot to require it into the SparkPost class) but if it'll be a while longer then I'll have to run with my fork until 2.0.0 is actually released.

@aydrian
Copy link
Contributor Author

aydrian commented Apr 7, 2016

We don't have a date yet. We are trying to make sure we cover any and all breaking changes. If you want to do a PR for what you left out of subaccounts, I can do a patch release.

@coldacid
Copy link
Contributor

coldacid commented Apr 7, 2016

Already did.

@coldacid
Copy link
Contributor

coldacid commented Apr 7, 2016

I'm not sure that the current code in the wip-2.0.0 branch is the best approach for this. Instead of the simple

  // set JSON (Always true)
  options.json = true;

I'd suggest checking that options.json isn't already set to a non-boolean value first. With the current state of the branch, no non-GET requests will succeed, because the body content (which is still being set on options.json everywhere) would be replaced with the value true.

My own fix attempt uses this code instead to deal with options.json, with the idea of a few lines in one place is easier to handle than many lines in almost as many files:

  // shift request body to options.body if set on options.json
  // otherwise default to accepting json responses
  if (options.json && typeof options.json !== 'boolean') {
    options.body = options.json;
    options.json = true;
  } else if (typeof options.json === 'undefined') {
    options.json = true;
  }

You might or might not find this a better approach, but it might help in moving things along towards final 2.0.0.

@aydrian
Copy link
Contributor Author

aydrian commented Aug 26, 2016

@coldacid I think we found a better approach. If you'd like to take a look at PR #163. We also have tests checking that responses are returned as parsed json.

@aydrian
Copy link
Contributor Author

aydrian commented Aug 26, 2016

Closed by #163

@aydrian aydrian closed this as completed Aug 26, 2016
@coldacid
Copy link
Contributor

Sorry I didn't get a chance earlier but yeah that looks just fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants