dit3-api 0.0.11 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ea5e5abdb6811c9a6b7a067b4c853a2de6ef00f3
4
- data.tar.gz: 6933bf411feed9df5d0b769119b6b027e2c2d905
3
+ metadata.gz: 2c3f0c53e2a04f31971fd0f5e76900346a6a8ab7
4
+ data.tar.gz: f4dff5f15a57489a0b79b9dd2079217fbf725ae1
5
5
  SHA512:
6
- metadata.gz: a7825763c76254523c6b340636ab9de5ccdb4466fb200315a80b090be9fafc1294789791825983a37fb84a7a5ae5ad49c8d6ca62b60633d131a0d2aa9c3bda28
7
- data.tar.gz: 9bbae10dea6cc65b46cbb00691c1ee1adf7d08e53b3a2e980b0cf6ebb6900195c0d215a9855fc5563edc763f624c6f306c4e0f13dee88e397228ee55e71a4bae
6
+ metadata.gz: a023327c150f1b125c3753a965d4284a6d63459629afed2934e6a5f4c123ee2323cfb0a519da693daf17ab91ef77cbbd497ec8394990b12b519ee773fa053355
7
+ data.tar.gz: 6b93ab0fc2ce576baf3848ede1877b2722689a25bc957abd8942c83d26415304f61f68b85c4dde106d5aca4340d9ad5ce6bf702c66d44eba2d3bd90660d260a7
@@ -1,11 +1,6 @@
1
1
  module DIT3
2
2
  module Api
3
3
  class ApiError < StandardError
4
- attr_reader :response
5
-
6
- def initialize(response)
7
- @response = response
8
- end
9
4
  end
10
5
  end
11
6
  end
@@ -4,81 +4,66 @@ require 'uri'
4
4
 
5
5
  require_relative 'api_error'
6
6
  require_relative 'oauth_client'
7
- require_relative 'wrappers/accounts'
8
7
  require_relative 'wrappers/tenants'
9
8
 
10
9
  module DIT3::Api
11
10
  class Client
12
- def initialize(host, client_id, client_secret)
11
+ def initialize(host, client_id, client_secret, login, password)
13
12
  @api = host + "/api"
14
13
  @oauth_client = OAuthClient.new(host, client_id, client_secret)
15
- @token = get_token @oauth_client
16
- @request_factory = {
17
- "GET" => method(:get_request),
18
- "POST" => method(:post_request),
19
- "DELETE" => method(:delete_request),
20
- "PUT" => method(:put_request)
21
- }
14
+ @token = get_token @oauth_client, login, password
22
15
  end
23
16
 
24
17
  def tenants
25
18
  Wrappers::Tenants.new(self)
26
19
  end
27
20
 
28
- def accounts
29
- Wrappers::Accounts.new(self)
30
- end
31
-
32
- def exec_request method, endpoint
21
+ def get endpoint
33
22
  uri = URI(@api + endpoint)
34
- request = @request_factory[method].call(uri)
23
+ request = get_request uri
35
24
  http = get_http uri
36
25
 
37
- yield(request) if block_given?
38
-
39
26
  response = http.request request
40
27
  if (!response.kind_of? Net::HTTPSuccess)
41
- raise ApiError.new(response), "Failed to execute #{method} #{endpoint} request - #{response.code}"
28
+ raise ApiError, "Failed to execute GET #{endpoint} request - #{request.code}"
42
29
  end
43
- if !response.body.nil?
44
- JSON.parse response.body
30
+ if request.body_permitted?
31
+ JSON.parse request.body
45
32
  else
46
33
  nil
47
34
  end
48
35
  end
49
36
 
50
- def get endpoint
51
- exec_request("GET", endpoint)
52
- end
53
-
54
37
  def post endpoint, data
55
- exec_request("POST", endpoint) do |request|
56
- request['Content-Type'] = 'application/json'
57
- request.body = data.to_json
58
- end
59
- end
38
+ uri = URI(@api + endpoint)
39
+ request = post_request uri
40
+ http = get_http uri
60
41
 
61
- def put endpoint, data
62
- exec_request("PUT", endpoint) do |request|
63
- request['Content-Type'] = 'application/json'
64
- request.body = data.to_json
65
- end
66
- end
42
+ request['Content-Type'] = 'application/json'
43
+ request.body = data.to_json
67
44
 
68
- def delete endpoint
69
- exec_request("DELETE", endpoint)
45
+ response = http.request request
46
+ if (!response.kind_of? Net::HTTPSuccess)
47
+ raise ApiError, "Failed to execute POST #{endpoint} request - #{request.code}"
48
+ end
49
+ if request.body_permitted?
50
+ JSON.parse request.body
51
+ else
52
+ nil
53
+ end
70
54
  end
71
55
 
72
56
  private
73
57
 
74
- def get_token oauth_client
75
- opts = { :params => { :scope => oauth_client.normalize_scope(['platform.tenants.read', 'platform.tenants.write', 'platform.tenants.delete', 'platform.accounts.read', 'platform.accounts.write']) }}
76
- response = oauth_client.client_credentials.get_token(opts)
77
- if (!response.kind_of? Net::HTTPSuccess)
78
- raise ApiError, "Failed to get access token: #{response.body}"
79
- end
80
- response_body = JSON.parse(response.body)
81
- response_body['access_token']
58
+ def get_token oauth_client, login, password
59
+ # opts = { :params => { :scope => oauth_client.normalize_scope(['platform.tenants.read', 'platform.tenants.write']) }}
60
+ # response = oauth_client.client_credentials.get_token(opts)
61
+ # if (!response.kind_of? Net::HTTPSuccess)
62
+ # raise ApiError, "Failed to get access token: #{response.body}"
63
+ # end
64
+ # response_body = JSON.parse(response.body)
65
+ # response_body['access_token']
66
+ client.password.get_token(login, password)
82
67
  end
83
68
 
84
69
  def get_request endpoint_uri
@@ -89,14 +74,6 @@ module DIT3::Api
89
74
  Net::HTTP::Post.new(endpoint_uri.request_uri, 'Authorization' => "Bearer #{@token}")
90
75
  end
91
76
 
92
- def delete_request endpoint_uri
93
- Net::HTTP::Delete.new(endpoint_uri.request_uri, 'Authorization' => "Bearer #{@token}")
94
- end
95
-
96
- def put_request endpoint_uri
97
- Net::HTTP::Put.new(endpoint_uri.request_uri, 'Authorization' => "Bearer #{@token}")
98
- end
99
-
100
77
  def get_http endpoint_uri
101
78
  http = Net::HTTP.new(endpoint_uri.host, endpoint_uri.port)
102
79
  if (endpoint_uri.scheme == 'https')
@@ -1,5 +1,3 @@
1
- require_relative '../model/tenant.rb'
2
-
3
1
  module DIT3::Api::Wrappers
4
2
  class Tenants
5
3
  def initialize client
@@ -10,13 +8,9 @@ module DIT3::Api::Wrappers
10
8
  @client.post("/tenants", {'name' => name})
11
9
  end
12
10
 
13
- def delete name
14
- @client.delete("/tenants/#{name}")
15
- end
16
-
17
11
  def get name
18
12
  begin
19
- DIT3::Api::Model::Tenant.new(@client.get("/tenants/#{name}"))
13
+ @client.get("/tenants/#{name}")
20
14
  rescue DIT3::Api::ApiError
21
15
  nil
22
16
  end
metadata CHANGED
@@ -1,30 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dit3-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.11
4
+ version: 0.0.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Maraev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-10 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
11
+ date: 2018-01-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: DIT3 API Client
28
14
  email: a.maraev@simbirsoft.com
29
15
  executables: []
30
16
  extensions: []
@@ -33,10 +19,7 @@ files:
33
19
  - lib/dit3/api.rb
34
20
  - lib/dit3/api/api_error.rb
35
21
  - lib/dit3/api/client.rb
36
- - lib/dit3/api/model/account.rb
37
- - lib/dit3/api/model/tenant.rb
38
22
  - lib/dit3/api/oauth_client.rb
39
- - lib/dit3/api/wrappers/accounts.rb
40
23
  - lib/dit3/api/wrappers/tenants.rb
41
24
  homepage: http://rubygems.org/gems/dit3-api
42
25
  licenses:
@@ -61,5 +44,5 @@ rubyforge_project:
61
44
  rubygems_version: 2.5.1
62
45
  signing_key:
63
46
  specification_version: 4
64
- summary: DIT 3 API Ruby client.
47
+ summary: DIT3 API Client
65
48
  test_files: []
@@ -1,17 +0,0 @@
1
- module DIT3::Api::Model
2
- class Account
3
- attr_reader :id
4
- attr_reader :name
5
- attr_reader :email
6
- attr_reader :locale
7
- attr_reader :tenant
8
-
9
- def initialize hash
10
- @id = hash['id']
11
- @name = hash['name']
12
- @email = hash['email']
13
- @locale = hash['locale']
14
- @tenant = hash['tenant']
15
- end
16
- end
17
- end
@@ -1,17 +0,0 @@
1
- module DIT3::Api::Model
2
- class Tenant
3
- attr_reader :name
4
- attr_reader :id
5
-
6
- def initialize hash
7
- @name = hash['name']
8
- @id = hash['id']
9
- end
10
-
11
- def to_hash
12
- hash = {}
13
- instance_variables.each {|var| hash[var.to_s.delete("@")] = instance_variable_get(var) }
14
- hash
15
- end
16
- end
17
- end
@@ -1,25 +0,0 @@
1
- require_relative '../model/account.rb'
2
-
3
- module DIT3::Api::Wrappers
4
- class Accounts
5
- def initialize client
6
- @client = client
7
- end
8
-
9
- def get name
10
- begin
11
- DIT3::Api::Model::Account.new(@client.get("/accounts/#{name}"))
12
- rescue DIT3::Api::ApiError
13
- nil
14
- end
15
- end
16
-
17
- def set_tenant name, tenant
18
- @client.put("/accounts/#{name}/tenant", tenant.to_hash())
19
- end
20
-
21
- def exists? name
22
- !get(name).nil?
23
- end
24
- end
25
- end