Skip to content

Commit

Permalink
can configure http/https
Browse files Browse the repository at this point in the history
  • Loading branch information
yanns committed Oct 12, 2015
1 parent 23d46ca commit 0494edb
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 16 deletions.
8 changes: 5 additions & 3 deletions src/coffee/connect/oauth2.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class OAuth2
# options - An {Object} to configure the service
# :config - {Object} It contains the credentials `project_key`, `client_id`, `client_secret`
# :host - {String} The host (default `auth.sphere.io`)
# :protocol - {String} The protocol (default `https`)
# :accessTokenUrl - {String} The url path to the token endpoint (default `/oauth/token`)
# :user_agent - {String} The request `User-Agent` (default `sphere-node-connect`)
# :timeout - {Number} The request timeout (default `20000`)
Expand All @@ -39,13 +40,14 @@ class OAuth2
throw new Error('Missing \'project_key\'') unless config.project_key

host = opts.host or 'auth.sphere.io'
baseUrl = opts.baseUrl or "https://#{host}"
protocol = opts.protocol or 'https'

rejectUnauthorized = if _.isUndefined(opts.rejectUnauthorized) then true else opts.rejectUnauthorized
userAgent = if _.isUndefined(opts.user_agent) then 'sphere-node-connect' else opts.user_agent
@_options =
config: config
baseUrl: baseUrl
host: host
protocol: protocol
accessTokenUrl: opts.accessTokenUrl or '/oauth/token'
timeout: opts.timeout or 20000
rejectUnauthorized: rejectUnauthorized
Expand All @@ -64,7 +66,7 @@ class OAuth2

payload = _.stringifyQuery(params)
request_options =
uri: "#{@_options.baseUrl}#{@_options.accessTokenUrl}"
uri: "#{@_options.protocol}://#{@_options.host}#{@_options.accessTokenUrl}"
auth:
user: @_options.config.client_id
pass: @_options.config.client_secret
Expand Down
16 changes: 9 additions & 7 deletions src/coffee/connect/rest.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ OAuth2 = require './oauth2'
# client_secret: "CLIENT_SECRET_HERE"
# project_key: "PROJECT_KEY_HERE"
# host: 'api.sphere.io' # optional
# baseUrl: 'https://api.sphere.io' # optional
# protocol: 'https' # optional
# access_token: '' # optional (if not provided it will automatically retrieve an `access_token`)
# timeout: 20000 # optional
# rejectUnauthorized: true # optional
# oauth_host: 'auth.sphere.io' # optional (used when retrieving the `access_token` internally)
# oauth_baseUrl: 'https://auth.sphere.io' # optional (used when retrieving the `access_token` internally)
# oauth_protocol: 'https' # optional (used when retrieving the `access_token` internally)
# user_agent: 'sphere-node-connect' # optional
class Rest

Expand All @@ -28,6 +28,7 @@ class Rest
# options - An {Object} to configure the service
# :config - {Object} It contains the credentials `project_key`, `client_id`, `client_secret`
# :host - {String} The host (default `api.sphere.io`)
# :protocol - {String} The protocol (default `https`)
# :user_agent - {String} The request `User-Agent` (default `sphere-node-connect`)
# :timeout - {Number} The request timeout (default `20000`)
# :rejectUnauthorized - {Boolean} Whether to reject clients with invalid certificates or not (default `true`)
Expand All @@ -42,24 +43,25 @@ class Rest
throw new Error('Missing \'project_key\'') unless config.project_key

host = opts.host or 'api.sphere.io'
baseUrl = opts.baseUrl or "https://#{host}"
protocol = opts.protocol or 'https'

rejectUnauthorized = if _.isUndefined(opts.rejectUnauthorized) then true else opts.rejectUnauthorized
userAgent = if _.isUndefined(opts.user_agent) then 'sphere-node-connect' else opts.user_agent
@_options =
config: config
baseUrl: baseUrl
host: host
protocol: protocol
access_token: opts.access_token or undefined
timeout: opts.timeout or 20000
rejectUnauthorized: rejectUnauthorized
headers:
'User-Agent': userAgent
@_options.uri = "#{@_options.baseUrl}/#{@_options.config.project_key}"
@_options.uri = "#{@_options.protocol}://#{@_options.host}/#{@_options.config.project_key}"

