Skip to content

Commit

Permalink
Merge pull request #38 from 3scale/serviceIdUpdate
Browse files Browse the repository at this point in the history
Service id update
  • Loading branch information
davidor committed Feb 1, 2017
2 parents 73339d7 + 173d4dc commit 32c5855
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 44 deletions.
22 changes: 12 additions & 10 deletions .travis.yml
Expand Up @@ -2,20 +2,22 @@ language: node_js
sudo: false
cache: npm
node_js:
- "0.10"
- "0.12"
- "4"
- '0.10'
- '0.12'
- '4'
before_install:
- npm install -g npm@2
- npm install -g npm@2
env:
global:
- CXX=g++-4.8
- TEST_3SCALE_APP_ID=4d4b20b9
- TEST_3SCALE_APP_KEY=ecce202ecc2eb8dc7a499c34a34d5987
- secure: B/3oWYZZttrXVM5IFn2evSuWMOQqEOOMol3nEQ2vEDGQ6rdGjpYLFDO/qMDPA8Af6Vt72lOFKkgzivuBp8nSd4S1YRTazilMle4zCJiJxzPs69kRDVXf/Mn8Qr/YrmgVzhblvdNqbKxTGrYyvDziPkliQKtIhqyILm3d6+CqmYw=
- CXX=g++-4.8
- TEST_3SCALE_APP_ID=4d4b20b9
- TEST_3SCALE_APP_KEY=ecce202ecc2eb8dc7a499c34a34d5987
- secure: B/3oWYZZttrXVM5IFn2evSuWMOQqEOOMol3nEQ2vEDGQ6rdGjpYLFDO/qMDPA8Af6Vt72lOFKkgzivuBp8nSd4S1YRTazilMle4zCJiJxzPs69kRDVXf/Mn8Qr/YrmgVzhblvdNqbKxTGrYyvDziPkliQKtIhqyILm3d6+CqmYw=
- secure: gvoSRZEpQaq/8MhuK/pMevm9mMXsaPoX5OkUA475lSsuAUIla6zbfGynM+a4aibPXsQkIxUG1pfwk3K1JjvsYaAfHLIQreX37mWicXRhY9BItZMzTD8AxW70/z03Y3ifIkTZdkEzKzZ9hLiJ4X2WYJvSJr72fWAjtvOJOKk+0+A=

addons:
apt:
sources:
- ubuntu-toolchain-r-test
- ubuntu-toolchain-r-test
packages:
- g++-4.8
- g++-4.8
28 changes: 15 additions & 13 deletions README.md
Expand Up @@ -26,6 +26,8 @@ This plugin supports the 3 main calls to the 3scale backend:

## Usage

> NOTE: From November 2016 `service_id` is mandatory.
### Authrep

