maas-client 0.2.4 → 0.3.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8c5e1e49b5860c2dd08f3f54f50e4bb2e0ec0159d55f151351d382e997e3372f
4
- data.tar.gz: 4bcf35e1650fda5b9fbb618623c63b2a2a424380bdc23bcc4c70c74385a73098
3
+ metadata.gz: 471f5ee6442b4722061249e081b46b9ac8e1536ffaa8211d5288b8f924b3b52f
4
+ data.tar.gz: 9d8e5af1e3d0150acc0002ee5701f15ab2a49252a3cd27f93ba50e49bb3ab722
5
5
  SHA512:
6
- metadata.gz: ec4cc750290fdada55ef41f463d3daf2ff3d2d42b220ad6caaf0c425f57cc4f9c8f381204657b6706877de8429fc456ff917d10a7ac307d9b647897d06883a0c
7
- data.tar.gz: 9d51ba8ad6e5c0326ee3cec8035afcb1ac31440fc9c1e4cfec1592ab282dfffa226dd103fbfd34f6faf93776649427164be925b9f6906ecaf1421d684962b9ef
6
+ metadata.gz: 77db95ae809a57ecb501b7e3b6283840c16d3a5ac2cb3e86c95b4d2bb51c9a84497937a45ba7a7405a78726bc01d3450a90bbe638ce600a62327622beeec49f3
7
+ data.tar.gz: 007f067feddda6a797bf0475509ce7a01fafd0d03a8244afea9058e19c03fc3d5e6b091a17799be9f8512b8cebab363a2a65e1a23791e83c3f5aa3c5522ed870
data/README.md CHANGED
@@ -180,8 +180,12 @@ Pushed maas-client x.x.xx to rubygems.org.
180
180
  ** Execute release
181
181
 
182
182
  ```
183
+ ## Reference
184
+ https://docs.maas.io/2.4/en/api-authentication
185
+ https://github.com/oauth-xx/oauth-ruby
186
+ https://github.com/typhoeus/typhoeus
187
+ https://docs.maas.io/2.4/en/api
183
188
 
184
189
  ## License
185
190
 
186
191
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
187
-
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Maas
4
4
  module Client
5
- VERSION = '0.2.4'
5
+ VERSION = '0.3.0'
6
6
  end
7
7
  end
data/lib/maas/client.rb CHANGED
@@ -1,7 +1,10 @@
1
1
  require 'maas/client/config'
2
2
  require 'oauth'
3
3
  require 'oauth/signature/plaintext'
4
+ require 'oauth/request_proxy/typhoeus_request'
4
5
  require 'json'
6
+ require 'hashie'
7
+ require 'typhoeus'
5
8
 
6
9
  module Maas
7
10
  module Client
@@ -10,6 +13,7 @@ module Maas
10
13
  attr_reader :access_token
11
14
 
12
15
  def initialize(api_key = nil, endpoint = nil)
16
+
13
17
  if api_key and endpoint
14
18
  @api_key = api_key
15
19
  @endpoint = endpoint
@@ -20,17 +24,20 @@ module Maas
20
24
  else
21
25
  abort("There is no server Info provided.")
22
26
  end
27
+
23
28
  @consumer_key = @api_key.split(/:/)[0]
24
29
  @key = @api_key.split(/:/)[1]
25
30
  @secret = @api_key.split(/:/)[2]
31
+
26
32
  consumer = OAuth::Consumer.new(
27
- @consumer_key,
28
- '',
29
- realm: '',
30
- site: @endpoint,
31
- scheme: :header,
32
- signature_method: 'PLAINTEXT'
33
+ @consumer_key, '',
34
+ {
35
+ :site => @endpoint,
36
+ :scheme => :header,
37
+ :signature_method => "PLAINTEXT"
38
+ }
33
39
  )
40
+
34
41
  @access_token = OAuth::AccessToken.new(
35
42
  consumer,
36
43
  @key,
@@ -39,21 +46,67 @@ module Maas
39
46
  end
40
47
 
41
48
  def request(method, subject, param = nil)
42
- default_param = {
43
- 'Accept' => 'application/json',
44
- 'Content-Type' => 'multipart/form-data'
49
+
50
+ headers = {Accept: 'application/json'}
51
+
52
+ uri = access_token.consumer.options[:site] +
53
+ '/' +
54
+ subject.join('/') +
55
+ '/'
56
+
57
+
58
+ oauth_params = {
59
+ :consumer => access_token.consumer,
60
+ :token => access_token
61
+ }
62
+
63
+ hydra = Typhoeus::Hydra.new
64
+
65
+ Hashie.symbolize_keys! param if param
66
+
67
+ options = {
68
+ method: method,
69
+ headers: headers
45
70
  }
46
- uri = '/' + subject.join('/') + '/'
47
- arguments = [method, uri, param, default_param].compact
48
- response = access_token.request(*arguments)
49
71
 
50
- return JSON.parse(response.body) if response.code == '200'
72
+ # https://github.com/typhoeus/typhoeus#sending-params-in-the-body-with-put
73
+ if method == :get
74
+ options.merge!({params: param})
75
+ elsif method == :post
76
+ options.merge!({body: param})
77
+ headers.merge!(
78
+ {'Content-Type'=> "application/x-www-form-urlencoded"}
79
+ )
80
+ end
81
+
82
+ req = Typhoeus::Request.new(uri, options)
83
+
84
+ oauth_helper = OAuth::Client::Helper.new(
85
+ req,
86
+ oauth_params.merge(
87
+ {
88
+ request_uri: uri,
89
+ signature_method: access_token
90
+ .consumer
91
+ .options[:signature_method]
92
+ }
93
+ )
94
+ )
95
+
96
+ req.options[:headers].merge!(
97
+ { "Authorization" => oauth_helper.header }
98
+ )
99
+
100
+ hydra.queue(req); hydra.run
101
+ response = req.response
102
+
103
+ return JSON.parse(response.body) if response.code == 200
51
104
 
52
- if response.code == '204'
105
+ if response.code == 204
53
106
  puts 'No Content'
54
107
  else
55
108
  raise "#{response.class} #{response.code} \
56
- #{response.message} #{response.body}"
109
+ #{response.status_message} #{response.body}"
57
110
  end
58
111
  end
59
112
  end
data/maas-client.gemspec CHANGED
@@ -32,4 +32,6 @@ Gem::Specification.new do |spec|
32
32
  spec.add_runtime_dependency 'json'
33
33
  spec.add_runtime_dependency 'thor'
34
34
  spec.add_runtime_dependency 'activesupport'
35
+ spec.add_runtime_dependency 'hashie'
36
+ spec.add_runtime_dependency 'typhoeus'
35
37
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: maas-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Don Draper
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-21 00:00:00.000000000 Z
11
+ date: 2018-06-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -150,6 +150,34 @@ dependencies:
150
150
  - - ">="
151
151
  - !ruby/object:Gem::Version
152
152
  version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: hashie
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :runtime
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: typhoeus
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :runtime
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
153
181
  description: A client library that can be used to call MAAS API.
154
182
  email:
155
183
  - donoldfashioned@gmail.com