abiquo-api 0.0.6 → 0.0.7

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
  SHA1:
3
- metadata.gz: 96a3226fb37881a259d05429a2f9e45af89c614c
4
- data.tar.gz: 692d1dd02caf070b11e79b916dac6186e442e5b4
3
+ metadata.gz: d3c3049291ec1569f72209a814aff513b14b3133
4
+ data.tar.gz: b0c28497ca9fb3fa847e3bec9b0a41d1cb6bf949
5
5
  SHA512:
6
- metadata.gz: 0c9a61c26e512491b40a07f553856e1705bf678047ae2c2f6405ad4fdbd99225fcde3aa0b2a3235bcb6d5615c8053e59021f54a8de1dc922255a7ddad3bea9a5
7
- data.tar.gz: a91b9bb302360c91ca442fc0ecf3eb99f930c630c144b8ec55b8baf048766d9a16508cc6d2f1bd46d63965b26aac26705951da1aeb10b2130113491d9767b116
6
+ metadata.gz: ca26573a649b5e145a6ab34a8ac187b15e4d60b286cdbfe4260c0f765d4f83ae9dfb6f9ecf3057ad2ce74bb77cc9bc0def42efb2e6c5c21a2dbda36c61df7fb7
7
+ data.tar.gz: e9ea6c2637cf89698d9fd38ab51977b781e4529de0f9131f1a1d1bb02fb3c080b2c3ec134c8c66526e35f3694e338c31ab4b7a040ee011cd39d7962a77d3ef83
data/abiquo-api.gemspec CHANGED
@@ -17,6 +17,10 @@ Gem::Specification.new do |gem|
17
17
  gem.require_paths = ["lib"]
18
18
 
19
19
  gem.add_runtime_dependency "excon", '~> 0.43', '>= 0.43.0'
20
+ gem.add_runtime_dependency "faraday", '~> 0.9.2', '>= 0.9.2'
21
+ gem.add_runtime_dependency "faraday_middleware", '~> 0.10.0', '>= 0.10.0'
22
+ gem.add_runtime_dependency "simple_oauth", '~> 0.3.1', '>= 0.3.1'
23
+ gem.add_runtime_dependency "addressable", '~> 2.4.0', '>= 2.4.0'
20
24
  gem.add_runtime_dependency "formatador", '~> 0.2', '>= 0.2.5'
21
25
  gem.add_runtime_dependency "json", '~> 1.8', '>= 1.8.0'
22
26
  end
data/lib/abiquo-api.rb CHANGED
@@ -54,13 +54,32 @@ class AbiquoAPI
54
54
  api_url = options[:abiquo_api_url]
55
55
  api_username = options[:abiquo_username]
56
56
  api_password = options[:abiquo_password]
57
+ api_key = options[:abiquo_api_key]
58
+ api_secret = options[:abiquo_api_secret]
59
+ token_key = options[:abiquo_token_key]
60
+ token_secret = options[:abiquo_token_secret]
57
61
  connection_options = options[:connection_options] || {}
58
62
 
59
- raise "You need to set :abiquo_api_url, :abiquo_username and :abiquo_password" if api_url.nil? or api_username.nil? or api_password.nil?
63
+ raise "You need to set :abiquo_api_url" if api_url.nil?
64
+ raise "You need to provide either basic auth or oauth credentials!!" if (api_username.nil? or api_password.nil?) and
65
+ (api_key.nil? or api_secret.nil? or token_key.nil? or token_secret.nil?)
66
+
67
+ unless api_key.nil?
68
+ credentials = {
69
+ :consumer_key => api_key,
70
+ :consumer_secret => api_secret,
71
+ :token => token_key,
72
+ :token_secret => token_secret
73
+ }
74
+ else
75
+ credentials = {
76
+ :api_username => api_username,
77
+ :api_password => api_password
78
+ }
79
+ end
60
80
 
61
81
  @http_client = AbiquoAPIClient::HTTPClient.new(api_url,
62
- api_username,
63
- api_password,
82
+ credentials,
64
83
  connection_options)
65
84
 
66
85
  api_path = URI.parse(api_url).path
@@ -150,7 +169,6 @@ class AbiquoAPI
150
169
  }
151
170
 