Authrep is a 'one-shot' operation to authorize an application and report the associated transaction at the same time. The main difference between this call and the regular **authorize** call is that usage will be reported if the authorization is successful. Read more about authrep at the active docs page on the [3scale's support site](https://support.3scale.net/reference/active-docs).
Expand All @@ -36,7 +38,7 @@ var Client = require('3scale').Client;

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

client.authrep({"app_id": "your application id", "app_key": "your application key", "usage": { "hits": 1 } }, function(response){
client.authrep({ "service_id": "your service id", "app_id": "your application id", "app_key": "your application key", "usage": { "hits": 1 } }, function(response){
console.log(response);
});
```
Expand All @@ -48,7 +50,7 @@ var Client = require('3scale').Client;

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

client.authrep_with_user_key({ "user_key": "your key", "usage": { "hits": 1 } }, function(response){
client.authrep_with_user_key({ "service_id": "your service id", "user_key": "your key", "usage": { "hits": 1 } }, function(response){
console.log(response);
});
```
Expand All @@ -63,10 +65,10 @@ 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){
client.authorize({ "service_id": "your service id", "app_id": "your application id", "app_key": "your application key" }, function(response){
if (response.is_success()) {
var trans = [{ "app_id": "your application id", "usage": { "hits": 3 } }];
client.report(trans, function (response) {
client.report("your service id", trans, function (response) {
console.log(response);
});
}
Expand All @@ -83,10 +85,10 @@ var Client = require('3scale').Client;

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

client.authorize_with_user_key({ "user_key": "your key" }, function(response){
client.authorize_with_user_key({ "service_id": "your service id", "user_key": "your key" }, function(response){
if (response.is_success()) {
var trans = [{ "user_key": "your key", "usage": { "hits": 3 }}];
client.report(trans, function (response) {
var trans = [{ "user_key": "your key", "usage": { "hits": 3 } }];
client.report("your service id", trans, function (response) {
console.log(response);
});
}
Expand All @@ -100,11 +102,11 @@ Note that the **report** method supports sending the usage for multiple transact

```javascript
var trans = [
{ "app_id": "your application id", "usage": {"hits": 1}},
{ "app_id": "your application id", "usage": {"hits": 1000}}
{ "app_id": "your application id", "usage": {"hits": 1} },
{ "app_id": "your application id", "usage": {"hits": 1000} }
]

client.report(trans, function(response){
client.report("your service id", trans, function(response){
console.log(response);
});
```
Expand All @@ -118,10 +120,10 @@ var Client = require('3scale').Client;

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

client.oauth_authorize({"app_id": "your application id"}, function(response){
client.oauth_authorize({ "service_id": "your service id", "app_id": "your application id" }, function(response){
if (response.is_success()) {
var trans = [{"app_id": "your application id", "usage": {"hits": 3}}];
client.report(trans, function (response) {
var trans = [{ "app_id": "your application id", "usage": {"hits": 3} }];
client.report("your service id", trans, function (response) {
console.log(response);
});
}
Expand Down
35 changes: 24 additions & 11 deletions src/client.coffee
Expand Up @@ -38,14 +38,15 @@ module.exports = class Client
Parameters:
options is a Hash object with the following fields
app_id Required
app_key Required
service_id Required (from November 2016)
app_key Optional
referrer Optional
usage Optional
callback {Function} Is the callback function that receives the Response object which includes `is_success`
method to determine the status of the response
Example:
client.authorize {app_id: '75165984', app_key: '3e05c797ef193fee452b1ddf19defa74'}, (response) ->
client.authorize {service_id: '1234567890987', app_id: 'ca5c5a49'}, (response) ->
if response.is_success
# All Ok
else
Expand All @@ -56,7 +57,7 @@ module.exports = class Client
_self = this
result = null

if (typeof options isnt 'object') || (options.app_id is undefined)
if (typeof options isnt 'object') || (options.app_id is undefined)
throw "missing app_id"

url = "/transactions/authorize.xml?"
Expand Down Expand Up @@ -90,12 +91,12 @@ module.exports = class Client
Parameters:
options is a Hash object with the following fields
app_id Required
service_id Optional (In case of mmultiple services)
service_id Required (from November 2016)
callback {Function} Is the callback function that receives the Response object which includes `is_success`
method to determine the status of the response
Example:
client.oauth_authorize {app_id: '75165984', (response) ->
client.oauth_authorize {service_id: '1234567890987', app_id: 'ca5c5a49'}, (response) ->
if response.is_success
# All Ok
else
Expand Down Expand Up @@ -138,12 +139,12 @@ module.exports = class Client
Parameters:
options is a Hash object with the following fields
user_key Required
service_id Optional (In case of mmultiple services)
service_id Required (from November 2016)
callback {Function} Is the callback function that receives the Response object which includes `is_success`
method to determine the status of the response
Example:
client.authorize_with_user_key {user_key: '123456', (response) ->
client.authorize_with_user_key {service_id: '1234567890987', user_key: 'ca5c5a49'}, (response) ->
if response.is_success
# All Ok
else
Expand Down Expand Up @@ -186,12 +187,13 @@ module.exports = class Client
Authorize and Report in single call
options is a Hash object with the following fields
app_id Required
app_key, user_id, object, usage, no-body, service_id Optional
service_id Required (from November 2016)
app_key, user_id, object, usage, no-body
callback {Function} Is the callback function that receives the Response object which includes `is_success`
method to determine the status of the response
Example:
client.authrep {app_id: '75165984', (response) ->
client.authrep {service_id: '1234567890987', app_id: 'ca5c5a49'}, (response) ->
if response.is_success
# All Ok
else
Expand Down Expand Up @@ -232,7 +234,18 @@ module.exports = class Client

###
Authorize and Report with :user_key
options is a Hash object with the following fields
user_key Required
service_id Required (from November 2016)
callback {Function} Is the callback function that receives the Response object which includes `is_success`
method to determine the status of the response
Example:
client.authrep_with_user_key {service_id: '1234567890987', user_key: 'ca5c5a49'}, (response) ->
if response.is_success
# All Ok
else
sys.puts "#{response.error_message} with code: #{response.error_code}"
###
authrep_with_user_key: (options, callback) ->
_self = this
Expand Down Expand Up @@ -269,7 +282,7 @@ module.exports = class Client
Report transaction(s).
Parameters:
service_id {String} Optional (required only if you have more than one service)
service_id {String} Required (from November 2016)
trans {Array} each array element contain information of a transaction. That information is in a Hash in the form
{
app_id {String} Required
Expand All @@ -284,7 +297,7 @@ module.exports = class Client
{ "app_id": "abc123", "usage": {"hits": 1000}}
]
client.report trans, (response) ->
client.report "your service id", trans, (response) ->
if response.is_success
# All Ok
else
Expand Down
14 changes: 7 additions & 7 deletions test/client_test.coffee
Expand Up @@ -5,7 +5,7 @@ trans = [
{ 'app_id': 'foo', 'usage': { 'hits': 1 } },
{ 'app_id': 'foo', 'usage': { 'hits': 1000 } }
]
report_test = {transactions: trans, provider_key: '1234abcd'}
report_test = {service_id: '1234567890987', transactions: trans, provider_key: '1234abcd'}

Client = require('../src/client')

Expand Down Expand Up @@ -67,23 +67,23 @@ describe 'Basic test for the 3Scale::Client', ->
it 'should call the callback with a successful response', (done) ->
nock('https://su1.3scale.net')
.get('/transactions/authorize.xml')
.query({ app_key: 'bar', app_id: 'foo', provider_key: '1234abcd' })
.query({ service_id: '1234567890987', app_key: 'bar', app_id: 'foo', provider_key: '1234abcd' })
.reply(200, '<status><authorized>true</authorized><plan>Basic</plan></status>')

client = new Client '1234abcd'
client.authorize {app_key: 'bar', app_id: 'foo'}, (response) ->
client.authorize {service_id: '1234567890987', app_key: 'bar', app_id: 'foo'}, (response) ->
assert response.is_success()
assert.equal response.status_code, 200
done()

it 'should call the callback with a error response if app_id was wrong', (done) ->
nock('https://su1.3scale.net')
.get('/transactions/authorize.xml')
.query({ app_key: 'bar', app_id: 'ERROR', provider_key: '1234abcd' })
.query({ service_id: '1234567890987', app_key: 'bar', app_id: 'ERROR', provider_key: '1234abcd' })
.reply(403, '<error code="application_not_found">application with id="ERROR" was not found</error>')

client = new Client '1234abcd'
client.authorize {app_key: 'bar', app_id: 'ERROR'}, (response) ->
client.authorize {service_id: '1234567890987', app_key: 'bar', app_id: 'ERROR'}, (response) ->
assert.equal response.is_success(), false
assert.equal response.status_code, 403
done()
Expand Down Expand Up @@ -113,11 +113,11 @@ describe 'Basic test for the 3Scale::Client', ->
'X-3scale-User-Agent': 'plugin-node-v' + require('../package.json').version

match = nock('https://su1.3scale.net', opts)
.get('/transactions/authorize.xml?app_id=foo&provider_key=1234abcd')
.get('/transactions/authorize.xml?service_id=1234567890987&app_id=foo&provider_key=1234abcd')
.reply(200, '<status><authorized>true</authorized><plan>Basic</plan></status>')

client = new Client '1234abcd'
client.authorize { app_id: 'foo' }, (response) ->
client.authorize { service_id: '1234567890987', app_id: 'foo' }, (response) ->
assert match.isDone()
done()

Expand Down
7 changes: 4 additions & 3 deletions test/integration_test.coffee
Expand Up @@ -5,27 +5,28 @@ assert = require 'assert'
provider_key = process.env.TEST_3SCALE_PROVIDER_KEY
application_key = process.env.TEST_3SCALE_APP_KEY
application_id = process.env.TEST_3SCALE_APP_ID
service_id = process.env.TEST_3SCALE_SERVICE_ID

trans = [
{ 'app_id': application_id, 'usage': { 'hits': 1 } },
{ 'app_id': application_id, 'usage': { 'hits': 1000 } }
]
report_test = {transactions: trans, provider_key: provider_key}
report_test = {service_id: service_id, transactions: trans, provider_key: provider_key}

Client = require('../src/client')

describe 'Integration tests for the 3Scale::Client', ->
describe 'The authorize method', ->
it 'should call the callback with a successful response', (done) ->
client = new Client provider_key
client.authorize {app_key: application_key, app_id: application_id}, (response) ->
client.authorize {service_id: service_id, app_key: application_key, app_id: application_id}, (response) ->
assert response.is_success()
assert.equal response.status_code, 200
done()

it 'should call the callback with a error response if app_id was wrong', (done) ->
client = new Client provider_key
client.authorize {app_key: application_key, app_id: 'ERROR'}, (response) ->
client.authorize {service_id: service_id, app_key: application_key, app_id: 'ERROR'}, (response) ->
assert.equal response.is_success(), false
assert.equal response.status_code, 404
done()
Expand Down

0 comments on commit 32c5855

Please sign in to comment.