dit3-api 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e9199220af05f099c8ce450081cf73632bd75577
4
+ data.tar.gz: 8745843b12ba0f5a0334365b8eefee6045644d95
5
+ SHA512:
6
+ metadata.gz: 75edb37030954e5a878e033a5de136fe3ab907400528dc7d503b418e16b8cf8a5e0d033d14ec1dd7ef8caddf59e0e55b22e218b5bc30b7a4580ef649f5463373
7
+ data.tar.gz: ff22fa62f310368b7c53e854da21de28312e6e668453bbcc8c4ab8e1c98cde118d0a38b10dcd4edf5ccbf9d52366ee2a3604a81bcfee6c3f770726a875be122a
data/lib/dit3/api.rb ADDED
@@ -0,0 +1 @@
1
+ require_relative 'api/client'
@@ -0,0 +1,6 @@
1
+ module DIT3
2
+ module Api
3
+ class ApiError < StandardError
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,78 @@
1
+ require 'json'
2
+ require 'net/http'
3
+ require 'uri'
4
+
5
+ require_relative 'api_error'
6
+ require_relative 'oauth_client'
7
+ require_relative 'wrappers/tenants'
8
+
9
+ module DIT3::Api
10
+ class Client
11
+ def initialize(host, client_id, client_secret)
12
+ @api = host + "/api"
13
+ @oauth_client = OAuthClient.new(host, client_id, client_secret)
14
+ @token = get_token @oauth_client
15
+ end
16
+
17
+ def tenants
18
+ Wrappers::Tenants.new(self)
19
+ end
20
+
21
+ def get endpoint
22
+ uri = URI(@api + endpoint)
23
+ request = get_request uri
24
+ http = get_http uri
25
+
26
+ response = http.request request
27
+ response_body = JSON.parse response.body
28
+ if (!response.kind_of? Net::HTTPSuccess)
29
+ raise ApiError, "Failed to execute GET #{endpoint} request: #{response_body['message']}"
30
+ end
31
+ return
32
+ end
33
+
34
+ def post endpoint, data
35
+ uri = URI(@api + endpoint)
36
+ request = post_request uri
37
+ http = get_http uri
38
+
39
+ request['Content-Type'] = 'application/json'
40
+ request.body = data.to_json
41
+
42
+ response = http.request request
43
+ response_body = JSON.parse response.body
44
+ if (!response.kind_of? Net::HTTPSuccess)
45
+ raise ApiError, "Failed to execute POST #{endpoint} request: #{response_body['message']}"
46
+ end
47
+ end
48
+
49
+ private
50
+
51
+ def get_token oauth_client
52
+ opts = { :params => { :scope => oauth_client.normalize_scope(['platform.tenants.read', 'platform.tenants.write']) }}
53
+ response = oauth_client.client_credentials.get_token(opts)
54
+ if (!response.kind_of? Net::HTTPSuccess)
55
+ raise ApiError, "Failed to get access token: #{response.body}"
56
+ end
57
+ response_body = JSON.parse(response.body)
58
+ response_body['access_token']
59
+ end
60
+
61
+ def get_request endpoint_uri
62
+ Net::HTTP::Get.new(endpoint_uri.request_uri, 'Authorization' => "Bearer #{@token}")
63
+ end
64
+
65
+ def post_request endpoint_uri
66
+ Net::HTTP::Post.new(endpoint_uri.request_uri, 'Authorization' => "Bearer #{@token}")
67
+ end
68
+
69
+ def get_http endpoint_uri
70
+ http = Net::HTTP.new(endpoint_uri.host, endpoint_uri.port)
71
+ if (endpoint_uri.scheme == 'https')
72
+ http.use_ssl = true
73
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
74
+ end
75
+ http
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,11 @@
1
+ require 'oauth2-client'
2
+
3
+ module DIT3::Api
4
+ class OAuthClient < OAuth2Client::Client
5
+ def initialize(*args)
6
+ super
7
+ @token_path = '/oauth/token'
8
+ @authorize_path = '/oauth/authorize'
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,21 @@
1
+ module DIT3::Api::Wrappers
2
+ class Tenants
3
+ def initialize client
4
+ @client = client
5
+ end
6
+
7
+ def create name
8
+ @client.post("/tenants", {'name' => name})
9
+ end
10
+
11
+ def exists? name
12
+ begin
13
+ @client.get("/tenants/#{name}")
14
+ true
15
+ rescue DIT3::Api::ApiError => error
16
+ print error
17
+ false
18
+ end
19
+ end
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dit3-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Maraev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-07-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: oauth2-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ description: A simple hello world gem
28
+ email: a.maraev@simbirsoft.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/dit3/api.rb
34
+ - lib/dit3/api/api_error.rb
35
+ - lib/dit3/api/client.rb
36
+ - lib/dit3/api/oauth_client.rb
37
+ - lib/dit3/api/wrappers/tenants.rb
38
+ homepage: http://rubygems.org/gems/dit3-api
39
+ licenses:
40
+ - MIT
41
+ metadata: {}
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubyforge_project:
58
+ rubygems_version: 2.5.1
59
+ signing_key:
60
+ specification_version: 4
61
+ summary: DIT 3 API Ruby client.
62
+ test_files: []