oauth_options = _.deepClone(opts)
_.extend oauth_options,
host: opts.oauth_host,
baseUrl: opts.oauth_baseUrl
protocol: opts.oauth_protocol
@_oauth = new OAuth2 oauth_options

if @_options.access_token
Expand Down Expand Up @@ -144,7 +146,7 @@ class Rest
uri: "#{@_options.uri}#{params.resource}"
json: true
method: params.method
baseUrl: @_options.baseUrl
host: @_options.host
headers: @_options.headers
timeout: @_options.timeout
rejectUnauthorized: @_options.rejectUnauthorized
Expand Down
11 changes: 9 additions & 2 deletions src/spec/connect/oauth2.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ describe 'OAuth2', ->
it 'should initialize with default options', ->
oa = new OAuth2 config: Config
expect(oa).toBeDefined()
expect(oa._options.baseUrl).toBe 'https://auth.sphere.io'
expect(oa._options.host).toBe 'auth.sphere.io'
expect(oa._options.protocol).toBe 'https'
expect(oa._options.accessTokenUrl).toBe '/oauth/token'
expect(oa._options.timeout).toBe 20000
expect(oa._options.rejectUnauthorized).toBe true
Expand All @@ -27,7 +28,13 @@ describe 'OAuth2', ->
oa = new OAuth2
config: Config
host: 'example.com'
expect(oa._options.baseUrl).toBe 'https://example.com'
expect(oa._options.host).toBe 'example.com'

it "should pass 'protocol' option", ->
oa = new OAuth2
config: Config
protocol: 'http'
expect(oa._options.protocol).toBe 'http'

it 'should pass \'accessTokenUrl\' option', ->
oa = new OAuth2
Expand Down
21 changes: 17 additions & 4 deletions src/spec/connect/rest.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ describe 'Rest', ->
rest = new Rest config: Config
expect(rest).toBeDefined()
expect(rest._oauth).toBeDefined()
expect(rest._options.baseUrl).toBe 'https://api.sphere.io'
expect(rest._options.host).toBe 'api.sphere.io'
expect(rest._options.protocol).toBe 'https'
expect(rest._options.access_token).not.toBeDefined()
expect(rest._options.uri).toBe "https://api.sphere.io/#{Config.project_key}"
expect(rest._options.timeout).toBe 20000
Expand All @@ -31,7 +32,13 @@ describe 'Rest', ->
rest = new Rest
config: Config
host: 'example.com'
expect(rest._options.baseUrl).toBe 'https://example.com'
expect(rest._options.host).toBe 'example.com'

it 'should pass \'protocol\' option', ->
rest = new Rest
config: Config
protocol: 'http'
expect(rest._options.protocol).toBe 'http'

it 'should pass \'access_token\' option', ->
rest = new Rest
Expand All @@ -55,7 +62,13 @@ describe 'Rest', ->
rest = new Rest
config: Config
oauth_host: 'auth.escemo.com'
expect(rest._oauth._options.baseUrl).toBe 'https://auth.escemo.com'
expect(rest._oauth._options.host).toBe 'auth.escemo.com'

it 'should pass \'oauth_protocol\' option', ->
rest = new Rest
config: Config
oauth_protocol: 'http'
expect(rest._oauth._options.protocol).toBe 'http'

it 'should pass \'user_agent\' option', ->
rest = new Rest
Expand Down Expand Up @@ -85,7 +98,7 @@ describe 'Rest', ->
uri: "https://api.sphere.io/#{Config.project_key}#{endpoint}"
json: true
method: method
baseUrl: 'https://api.sphere.io'
host: 'api.sphere.io'
headers:
'User-Agent': 'sphere-node-connect'
'Authorization': 'Bearer foo'
Expand Down

0 comments on commit 0494edb

Please sign in to comment.