3scale_client 2.2.10 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{3scale_client}
5
- s.version = "2.2.10"
5
+ s.version = "2.3.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
- s.authors = ["Adam Cigánek", "Tiago Macedo"]
9
- s.description = %q{This gem allows to easily connect an application that provides a Web Service with the 3scale API Management System to authorize its users and report the usage.
8
+ s.authors = ["Adam Cigánek", "Tiago Macedo", "Joaquin Rivera Padron (joahking)"]
9
+ s.description = %q{Ruby client for 3scale platform. 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 information at http://www.3scale.net/ or http://support.3scale.net/.
10
10
  }
11
11
  s.email = %q{adam@3scale.net tiago@3scale.net}
12
12
  s.extra_rdoc_files = [
data/README.rdoc CHANGED
@@ -21,7 +21,7 @@ and do a bundle install.
21
21
 
22
22
  If you are using Rails' config.gems, put this into your config/environment.rb
23
23
 
24
- config.gem '3scale_client
24
+ config.gem '3scale_client'
25
25
 
26
26
  Otherwise, require the gem in whatever way is natural to your framework of choice.
27
27
 
@@ -33,6 +33,36 @@ First, create an instance of the client, giving it your provider API key:
33
33
 
34
34
  Because the object is stateless, you can create just one and store it globally.
35
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
+
36
66
  === Authorize
37
67
 
38
68
  To authorize an application, call the +authorize+ method passing it the application's id and
data/Rakefile CHANGED
@@ -33,9 +33,9 @@ begin
33
33
  gemspec.description = <<END
34
34
  This gem allows to easily connect an application that provides a Web Service with the 3scale API Management System to authorize it's users and report the usage.
35
35
  END
36
- gemspec.email = 'adam@3scale.net'
36
+ gemspec.email = 'support@3scale.net'
37
37
  gemspec.homepage = 'http://www.3scale.net'
38
- gemspec.authors = ['Adam Cigánek']
38
+ gemspec.authors = ['Adam Cigánek', 'Tiago Macedo', 'Joaquin Rivera Padron (joahking)']
39
39
 
40
40
  gemspec.add_dependency 'nokogiri'
41
41
  end
data/lib/3scale/client.rb CHANGED
@@ -51,6 +51,49 @@ module ThreeScale
51
51
  attr_reader :provider_key
52
52
  attr_reader :host
53
53
 
54
+ # Authorize and report an application.
55
+ # TODO (in the mean time read authorize comments or head over to https://support.3scale.net/reference/activedocs#operation/66 for details
56
+ #
57
+ def authrep(options)
58
+ path = "/transactions/authrep.xml?provider_key=#{CGI.escape(provider_key)}"
59
+
60
+ options_usage = options.delete :usage
61
+ options_log = options.delete :log
62
+
63
+ options.each_pair do |param, value|
64
+ path += "&#{param}=#{CGI.escape(value.to_s)}"
65
+ end
66
+
67
+ options_usage ||= {:hits => 1}
68
+ usage = []
69
+ options_usage.each_pair do |metric, value|
70
+ escaped_metric = CGI.escape "[usage][#{metric}]"
71
+ usage << "#{escaped_metric}=#{CGI.escape(value.to_s)}"
72
+ end
73
+ path += "&#{usage.join('&')}"
74
+
75
+ if options_log
76
+ log = []
77
+ options_log.each_pair do |key, value|
78
+ escaped_key = CGI.escape "[log][#{key}]"
79
+ log << "#{escaped_key}=#{CGI.escape(value)}"
80
+ end
81
+ path += "&#{log.join('&')}"
82
+ end
83
+
84
+ uri = URI.parse("http://#{host}#{path}")
85
+ http_response = Net::HTTP.get_response(uri)
86
+
87
+ case http_response
88
+ when Net::HTTPSuccess,Net::HTTPConflict
89
+ build_authorize_response(http_response.body)
90
+ when Net::HTTPClientError
91
+ build_error_response(http_response.body)
92
+ else
93
+ raise ServerError.new(http_response)
94
+ end
95
+ end
96
+
54
97
  # Report transaction(s).
55
98
  #
56
99
  # == Parameters
data/test/client_test.rb CHANGED
@@ -25,6 +25,41 @@ class ThreeScale::ClientTest < Test::Unit::TestCase
25
25
  assert_equal 'su1.3scale.net', client.host
26
26
  end
27
27
 
28
+ def test_custom_host
29
+ client = ThreeScale::Client.new(:provider_key => '1234abcd', :host => "example.com")
30
+
31
+ assert_equal 'example.com', client.host
32
+ end
33
+
34
+ def test_authrep_usage_is_encoded
35
+ assert_authrep_url_with_params "&%5Busage%5D%5Bmethod%5D=666"
36
+
37
+ @client.authrep({:usage => {:method=> 666}})
38
+ end
39
+
40
+ def test_authrep_usage_values_are_encoded
41
+ assert_authrep_url_with_params "&%5Busage%5D%5Bhits%5D=%230"
42
+
43
+ @client.authrep({:usage => {:hits => "#0"}})
44
+ end
45
+
46
+ def test_authrep_usage_defaults_to_hits_1
47
+ assert_authrep_url_with_params "&%5Busage%5D%5Bhits%5D=1"
48
+
49
+ @client.authrep({})
50
+ end
51
+
52
+ def test_authrep_supports_app_id_app_key_auth_mode
53
+ assert_authrep_url_with_params "&app_id=appid&app_key=appkey&%5Busage%5D%5Bhits%5D=1"
54
+
55
+ @client.authrep(:app_id => "appid", :app_key => "appkey")
56
+ end
57
+
58
+ #TODO these authrep tests
59
+ def test_authrep_supports_api_key_auth_mode; end
60
+ def test_authrep_log_is_encoded;end
61
+ def test_authrep_passes_all_params_to_backend;end
62
+
28
63
  def test_successful_authorize
29
64
  body = '<status>
30
65
  <authorized>true</authorized>
@@ -303,4 +338,24 @@ class ThreeScale::ClientTest < Test::Unit::TestCase
303
338
  @client.report({:app_id => 'foo', :usage => {'hits' => 1}})
304
339
  end
305
340
  end
341
+
342
+ private
343
+
344
+ #OPTIMIZE this tricky test helper relies on fakeweb catching the urls requested by the client
345
+ # it is brittle: it depends in the correct order or params in the url
346
+ #
347
+ def assert_authrep_url_with_params(str)
348
+ authrep_url = "http://#{@host}/transactions/authrep.xml?provider_key=#{@client.provider_key}"
349
+ params = str # unless str.scan(/log/)
350
+ params << "&%5Busage%5D%5Bhits%5D=1" unless params.scan(/usage.*hits/)
351
+ parsed_authrep_url = URI.parse(authrep_url + params)
352
+ # set to have the client working
353
+ body = '<status>
354
+ <authorized>true</authorized>
355
+ <plan>Ultimate</plan>
356
+ </status>'
357
+
358
+ # this is the actual assertion, if fakeweb raises the client is submiting with wrong params
359
+ FakeWeb.register_uri(:get, parsed_authrep_url, :status => ['200', 'OK'], :body => body)
360
+ end
306
361
  end
data/test/remote_test.rb CHANGED
@@ -21,6 +21,23 @@ if ENV['TEST_3SCALE_PROVIDER_KEY'] &&
21
21
  end
22
22
  end
23
23
 
24
+ def test_successful_authrep
25
+ @app_keys.each do |app_key|
26
+ response = @client.authrep(:app_id => @app_ids[0], :app_key => app_key,
27
+ :usage => {:hits => 2},
28
+ :log => {:request => "a/a", :response => "b/b", :code => "c/c"})
29
+ assert response.success?, "AuthRep should succeed for app_id=#{@app_ids[0]} and app_key=#{app_key}, but it failed with: '#{response.error_message}'"
30
+ end
31
+ end
32
+
33
+
34
+ def test_failed_authrep
35
+ response = @client.authrep(:app_id => 'invalid-id')
36
+ assert !response.success?
37
+ assert_equal 'application_not_found', response.error_code
38
+ assert_equal 'application with id="invalid-id" was not found', response.error_message
39
+ end
40
+
24
41
  def test_successful_authorize
25
42
  @app_keys.each do |app_key|
26
43
  response = @client.authorize(:app_id => @app_ids[0], :app_key => app_key)
metadata CHANGED
@@ -1,20 +1,21 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: 3scale_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.10
4
+ version: 2.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Adam Cigánek
9
9
  - Tiago Macedo
10
+ - Joaquin Rivera Padron (joahking)
10
11
  autorequire:
11
12
  bindir: bin
12
13
  cert_chain: []
13
- date: 2012-06-06 00:00:00.000000000 Z
14
+ date: 2012-08-22 00:00:00.000000000 Z
14
15
  dependencies:
15
16
  - !ruby/object:Gem::Dependency
16
17
  name: nokogiri
17
- requirement: &24631000 !ruby/object:Gem::Requirement
18
+ requirement: &20757120 !ruby/object:Gem::Requirement
18
19
  none: false
19
20
  requirements:
20
21
  - - ! '>='
@@ -22,10 +23,10 @@ dependencies:
22
23
  version: '0'
23
24
  type: :runtime
24
25
  prerelease: false
25
- version_requirements: *24631000
26
+ version_requirements: *20757120
26
27
  - !ruby/object:Gem::Dependency
27
28
  name: fakeweb
28
- requirement: &24630620 !ruby/object:Gem::Requirement
29
+ requirement: &20756740 !ruby/object:Gem::Requirement
29
30
  none: false
30
31
  requirements:
31
32
  - - ! '>='
@@ -33,10 +34,10 @@ dependencies:
33
34
  version: '0'
34
35
  type: :development
35
36
  prerelease: false
36
- version_requirements: *24630620
37
+ version_requirements: *20756740
37
38
  - !ruby/object:Gem::Dependency
38
39
  name: jeweler
39
- requirement: &24630160 !ruby/object:Gem::Requirement
40
+ requirement: &20756260 !ruby/object:Gem::Requirement
40
41
  none: false
41
42
  requirements:
42
43
  - - ! '>='
@@ -44,10 +45,10 @@ dependencies:
44
45
  version: '0'
45
46
  type: :development
46
47
  prerelease: false
47
- version_requirements: *24630160
48
+ version_requirements: *20756260
48
49
  - !ruby/object:Gem::Dependency
49
50
  name: mocha
50
- requirement: &24629740 !ruby/object:Gem::Requirement
51
+ requirement: &20773160 !ruby/object:Gem::Requirement
51
52
  none: false
52
53
  requirements:
53
54
  - - ! '>='
@@ -55,10 +56,11 @@ dependencies:
55
56
  version: '0'
56
57
  type: :development
57
58
  prerelease: false
58
- version_requirements: *24629740
59
- description: ! 'This gem allows to easily connect an application that provides a Web
60
- Service with the 3scale API Management System to authorize its users and report
61
- the usage.
59
+ version_requirements: *20773160
60
+ description: ! 'Ruby client for 3scale platform. 3scale is an API Infrastructure service
61
+ which handles API Keys, Rate Limiting, Analytics, Billing Payments and Developer
62
+ Management. Includes a configurable API dashboard and developer portal CMS. More
63
+ information at http://www.3scale.net/ or http://support.3scale.net/.
62
64
 
63
65
  '
64
66
  email: adam@3scale.net tiago@3scale.net