dit3-api 0.0.8 → 0.0.9

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: 5c9512452c3c4666305ec0081df7d0d0728a2080
4
- data.tar.gz: c01ced71326660ffec3769a9d033f70de5d624a1
3
+ metadata.gz: 464a19d6b859063bcfec22625531469467d5b79c
4
+ data.tar.gz: be8adfd93e7c59e045f55bee94b0ad8882c5b69a
5
5
  SHA512:
6
- metadata.gz: 8864513cb70cf7a324abdca9081e60787fd911b8880f209e0722a5c7e5e67da4e38f6e81b49bb34ea43ed502c9c6cca56285269e188202b2de3a08d88265ecc8
7
- data.tar.gz: 8ac0b5f7fb2bb4e66d0b5b79ecb8d3c9b8c81de65782d561a464302f4d50fc76af94758361edd8882349ba7cf6043e9f03453f3834295c9374b125c1bcbf4cbb
6
+ metadata.gz: 212b4cae7264d9ad9505edc02d9591809913dd3852b9206ff57a87f347abf10946701299456722ff9a0aac847b3f77778773c4d9c7e76df8997c633b9710efd8
7
+ data.tar.gz: 7d78dcc84ddf672dc7a3eb7ee2d60161d960298561a6e58dfee14ad4b7e922d897e77562f63c22aa09099b2660aed3b1b1c832038856ce7ad4efa03e2f0863f0
@@ -1,6 +1,11 @@
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
4
9
  end
5
10
  end
6
11
  end
@@ -12,20 +12,27 @@ module DIT3::Api
12
12
  @api = host + "/api"
13
13
  @oauth_client = OAuthClient.new(host, client_id, client_secret)
14
14
  @token = get_token @oauth_client
15
+ @request_factory = {
16
+ "GET" => method(:get_request),
17
+ "POST" => method(:post_request),
18
+ "DELETE" => method(:delete_request)
19
+ }
15
20
  end
16
21
 
17
22
  def tenants
18
23
  Wrappers::Tenants.new(self)
19
24
  end
20
25
 
21
- def get endpoint
26
+ def exec_request method, endpoint
22
27
  uri = URI(@api + endpoint)
23
- request = get_request uri
28
+ request = @request_factory[method].call(uri)
24
29
  http = get_http uri
25
30
 
31
+ yield(request) if block_given?
32
+
26
33
  response = http.request request
27
34
  if (!response.kind_of? Net::HTTPSuccess)
28
- raise ApiError, "Failed to execute GET #{endpoint} request - #{response.code}"
35
+ raise ApiError.new(response), "Failed to execute #{method} #{endpoint} request - #{response.code}"
29
36
  end
30
37
  if !response.body.nil?
31
38
  JSON.parse response.body
@@ -34,29 +41,25 @@ module DIT3::Api
34
41
  end
35
42
  end
36
43
 
37
- def post endpoint, data
38
- uri = URI(@api + endpoint)
39
- request = post_request uri
40
- http = get_http uri
41
-
42
- request['Content-Type'] = 'application/json'
43
- request.body = data.to_json
44
+ def get endpoint
45
+ exec_request("GET", endpoint)
46
+ end
44
47
 
45
- response = http.request request
46
- if (!response.kind_of? Net::HTTPSuccess)
47
- raise ApiError, "Failed to execute POST #{endpoint} request - #{response.code}"
48
- end
49
- if !response.body.nil?
50
- JSON.parse response.body
51
- else
52
- nil
48
+ def post endpoint, data
49
+ exec_request("POST", endpoint) do |request|
50
+ request['Content-Type'] = 'application/json'
51
+ request.body = data.to_json
53
52
  end
54
53
  end
55
54
 
55
+ def delete endpoint
56
+ exec_request("DELETE", endpoint)
57
+ end
58
+
56
59
  private
57
60
 
58
61
  def get_token oauth_client
59
- opts = { :params => { :scope => oauth_client.normalize_scope(['platform.tenants.read', 'platform.tenants.write']) }}
62
+ opts = { :params => { :scope => oauth_client.normalize_scope(['platform.tenants.read', 'platform.tenants.write', 'platform.tenants.delete']) }}
60
63
  response = oauth_client.client_credentials.get_token(opts)
61
64
  if (!response.kind_of? Net::HTTPSuccess)
62
65
  raise ApiError, "Failed to get access token: #{response.body}"
@@ -73,6 +76,10 @@ module DIT3::Api
73
76
  Net::HTTP::Post.new(endpoint_uri.request_uri, 'Authorization' => "Bearer #{@token}")
74
77
  end
75
78
 
79
+ def delete_request endpoint_uri
80
+ Net::HTTP::Delete.new(endpoint_uri.request_uri, 'Authorization' => "Bearer #{@token}")
81
+ end
82
+
76
83
  def get_http endpoint_uri
77
84
  http = Net::HTTP.new(endpoint_uri.host, endpoint_uri.port)
78
85
  if (endpoint_uri.scheme == 'https')
@@ -10,6 +10,10 @@ module DIT3::Api::Wrappers
10
10
  @client.post("/tenants", {'name' => name})
11
11
  end
12
12
 
13
+ def delete name
14
+ @client.delete("/tenants/#{name}")
15
+ end
16
+
13
17
  def get name
14
18
  begin
15
19
  DIT3::Api::Model::Tenant.new(@client.get("/tenants/#{name}"))
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dit3-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
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-07-14 00:00:00.000000000 Z
11
+ date: 2016-07-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oauth2-client