152
171
  req_hash[:accept] = "#{accept}; version=#{@version};" unless accept.eql? ""
153
-
154
172
  resp = @http_client.request(req_hash)
155
173
 
156
174
  if resp['collection'].nil?
@@ -12,7 +12,7 @@ module AbiquoAPIClient
12
12
  else
13
13
  @type = type
14
14
  end
15
-
15
+
16
16
  unless parsed_response['links'].empty?
17
17
  coluri = URI.parse(parsed_response['links'].first['href'])
18
18
  @path = coluri.path
@@ -23,14 +23,17 @@ module AbiquoAPIClient
23
23
  @page_size = opt_hash[:limit].to_i
24
24
 
25
25
  st = opt_hash[:startwith].nil? ? 0 : opt_hash[:startwith].to_i
26
- @current_page = (st / @page_size) + 1
26
+ @current_page = case
27
+ when @page_size == 0 then st
28
+ when @page_size > 0 then (st / @page_size) + 1
29
+ end
27
30
  end
28
-
31
+
29
32
  @links = parsed_response['links']
30
33
  end
31
34
 
32
35
  @collection = parsed_response['collection'].map {|r| client.new_object(r)}
33
-
36
+
34
37
  @client = client
35
38
  end
36
39
 
@@ -64,7 +67,7 @@ module AbiquoAPIClient
64
67
  out = nil
65
68
 
66
69
  each {|i| out = i }
67
-
70
+
68
71
  out
69
72
  end
70
73
 
@@ -114,13 +117,13 @@ module AbiquoAPIClient
114
117
  @collection = next_page.nil? ? [] : next_page
115
118
  end
116
119
 
117
- loop do
120
+ loop do
118
121
  @collection.each do |item|
119
122
  yield item
120
123
  end
121
-
124
+
122
125
  break if @links.nil? or @links.select {|l| l['rel'].eql? "next" }.first.nil?
123
-
126
+
124
127
  next_page = retrieve('next')
125
128
  @collection = next_page.nil? ? [] : next_page
126
129
  end
@@ -160,10 +163,13 @@ module AbiquoAPIClient
160
163
 
161
164
  l = AbiquoAPIClient::Link.new(:href => f['href'],
162
165
  :type => @type)
163
- resp = @client.get(l, opts)
164
-
166
+ resp = @client.get(l, opts)
167
+
165
168
  st = opts[:startwith].nil? ? 0 : opts[:startwith].to_i
166
- @current_page = (st / @page_size) + 1
169
+ @current_page = case
170
+ when @page_size == 0 then st
171
+ when @page_size > 0 then (st / @page_size) + 1
172
+ end
167
173
 
168
174
  @links = resp['links']
169
175
 
@@ -1,5 +1,7 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
1
3
  require 'excon'
2
- require 'uri'
4
+ require 'addressable/uri'
3
5
  require 'json'
4
6
 
5
7
  module AbiquoAPIClient
@@ -10,7 +12,7 @@ module AbiquoAPIClient
10
12
  #
11
13
  class HTTPClient
12
14
  ##
13
- # Excon connection object.
15
+ # Faraday connection object.
14
16
  #
15
17
  attr_reader :connection
16
18
 
@@ -26,25 +28,22 @@ module AbiquoAPIClient
26
28
  #
27
29
  # Parameters:
28
30
  # :abiquo_api_url:: The URL of the Abiquo API. ie. https://yourserver/api
29
- # :abiquo_username:: The username used to connect to the Abiquo API.
30
- # :abiquo_password:: The password for your user.
31
+ # :creds:: The credentials used to connect to the Abiquo API (basic auth or oauth).
31
32
  # :connection_options:: { :connect_timeout => <time_in_secs>, :read_timeout => <time_in_secs>, :write_timeout => <time_in_secs>,
32
33
  # :ssl_verify_peer => <true_or_false>, :ssl_ca_path => <path_to_ca_file> }
33
34
  #
34
- def initialize(api_url, api_username, api_password, connection_options)
35
- Excon.defaults[:ssl_ca_path] = connection_options[:ssl_ca_path] || ''
36
- Excon.defaults[:ssl_verify_peer] = connection_options[:ssl_verify_peer] || false
37
-
38
- connect_timeout = connection_options[:connect_timeout] || 60
39
- read_timeout = connection_options[:read_timeout] || 60
40
- write_timeout = connection_options[:write_timeout] || 60
41
-
42
- @connection = Excon.new(api_url,
43
- :user => api_username,
44
- :password => api_password,
45
- :connect_timeout => connect_timeout,
46
- :read_timeout => read_timeout,
47
- :write_timeout => write_timeout)
35
+ def initialize(api_url, creds, connection_options)
36
+ if creds.has_key? :consumer_key
37
+ @connection = Faraday.new(api_url, connection_options) do |c|
38
+ c.request :oauth, creds
39
+ c.adapter :excon
40
+ end
41
+ else
42
+ @connection = Faraday.new(api_url, connection_options ) do |c|
43
+ c.basic_auth(creds[:api_username], creds[:api_password])
44
+ c.adapter :excon
45
+ end
46
+ end
48
47
 
49
48
  self
50
49
  end
@@ -54,7 +53,7 @@ module AbiquoAPIClient
54
53
  #
55
54
  # Parameters:
56
55
  # [params] A hash of options to be passed to the
57
- # Excon connection.
56
+ # Faraday connection.
58
57
  #
59
58
  # Returns a hash resulting of parsing the response
60
59
  # body as JSON, or nil if the response is "No
@@ -71,17 +70,16 @@ module AbiquoAPIClient
71
70
 
72
71
  # Set Auth cookie and delete user and password if present
73
72
  unless @cookies.nil?
74
- @connection.data.delete(:user) unless @connection.data[:user].nil?
75
- @connection.data.delete(:password) unless @connection.data[:password].nil?
73
+ # @connection.data.delete(:user) unless @connection.data[:user].nil?
74
+ # @connection.data.delete(:password) unless @connection.data[:password].nil?
76
75
  headers.merge!(@cookies)
77
76
  end
77
+ if params.has_key? :headers
78
+ params[:headers].merge!(headers)
79
+ else
80
+ params[:headers] = headers
81
+ end
78
82
 
79
- params[:headers] = headers
80
-
81
- # Correct API path
82
- path = URI.parse(params[:path]).path
83
- params[:path] = path
84
-
85
83
  response = issue_request(params)
86
84
  return nil if response.nil?
87
85
 
@@ -95,59 +93,63 @@ module AbiquoAPIClient
95
93
  private
96
94
 
97
95
  ##
98
- # Issues the HTTP request using the Excon connection
96
+ # Issues the HTTP request using the Faraday connection
99
97
  # object.
100
98
  #
101
99
  # Handles API error codes.
102
100
  #
103
- def issue_request(options)
104
- begin
105
- options[:headers].merge!(@connection.headers)
106
- resp = @connection.request(options)
107
-
108
- # Save cookies
109
- # Get all "Set-Cookie" headers and replace them with "Cookie" header.
110
- @cookies = Hash[resp.headers.select{|k| k.eql? "Set-Cookie" }.map {|k,v| ["Cookie", v] }]
111
-
112
- if resp.data[:status] == 204
113
- nil
114
- else
115
- resp
101
+ def issue_request(params)
102
+ full_path = Addressable::URI.parse(params[:path])
103
+ if full_path.host.nil?
104
+ # only path
105
+ full_url = Addressable::URI.parse(@connection.url_prefix.to_s + '/' + params[:path].gsub(@connection.url_prefix.path, ""))
106
+ else
107
+ full_url = Addressable::URI.parse(params[:path])
108
+ end
109
+ full_url.query_values = params[:query]
110
+
111
+ resp = @connection.run_request(params[:method].downcase.to_sym,
112
+ full_url.to_s,
113
+ params[:body],
114
+ params[:headers])
115
+ # Save cookies
116
+ # Get all "Set-Cookie" headers and replace them with "Cookie" header.
117
+ @cookies = Hash[resp.headers.select{|k| k.eql? "Set-Cookie" }.map {|k,v| ["Cookie", v] }]
118
+
119
+ if resp.status == 204
120
+ nil
121
+ elsif resp.status >= 400 and resp.status <= 600
122
+ unless resp.body.nil?
123
+ begin
124
+ error_response = JSON.parse(resp.body)
125
+ error_code = error_response['collection'][0]['code']
126
+ error_text = error_response['collection'][0]['message']
127
+ rescue JSON::ParserError
128
+ error_code = ''
129
+ error_text = ''
130
+ end
116
131
  end
