Permalink
Browse files

(#34) Support passing in a proxy URL.

  • Loading branch information...
aseemk committed Sep 11, 2012
1 parent fd56065 commit 29c3da4363c51ef80982ab4fc5a3f16c2e0ab71f
Showing with 18 additions and 8 deletions.
  1. +8 −3 lib/GraphDatabase._coffee
  2. +10 −5 lib/util.coffee
View
@@ -13,9 +13,14 @@ Relationship = require './Relationship'
Node = require './Node'
module.exports = class GraphDatabase
constructor: (url) ->
@url = url
@_request = util.wrapRequest url
constructor: (opts) ->
# normalize arg:
opts =
if typeof opts is 'string' then {url: opts}
else opts
{@url} = opts
@_request = util.wrapRequest opts
# Cache
@_root = null
View
@@ -15,11 +15,17 @@ USER_AGENT = "node-neo4j/#{lib.version}"
# - add a user-agent header with this library's info.
# - auto-set all requests and auto-parse all responses as JSON.
# returns a minimal wrapper (HTTP methods only) around request.
exports.wrapRequest = (url) ->
exports.wrapRequest = ({url, proxy}) ->
# default request opts where possible (no headers since the whole headers
# collection will be overridden if any one header is provided):
req = request.defaults
json: true
proxy: proxy
# parse auth info:
auth = URL.parse(url).auth
# helper function to modify args to request:
# helper function to modify args to request where defaults not possible:
modifyArgs = (args) ->
# the main arg may be just a string URL, or an options object.
# normalize it to an options object, and derive URL:
@@ -40,8 +46,7 @@ exports.wrapRequest = (url) ->
# now update the url arg and other options:
opts.url = opts.uri = url
opts.json or= true # preserve request data if present!
opts.headers or= {}
opts.headers or= {} # preserve existing headers
opts.headers['User-Agent'] = USER_AGENT
# finally, update and return the modified args:
@@ -53,7 +58,7 @@ exports.wrapRequest = (url) ->
for verb in ['get', 'post', 'put', 'del', 'head']
do (verb) -> # freaking closures!
wrapper[verb] = (args...) ->
request[verb].apply request, modifyArgs args
req[verb].apply req, modifyArgs args
# and return this set of wrapped methods:
return wrapper

0 comments on commit 29c3da4

Please sign in to comment.