3scale-api 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e87913474e0e2e316880dd063c559bbee086470b
4
+ data.tar.gz: 6f734f9eb252fe2cb0c99e12d0cb6c21694d7932
5
+ SHA512:
6
+ metadata.gz: 73ea2d8cade0bb30df5dd0f5d7d79a70bbf26911accc042d6c7ac063ea5ef2c1fa03be8197919e3499743974f1c8b01acbf0cab29aa3ff3a1f4483983d67be2c
7
+ data.tar.gz: 530916190be47420386900d6de3489062dedf9ef33f44d88f8ad906039373dcff847e7816686d7ee66905e0ea739999a96c8b91be25d094db3c4d62195065928
@@ -0,0 +1,52 @@
1
+ # ThreeScale::API
2
+
3
+
4
+ This gem aims to expose all [3scale](http://3scale.net) APIs with a Ruby interface.
5
+
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem '3scale-api'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install 3scale-api
22
+
23
+ ## Usage
24
+
25
+
26
+ ```ruby
27
+ require '3scale/api'
28
+ client = ThreeScale::API.new(endpoint: 'https://foo-admin.3scale.net', provider_key: 'foobar')
29
+
30
+ services = client.list_services
31
+ ```
32
+
33
+ ## Design
34
+
35
+ Design decisions:
36
+
37
+ * 0 runtime dependencies
38
+ * thread safety
39
+ * tested
40
+
41
+ ## Development
42
+
43
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
44
+
45
+ To run tests run `rake` or `rspec`.
46
+
47
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
48
+
49
+ ## Contributing
50
+
51
+ Bug reports and pull requests are welcome on GitHub at https://github.com/3scale/3scale-api-ruby.
52
+
@@ -0,0 +1,14 @@
1
+ require '3scale/api/version'
2
+
3
+ module ThreeScale
4
+ module API
5
+ autoload :Client, '3scale/api/client'
6
+ autoload :HttpClient, '3scale/api/http_client'
7
+
8
+ def self.new(endpoint:, provider_key:)
9
+ http_client = HttpClient.new(endpoint: endpoint,
10
+ provider_key: provider_key)
11
+ Client.new(http_client)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,144 @@
1
+ module ThreeScale
2
+ module API
3
+ class Client
4
+
5
+ attr_reader :http_client
6
+
7
+ # @param [ThreeScale::API::HttpClient] http_client
8
+
9
+ def initialize(http_client)
10
+ @http_client = http_client
11
+ end
12
+
13
+ # @api public
14
+ # @return [Hash]
15
+ # @param [Fixnum] id Service ID
16
+ def show_service(id)
17
+ response = http_client.get("/admin/api/services/#{id}")
18
+ extract(entity: 'service', from: response)
19
+ end
20
+
21
+ # @api public
22
+ # @return [Array<Hash>]
23
+ def list_services
24
+ response = http_client.get('/admin/api/services')
25
+ extract(collection: 'services', entity: 'service', from: response)
26
+ end
27
+
28
+ # @api public
29
+ # @return [Hash]
30
+ # @param [Hash] attributes Service Attributes
31
+ # @option attributes [String] :name Service Name
32
+ def create_service(attributes)
33
+ response = http_client.post('/admin/api/services', body: { service: attributes })
34
+ extract(entity: 'service', from: response)
35
+ end
36
+
37
+ # @api public
38
+ # @return [Array<Hash>]
39
+ # @param [Fixnum] service_id Service ID
40
+ def list_metrics(service_id)
41
+ response = http_client.get("/admin/api/services/#{service_id}/metrics")
42
+ extract(collection: 'metrics', entity: 'metric', from: response)
43
+ end
44
+
45
+ # @api public
46
+ # @return [Hash]
47
+ # @param [Fixnum] service_id Service ID
48
+ # @param [Hash] attributes Metric Attributes
49
+ # @option attributes [String] :name Metric Name
50
+ def create_metric(service_id, attributes)
51
+ response = http_client.post("/admin/api/services/#{service_id}/metrics", body: { metric: attributes })
52
+ extract(entity: 'metric', from: response)
53
+ end
54
+
55
+ # @api public
56
+ # @return [Array<Hash>]
57
+ # @param [Fixnum] service_id Service ID
58
+ # @param [Fixnum] metric_id Metric ID
59
+ def list_methods(service_id, metric_id)
60
+ response = http_client.get("/admin/api/services/#{service_id}/metrics/#{metric_id}/methods")
61
+ extract(collection: 'methods', entity: 'method', from: response)
62
+ end
63
+
64
+ # @api public
65
+ # @return [Hash]
66
+ # @param [Fixnum] service_id Service ID
67
+ # @param [Fixnum] metric_id Metric ID
68
+ # @param [Hash] attributes Metric Attributes
69
+ # @option attributes [String] :name Method Name
70
+ def create_method(service_id, metric_id, attributes)
71
+ response = http_client.post("/admin/api/services/#{service_id}/metrics/#{metric_id}/methods",
72
+ body: { metric: attributes })
73
+ extract(entity: 'method', from: response)
74
+ end
75
+
76
+ # @api public
77
+ # @return [Array<Hash>]
78
+ # @param [Fixnum] service_id Service ID
79
+ def list_service_application_plans(service_id)
80
+ response = http_client.get("/admin/api/services/#{service_id}/application_plans")
81
+
82
+ extract(collection: 'plans', entity: 'application_plan', from: response)
83
+ end
84
+
85
+ # @api public
86
+ # @return [Hash]
87
+ # @param [Fixnum] service_id Service ID
88
+ # @param [Hash] attributes Metric Attributes
89
+ # @option attributes [String] :name Application Plan Name
90
+ def create_application_plan(service_id, attributes)
91
+ response = http_client.post("/admin/api/services/#{service_id}/application_plans",
92
+ body: { application_plan: attributes })
93
+ extract(entity: 'application_plan', from: response)
94
+ end
95
+
96
+ # @api public
97
+ # @return [Array<Hash>]
98
+ # @param [Fixnum] application_plan_id Application Plan ID
99
+ def list_application_plan_limits(application_plan_id)
100
+ response = http_client.get("/admin/api/application_plans/#{application_plan_id}/limits")
101
+
102
+ extract(collection: 'limits', entity: 'limit', from: response)
103
+ end
104
+
105
+ # @api public
106
+ # @return [Hash]
107
+ # @param [Fixnum] application_plan_id Application Plan ID
108
+ # @param [Hash] attributes Metric Attributes
109
+ # @param [Fixnum] metric_id Metric ID
110
+ # @option attributes [String] :period Usage Limit period
111
+ # @option attributes [String] :value Usage Limit value
112
+ def create_application_plan_limit(application_plan_id, metric_id, attributes)
113
+ response = http_client.post("/admin/api/application_plans/#{application_plan_id}/metrics/#{metric_id}/limits",
114
+ body: { usage_limit: attributes })
115
+ extract(entity: 'limit', from: response)
116
+ end
117
+
118
+ # @param [Fixnum] application_plan_id Application Plan ID
119
+ # @param [Fixnum] metric_id Metric ID
120
+ # @param [Fixnum] limit_id Usage Limit ID
121
+ def delete_application_plan_limit(application_plan_id, metric_id, limit_id)
122
+ http_client.delete("/admin/api/application_plans/#{application_plan_id}/metrics/#{metric_id}/limits/#{limit_id}")
123
+ true
124
+ end
125
+
126
+ protected
127
+
128
+ def extract(collection: nil, entity: , from: )
129
+ if collection
130
+ from = from.fetch(collection)
131
+ end
132
+
133
+ case from
134
+ when Array then from.map { |e| e.fetch(entity) }
135
+ when Hash then from.fetch(entity) { from }
136
+ when nil then nil # raise exception?
137
+ else
138
+ raise "unknown #{from}"
139
+ end
140
+
141
+ end
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,94 @@
1
+ require 'json'
2
+
3
+ module ThreeScale
4
+ module API
5
+ class HttpClient
6
+ attr_reader :endpoint, :admin_domain, :provider_key, :headers, :format
7
+
8
+ def initialize(endpoint: , provider_key: , format: :json)
9
+ @endpoint = URI(endpoint).freeze
10
+ @admin_domain = @endpoint.host.freeze
11
+ @provider_key = provider_key.freeze
12
+ @http = Net::HTTP.new(admin_domain, @endpoint.port)
13
+ @http.use_ssl = @endpoint.is_a?(URI::HTTPS)
14
+
15
+ @headers = {
16
+ 'Accept' => "application/#{format}",
17
+ 'Content-Type' => "application/#{format}",
18
+ 'Authorization' => 'Basic ' + [":#{@provider_key}"].pack('m').delete("\r\n")
19
+ }
20
+
21
+ if debug?
22
+ @http.set_debug_output($stdout)
23
+ @headers['Accept-Encoding'] = 'identity'
24
+ end
25
+
26
+ @headers.freeze
27
+
28
+ @format = format
29
+ end
30
+
31
+ def get(path)
32
+ parse @http.get("#{path}.#{format}", headers)
33
+ end
34
+
35
+ def post(path, body: )
36
+ parse @http.post("#{path}.#{format}", serialize(body), headers)
37
+ end
38
+
39
+ def delete(path)
40
+ parse @http.delete("#{path}.#{format}", headers)
41
+ end
42
+
43
+ # @param [::Net::HTTPResponse] response
44
+ def parse(response)
45
+ case response
46
+ when Net::HTTPUnprocessableEntity, Net::HTTPSuccess then parser.decode(response.body)
47
+ when Net::HTTPForbidden then forbidden!(response)
48
+ else "Can't handle #{response.inspect}"
49
+ end
50
+ end
51
+
52
+ class ForbiddenError < StandardError; end
53
+
54
+ def forbidden!(response)
55
+ raise ForbiddenError, response
56
+ end
57
+
58
+ def serialize(body)
59
+ case body
60
+ when String then body
61
+ else parser.encode(body)
62
+ end
63
+ end
64
+
65
+ def parser
66
+ case format
67
+ when :json then JSONParser
68
+ else "unknown format #{format}"
69
+ end
70
+ end
71
+
72
+ protected
73
+
74
+ def debug?
75
+ ENV.fetch('3SCALE_DEBUG', '0') == '1'
76
+ end
77
+
78
+ module JSONParser
79
+ module_function
80
+
81
+ def decode(string)
82
+ case string
83
+ when ' '.freeze, ''.freeze then nil
84
+ else ::JSON.parse(string)
85
+ end
86
+ end
87
+
88
+ def encode(query)
89
+ ::JSON.generate(query)
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,5 @@
1
+ module ThreeScale
2
+ module API
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ require '3scale/api'
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: 3scale-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Michal Cichra
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-03-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.4'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.4'
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.24'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.24'
69
+ description: 'API Client to access your 3scale APIs: Account Management API'
70
+ email:
71
+ - michal@3scale.net
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - README.md
77
+ - lib/3scale/api.rb
78
+ - lib/3scale/api/client.rb
79
+ - lib/3scale/api/http_client.rb
80
+ - lib/3scale/api/version.rb
81
+ - lib/three_scale/api.rb
82
+ homepage: https://github.com/3scale/3scale-api-ruby.
83
+ licenses: []
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.5.1
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: API Client for 3scale APIs
105
+ test_files: []
106
+ has_rdoc: