Skip to content

handle proxy option in Client constructor + change connections callback signature #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.DS_Store
/node_modules
.idea/
npm-debug.log
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Authrep is a 'one-shot' operation to authorize an application and report the ass
```javascript
var Client = require('3scale').Client;

client = new Client("your provider key");
client = new Client("your provider key",["3Scale URL(default : su1.3scale.net)"],["proxy URL"]);

client.authrep({"app_id": "your application id", "app_key": "your application key", "usage": { "hits": 1 } }, function(response){
sys.log(sys.inspect(response));
Expand All @@ -52,8 +52,9 @@ var Client = require('3scale').Client;

client = new Client("your provider key");

client.authorize({"app_id": "your application id", "app_key": "your application key"}, function(response){
if (response.is_success()) {
client.authorize({"app_id": "your application id", "app_key": "your application key"}, function(error, response){
if(error) {console.err(error);}
else if (response.is_success()) {
var trans = [{"app_id": "your application id", "usage": {"hits": 3}}];
client.report(trans, function (response) {
console.log(response);
Expand Down Expand Up @@ -87,8 +88,9 @@ var Client = require('3scale').Client;

client = new Client("your provider key");

client.oauth_authorize({"app_id": "your application id"}, function(response){
if (response.is_success()) {
client.oauth_authorize({"app_id": "your application id"}, function(error, response){
if(error) {console.err(error);}
else if (response.is_success()) {
var trans = [{"app_id": "your application id", "usage": {"hits": 3}}];
client.report(trans, function (response) {
console.log(response);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
},
"dependencies": {
"libxmljs": "*",
"qs": "*"
"qs": "*",
"https-proxy-agent": "*"
},
"devDependencies": {
"coffee-script": "1.x",
Expand Down
49 changes: 38 additions & 11 deletions src/client.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ VERSION = require('../package.json').version

Response = require './response'
AuthorizeResponse = require './authorize_response'
HttpsProxyAgent = require 'https-proxy-agent'
agent = null

###
3Scale client API
Expand All @@ -19,11 +21,14 @@ AuthorizeResponse = require './authorize_response'
module.exports = class Client
DEFAULT_HEADERS: { "X-3scale-User-Agent": "plugin-node-v#{VERSION}" }

constructor: (provider_key, default_host = "su1.3scale.net") ->
constructor: (provider_key, default_host = "su1.3scale.net", proxyUrl) ->
unless provider_key?
throw new Error("missing provider_key")
@provider_key = provider_key
@host = default_host
if proxyUrl
agent = new HttpsProxyAgent(proxyUrl);


###
Authorize a application
Expand Down Expand Up @@ -62,6 +67,8 @@ module.exports = class Client
path: url + query
method: 'GET'
headers: @DEFAULT_HEADERS
if agent
req_opts.agent = agent

request = https.request req_opts, (response) ->
response.setEncoding 'utf8'
Expand All @@ -71,11 +78,13 @@ module.exports = class Client

response.on 'end', ->
if response.statusCode == 200 || response.statusCode == 409
callback _self._build_success_authorize_response xml
callback null, _self._build_success_authorize_response xml
else if response.statusCode in [400...409]
callback _self._build_error_response xml
callback null, _self._build_error_response xml
else
throw "[Client::authorize] Server Error Code: #{response.statusCode}"
request.on 'error', (err) ->
callback err
request.end()

###
Expand Down Expand Up @@ -110,6 +119,8 @@ module.exports = class Client
path: url + query
method: 'GET'
headers: @DEFAULT_HEADERS
if agent
req_opts.agent = agent

request = https.request req_opts, (response) ->
response.setEncoding 'utf8'
Expand All @@ -119,11 +130,13 @@ module.exports = class Client

response.on 'end', ->
if response.statusCode == 200 || response.statusCode == 409
callback _self._build_success_authorize_response xml
callback null, _self._build_success_authorize_response xml
else if response.statusCode in [400...409]
callback _self._build_error_response xml
callback null, _self._build_error_response xml
else
throw "[Client::oauth_authorize] Server Error Code: #{response.statusCode}"
request.on 'error', (err) ->
callback err
request.end()

###
Expand Down Expand Up @@ -159,6 +172,8 @@ module.exports = class Client
path: url + query
method: 'GET'
headers: @DEFAULT_HEADERS
if agent
req_opts.agent = agent

request = https.request req_opts, (response) ->
response.setEncoding 'utf8'
Expand All @@ -168,11 +183,13 @@ module.exports = class Client

response.on 'end', ->
if response.statusCode == 200 || response.statusCode == 409
callback _self._build_success_authorize_response xml
callback null, _self._build_success_authorize_response xml
else if response.statusCode in [400...409]
callback _self._build_error_response xml
callback null, _self._build_error_response xml
else
throw "[Client::authorize_with_user_key] Server Error Code: #{response.statusCode}"
request.on 'error', (err) ->
callback err
request.end()

###
Expand Down Expand Up @@ -207,6 +224,8 @@ module.exports = class Client
path: url + query
method: 'GET'
headers: @DEFAULT_HEADERS
if agent
req_opts.agent = agent

request = https.request req_opts, (response) ->
response.setEncoding 'utf8'
Expand All @@ -216,11 +235,13 @@ module.exports = class Client

response.on 'end', ->
if response.statusCode == 200 || response.statusCode == 409
callback _self._build_success_authorize_response xml
callback null, _self._build_success_authorize_response xml
else if response.statusCode in [400...409]
callback _self._build_error_response xml
callback null, _self._build_error_response xml
else
throw "[Client::authrep] Server Error Code: #{response.statusCode}"
request.on 'error', (err) ->
callback err
request.end()

###
Expand All @@ -242,6 +263,8 @@ module.exports = class Client
path: url + query
method: 'GET'
headers: @DEFAULT_HEADERS
if agent
req_opts.agent = agent

request = https.request req_opts, (response) ->
response.setEncoding 'utf8'
Expand All @@ -251,11 +274,13 @@ module.exports = class Client

response.on 'end', ->
if response.statusCode == 200 || response.statusCode == 409
callback _self._build_success_authorize_response xml
callback null, _self._build_success_authorize_response xml
else if response.statusCode in [400...409]
callback _self._build_error_response xml
callback null, _self._build_error_response xml
else
throw "[Client::authrep_with_user_key] Server Error Code: #{response.statusCode}"
request.on 'error', (err) ->
callback err
request.end()

###
Expand Down Expand Up @@ -310,6 +335,8 @@ module.exports = class Client
"host": @host
"Content-Type": "application/x-www-form-urlencoded"
"Content-Length": query.length
if agent
req_opts.agent = agent

req_opts.headers[key] = value for key, value of @DEFAULT_HEADERS

Expand Down