cloudally 0.1.3 → 0.2.1

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: 68a94c91f6e5622ce7e6bec6df252666c423e26b713ee198833ee61c653fee61
4
- data.tar.gz: a3e801fc1226015b89f5223f019272bf7a9a53820787f397d0ea6dfdafbe4801
3
+ metadata.gz: f2f46745c7981091945d40c86bd68e6004dff4ea39ecaa436225c9d5df00a535
4
+ data.tar.gz: 2bf09895b3b8df0b1741f87b41c1f8d2a16c4b5fbb53c0047b9eaa5adcc7e399
5
5
  SHA512:
6
- metadata.gz: c2b4077a590dd0fba1788cf845396ef793944a1c032003fd5e84bc84102f34132494f5ff3c85aba33958ab0aa2a09951d867b5081031021c2c7ce6e7bd620471
7
- data.tar.gz: c96283aa619293b887fc5299db06dbda3c613196de65504230ab8a692fff4359ae020d2a19d67c56ed235474f11492c5af6c52a9d9a22276524355bb4a869965
6
+ metadata.gz: '068f074ab511b4f91073553debfb77f96a81a0e7e82f1c3f02d02618be89c50217acb19fa18f964ee73270e9cb740654b426a159d60d84b846bed9eadceabc2e'
7
+ data.tar.gz: 78b7f883e82d2e0bd2e25654d74223c3409ace9bc1f64d6f374b3ca8e5b565bfece1de7bd822e3ec6d0891ec4102939358d56e91c270380288461d120dd0735a
data/CHANGELOG.md CHANGED
@@ -11,3 +11,9 @@
11
11
 
12
12
  ## [0.1.3] - 2024-01-30
13
13
  - Fix defect in request paging to return all pages when no block is given
14
+
15
+ ## [0.2.0] - 2024-01-30
16
+ - json return for entities
17
+
18
+ ## [0.2.1] - 2024-01-30
19
+ - use wrapi gem
data/README.md CHANGED
@@ -33,8 +33,9 @@ CloudAlly.configure do |config|
33
33
  config.password = ENV["CLOUDALLY_PASSWORD"]
34
34
  config.logger = Logger.new( "./cloudally-http.log" )
35
35
  end
36
- CloudAlly.partner_login
36
+
37
37
  client = CloudAlly.client