117
- rescue Excon::Errors::HTTPStatusError => error
118
- case error.response.status
132
+ case resp.status
119
133
  when 401
120
- raise AbiquoAPIClient::InvalidCredentials, "Wrong username or password"
134
+ raise AbiquoAPIClient::InvalidCredentials, "Wrong username or password - #{error_code} - #{error_text}"
121
135
  when 403
122
- raise AbiquoAPIClient::Forbidden, "Not Authorized"
136
+ raise AbiquoAPIClient::Forbidden, "Not Authorized - #{error_code} - #{error_text}"
123
137
  when 406, 400
124
- raise AbiquoAPIClient::BadRequest, "Bad request"
138
+ raise AbiquoAPIClient::BadRequest, "Bad request - #{error_code} - #{error_text}"
125
139
  when 415
126
- raise AbiquoAPIClient::UnsupportedMediaType, "Unsupported mediatype"
140
+ raise AbiquoAPIClient::UnsupportedMediaType, "Unsupported mediatype - #{error_code} - #{error_text}"
127
141
  when 404
128
- begin
129
- error_response = JSON.parse(error.response.body)
130
-
131
- error_code = error_response['collection'][0]['code']
132
- error_text = error_response['collection'][0]['message']
133
-
134
- rescue
135
- raise AbiquoAPIClient::NotFound, "Not Found; #{error_code} - #{error_text}"
136
- end
137
- raise AbiquoAPIClient::NotFound, "Not Found"
142
+ raise AbiquoAPIClient::NotFound, "Not Found - #{error_code} - #{error_text}"
138
143
  else
139
- begin
140
- error_response = JSON.parse(error.response.body)
141
-
142
- error_code = error_response['collection'][0]['code']
143
- error_text = error_response['collection'][0]['message']
144
-
145
- rescue
146
- raise AbiquoAPIClient::Error, error.response.body
144
+ if error_code == '' and error_text == '' # Error and no json response
145
+ raise AbiquoAPIClient::Error, "#{resp.body}"
146
+ else # error with json response
147
+ raise AbiquoAPIClient::Error, "#{error_code} - #{error_text}"
147
148
  end
148
- raise AbiquoAPIClient::Error, "#{error_code} - #{error_text}"
149
- end
149
+ end
150
+ else
151
+ resp
150
152
  end
151
153
  end
152
154
  end
153
- end
155
+ end
@@ -1,3 +1,3 @@
1
1
  module AbiquoAPIClient
2
- VERSION = '0.0.6'
2
+ VERSION = '0.0.7'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: abiquo-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marc Cirauqui
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-02 00:00:00.000000000 Z
11
+ date: 2016-09-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: excon
@@ -30,6 +30,86 @@ dependencies:
30
30
  - - ">="
31
31
  - !ruby/object:Gem::Version
32
32
  version: 0.43.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: faraday
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: 0.9.2
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 0.9.2
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: 0.9.2
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 0.9.2
53
+ - !ruby/object:Gem::Dependency
54
+ name: faraday_middleware
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: 0.10.0
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 0.10.0
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: 0.10.0
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 0.10.0
73
+ - !ruby/object:Gem::Dependency
74
+ name: simple_oauth
75
+ requirement: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - "~>"
78
+ - !ruby/object:Gem::Version
79
+ version: 0.3.1
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 0.3.1
83
+ type: :runtime
84
+ prerelease: false
85
+ version_requirements: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.3.1
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: 0.3.1
93
+ - !ruby/object:Gem::Dependency
94
+ name: addressable
95
+ requirement: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - "~>"
98
+ - !ruby/object:Gem::Version
99
+ version: 2.4.0
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: 2.4.0
103
+ type: :runtime
104
+ prerelease: false
105
+ version_requirements: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: 2.4.0
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: 2.4.0
33
113
  - !ruby/object:Gem::Dependency
34
114
  name: formatador
35
115
  requirement: !ruby/object:Gem::Requirement