Class: Ncio::Api::V1
- Inherits:
-
Object
- Object
- Ncio::Api::V1
- Defined in:
- lib/ncio/api/v1.rb
Overview
Node Classifier API Version 1
See Groups endpoint
Defined Under Namespace
Classes: ApiAuthenticationError, ApiError
Constant Summary collapse
- DEFAULT_HEADERS =
{ 'Content-Type' => 'application/json', }.freeze
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Instance Method Summary collapse
-
#build_uri(path, params = {}) ⇒ URI
Return a URI instance with the given path and parameters.
-
#connection ⇒ Object
Return a memoized HTTP connection.
-
#groups(inherited = false) ⇒ Array
Return all of the groups currently defined in the node classifier API.
-
#import_hierarchy(stream) ⇒ Boolean
Import all of the classification groups using the POST /v1/import-hierarchy endpoint.
-
#initialize(opts) ⇒ V1
constructor
Initialize and return a new API instance.
- #log ⇒ Object
-
#raise_on_non_200(resp, expected_code = 200) ⇒ Object
Handle a non 200 response.
-
#request(req) ⇒ Object
Make a request, return a response.
-
#request_with_timeout(req) ⇒ Object
Make a request respecting the timeout global option.
-
#request_without_timeout(req) ⇒ Object
Make a request without a timeout.
Constructor Details
#initialize(opts) ⇒ V1
Initialize and return a new API instance.
33 34 35 36 37 38 |
# File 'lib/ncio/api/v1.rb', line 33 def initialize(opts) @opts = opts uri = URI(opts[:uri]) @host = uri.host @port = uri.port end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
17 18 19 |
# File 'lib/ncio/api/v1.rb', line 17 def host @host end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
19 20 21 |
# File 'lib/ncio/api/v1.rb', line 19 def opts @opts end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
18 19 20 |
# File 'lib/ncio/api/v1.rb', line 18 def port @port end |
Instance Method Details
#build_uri(path, params = {}) ⇒ URI
Return a URI instance with the given path and parameters. The connection base URI is used to construct the full URI to the service.
151 152 153 154 155 156 |
# File 'lib/ncio/api/v1.rb', line 151 def build_uri(path, params = {}) uri = connection.uri uri.path = "/classifier-api/v1/#{path}" uri.query = URI.encode_www_form(params) uri end |
#connection ⇒ Object
Return a memoized HTTP connection
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/ncio/api/v1.rb', line 46 def connection return @connection if @connection conn_opts = { host: host, port: port, cert: opts[:cert], key: opts[:key], cacert: opts[:cacert] } @connection = Ncio::HttpClient.new(conn_opts) end |
#groups(inherited = false) ⇒ Array
Return all of the groups currently defined in the node classifier API.
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/ncio/api/v1.rb', line 97 def groups(inherited = false) uri = build_uri('groups', inherited: inherited.to_s) req = Net::HTTP::Get.new(uri, DEFAULT_HEADERS) resp = request(req) obj = if resp.code == '200' JSON.parse(resp.body) else raise_on_non_200(resp, 200) end obj end |
#import_hierarchy(stream) ⇒ Boolean
Import all of the classification groups using the POST /v1/import-hierarchy endpoint. See: NC Import Hierarchy
129 130 131 132 133 134 135 136 137 |
# File 'lib/ncio/api/v1.rb', line 129 def import_hierarchy(stream) uri = build_uri('import-hierarchy') req = Net::HTTP::Post.new(uri, DEFAULT_HEADERS) req['Transfer-Encoding'] = ['chunked'] req.body_stream = stream resp = request(req) return true if resp.code == '204' raise_on_non_200(resp, 204) end |
#raise_on_non_200(resp, expected_code = 200) ⇒ Object
Handle a non 200 response.
111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/ncio/api/v1.rb', line 111 def raise_on_non_200(resp, expected_code=200) if resp.code == '401' && %r{rbac/user-unauthenticated}.match(resp.body) obj = JSON.parse(resp.body) msg = obj['msg'] || '401 User Unauthenticated Error' raise ApiAuthenticationError, msg else msg = "Expected #{expected_code} response, got #{resp.code} "\ "body: #{resp.body}" raise ApiError, msg end end |
#request(req) ⇒ Object
Make a request, return a response
81 82 83 84 85 86 87 |
# File 'lib/ncio/api/v1.rb', line 81 def request(req) if opts[:retry_connections] request_with_timeout(req) else request_without_timeout(req) end end |
#request_with_timeout(req) ⇒ Object
Make a request respecting the timeout global option
Assumes the timeout value is available in opts[:connect_timeout]
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/ncio/api/v1.rb', line 62 def request_with_timeout(req) params = { timeout: opts[:connect_timeout], retry_exceptions: [Errno::ECONNREFUSED], log: self.log, } Ncio::Support::RetryAction.retry_action(params) do connection.request(req) end end |
#request_without_timeout(req) ⇒ Object
Make a request without a timeout
75 76 77 |
# File 'lib/ncio/api/v1.rb', line 75 def request_without_timeout(req) connection.request(req) end |