38
+ client.partner_login
38
39
  ```
39
40
 
40
41
  ## Resources
data/cloudally.gemspec CHANGED
@@ -29,6 +29,7 @@ Gem::Specification.new do |s|
29
29
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
30
30
  s.platform = Gem::Platform::RUBY
31
31
  s.add_runtime_dependency 'faraday'
32
+ s.add_runtime_dependency 'wrapi', ">= 0.1.3"
32
33
  s.add_development_dependency 'dotenv'
33
34
  s.add_development_dependency 'minitest'
34
35
  s.add_development_dependency 'rubocop'
data/lib/cloudally/api.rb CHANGED
@@ -1,17 +1,16 @@
1
- require File.expand_path('connection', __dir__)
2
- require File.expand_path('request', __dir__)
1
+ require "wrapi"
3
2
  require File.expand_path('authentication', __dir__)
4
3
 
5
4
  module CloudAlly
6
5
  # @private
7
6
  class API
8
7
  # @private
9
- attr_accessor *Configuration::VALID_OPTIONS_KEYS
8
+ attr_accessor *WrAPI::Configuration::VALID_OPTIONS_KEYS
10
9
 
11
10
  # Creates a new API
12
11
  def initialize(options = {})
13
12
  options = CloudAlly.options.merge(options)
14
- Configuration::VALID_OPTIONS_KEYS.each do |key|
13
+ WrAPI::Configuration::VALID_OPTIONS_KEYS.each do |key|
15
14
  send("#{key}=", options[key])
16
15
  end
17
16
  end
@@ -24,8 +23,9 @@ module CloudAlly
24
23
  conf
25
24
  end
26
25
 
27
- include Connection
28
- include Request
26
+ include WrAPI::Connection
27
+ include WrAPI::Request
28
+ include WrAPI::Authentication
29
29
  include Authentication
30
30
  end
31
31
  end
@@ -1,53 +1,29 @@
1
+
1
2
  module CloudAlly
2
3
  # Deals with authentication flow and stores it within global configuration
3
4
  module Authentication
4
5
  # Authorize to the CloudAlly portal and return access_token
5
6
  def auth(options = {})
6
- params = access_token_params.merge(options)
7
- response = post("/auth", params)
8
- # return access_token
9
- process_token(response.body)
7
+ api_auth('/auth', options)
10
8
  end
11
9
  alias login auth
12
10
 
13
11
  # Return an access token from authorization
14
12
  def auth_refresh(token)
15
- params = { refreshToken: token }
16
-
17
- response = post("/auth/refresh", params)
18
- # return access_token
19
- process_token(response.body)
13
+ api_refresh('/auth/refresh', token)
20
14
  end
21
15
 
22
16
  # Authorize to the partner portal and return access_token
23
17
  def auth_partner(options = {})
24
- params = access_token_params.merge(options)
25
- response = post("/auth/partner", params)
26
- # return access_token
27
- process_token(response.body)
18
+ api_auth('/auth/partner', options)
28
19
  end
29
20
  alias partner_login auth_partner
30
-
31
- private
32
-
33
- def access_token_params
21
+ private
22
+ def api_access_token_params
34
23
  {
35
24
  email: username,
36
25
  password: password
37
26
  }
38
27
  end
39
-
40
- def process_token(response)
41
- at = nil
42
- CloudAlly.configure do |config|
43
- at = config.access_token = response["accessToken"]
44
- config.token_type = response["tokenType"]
45
- config.refresh_token = response["refreshToken"]
46
- config.token_expires = response["expiresIn"]
47
- end
48
- raise StandardError.new 'Could not find valid accessToken; response ' + response.to_s if at == '' || at.nil?
49
-
50
- at
51
- end
52
28
  end
53
29
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CloudAlly
4
- VERSION = '0.1.3'
4
+ VERSION = '0.2.1'
5
5
  end
data/lib/cloudally.rb CHANGED
@@ -1,27 +1,29 @@
1
- require File.expand_path('cloudally/configuration', __dir__)
1
+ require "wrapi"
2
+
2
3
  require File.expand_path('cloudally/api', __dir__)
3
4
  require File.expand_path('cloudally/client', __dir__)
4
5
  require File.expand_path('cloudally/version', __dir__)
5
6
 
6
7
  module CloudAlly
7
- extend Configuration
8
+ extend WrAPI::Configuration
9
+ extend WrAPI::RespondTo
10
+
11
+ DEFAULT_ENDPOINT = 'https://api.cloudally.com/v1/'.freeze
12
+ DEFAULT_USERAGENT = "CloudAlly Ruby API wrapper #{CloudAlly::VERSION}".freeze
8
13
 
9
14
  # Alias for CloudAlly::Client.new
10
15
  #
11
16
  # @return [CloudAlly::Client]
12
17
  def self.client(options = {})
13
- CloudAlly::Client.new(options)
14
- end
15
-
16
- # Delegate to CloudAlly::Client
17
- def self.method_missing(method, *args, &block)
18
- return super unless client.respond_to?(method)
19
-
20
- client.send(method, *args, &block)
18
+ CloudAlly::Client.new({
19
+ endpoint: DEFAULT_ENDPOINT,
20
+ user_agent: DEFAULT_USERAGENT
21
+ }.merge(options))
21
22
  end
22
-
23
- # Delegate to CloudAlly::Client
24
- def self.respond_to?(method, include_all = false)
25
- client.respond_to?(method, include_all) || super
23
+
24
+ def self.reset
25
+ super
26
+ self.endpoint = DEFAULT_ENDPOINT
27
+ self.user_agent = DEFAULT_USERAGENT
26
28
  end
27
29
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cloudally
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Janco Tanis
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-01-30 00:00:00.000000000 Z
11
+ date: 2024-02-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: wrapi
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.1.3
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.1.3
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: dotenv
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -84,9 +98,6 @@ files:
84
98
  - lib/cloudally/authentication.rb
85
99
  - lib/cloudally/client.rb
86
100
  - lib/cloudally/client/partners.rb
87
- - lib/cloudally/configuration.rb
88
- - lib/cloudally/connection.rb
89
- - lib/cloudally/request.rb
90
101
  - lib/cloudally/version.rb
91
102
  homepage: https://rubygems.org/gems/cloudally
92
103
  licenses:
@@ -1,86 +0,0 @@
1
- require_relative "./version"
2
- module CloudAlly
3
- # Defines constants and methods related to configuration
4
- module Configuration
5
- # An array of valid keys in the options hash when configuring a {CloudAlly::API}
6
-
7
- VALID_OPTIONS_KEYS = [
8
- :access_token,
9
- :token_type,
10
- :refresh_token,
11
- :token_expires,
12
- :client_id,
13
- :client_secret,
14
- :connection_options,
15
- :username,
16
- :password,
17
- :endpoint,
18
- :logger,
19
- :format,
20
- :page_size,
21
- :user_agent
22
- ].freeze
23
-
24
-
25
- # By default, don't set any connection options
26
- DEFAULT_CONNECTION_OPTIONS = {}
27
-
28
- # The endpoint that will be used to connect if none is set
29
- #
30
- # @note There is no reason to use any other endpoint at this time
31
- DEFAULT_ENDPOINT = "https://api.cloudally.com/v1/".freeze
32
-
33
- # The response format appended to the path and sent in the 'Accept' header if none is set
34
- #
35
- # @note JSON is the only available format at this time
36
- DEFAULT_FORMAT = :json
37
-
38
- # The page size for paged rest responses
39
- #
40
- # @note default JSON is the only available format at this time
41
- DEFAULT_PAGE_SIZE = 500
42
-
43
- # The user agent that will be sent to the API endpoint if none is set
44
- DEFAULT_USER_AGENT = "CloudAlly Ruby API wrapper #{CloudAlly::VERSION}".freeze
45
-
46
- # @private
47
- attr_accessor *VALID_OPTIONS_KEYS
48
-
49
- # When this module is extended, set all configuration options to their default values
50
- def self.extended(base)
51
- base.reset
52
- end
53
-
54
- # Convenience method to allow configuration options to be set in a block
55
- def configure
56
- yield self
57
- end
58
-
59
- # Create a hash of options and their values
60
- def options
61
- VALID_OPTIONS_KEYS.inject({}) do |option, key|
62
- option.merge!(key => send(key))
63
- end
64
- end
65
-
66
- # Reset all configuration options to defaults
67
- def reset
68
- self.access_token = nil
69
- self.token_type = nil
70
- self.refresh_token = nil
71
- self.token_expires = nil
72
- self.client_id = nil
73
- self.client_secret = nil
74
- self.username = nil
75
- self.password = nil
76
-
77
- self.logger = nil
78
- self.connection_options = DEFAULT_CONNECTION_OPTIONS
79
- self.endpoint = DEFAULT_ENDPOINT
80
- self.format = DEFAULT_FORMAT
81
- self.page_size = DEFAULT_PAGE_SIZE
82
- self.user_agent = DEFAULT_USER_AGENT
83
-
84
- end
85
- end
86
- end
@@ -1,42 +0,0 @@
1
- require 'faraday'
2
-
3
- module CloudAlly
4
- # @private
5
- module Connection
6
- private
7
-
8
- def connection
9
- options = {
10
- headers: {
11
- 'Accept': "application/#{format}; charset=utf-8",
12
- 'User-Agent': user_agent
13
- },
14
- url: endpoint
15
- }.merge(connection_options)
16
-
17
- Faraday::Connection.new(options) do |connection|
18
- connection.use Faraday::Response::RaiseError
19
- connection.adapter Faraday.default_adapter
20
-
21
- connection.authorization :Bearer, access_token if access_token
22
- connection.headers['client-id'] = client_id
23
- connection.headers['client-secret'] = client_secret
24
- connection.response :json, content_type: /\bjson$/
25
- connection.use Faraday::Request::UrlEncoded
26
-
27
- setup_logger_filtering(connection,logger) if logger
28
- end
29
- end
30
-
31
- def setup_logger_filtering(connection,logger)
32
- connection.response :logger, logger, { headers: true, bodies: true } do |l|
33
- # filter json content
34
- l.filter(/("password":")(.+?)(".*)/, '\1[REMOVED]\3')
35
- l.filter(/("accessToken":")(.+?)(".*)/, '\1[REMOVED]\3')
36
- # filter header content
37
- l.filter(/(client-secret\:.)([^&]+)/, '\1[REMOVED]')
38
- l.filter(/(Authorization\:.)([^&]+)/, '\1[REMOVED]')
39
- end
40
- end
41
- end
42
- end
@@ -1,121 +0,0 @@
1
- require 'uri'
2
- require 'json'
3
-
4
- module CloudAlly
5
- # Defines HTTP request methods
6
- module Request
7
- class Entity
8
- attr_reader :attributes
9
-
10
- def initialize attributes
11
- @attributes = attributes.clone.transform_keys(&:to_s)
12
- end
13
-
14
- def method_missing(method_sym, *arguments, &block)
15
- len = arguments.length
16
- if method = method_sym[/.*(?==\z)/m]
17
- # assignment
18
- if len != 1
19
- raise! ArgumentError, "wrong number of arguments (given #{len}, expected 1)", caller(1)
20
- end
21
- @attributes[method] = arguments[0]
22
- elsif @attributes.include? method_sym.to_s
23
- r = @attributes[method_sym.to_s]
24
- case r
25
- when Hash
26
- r = @attributes[method_sym.to_s] = self.class.new(r)
27
- when Array
28
- # make deep copy
29
- @attributes[method_sym.to_s] = r = r.map { |item|
30
- self.class.new(item)
31
- } if r.length > 0 && r[0].is_a?(Hash)
32
- r
33
- else
34
- r
35
- end
36
- else
37
- super
38
- end
39
- end
40
-
41
- def respond_to?(method_sym, include_private = false)
42
- if @attributes.include? method_sym.to_s
43
- true
44
- else
45
- super
46
- end
47
- end
48
- end
49
-
50
- # Perform an HTTP GET request and return entity
51
- def get(path, options = {})
52
- response = request(:get, path, options)
53
- :json.eql?(format) ? Entity.new(response.body) : response.body
54
- end
55
-
56
- # Perform an HTTP GET request for paged date sets responsind to
57
- # Name Description
58
- # pageSize The number of records to display per page
59
- # page The page number
60
- # nextPageToken Next page token
61
- def get_paged(path, options = {}, &block)
62
- raise! ArgumentError,
63
- "Pages requests should be json formatted (given format '#{format}')" unless :json.eql? format
64
- result = []
65
- page = 1
66
- total = page + 1
67
- nextPage = ""
68
- while page <= total
69
- # https://api.cloudally.com/v3/api-docs/v1
70
- followingPage = { pageSize: page_size }
71
- followingPage.merge!({ page: page, nextPageToken: nextPage }) unless nextPage.empty?
72
-
73
- response = request(:get, path, options.merge(followingPage))
74
- data = response.body
75
- d = data["data"].map { |e| Entity.new(e) }
76
- if block_given?
77
- yield(d)
78
- else
79
- result += d
80
- end
81
- page += 1
82
- total = data["totalPages"].to_i
83
- nextPage = data["nextPageToken"]
84
- end
85
- result unless block_given?
86
- end
87
-
88
- # Perform an HTTP POST request
89
- def post(path, options = {})
90
- request(:post, path, options)
91
- end
92
-
93
- # Perform an HTTP PUT request
94
- def put(path, options = {})
95
- request(:put, path, options)
96
- end
97
-
98
- # Perform an HTTP DELETE request
99
- def delete(path, options = {})
100
- request(:delete, path, options)
101
- end
102
-
103
- private
104
-
105
- # Perform an HTTP request
106
- def request(method, path, options)
107
- response = connection().send(method) do |request|
108
- uri = URI::Parser.new
109
- case method
110
- when :get, :delete
111
- request.url(uri.escape(path), options)
112
- when :post, :put
113
- request.headers['Content-Type'] = "application/#{format}"
114
- request.path = uri.escape(path)
115
- request.body = options.to_json unless options.empty?
116
- end
117
- end
118
- response
119
- end
120
- end
121
- end