3scale_client 2.3.3 → 2.3.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.travis.yml +3 -4
- data/README.md +237 -0
- data/lib/3scale/authorize_response.rb +2 -1
- data/lib/3scale/client.rb +24 -12
- data/lib/3scale/client/version.rb +1 -1
- data/test/client_test.rb +18 -0
- metadata +18 -38
- data/README.rdoc +0 -163
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6f95988b20e7b5a97d35e78533ebcf14c1fbbe79
|
4
|
+
data.tar.gz: c53f311c11d7026ebd7209623623ae7647c17826
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3f1952038035863de0ee38e63eff23f16f32bdb2d779ada36193b9f1ea204331743c99fb6c9d8004f8f9fe8cae27dc1905d4f9bbcb1b28fd796e8e32ee9bfe48
|
7
|
+
data.tar.gz: a8f67924fb05935f4113102b41fcb321fbec2fca212ff2a5bab234b12cb3264e3e05a7d43b947b1bdccadf379490deb6daf07f7e6fb270093a6ccc5b470b2798
|
data/.travis.yml
CHANGED
data/README.md
ADDED
@@ -0,0 +1,237 @@
|
|
1
|
+
# Rubygem for 3scale Web Service Management API
|
2
|
+
|
3
|
+
|
4
|
+
[<img src="https://secure.travis-ci.org/3scale/3scale_ws_api_for_ruby.png?branch=master" alt="Build Status" />](http://travis-ci.org/3scale/3scale_ws_api_for_ruby)
|
5
|
+
|
6
|
+
3scale is an API Infrastructure service which handles API Keys, Rate Limiting, Analytics, Billing Payments and Developer Management. Includes a configurable API dashboard and developer portal CMS. More product stuff at http://www.3scale.net/, support information at http://support.3scale.net/.
|
7
|
+
|
8
|
+
### Tutorials
|
9
|
+
Plugin Setup: https://support.3scale.net/howtos/api-configuration/plugin-setup
|
10
|
+
Rate Limiting: https://support.3scale.net/howtos/basics/provision-rate-limits
|
11
|
+
Analytics Setup: https://support.3scale.net/quickstarts/3scale-api-analytics
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
This library is distributed as a gem:
|
16
|
+
```sh
|
17
|
+
gem install 3scale_client
|
18
|
+
```
|
19
|
+
Or alternatively, download the source code from github:
|
20
|
+
http://github.com/3scale/3scale_ws_api_for_ruby
|
21
|
+
|
22
|
+
If you are using Bundler, please add this to your Gemfile:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
gem '3scale_client'
|
26
|
+
```
|
27
|
+
and do a bundle install.
|
28
|
+
|
29
|
+
If you are using Rails' config.gems, put this into your config/environment.rb
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
config.gem '3scale_client'
|
33
|
+
```
|
34
|
+
Otherwise, require the gem in whatever way is natural to your framework of choice.
|
35
|
+
|
36
|
+
## Usage
|
37
|
+
|
38
|
+
First, create an instance of the client, giving it your provider API key:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
client = ThreeScale::Client.new(:provider_key => "your provider key")
|
42
|
+
```
|
43
|
+
Because the object is stateless, you can create just one and store it globally.
|
44
|
+
|
45
|
+
### Authrep
|
46
|
+
|
47
|
+
Authrep is a 'one-shot' operation to authorize an application and report the associated transaction at the same time.
|
48
|
+
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/activedocs#operation/66)
|
49
|
+
|
50
|
+
You can make request to this backend operation like this:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
response = client.authrep(:app_id => "the app id", :app_key => "the app key")
|
54
|
+
```
|
55
|
+
|
56
|
+
Then call the +success?+ method on the returned object to see if the authorization was
|
57
|
+
successful.
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
if response.success?
|
61
|
+
# All fine, the usage will be reported automatically. Proceeed.
|
62
|
+
else
|
63
|
+
# Something's wrong with this application.
|
64
|
+
end
|
65
|
+
```
|
66
|
+
|
67
|
+
The example is using the app_id authentication pattern, but you can also use other patterns.
|
68
|
+
|
69
|
+
#### A rails example
|
70
|
+
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
class ApplicationController < ActionController
|
74
|
+
# Call the authenticate method on each request to the API
|
75
|
+
before_filter :authenticate
|
76
|
+
|
77
|
+
# You only need to instantiate a new Client once and store it as a global variable
|
78
|
+
# You should store your provider key in the environment because this key is secret!
|
79
|
+
def create_client
|
80
|
+
@@threescale_client ||= ThreeScale::Client.new(:provider_key => ENV['PROVIDER_KEY'])
|
81
|
+
end
|
82
|
+
|
83
|
+
# To record usage, create a new metric in your application plan. You will use the
|
84
|
+
# "system name" that you specifed on the metric/method to pass in as the key to the usage hash.
|
85
|
+
# The key needs to be a symbol.
|
86
|
+
# A way to pass the metric is to add a parameter that will pass the name of the metric/method along
|
87
|
+
def authenticate
|
88
|
+
response = create_client.authrep(:app_id => params["app_id"],
|
89
|
+
:app_key => params["app_key"],
|
90
|
+
:usage => { params[:metric].to_sym => 1 }
|
91
|
+
if response.success?
|
92
|
+
return true
|
93
|
+
# All fine, the usage will be reported automatically. Proceeed.
|
94
|
+
else
|
95
|
+
# Something's wrong with this application.
|
96
|
+
puts "#{response.error_message}"
|
97
|
+
# raise error
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
```
|
102
|
+
|
103
|
+
### Using Varnish to speed up things
|
104
|
+
|
105
|
+
3scale provides a [varnish module](https://github.com/3scale/libvmod-3scale) to cache the responses of its backend to help you achieve a close to zero latency. Go and install the module and [configure it the easy way](https://github.com/3scale/libvmod-3scale/blob/master/vcl/default_3scale_simple.vcl)
|
106
|
+
|
107
|
+
When that's done all you have to do is to initialize your 3scale client pointing to the location of your varnish, by passing the host parameter to it, like this:
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
client = ThreeScale::Client.new(:provider_key => "your provider key", :host => "your.varnish.net:8080")
|
111
|
+
```
|
112
|
+
|
113
|
+
that's it, your API should now be authorized and reported for you, and all that at full speed.
|
114
|
+
|
115
|
+
### Authorize
|
116
|
+
|
117
|
+
To authorize an application, call the +authorize+ method passing it the application's id and
|
118
|
+
optionally a key:
|
119
|
+
|
120
|
+
```ruby
|
121
|
+
response = client.authorize(:app_id => "the app id", :app_key => "the app key")
|
122
|
+
```
|
123
|
+
|
124
|
+
Then call the +success?+ method on the returned object to see if the authorization was
|
125
|
+
successful.
|
126
|
+
|
127
|
+
```ruby
|
128
|
+
if response.success?
|
129
|
+
# All fine, the usage will be reported automatically. Proceeed.
|
130
|
+
else
|
131
|
+
# Something's wrong with this application.
|
132
|
+
end
|
133
|
+
```
|
134
|
+
|
135
|
+
If both provider key and app id are valid, the response object contains additional
|
136
|
+
information about the status of the application:
|
137
|
+
|
138
|
+
```ruby
|
139
|
+
# Returns the name of the plan the application is signed up to.
|
140
|
+
response.plan
|
141
|
+
```
|
142
|
+
|
143
|
+
If the plan has defined usage limits, the response contains details about the usage broken
|
144
|
+
down by the metrics and usage limit periods.
|
145
|
+
|
146
|
+
```ruby
|
147
|
+
# The usage_reports array contains one element per each usage limit defined on the plan.
|
148
|
+
usage_report = response.usage_reports[0]
|
149
|
+
|
150
|
+
# The metric
|
151
|
+
usage_report.metric # "hits"
|
152
|
+
|
153
|
+
# The period the limit applies to
|
154
|
+
usage_report.period # :day
|
155
|
+
usage_report.period_start # "Wed Apr 28 00:00:00 +0200 2010"
|
156
|
+
usage_report.period_end # "Wed Apr 28 23:59:59 +0200 2010"
|
157
|
+
|
158
|
+
# The current value the application already consumed in the period
|
159
|
+
usage_report.current_value # 8032
|
160
|
+
|
161
|
+
# The maximal value allowed by the limit in the period
|
162
|
+
usage_report.max_value # 10000
|
163
|
+
|
164
|
+
# If the limit is exceeded, this will be true, otherwise false:
|
165
|
+
usage_report.exceeded? # false
|
166
|
+
```
|
167
|
+
|
168
|
+
If the authorization failed, the +error_code+ returns system error code and +error_message+
|
169
|
+
human readable error description:
|
170
|
+
|
171
|
+
```ruby
|
172
|
+
response.error_code # "usage_limits_exceeded"
|
173
|
+
response.error_message # "Usage limits are exceeded"
|
174
|
+
```
|
175
|
+
|
176
|
+
### OAuth Authorize
|
177
|
+
|
178
|
+
To authorize an application with OAuth, call the +oauth_authorize+ method passing it the application's id.
|
179
|
+
|
180
|
+
```ruby
|
181
|
+
response = client.oauth_authorize(:app_id => "the app id")
|
182
|
+
```
|
183
|
+
|
184
|
+
If the authorization is successful, the response will contain the +app_key+ and +redirect_url+ defined for this application:
|
185
|
+
|
186
|
+
```ruby
|
187
|
+
response.app_key
|
188
|
+
response.redirect_url
|
189
|
+
```
|
190
|
+
|
191
|
+
### Report
|
192
|
+
|
193
|
+
To report usage, use the +report+ method. You can report multiple transaction at the same time:
|
194
|
+
|
195
|
+
```ruby
|
196
|
+
response = client.report({:app_id => "first app id", :usage => {'hits' => 1}},
|
197
|
+
{:app_id => "second app id", :usage => {'hits' => 1}})
|
198
|
+
```
|
199
|
+
|
200
|
+
The :app_id and :usage parameters are required. Additionaly, you can specify a timestamp
|
201
|
+
of transaction:
|
202
|
+
|
203
|
+
```ruby
|
204
|
+
response = client.report({:app_id => "app id", :usage => {'hits' => 1},
|
205
|
+
:timestamp => Time.local(2010, 4, 28, 12, 36)})
|
206
|
+
```
|
207
|
+
|
208
|
+
The timestamp can be either a Time object (from ruby's standard library) or something that
|
209
|
+
"quacks" like it (for example, the ActiveSupport::TimeWithZone from Rails) or a string. The
|
210
|
+
string has to be in a format parseable by the Time.parse method. For example:
|
211
|
+
|
212
|
+
```ruby
|
213
|
+
"2010-04-28 12:38:33 +0200"
|
214
|
+
```
|
215
|
+
|
216
|
+
If the timestamp is not in UTC, you have to specify a time offset. That's the "+0200"
|
217
|
+
(two hours ahead of the Universal Coordinate Time) in the example abowe.
|
218
|
+
|
219
|
+
Then call the +success?+ method on the returned response object to see if the report was
|
220
|
+
successful.
|
221
|
+
|
222
|
+
```ruby
|
223
|
+
if response.success?
|
224
|
+
# All OK.
|
225
|
+
else
|
226
|
+
# There was an error.
|
227
|
+
end
|
228
|
+
```
|
229
|
+
|
230
|
+
In case of error, the +error_code+ returns system error code and +error_message+
|
231
|
+
human readable error description:
|
232
|
+
|
233
|
+
```ruby
|
234
|
+
response.error_code # "provider_key_invalid"
|
235
|
+
response.error_message # "provider key \"foo\" is invalid"
|
236
|
+
```
|
237
|
+
|
@@ -10,6 +10,7 @@ module ThreeScale
|
|
10
10
|
attr_accessor :plan
|
11
11
|
attr_accessor :app_key
|
12
12
|
attr_accessor :redirect_url
|
13
|
+
attr_accessor :service_id
|
13
14
|
|
14
15
|
class UsageReport
|
15
16
|
attr_reader :metric
|
@@ -42,4 +43,4 @@ module ThreeScale
|
|
42
43
|
@usage_reports << UsageReport.new(options)
|
43
44
|
end
|
44
45
|
end
|
45
|
-
end
|
46
|
+
end
|
data/lib/3scale/client.rb
CHANGED
@@ -158,9 +158,10 @@ module ThreeScale
|
|
158
158
|
#
|
159
159
|
# Hash with options:
|
160
160
|
#
|
161
|
-
# app_id::
|
162
|
-
# app_key::
|
163
|
-
#
|
161
|
+
# app_id:: id of the application to authorize. This is required.
|
162
|
+
# app_key:: secret key assigned to the application. Required only if application has
|
163
|
+
# a key defined.
|
164
|
+
# service_id:: id of the service (required if you have more than one service)
|
164
165
|
#
|
165
166
|
# == Return
|
166
167
|
#
|
@@ -182,10 +183,7 @@ module ThreeScale
|
|
182
183
|
# end
|
183
184
|
#
|
184
185
|
def authorize(options)
|
185
|
-
path = "/transactions/authorize.xml" +
|
186
|
-
"?provider_key=#{CGI.escape(provider_key)}" +
|
187
|
-
"&app_id=#{CGI.escape(options[:app_id].to_s)}"
|
188
|
-
path += "&app_key=#{CGI.escape(options[:app_key])}" if options[:app_key]
|
186
|
+
path = "/transactions/authorize.xml" + options_to_params(options, ALL_PARAMS)
|
189
187
|
|
190
188
|
uri = URI.parse("http://#{host}#{path}")
|
191
189
|
http_response = Net::HTTP.get_response(uri)
|
@@ -207,6 +205,7 @@ module ThreeScale
|
|
207
205
|
# Hash with options:
|
208
206
|
#
|
209
207
|
# app_id:: id of the application to authorize. This is required.
|
208
|
+
# service_id:: id of the service (required if you have more than one service)
|
210
209
|
#
|
211
210
|
# == Return
|
212
211
|
#
|
@@ -231,11 +230,7 @@ module ThreeScale
|
|
231
230
|
# end
|
232
231
|
#
|
233
232
|
def oauth_authorize(options)
|
234
|
-
path = "/transactions/oauth_authorize.xml" +
|
235
|
-
"?provider_key=#{CGI.escape(provider_key)}" +
|
236
|
-
"&app_id=#{CGI.escape(options[:app_id].to_s)}"
|
237
|
-
path += "&app_key=#{CGI.escape(options[:app_key])}" if options[:app_key]
|
238
|
-
path += "&redirect_url=#{CGI.escape(options[:redirect_url])}" if options[:redirect_url]
|
233
|
+
path = "/transactions/oauth_authorize.xml" + options_to_params(options, OAUTH_PARAMS)
|
239
234
|
|
240
235
|
uri = URI.parse("http://#{host}#{path}")
|
241
236
|
http_response = Net::HTTP.get_response(uri)
|
@@ -252,6 +247,23 @@ module ThreeScale
|
|
252
247
|
|
253
248
|
private
|
254
249
|
|
250
|
+
OAUTH_PARAMS = [:app_id, :app_key, :service_id, :redirect_url]
|
251
|
+
ALL_PARAMS = [:user_key, :app_id, :app_key, :service_id, :redirect_url]
|
252
|
+
|
253
|
+
def options_to_params(options, allowed_keys)
|
254
|
+
params = { :provider_key => provider_key }
|
255
|
+
|
256
|
+
allowed_keys.each do |key|
|
257
|
+
params[key] = options[key] if options.has_key?(key)
|
258
|
+
end
|
259
|
+
|
260
|
+
tuples = params.map do |key, value|
|
261
|
+
"#{key}=#{CGI.escape(value.to_s)}"
|
262
|
+
end
|
263
|
+
|
264
|
+
'?' + tuples.join('&')
|
265
|
+
end
|
266
|
+
|
255
267
|
def encode_transactions(transactions)
|
256
268
|
result = {}
|
257
269
|
|
data/test/client_test.rb
CHANGED
@@ -55,6 +55,12 @@ class ThreeScale::ClientTest < Test::Unit::TestCase
|
|
55
55
|
@client.authrep(:app_id => "appid", :app_key => "appkey")
|
56
56
|
end
|
57
57
|
|
58
|
+
def test_authrep_supports_service_id
|
59
|
+
assert_authrep_url_with_params "&%5Busage%5D%5Bhits%5D=1&service_id=serviceid"
|
60
|
+
|
61
|
+
@client.authrep(:service_id => "serviceid")
|
62
|
+
end
|
63
|
+
|
58
64
|
#TODO these authrep tests
|
59
65
|
def test_authrep_supports_api_key_auth_mode; end
|
60
66
|
def test_authrep_log_is_encoded;end
|
@@ -115,6 +121,18 @@ class ThreeScale::ClientTest < Test::Unit::TestCase
|
|
115
121
|
assert response.success?
|
116
122
|
end
|
117
123
|
|
124
|
+
def test_successful_authorize_with_user_key
|
125
|
+
body = '<status>
|
126
|
+
<authorized>true</authorized>
|
127
|
+
<plan>Ultimate</plan>
|
128
|
+
</status>'
|
129
|
+
|
130
|
+
FakeWeb.register_uri(:get, "http://#{@host}/transactions/authorize.xml?provider_key=1234abcd&user_key=foo", :status => ['200', 'OK'], :body => body)
|
131
|
+
|
132
|
+
response = @client.authorize(:user_key => 'foo')
|
133
|
+
assert response.success?
|
134
|
+
end
|
135
|
+
|
118
136
|
def test_authorize_with_exceeded_usage_limits
|
119
137
|
body = '<status>
|
120
138
|
<authorized>false</authorized>
|
metadata
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: 3scale_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.3.
|
5
|
-
prerelease:
|
4
|
+
version: 2.3.4
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Michal Cichra
|
@@ -13,12 +12,11 @@ authors:
|
|
13
12
|
autorequire:
|
14
13
|
bindir: bin
|
15
14
|
cert_chain: []
|
16
|
-
date:
|
15
|
+
date: 2014-01-09 00:00:00.000000000 Z
|
17
16
|
dependencies:
|
18
17
|
- !ruby/object:Gem::Dependency
|
19
18
|
name: bundler
|
20
19
|
requirement: !ruby/object:Gem::Requirement
|
21
|
-
none: false
|
22
20
|
requirements:
|
23
21
|
- - ~>
|
24
22
|
- !ruby/object:Gem::Version
|
@@ -26,7 +24,6 @@ dependencies:
|
|
26
24
|
type: :development
|
27
25
|
prerelease: false
|
28
26
|
version_requirements: !ruby/object:Gem::Requirement
|
29
|
-
none: false
|
30
27
|
requirements:
|
31
28
|
- - ~>
|
32
29
|
- !ruby/object:Gem::Version
|
@@ -34,81 +31,71 @@ dependencies:
|
|
34
31
|
- !ruby/object:Gem::Dependency
|
35
32
|
name: rake
|
36
33
|
requirement: !ruby/object:Gem::Requirement
|
37
|
-
none: false
|
38
34
|
requirements:
|
39
|
-
- -
|
35
|
+
- - '>='
|
40
36
|
- !ruby/object:Gem::Version
|
41
37
|
version: '0'
|
42
38
|
type: :development
|
43
39
|
prerelease: false
|
44
40
|
version_requirements: !ruby/object:Gem::Requirement
|
45
|
-
none: false
|
46
41
|
requirements:
|
47
|
-
- -
|
42
|
+
- - '>='
|
48
43
|
- !ruby/object:Gem::Version
|
49
44
|
version: '0'
|
50
45
|
- !ruby/object:Gem::Dependency
|
51
46
|
name: rdoc
|
52
47
|
requirement: !ruby/object:Gem::Requirement
|
53
|
-
none: false
|
54
48
|
requirements:
|
55
|
-
- -
|
49
|
+
- - '>='
|
56
50
|
- !ruby/object:Gem::Version
|
57
51
|
version: '0'
|
58
52
|
type: :development
|
59
53
|
prerelease: false
|
60
54
|
version_requirements: !ruby/object:Gem::Requirement
|
61
|
-
none: false
|
62
55
|
requirements:
|
63
|
-
- -
|
56
|
+
- - '>='
|
64
57
|
- !ruby/object:Gem::Version
|
65
58
|
version: '0'
|
66
59
|
- !ruby/object:Gem::Dependency
|
67
60
|
name: fakeweb
|
68
61
|
requirement: !ruby/object:Gem::Requirement
|
69
|
-
none: false
|
70
62
|
requirements:
|
71
|
-
- -
|
63
|
+
- - '>='
|
72
64
|
- !ruby/object:Gem::Version
|
73
65
|
version: '0'
|
74
66
|
type: :development
|
75
67
|
prerelease: false
|
76
68
|
version_requirements: !ruby/object:Gem::Requirement
|
77
|
-
none: false
|
78
69
|
requirements:
|
79
|
-
- -
|
70
|
+
- - '>='
|
80
71
|
- !ruby/object:Gem::Version
|
81
72
|
version: '0'
|
82
73
|
- !ruby/object:Gem::Dependency
|
83
74
|
name: mocha
|
84
75
|
requirement: !ruby/object:Gem::Requirement
|
85
|
-
none: false
|
86
76
|
requirements:
|
87
|
-
- -
|
77
|
+
- - '>='
|
88
78
|
- !ruby/object:Gem::Version
|
89
79
|
version: '0'
|
90
80
|
type: :development
|
91
81
|
prerelease: false
|
92
82
|
version_requirements: !ruby/object:Gem::Requirement
|
93
|
-
none: false
|
94
83
|
requirements:
|
95
|
-
- -
|
84
|
+
- - '>='
|
96
85
|
- !ruby/object:Gem::Version
|
97
86
|
version: '0'
|
98
87
|
- !ruby/object:Gem::Dependency
|
99
88
|
name: nokogiri
|
100
89
|
requirement: !ruby/object:Gem::Requirement
|
101
|
-
none: false
|
102
90
|
requirements:
|
103
|
-
- -
|
91
|
+
- - '>='
|
104
92
|
- !ruby/object:Gem::Version
|
105
93
|
version: '0'
|
106
94
|
type: :runtime
|
107
95
|
prerelease: false
|
108
96
|
version_requirements: !ruby/object:Gem::Requirement
|
109
|
-
none: false
|
110
97
|
requirements:
|
111
|
-
- -
|
98
|
+
- - '>='
|
112
99
|
- !ruby/object:Gem::Version
|
113
100
|
version: '0'
|
114
101
|
description: This gem allows to easily connect an application that provides a Web
|
@@ -125,7 +112,7 @@ files:
|
|
125
112
|
- 3scale_client.gemspec
|
126
113
|
- Gemfile
|
127
114
|
- LICENCE
|
128
|
-
- README.
|
115
|
+
- README.md
|
129
116
|
- Rakefile
|
130
117
|
- VERSION
|
131
118
|
- lib/3scale/authorize_response.rb
|
@@ -138,33 +125,26 @@ files:
|
|
138
125
|
homepage: http://www.3scale.net
|
139
126
|
licenses:
|
140
127
|
- MIT
|
128
|
+
metadata: {}
|
141
129
|
post_install_message:
|
142
130
|
rdoc_options: []
|
143
131
|
require_paths:
|
144
132
|
- lib
|
145
133
|
required_ruby_version: !ruby/object:Gem::Requirement
|
146
|
-
none: false
|
147
134
|
requirements:
|
148
|
-
- -
|
135
|
+
- - '>='
|
149
136
|
- !ruby/object:Gem::Version
|
150
137
|
version: '0'
|
151
|
-
segments:
|
152
|
-
- 0
|
153
|
-
hash: -4299208069213055852
|
154
138
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
155
|
-
none: false
|
156
139
|
requirements:
|
157
|
-
- -
|
140
|
+
- - '>='
|
158
141
|
- !ruby/object:Gem::Version
|
159
142
|
version: '0'
|
160
|
-
segments:
|
161
|
-
- 0
|
162
|
-
hash: -4299208069213055852
|
163
143
|
requirements: []
|
164
144
|
rubyforge_project:
|
165
|
-
rubygems_version: 1.
|
145
|
+
rubygems_version: 2.1.11
|
166
146
|
signing_key:
|
167
|
-
specification_version:
|
147
|
+
specification_version: 4
|
168
148
|
summary: Client for 3scale Web Service Management System API
|
169
149
|
test_files:
|
170
150
|
- test/client_test.rb
|
data/README.rdoc
DELETED
@@ -1,163 +0,0 @@
|
|
1
|
-
= Rubygem for 3scale Web Service Management API
|
2
|
-
|
3
|
-
{<img src="https://secure.travis-ci.org/3scale/3scale_ws_api_for_ruby.png?branch=master" alt="Build Status" />}[http://travis-ci.org/3scale/3scale_ws_api_for_ruby]
|
4
|
-
|
5
|
-
3scale is an API Infrastructure service which handles API Keys, Rate Limiting, Analytics, Billing Payments and Developer Management. Includes a configurable API dashboard and developer portal CMS. More product stuff at http://www.3scale.net/, support information at http://support.3scale.net/.
|
6
|
-
|
7
|
-
== Installation
|
8
|
-
|
9
|
-
This library is distributed as a gem:
|
10
|
-
|
11
|
-
gem install 3scale_client
|
12
|
-
|
13
|
-
Or alternatively, download the source code from github:
|
14
|
-
http://github.com/3scale/3scale_ws_api_for_ruby
|
15
|
-
|
16
|
-
If you are using Bundler, please add this to your Gemfile:
|
17
|
-
|
18
|
-
gem '3scale_client'
|
19
|
-
|
20
|
-
and do a bundle install.
|
21
|
-
|
22
|
-
If you are using Rails' config.gems, put this into your config/environment.rb
|
23
|
-
|
24
|
-
config.gem '3scale_client'
|
25
|
-
|
26
|
-
Otherwise, require the gem in whatever way is natural to your framework of choice.
|
27
|
-
|
28
|
-
== Usage
|
29
|
-
|
30
|
-
First, create an instance of the client, giving it your provider API key:
|
31
|
-
|
32
|
-
client = ThreeScale::Client.new(:provider_key => "your provider key")
|
33
|
-
|
34
|
-
Because the object is stateless, you can create just one and store it globally.
|
35
|
-
|
36
|
-
==== Authrep
|
37
|
-
|
38
|
-
Authrep is a 'one-shot' operation to authorize an application and report the associated transaction at the same time.
|
39
|
-
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/activedocs#operation/66)
|
40
|
-
|
41
|
-
You can make request to this backend operation like this:
|
42
|
-
|
43
|
-
response = client.authrep(:app_id => "the app id", :app_key => "the app key")
|
44
|
-
|
45
|
-
Then call the +success?+ method on the returned object to see if the authorization was
|
46
|
-
successful.
|
47
|
-
|
48
|
-
if response.success?
|
49
|
-
# All fine, the usage will be reported automatically. Proceeed.
|
50
|
-
else
|
51
|
-
# Something's wrong with this application.
|
52
|
-
end
|
53
|
-
|
54
|
-
The example is using the app_id authentication pattern, but you can also use other patterns.
|
55
|
-
|
56
|
-
=== Using Varnish to speed up things
|
57
|
-
|
58
|
-
3scale provides a [varnish module](https://github.com/3scale/libvmod-3scale) to cache the responses of its backend to help you achieve a close to zero latency. Go and install the module and [configure it the easy way](https://github.com/3scale/libvmod-3scale/blob/master/vcl/default_3scale_simple.vcl)
|
59
|
-
|
60
|
-
When that's done all you have to do is to initialize your 3scale client pointing to the location of your varnish, by passing the host parameter to it, like this:
|
61
|
-
|
62
|
-
client = ThreeScale::Client.new(:provider_key => "your provider key", :host => "your.varnish.net:8080")
|
63
|
-
|
64
|
-
that's it, your API should now be authorized and reported for you, and all that at full speed.
|
65
|
-
|
66
|
-
=== Authorize
|
67
|
-
|
68
|
-
To authorize an application, call the +authorize+ method passing it the application's id and
|
69
|
-
optionally a key:
|
70
|
-
|
71
|
-
response = client.authorize(:app_id => "the app id", :app_key => "the app key")
|
72
|
-
|
73
|
-
Then call the +success?+ method on the returned object to see if the authorization was
|
74
|
-
successful.
|
75
|
-
|
76
|
-
if response.success?
|
77
|
-
# All fine, proceeed.
|
78
|
-
else
|
79
|
-
# Something's wrong with this application.
|
80
|
-
end
|
81
|
-
|
82
|
-
If both provider key and app id are valid, the response object contains additional
|
83
|
-
information about the status of the application:
|
84
|
-
|
85
|
-
# Returns the name of the plan the application is signed up to.
|
86
|
-
response.plan
|
87
|
-
|
88
|
-
If the plan has defined usage limits, the response contains details about the usage broken
|
89
|
-
down by the metrics and usage limit periods.
|
90
|
-
|
91
|
-
# The usage_reports array contains one element per each usage limit defined on the plan.
|
92
|
-
usage_report = response.usage_reports[0]
|
93
|
-
|
94
|
-
# The metric
|
95
|
-
usage_report.metric # "hits"
|
96
|
-
|
97
|
-
# The period the limit applies to
|
98
|
-
usage_report.period # :day
|
99
|
-
usage_report.period_start # "Wed Apr 28 00:00:00 +0200 2010"
|
100
|
-
usage_report.period_end # "Wed Apr 28 23:59:59 +0200 2010"
|
101
|
-
|
102
|
-
# The current value the application already consumed in the period
|
103
|
-
usage_report.current_value # 8032
|
104
|
-
|
105
|
-
# The maximal value allowed by the limit in the period
|
106
|
-
usage_report.max_value # 10000
|
107
|
-
|
108
|
-
# If the limit is exceeded, this will be true, otherwise false:
|
109
|
-
usage_report.exceeded? # false
|
110
|
-
|
111
|
-
If the authorization failed, the +error_code+ returns system error code and +error_message+
|
112
|
-
human readable error description:
|
113
|
-
|
114
|
-
response.error_code # "usage_limits_exceeded"
|
115
|
-
response.error_message # "Usage limits are exceeded"
|
116
|
-
|
117
|
-
=== OAuth Authorize
|
118
|
-
|
119
|
-
To authorize an application with OAuth, call the +oauth_authorize+ method passing it the application's id.
|
120
|
-
|
121
|
-
response = client.oauth_authorize(:app_id => "the app id")
|
122
|
-
|
123
|
-
If the authorization is successful, the response will contain the +app_key+ and +redirect_url+ defined for this application:
|
124
|
-
|
125
|
-
response.app_key
|
126
|
-
response.redirect_url
|
127
|
-
|
128
|
-
=== Report
|
129
|
-
|
130
|
-
To report usage, use the +report+ method. You can report multiple transaction at the same time:
|
131
|
-
|
132
|
-
response = client.report({:app_id => "first app id", :usage => {'hits' => 1}},
|
133
|
-
{:app_id => "second app id", :usage => {'hits' => 1}})
|
134
|
-
|
135
|
-
The :app_id and :usage parameters are required. Additionaly, you can specify a timestamp
|
136
|
-
of transaction:
|
137
|
-
|
138
|
-
response = client.report({:app_id => "app id", :usage => {'hits' => 1},
|
139
|
-
:timestamp => Time.local(2010, 4, 28, 12, 36)})
|
140
|
-
|
141
|
-
The timestamp can be either a Time object (from ruby's standard library) or something that
|
142
|
-
"quacks" like it (for example, the ActiveSupport::TimeWithZone from Rails) or a string. The
|
143
|
-
string has to be in a format parseable by the Time.parse method. For example:
|
144
|
-
|
145
|
-
"2010-04-28 12:38:33 +0200"
|
146
|
-
|
147
|
-
If the timestamp is not in UTC, you have to specify a time offset. That's the "+0200"
|
148
|
-
(two hours ahead of the Universal Coordinate Time) in the example abowe.
|
149
|
-
|
150
|
-
Then call the +success?+ method on the returned response object to see if the report was
|
151
|
-
successful.
|
152
|
-
|
153
|
-
if response.success?
|
154
|
-
# All OK.
|
155
|
-
else
|
156
|
-
# There was an error.
|
157
|
-
end
|
158
|
-
|
159
|
-
In case of error, the +error_code+ returns system error code and +error_message+
|
160
|
-
human readable error description:
|
161
|
-
|
162
|
-
response.error_code # "provider_key_invalid"
|
163
|
-
response.error_message # "provider key \"foo\" is invalid"
|