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 +4 -4
- data/lib/dit3/api/api_error.rb +5 -0
- data/lib/dit3/api/client.rb +26 -19
- data/lib/dit3/api/wrappers/tenants.rb +4 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 464a19d6b859063bcfec22625531469467d5b79c
|
4
|
+
data.tar.gz: be8adfd93e7c59e045f55bee94b0ad8882c5b69a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 212b4cae7264d9ad9505edc02d9591809913dd3852b9206ff57a87f347abf10946701299456722ff9a0aac847b3f77778773c4d9c7e76df8997c633b9710efd8
|
7
|
+
data.tar.gz: 7d78dcc84ddf672dc7a3eb7ee2d60161d960298561a6e58dfee14ad4b7e922d897e77562f63c22aa09099b2660aed3b1b1c832038856ce7ad4efa03e2f0863f0
|
data/lib/dit3/api/api_error.rb
CHANGED
data/lib/dit3/api/client.rb
CHANGED
@@ -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
|
26
|
+
def exec_request method, endpoint
|
22
27
|
uri = URI(@api + endpoint)
|
23
|
-
request =
|
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
|
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
|
38
|
-
|
39
|
-
|
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
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
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')
|
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.
|
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-
|
11
|
+
date: 2016-07-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: oauth2-client
|