Class: Cyclid::Client::Api::Base
- Inherits:
-
Object
- Object
- Cyclid::Client::Api::Base
- Defined in:
- lib/cyclid/client/api.rb
Overview
Base class for API request implementations
Instance Method Summary collapse
-
#api_delete(uri) ⇒ Object
Add authentication details perform a DELETE request.
-
#api_get(uri) ⇒ Object
Add authentication details & perform a GET request.
-
#api_json_post(uri, data) ⇒ Object
Add authentication details perform a POST request with a JSON body.
-
#api_json_put(uri, data) ⇒ Object
Add authentication details perform a PUT request with a JSON body.
-
#api_raw_post(uri, data, content_type) ⇒ Object
Add authentication details perform a POST request with a pre-formatted body.
-
#api_request(uri, req) ⇒ Object
Perform an API HTTP request & return the parsed response body.
-
#api_yaml_post(uri, data) ⇒ Object
Add authentication details perform a POST request with a YAML body.
-
#authenticate_request(_request, _uri) ⇒ Object
The API does not support non-authenticated requests, so this method must be implemented.
-
#initialize(config, logger) ⇒ Base
constructor
A new instance of Base.
-
#parse_response(res) ⇒ Object
Parse, validate & handle response data.
Constructor Details
#initialize(config, logger) ⇒ Base
Returns a new instance of Base.
27 28 29 30 |
# File 'lib/cyclid/client/api.rb', line 27 def initialize(config, logger) @config = config @logger = logger end |
Instance Method Details
#api_delete(uri) ⇒ Object
Add authentication details perform a DELETE request
74 75 76 77 78 |
# File 'lib/cyclid/client/api.rb', line 74 def api_delete(uri) req = authenticate_request(Net::HTTP::Delete.new(uri), uri) api_request(uri, req) end |
#api_get(uri) ⇒ Object
Add authentication details & perform a GET request
33 34 35 36 37 |
# File 'lib/cyclid/client/api.rb', line 33 def api_get(uri) req = authenticate_request(Net::HTTP::Get.new(uri), uri) api_request(uri, req) end |
#api_json_post(uri, data) ⇒ Object
Add authentication details perform a POST request with a JSON body
51 52 53 54 |
# File 'lib/cyclid/client/api.rb', line 51 def api_json_post(uri, data) json = Oj.dump(data) api_raw_post(uri, json, 'application/json') end |
#api_json_put(uri, data) ⇒ Object
Add authentication details perform a PUT request with a JSON body
63 64 65 66 67 68 69 70 71 |
# File 'lib/cyclid/client/api.rb', line 63 def api_json_put(uri, data) unsigned = Net::HTTP::Put.new(uri) unsigned.content_type = 'application/json' unsigned.body = Oj.dump(data) req = authenticate_request(unsigned, uri) api_request(uri, req) end |
#api_raw_post(uri, data, content_type) ⇒ Object
Add authentication details perform a POST request with a pre-formatted body
40 41 42 43 44 45 46 47 48 |
# File 'lib/cyclid/client/api.rb', line 40 def api_raw_post(uri, data, content_type) unsigned = Net::HTTP::Post.new(uri) unsigned.content_type = content_type unsigned.body = data req = authenticate_request(unsigned, uri) api_request(uri, req) end |
#api_request(uri, req) ⇒ Object
Perform an API HTTP request & return the parsed response body
81 82 83 84 85 86 87 88 89 |
# File 'lib/cyclid/client/api.rb', line 81 def api_request(uri, req) http = Net::HTTP.new(uri.hostname, uri.port) http.use_ssl = uri.scheme == 'https' http.verify_mode = OpenSSL::SSL::VERIFY_NONE if @config.ssl_verify_none res = http.request(req) parse_response(res) end |
#api_yaml_post(uri, data) ⇒ Object
Add authentication details perform a POST request with a YAML body
57 58 59 60 |
# File 'lib/cyclid/client/api.rb', line 57 def api_yaml_post(uri, data) yaml = YAML.dump(data) api_raw_post(uri, yaml, 'application/x-yaml') end |
#authenticate_request(_request, _uri) ⇒ Object
The API does not support non-authenticated requests, so this method must be implemented.
108 109 110 |
# File 'lib/cyclid/client/api.rb', line 108 def authenticate_request(_request, _uri) raise NotImplementedError end |
#parse_response(res) ⇒ Object
Parse, validate & handle response data
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/cyclid/client/api.rb', line 92 def parse_response(res) response_data = Oj.load(res.body) # Return immediately if the response was an HTTP 200 OK return response_data if res.code == '200' @logger.info "server request failed with error ##{res.code}: \ #{response_data['description']}" raise response_data['description'] rescue Oj::ParseError => ex @logger.debug "body: #{res.body}\n#{ex}" raise 'failed to decode server response body' end |