On Wed, Jun 26, 2013 at 4:42 PM, Nate D <[email protected]> wrote:
> I am writing some node based services that run all day, pull data of a
> queue, perform some processing steps, then does some CRUD operations to a
> REST service/API with an authenticated session. The authentication is
> standard POST user/pass params to <site root>/api/auth/login, session is
> created and token returned.
>
> After trying out a few modules I have started with request (
> https://github.com/mikeal/request) and written a module that creates a
> little API wrapper around it.
>
> The pattern I would like to do is each API call will check the response,
> if there is an "unauthorized" error, it will attempt X times to create a
> new session, and re-call the API call. There is design issue around not
> having a handle on the original callback and losign its context should a
> re-auth be necessary.
>
> The processor would make a call like this:
>
> var requestID = apiClient.testPing(function(err,respObj,body){
> console.log("inside of processor api callback...");
> });
>
> Then inside the apiClient, the testPing call would be:
>
>
> this.testPing = function(callback) {
> var opts = {
> "uri":_this._host+'/'+_this._apiVersion+'/test/ping'
> };
> var reqId = _this._uuid.v4();
> _this._reqs[reqId] = {
> "id":reqId,
> "callback":callback, //this is the callback passed to
> the API client call from the queue processor call
> "func":function(err,resp,body) {
> console.log("inside api response callback, if auth
> error, call reAuth, on successful re-authentication re-call testPing");
> }
> };
> _this._request.get(opts,_this._reqs[reqId].func); //use the
> node-request module object to carry out the REST call
>
> return reqId; //return the request ID
> }
>
> I am trying the following pattern where I generate a "request object",
> with an assigned UUID as its key and use that to maintain state. Issue is
> that I have no way to know which "request ID" I am in since the
> node-request module, and others I have tried, fire the callbacks under the
> context of its request.
>
Perhaps I'm misunderstanding, but it looks to me like all you need is to
bind the callback to your context object. Something like this (which is
untested) will result in 'this' in your callback being the ctx object. Note
the call to bind(). I've added 'self' to the context object so you can get
back to '_this' if you need to.
var ctx = {
id:reqId,
self: _this, // so that you can get back to
whatever _this is, if you need to
callback:callback, //this is the callback passed to
the API client call from the queue processor call
};
ctx.func = function(err,resp,body) {
console.log("inside api response callback, if auth error,
call reAuth, on successful re-authentication re-call testPing");
}.bind(ctx);
_this._reqs[reqId] = ctx;
_this._request.get(opts,ctx.func); //use the node-request
module object to carry out the REST call
Hope that helps.
--
Martin Cooper
After looking as many of the REST Client modules most handle basic
> HTTPAuth, but no hooks/tools for standard forms of authing/auto-re-auth.
> The restler module seemed like it might be a fit with its .on callbacks and
> retries built in, but still doesn't quite fit.
>
> Is there any node pattern(s) people have used that would solve this
> problem? Or maybe a module out there my searches haven't revealed yet, so
> far looked through (request,restify,restler,node-rest-client)
>
> --
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>
> ---
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>
--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.