ibm-cloud-sdk 0.1.3 → 0.1.8
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 +4 -4
- data/CHANGELOG.md +23 -0
- data/Gemfile +2 -0
- data/ibm-cloud-sdk.gemspec +1 -0
- data/lib/ibm/cloud/sdk.rb +3 -0
- data/lib/ibm/cloud/sdk/logging.rb +21 -0
- data/lib/ibm/cloud/sdk/null_logger.rb +19 -0
- data/lib/ibm/cloud/sdk/power_iaas.rb +133 -10
- data/lib/ibm/cloud/sdk/resource_controller.rb +2 -8
- data/lib/ibm/cloud/sdk/version.rb +1 -1
- data/lib/ibm/cloud/sdk/vpc.rb +133 -0
- data/lib/ibm/cloud/sdk/vpc/base_collection.rb +107 -0
- data/lib/ibm/cloud/sdk/vpc/base_instance.rb +23 -0
- data/lib/ibm/cloud/sdk/vpc/base_mixins/has_child.rb +27 -0
- data/lib/ibm/cloud/sdk/vpc/base_vpc.rb +61 -0
- data/lib/ibm/cloud/sdk/vpc/cloud_sdk.rb +33 -0
- data/lib/ibm/cloud/sdk/vpc/exceptions.rb +33 -0
- data/lib/ibm/cloud/sdk/vpc/floatingips.rb +20 -0
- data/lib/ibm/cloud/sdk/vpc/flowlogcollectors.rb +20 -0
- data/lib/ibm/cloud/sdk/vpc/helpers/connection.rb +96 -0
- data/lib/ibm/cloud/sdk/vpc/helpers/response.rb +134 -0
- data/lib/ibm/cloud/sdk/vpc/ike_policies.rb +24 -0
- data/lib/ibm/cloud/sdk/vpc/images.rb +21 -0
- data/lib/ibm/cloud/sdk/vpc/instance/actions.rb +19 -0
- data/lib/ibm/cloud/sdk/vpc/instance/floating_ips.rb +23 -0
- data/lib/ibm/cloud/sdk/vpc/instance/network_interfaces.rb +28 -0
- data/lib/ibm/cloud/sdk/vpc/instance/volume_attachments.rb +23 -0
- data/lib/ibm/cloud/sdk/vpc/instance_profiles.rb +21 -0
- data/lib/ibm/cloud/sdk/vpc/instances.rb +60 -0
- data/lib/ibm/cloud/sdk/vpc/ipsec_policies.rb +24 -0
- data/lib/ibm/cloud/sdk/vpc/keys.rb +43 -0
- data/lib/ibm/cloud/sdk/vpc/load_balancer.rb +23 -0
- data/lib/ibm/cloud/sdk/vpc/load_balancer/listeners.rb +30 -0
- data/lib/ibm/cloud/sdk/vpc/load_balancer/members.rb +23 -0
- data/lib/ibm/cloud/sdk/vpc/load_balancer/policies.rb +30 -0
- data/lib/ibm/cloud/sdk/vpc/load_balancer/pools.rb +28 -0
- data/lib/ibm/cloud/sdk/vpc/load_balancer/rules.rb +25 -0
- data/lib/ibm/cloud/sdk/vpc/load_balancers.rb +19 -0
- data/lib/ibm/cloud/sdk/vpc/network_acls.rb +39 -0
- data/lib/ibm/cloud/sdk/vpc/operating_systems.rb +21 -0
- data/lib/ibm/cloud/sdk/vpc/public_gateways.rb +21 -0
- data/lib/ibm/cloud/sdk/vpc/regions.rb +35 -0
- data/lib/ibm/cloud/sdk/vpc/security_groups.rb +48 -0
- data/lib/ibm/cloud/sdk/vpc/subnets.rb +21 -0
- data/lib/ibm/cloud/sdk/vpc/volume_profiles.rb +21 -0
- data/lib/ibm/cloud/sdk/vpc/volumes.rb +21 -0
- data/lib/ibm/cloud/sdk/vpc/vpcs.rb +60 -0
- data/lib/ibm/cloud/sdk/vpc/vpn_gateway/connections.rb +35 -0
- data/lib/ibm/cloud/sdk/vpc/vpn_gateway/local_cidrs.rb +32 -0
- data/lib/ibm/cloud/sdk/vpc/vpn_gateway/peer_cidrs.rb +32 -0
- data/lib/ibm/cloud/sdk/vpc/vpn_gateways.rb +25 -0
- metadata +60 -3
@@ -0,0 +1,107 @@
|
|
1
|
+
# typed: true
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require_relative('base_mixins/has_child')
|
5
|
+
|
6
|
+
module IBM
|
7
|
+
module Cloud
|
8
|
+
module SDK
|
9
|
+
# Container that encapsulates the VPC API.
|
10
|
+
class BaseCollection < BaseVPC
|
11
|
+
# This class is used as a base for collection APIs.
|
12
|
+
# @param parent [Object] The parent instance in the API chain.
|
13
|
+
# @param endpoint [string] A path from the parent to the desired endpoint. In most cases is should be 1 word.
|
14
|
+
# @param array_key [string] The key that the API response holds the endpoint data. When nil the endpoint will be used.
|
15
|
+
# @param child_class [Object] The Object to be used when instanciating the single instance for this class.
|
16
|
+
def initialize(parent, endpoint, array_key: nil, child_class: nil)
|
17
|
+
# Setup empty base instance variables.
|
18
|
+
@params = nil
|
19
|
+
|
20
|
+
array_key ||= endpoint
|
21
|
+
|
22
|
+
# Set the array key and child class.
|
23
|
+
@array_key ||= array_key
|
24
|
+
@instance ||= child_class
|
25
|
+
|
26
|
+
(class << self; include ChildMixin; end) if child_class
|
27
|
+
|
28
|
+
super(parent, endpoint)
|
29
|
+
end
|
30
|
+
|
31
|
+
# A chainable method to set query filters on the collection.
|
32
|
+
# @example vpc.images.params(limit: 1).all
|
33
|
+
#
|
34
|
+
# @param start [String] A server-supplied token determining what resource to start the page on.
|
35
|
+
# @param limit [Integer] The number of resources to return on a page allowed values are between 1 and 100
|
36
|
+
# @param resource_group [String] Filters the collection to resources within one of the resource groups identified in a comma-separated list of resource group identifiers
|
37
|
+
# @return [BaseCollection] This class with the param instance variable set.
|
38
|
+
def params(start: nil, limit: nil, resource_group: nil)
|
39
|
+
@params = {}
|
40
|
+
@params[:start] = start if start
|
41
|
+
@params[:limit] = limit if limit
|
42
|
+
@params[:resource_group] = resource_group if resource_group
|
43
|
+
self
|
44
|
+
end
|
45
|
+
|
46
|
+
# Retrieve the collection from the cloud.
|
47
|
+
# @return [IBM::Cloud::SDK::VPC::Response] The http response object.
|
48
|
+
def fetch
|
49
|
+
@data ||= get(params: @params)
|
50
|
+
end
|
51
|
+
|
52
|
+
# Get an iterable for the resource collection.
|
53
|
+
# @return [Enumerator] Use standard each, next idioms.
|
54
|
+
def all
|
55
|
+
each_resource(url)
|
56
|
+
end
|
57
|
+
|
58
|
+
# Fetch all data and return in an array.
|
59
|
+
# @return [Array] Hashes of the returned data.
|
60
|
+
def data
|
61
|
+
all.to_a
|
62
|
+
end
|
63
|
+
|
64
|
+
# Determine if the collection has a total_count key in its response.
|
65
|
+
# @return [Boolean]
|
66
|
+
def has_count?
|
67
|
+
fetch.json&.key?(:total_count)
|
68
|
+
end
|
69
|
+
|
70
|
+
# Get the total count if it exists in the response. Returns nil otherwise.
|
71
|
+
# @return [Integer] The total count reuturned by the server.
|
72
|
+
def count
|
73
|
+
fetch.json&.fetch(:total_count, nil)
|
74
|
+
end
|
75
|
+
|
76
|
+
# A generic post method to create a resource on the collection.
|
77
|
+
# @param payload [Hash] A hash of parameters to send to the server.
|
78
|
+
# @param payload_type [String] One of the following options json, form, or body.
|
79
|
+
# @return [IBM::Cloud::SDK::VPC::Response] The http response object.
|
80
|
+
def create(payload, payload_type = 'json')
|
81
|
+
adhoc(method: 'post', payload_type: payload_type, payload: payload)
|
82
|
+
end
|
83
|
+
|
84
|
+
private
|
85
|
+
|
86
|
+
# Create a generator that removes the need for pagination.
|
87
|
+
def each_resource(url, &block)
|
88
|
+
return enum_for(:each_resource, url) unless block_given?
|
89
|
+
return unless url
|
90
|
+
|
91
|
+
response = @connection.adhoc('get', url, metadata(@params)).json
|
92
|
+
resources = response.fetch(@array_key.to_sym)
|
93
|
+
|
94
|
+
resources&.each do |value|
|
95
|
+
yield value
|
96
|
+
end
|
97
|
+
return unless response.key?(:next)
|
98
|
+
|
99
|
+
next_url = response.dig(:next, :href)
|
100
|
+
return unless next_url
|
101
|
+
|
102
|
+
each_resource(next_url, &block)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# typed: true
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
module IBM
|
5
|
+
module Cloud
|
6
|
+
module SDK
|
7
|
+
# Container that encapsulates the VPC API.
|
8
|
+
class BaseInstance < BaseVPC
|
9
|
+
def details
|
10
|
+
get.hash_response
|
11
|
+
end
|
12
|
+
|
13
|
+
def update(payload)
|
14
|
+
patch(payload)
|
15
|
+
end
|
16
|
+
|
17
|
+
def remove
|
18
|
+
delete
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# typed: true
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
module IBM
|
5
|
+
module Cloud
|
6
|
+
module SDK
|
7
|
+
# Access a specific instance by either id or name depending on API.
|
8
|
+
module ChildMixin
|
9
|
+
# Get an instance of the collection object.
|
10
|
+
# @param id [String] ID of Name to search on depending on API.
|
11
|
+
# @return [] The instance object.
|
12
|
+
def instance(id)
|
13
|
+
@instance.new(self, id)
|
14
|
+
end
|
15
|
+
|
16
|
+
# Return the first_instance returned from a collection get.
|
17
|
+
def first_instance
|
18
|
+
result = params(limit: 1).all.first
|
19
|
+
return nil unless result
|
20
|
+
return nil unless result.key?(:id)
|
21
|
+
|
22
|
+
instance(result.fetch(:id))
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# typed: true
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
module IBM
|
5
|
+
module Cloud
|
6
|
+
module SDK
|
7
|
+
# Container that encapsulates the VPC API.
|
8
|
+
class BaseVPC
|
9
|
+
def initialize(parent, endpoint = nil)
|
10
|
+
@endpoint = parent.url(endpoint)
|
11
|
+
@connection = parent.connection
|
12
|
+
@logger = parent.logger
|
13
|
+
end
|
14
|
+
|
15
|
+
attr_reader :endpoint, :connection, :logger
|
16
|
+
|
17
|
+
def adhoc(method: 'get', path: nil, params: {}, payload_type: 'json', payload: {})
|
18
|
+
@connection.adhoc(method.to_sym, url(path), metadata(params, payload_type, payload))
|
19
|
+
end
|
20
|
+
|
21
|
+
def get(path: nil, params: {})
|
22
|
+
adhoc(method: 'get', path: path, params: params)
|
23
|
+
end
|
24
|
+
|
25
|
+
def post(payload = {}, path: nil, params: {}, type: 'json')
|
26
|
+
adhoc(method: 'post', path: path, params: params, payload: payload, payload_type: type)
|
27
|
+
end
|
28
|
+
|
29
|
+
def put(payload = {}, path: nil, params: {})
|
30
|
+
adhoc(method: 'put', path: path, params: params, payload: payload)
|
31
|
+
end
|
32
|
+
|
33
|
+
def patch(payload = {}, path: nil, params: {})
|
34
|
+
adhoc(method: 'patch', path: path, params: params, payload: payload)
|
35
|
+
end
|
36
|
+
|
37
|
+
def delete(path: nil, params: {})
|
38
|
+
adhoc(method: 'delete', path: path, params: params)
|
39
|
+
end
|
40
|
+
|
41
|
+
def url(path = nil)
|
42
|
+
return endpoint unless path
|
43
|
+
|
44
|
+
"#{endpoint}/#{path}"
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def metadata(params = {}, payload_type = 'json', payload = {})
|
50
|
+
params ||= {}
|
51
|
+
pt = {
|
52
|
+
params: { version: '2020-08-01', generation: 2 }.merge(params)
|
53
|
+
}
|
54
|
+
|
55
|
+
pt[payload_type.to_sym] = payload unless payload.empty?
|
56
|
+
pt
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'http'
|
4
|
+
require 'ibm/cloud/sdk/vpc/helpers/connection'
|
5
|
+
require 'ibm/cloud/sdk/vpc'
|
6
|
+
|
7
|
+
module IBM
|
8
|
+
# Holds the SDK pieces.
|
9
|
+
class CloudSDK
|
10
|
+
def initialize(api_key, logger: nil, options: {})
|
11
|
+
@logger = logger
|
12
|
+
@logger ||= Logger.new($stdout, level: :warn)
|
13
|
+
|
14
|
+
@client = HTTP.use(http_options(options))
|
15
|
+
@connection = IBM::Cloud::SDK::VPC::Connection.new(api_key, logger: @logger, client: @client)
|
16
|
+
end
|
17
|
+
|
18
|
+
def http_options(options = {})
|
19
|
+
options.merge(
|
20
|
+
{
|
21
|
+
logging: { logger: @logger },
|
22
|
+
auto_deflate: {}
|
23
|
+
}
|
24
|
+
)
|
25
|
+
end
|
26
|
+
|
27
|
+
attr_reader :logger, :connection
|
28
|
+
|
29
|
+
def vpc(region = 'us-east')
|
30
|
+
IBM::Cloud::SDK::Vpc.new(region, @connection, logger: @logger)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module IBM
|
4
|
+
module Cloud
|
5
|
+
module SDK
|
6
|
+
module VPC
|
7
|
+
# Module to contain all custom exception classes.
|
8
|
+
module Exceptions
|
9
|
+
# An exception for http with a response attribute.
|
10
|
+
# @param response [IBM::Cloud::SDK::VPC::Response] The original response object.
|
11
|
+
# @param msg [String] A human readable message.
|
12
|
+
class ExceptionWithResponse < RuntimeError
|
13
|
+
def initialize(response, msg)
|
14
|
+
@response = response
|
15
|
+
super(msg)
|
16
|
+
end
|
17
|
+
# @return [IBM::Cloud::SDK::VPC::Response] The response
|
18
|
+
attr_reader :response
|
19
|
+
end
|
20
|
+
|
21
|
+
# An exception for http status errors with a response attribute.
|
22
|
+
# @param response [IBM::Cloud::SDK::VPC::Response] The original response object.
|
23
|
+
class HttpStatusError < ExceptionWithResponse
|
24
|
+
def initialize(response)
|
25
|
+
msg = "Invalid status #{response.code} for url \"#{response.url}\", #{response.reason}. #{response.body}"
|
26
|
+
super(response, msg)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# typed: false
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
module IBM
|
5
|
+
module Cloud
|
6
|
+
module SDK
|
7
|
+
module VPC
|
8
|
+
# Class that deals with groups of floating IPs.
|
9
|
+
class FloatingIPs < BaseCollection
|
10
|
+
def initialize(parent)
|
11
|
+
super(parent, 'floating_ips', child_class: FloatingIP)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
# Class that deals with a single floating IP.
|
16
|
+
class FloatingIP < BaseInstance; end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# typed: true
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
module IBM
|
5
|
+
module Cloud
|
6
|
+
module SDK
|
7
|
+
module VPC
|
8
|
+
# A list of subnets
|
9
|
+
class FlowLogCollectors < BaseCollection
|
10
|
+
def initialize(parent)
|
11
|
+
super(parent, 'flow_log_collectors', child_class: FlowLogCollectors)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
# A single subnet
|
16
|
+
class FlowLogCollector < BaseInstance; end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'time'
|
4
|
+
require 'http'
|
5
|
+
require_relative 'response'
|
6
|
+
|
7
|
+
module IBM
|
8
|
+
module Cloud
|
9
|
+
module SDK
|
10
|
+
module VPC
|
11
|
+
# The Connection object to be used for all HTTP requests.
|
12
|
+
# @param api_key [String] The API Key to be used for this account.
|
13
|
+
# @param logger [Logger] An instantiated logger instance.
|
14
|
+
# @param client [HTTP::Client] An instantiated HTTP client.
|
15
|
+
class Connection
|
16
|
+
def initialize(api_key, logger: nil, client: nil)
|
17
|
+
@api_key = api_key
|
18
|
+
@logger = logger
|
19
|
+
@client = client
|
20
|
+
@token = Token.new(api_key, logger: logger, client: client)
|
21
|
+
end
|
22
|
+
|
23
|
+
attr_reader :client
|
24
|
+
|
25
|
+
# Send a HTTP request. Checks the validity of the response.
|
26
|
+
# @param verb [String] THe HTTP verb to use for this request.
|
27
|
+
# @param uri [String] The Full URL to send.
|
28
|
+
# @option opts [Hash] :params Parameters for URL encoding parameters.
|
29
|
+
# @option opts [Hash] :json Used when sending a hash as application/json content type.
|
30
|
+
# @option opts [Hash] :form Used when sending a hash as a form.
|
31
|
+
# @option opts [Hash] :headers Used to modify headers for request.
|
32
|
+
# @return [IBM::Cloud::SDK::VPC::Response] Wrapped response to query.
|
33
|
+
# @raise [IBM::Cloud::SDK::VPC::Exceptions::HttpStatusError] Response code is not either in 200-series or 404.
|
34
|
+
def adhoc(verb, uri, opts = {})
|
35
|
+
unverified_request(verb, uri, opts).raise_for_status?
|
36
|
+
end
|
37
|
+
|
38
|
+
# Send a HTTP request. Don't do any validation checks.
|
39
|
+
# @see :adhoc for options.
|
40
|
+
def unverified_request(verb, uri, opts = {})
|
41
|
+
response = @client.auth(@token.authorization_header).request(verb.to_s.downcase.to_sym, uri, opts)
|
42
|
+
Response.new(response)
|
43
|
+
end
|
44
|
+
|
45
|
+
# Get bearer token string for clients not using the adhoc method.
|
46
|
+
def authorization_header
|
47
|
+
@token.authorization_header
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# The IAM token manager.
|
52
|
+
# @param api_key [String] The API Key to be used for this account.
|
53
|
+
# @param logger [Logger] An instantiated logger instance.
|
54
|
+
# @param client [HTTP::Client] An instantiated HTTP client.
|
55
|
+
class Token
|
56
|
+
def initialize(api_key, logger: nil, client: nil)
|
57
|
+
@api_key = api_key
|
58
|
+
@logger = logger
|
59
|
+
@client = client
|
60
|
+
@data = fetch
|
61
|
+
end
|
62
|
+
|
63
|
+
# Retrieve a new access_token from IAM.
|
64
|
+
# @return [IBM::Cloud::SDK::VPC::Response] Wrapped response to query.
|
65
|
+
# @raise [IBM::Cloud::SDK::VPC::Exceptions::HttpStatusError] Response code is not either in 200-series or 404.
|
66
|
+
def fetch
|
67
|
+
payload = {
|
68
|
+
form: {
|
69
|
+
grant_type: 'urn:ibm:params:oauth:grant-type:apikey',
|
70
|
+
apikey: @api_key
|
71
|
+
}
|
72
|
+
}
|
73
|
+
response = HTTP.post('https://iam.cloud.ibm.com/identity/token', payload)
|
74
|
+
@data = Response.new(response).raise_for_status?.json
|
75
|
+
end
|
76
|
+
|
77
|
+
# Check to see if the access_token is expired. Fetch a new token if none exists.
|
78
|
+
# @return [IBM::Cloud::SDK::VPC::Response] Wrapped response to query.
|
79
|
+
# @raise [IBM::Cloud::SDK::VPC::Exceptions::HttpStatusError] Response code is not either in 200-series or 404.
|
80
|
+
def expired?
|
81
|
+
fetch unless @data
|
82
|
+
@data.fetch(:expiration).to_i <= Time.now.to_i + 600
|
83
|
+
end
|
84
|
+
|
85
|
+
# Get a Bearer token string. Before returning check to see if token is expired.
|
86
|
+
# @return [String] The Bearer token header used in subsequent requests.
|
87
|
+
# @raise [IBM::Cloud::SDK::VPC::Exceptions::HttpStatusError] Response code is not either in 200-series or 404.
|
88
|
+
def authorization_header
|
89
|
+
fetch if expired?
|
90
|
+
"#{@data.fetch(:token_type)} #{@data.fetch(:access_token)}"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,134 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'time'
|
4
|
+
require 'http'
|
5
|
+
|
6
|
+
module IBM
|
7
|
+
module Cloud
|
8
|
+
module SDK
|
9
|
+
module VPC
|
10
|
+
# Encapsulate the HTTP response.
|
11
|
+
# @param response [HTTP::Response] The HTTP response object.
|
12
|
+
class Response
|
13
|
+
def initialize(response)
|
14
|
+
@response = response
|
15
|
+
end
|
16
|
+
|
17
|
+
# The raw HTTP response.
|
18
|
+
# @return [HTTP::Response]
|
19
|
+
attr_reader :response
|
20
|
+
|
21
|
+
# Return the response in a hash or array.
|
22
|
+
# @return [Hash] When response is a hash.
|
23
|
+
# @return [Array] When response is an array.
|
24
|
+
# @raise [Exceptions::ExceptionWithResponse] Contents of body is not properly formatted json.
|
25
|
+
def json
|
26
|
+
JSON.parse(body, symbolize_names: true)
|
27
|
+
rescue StandardError
|
28
|
+
raise Exceptions::ExceptionWithResponse.new(self, "#{url} Error while parsing response body. #{response.body}")
|
29
|
+
end
|
30
|
+
|
31
|
+
# Return the raw response string.
|
32
|
+
# @return [String]
|
33
|
+
# @return [nil] Response does not have body method.
|
34
|
+
def body
|
35
|
+
response&.body.to_s
|
36
|
+
end
|
37
|
+
|
38
|
+
# Return the response code.
|
39
|
+
# @return [Integer] Response has code method.
|
40
|
+
# @return [nil] Response does not have code method.
|
41
|
+
def code
|
42
|
+
response&.code
|
43
|
+
end
|
44
|
+
|
45
|
+
alias status code
|
46
|
+
|
47
|
+
# Return the raw connection object.
|
48
|
+
# @return [HTTP::Connection]
|
49
|
+
# @return [nil] Response does not have a connection method.
|
50
|
+
def connection
|
51
|
+
response&.connection
|
52
|
+
end
|
53
|
+
|
54
|
+
# Chainable method to verify the status code. Raise an exception for non 200-series or 404 status codes.
|
55
|
+
# @return [Response] Allows for method to be chainable.
|
56
|
+
# @raise [Exceptions::HttpStatusError] Raise if status checks failed.
|
57
|
+
def raise_for_status?
|
58
|
+
return self if (200..299).include?(code)
|
59
|
+
return self if code == 404
|
60
|
+
|
61
|
+
raise Exceptions::HttpStatusError.new(self)
|
62
|
+
end
|
63
|
+
|
64
|
+
# Return the content type of the response.
|
65
|
+
# @return [String] The mimetype of the response.
|
66
|
+
# @return [nil] Response does not have response method that responds to mime_type.
|
67
|
+
def content_type
|
68
|
+
response&.response&.mime_type
|
69
|
+
end
|
70
|
+
|
71
|
+
# Return the textual reason.
|
72
|
+
# @return [String] HTTP Reason
|
73
|
+
# @return [nil] Response does not have reaspn method that responds.
|
74
|
+
def reason
|
75
|
+
response&.reason
|
76
|
+
end
|
77
|
+
|
78
|
+
# Return the sent url as a string.
|
79
|
+
# @return [String] Full URL sent
|
80
|
+
# @return [nil] Response does not have response method that responds to mime_type.
|
81
|
+
def url
|
82
|
+
response&.uri.to_s
|
83
|
+
end
|
84
|
+
|
85
|
+
# Return the sent url as a URI class.
|
86
|
+
# @see https://github.com/httprb/http/blob/master/lib/http/uri.rb URI Class doc.
|
87
|
+
# @return [HTTP::URI]
|
88
|
+
# @return [nil] Response does not have response method that responds to mime_type.
|
89
|
+
def uri
|
90
|
+
response&.uri
|
91
|
+
end
|
92
|
+
|
93
|
+
# Verify that the json response is a hash.
|
94
|
+
# @return [Hash] Response from JSON
|
95
|
+
# @raise [RuntimeError] JSON object is not a Hash.
|
96
|
+
def hash_response
|
97
|
+
check_object(Hash)
|
98
|
+
end
|
99
|
+
|
100
|
+
# Verify that the json response is an array.
|
101
|
+
# @return [Array] Response from JSON
|
102
|
+
# @raise [RuntimeError] JSON object is not a Array.
|
103
|
+
def array_response
|
104
|
+
check_object(Array)
|
105
|
+
end
|
106
|
+
|
107
|
+
# Find a subkey within the returned response.
|
108
|
+
# @param key [String] Name of a first level key.
|
109
|
+
# @return [Any] Response from JSON
|
110
|
+
# @raise [RuntimeError] JSON object is not a Array.
|
111
|
+
def subkey(key)
|
112
|
+
ret = hash_response
|
113
|
+
sym_key = key.to_sym
|
114
|
+
return ret.fetch(sym_key) if ret.key?(sym_key)
|
115
|
+
|
116
|
+
msg = "Key #{key} not found in #{ret}."
|
117
|
+
raise Exceptions::ExceptionWithResponse.new(self, msg)
|
118
|
+
end
|
119
|
+
|
120
|
+
# Check to see if the returned object is the expected object.
|
121
|
+
# @param obj [Object] The object to test the response against.
|
122
|
+
# @raise [Exceptions::ExceptionWithResponse] Parsed JSON is not the expecte class.
|
123
|
+
def check_object(obj)
|
124
|
+
ret = json
|
125
|
+
return ret if ret.instance_of?(obj)
|
126
|
+
|
127
|
+
msg = "Expected #{obj} in response for #{url}. The returned object is a #{ret.class}."
|
128
|
+
raise Exceptions::ExceptionWithResponse.new(self, msg)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|