Asana / node-asana

Official node.js and browser JS client for the Asana API v1
MIT License
263 stars 74 forks source link

dispatchOptions not being respected #116

Open holsted opened 8 years ago

holsted commented 8 years ago

I was trying to populate expand the assignee field when calling client.tasks.create and it doesn't appear the dispatchOptions are actually being used anywhere. Here's the relevant dispatch call

/**
 * Dispatches a request to the Asana API. The request parameters are passed to
 * the request module.
 * @param  {Object}  params The params for request
 * @param  {Object}  [dispatchOptions] Options for handling request/response
 * @return {Promise}        The response for the request
 */
Dispatcher.prototype.dispatch = function(params, dispatchOptions) {
 var me = this;
 // TODO: actually honor these options as overriding defaults
 dispatchOptions = dispatchOptions || {};

if (this.requestTimeout) {
 params.timeout = this.requestTimeout;
 }

return new Bluebird(function(resolve, reject) {
 function doRequest() {
 if (me.authenticator !== null) {
 me.authenticator.authenticateRequest(params);
 }
 me._addVersionInfo(params);
 request(params, function(err, res, payload) {
 if (err) {
 return reject(err);
 }
 if (STATUS_MAP[res.statusCode]) {
 var error = new STATUS_MAP[res.statusCode](payload);

if (me.retryOnRateLimit &&
 error instanceof (errors.RateLimitEnforced)) {
 // Maybe attempt retry for rate limiting.
 // Delay a half-second more in case of rounding error.
 // TODO: verify Asana is always being conservative with duration
 setTimeout(doRequest, error.retryAfterSeconds * 1000 + 500);

} else if (me.handleUnauthorized &&
 error instanceof (errors.NoAuthorization)) {
 // Maybe attempt to retry unauthorized requests after getting
 // a new access token.
 Bluebird.resolve(me.handleUnauthorized()).then(function(reauth) {
 if (reauth) {
 doRequest();
 } else {
 reject(error);
 }
 });

} else {
 // Not an error we can handle.
 return reject(error);
 }
 } else {
 return resolve(payload);
 }
 });
 }
 doRequest();
 });
};

As a quick test, passing it inside of dispatch.post explicitly does work

/**
 * Dispatches a POST request to the Asana API.
 * @param  {String} path The path of the API
 * @param  {Object} data The data to be sent
 * @param  {Object}  [dispatchOptions] Options for handling the request and
 *     response. See `dispatch`.
 * @return {Promise}     The response for the request
 */
Dispatcher.prototype.post = function(path, data, dispatchOptions) {
 var params = {
 method: 'POST',
 url: this.url(path),
 json: {
 data: data,
 options: dispatchOptions // this works
 }
 };
 return this.dispatch(params, dispatchOptions);
};

I'm happy to work on a pull request, but would like a little guidance on implementation. Are dispatchOptions only used for PUT and POST? Looks like it's in the function signature for GET and DELETE as well though for GET I think those options should be passed in the query string?

agnoster commented 8 years ago

The assignee should I believe be passed in data, not dispatchOptions? Have you tried that?

So something like client.task.create({assignee: user_id})

If that doesn't work I can dig deeper. There's some amount of auto-generation of code which means that not every option is always used, so while the dispatchOptions is ignored in dispatch I don't think that affects your specific use-case.

holsted commented 8 years ago

@agnostic I am passing the assignee id in the data for the create. I was just hoping to expand the full object in the response.

Agnostic commented 8 years ago

wrong user @andrewholsted ;P

holsted commented 8 years ago

Whoops. @agnoster...

Nolanus commented 6 years ago

Any progress on that, as dispatchOptions still seems to be ignored...