Created
June 15, 2014 13:04
-
-
Save kumarishan/821cb940902fb7bba9f9 to your computer and use it in GitHub Desktop.
Promised Rest Client for Node.js using Q
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Client = (require 'node-rest-client').Client | |
| Q = require 'q' | |
| _ = require 'underscore' | |
| util = require 'util' | |
| getPromisedHttpRequestMethod = (client, method) -> | |
| () -> | |
| args = [].slice.call(arguments, 0) | |
| deferred = Q.defer() | |
| if _.isFunction args[args.length - 1] | |
| deferred.reject(new Error("cannot pass callback function to #{method} request")) | |
| else | |
| # Resolving with a promise of array | |
| # so that spread can be used later | |
| args.push((data, response) -> deferred.resolve(Q.all([Q(data), Q(response)]))) | |
| g = client[method].apply client, args | |
| g.on('error', (err) -> deferred.reject(err)) | |
| deferred.promise | |
| exports.asyncClient = -> | |
| client = new Client(arguments) | |
| # add async methods under 'q' | |
| # important the arguments should not contain | |
| # callback function as last argument | |
| # | |
| # Returns a promise for get request | |
| # | |
| # Example: | |
| # asyncClient().q.get("http://test.com/api/func").spread((data, response) -> | |
| # console.log(data) | |
| # console.log(response) | |
| # ) | |
| methods = ['get', 'post', 'put', 'delete', 'patch'] | |
| q = {} | |
| for method in methods | |
| q[method] = getPromisedHttpRequestMethod(client, method) | |
| client.q = q | |
| client.q.methods = {} | |
| # registers both callback and promised based methods | |
| # Example: | |
| # | |
| # client = asyncClient() | |
| # client.q.registerMethod("mymethod, url, "get") | |
| # client.q.methods.mymethod(args).spread((data, response) -> | |
| # console.log(data) | |
| # console.log(response) | |
| # ).fail((err) -> | |
| # console.log(err) | |
| # ) | |
| client.q.registerMethod = (name, url, method) -> | |
| client.registerMethod(name, url, method) # register the callback based method | |
| m = client.methods[name] | |
| # create promised version of m | |
| pm = () -> | |
| args = [].slice.call(arguments, 0) | |
| deferred = Q.defer() | |
| if _.isFunction args[args.length - 1] | |
| deferred.reject(new Error("cannot pass callback function to q.registerMethod")) | |
| else | |
| args.push((data, response) -> deferred.resolve(Q.all([Q(data), Q(response)]))) | |
| r = m.apply m, args | |
| r.on 'error', (err) -> deferred.reject(err) | |
| deferred.promise | |
| # registering the promised version of m | |
| client.q.methods[name] = pm | |
| oUnregisterMethod = client.unregisterMethod | |
| # overide existing unregister method so that both methods can be | |
| # deleted | |
| client.unregisterMethod = (name) -> | |
| oUnregisterMethod.call client, name | |
| delete client.q.methods[name] | |
| client |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Q = require 'q' | |
| asyncClient = require('./async-rest-client').asyncClient | |
| # Test | |
| Q.fcall(() -> | |
| console.log("[Test] rest client get request") | |
| ).then(() -> | |
| client = asyncClient() | |
| Q.allSettled([ | |
| client.q.get("https://public.opencpu.org/ocpu/library/").spread((data, response) -> console.log(data.substring(0, 100))), | |
| client.q.get("https://public.oopsnotpresent.org/ocpu/libr/").spread((data, response) -> console.log(data)).fail((err) -> console.error(err)), | |
| client.q.get("https://public.opencpu.org/ocpu/libr/").spread((data, response) -> console.log(data)).fail((err) -> console.error(err)) | |
| ]) | |
| ).done(() -> | |
| console.log("[Finished Test] get request\n---------------------") | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment