Class: Central::Client
- Inherits:
-
Object
- Object
- Central::Client
- Defined in:
- lib/central/client.rb
Instance Attribute Summary collapse
-
#default_headers ⇒ Object
Returns the value of attribute default_headers.
-
#http_client ⇒ Object
readonly
Returns the value of attribute http_client.
Instance Method Summary collapse
-
#delete(path, body = nil, params = {}, headers = {}) ⇒ Hash
Delete request.
-
#get(path, params = nil, headers = {}) ⇒ Hash
Get request.
-
#get_stream(path, response_block, params = nil, headers = {}) ⇒ Object
Get request.
-
#initialize(api_url, default_headers = {}) ⇒ Client
constructor
Initialize api client.
-
#post(path, obj, params = {}, headers = {}) ⇒ Hash
Post request.
-
#put(path, obj, params = {}, headers = {}) ⇒ Hash
Put request.
Constructor Details
#initialize(api_url, default_headers = {}) ⇒ Client
Initialize api client
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/central/client.rb', line 15 def initialize(api_url, default_headers = {}) Excon.defaults[:ssl_verify_peer] = false if ignore_ssl_errors? @http_client = Excon.new(api_url) @default_headers = { 'Accept' => 'application/json', 'Content-Type' => 'application/json', 'User-Agent' => "central-cli/#{Central::Cli::VERSION}" }.merge(default_headers) @api_url = api_url end |
Instance Attribute Details
#default_headers ⇒ Object
Returns the value of attribute default_headers.
8 9 10 |
# File 'lib/central/client.rb', line 8 def default_headers @default_headers end |
#http_client ⇒ Object (readonly)
Returns the value of attribute http_client.
9 10 11 |
# File 'lib/central/client.rb', line 9 def http_client @http_client end |
Instance Method Details
#delete(path, body = nil, params = {}, headers = {}) ⇒ Hash
Delete request
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/central/client.rb', line 116 def delete(path, body = nil, params = {}, headers = {}) request_headers = request_headers(headers) = { path: request_uri(path), headers: request_headers, body: encode_body(body, request_headers['Content-Type']), query: params } response = http_client.delete() if response.status == 200 parse_response(response) else handle_error_response(response) end end |
#get(path, params = nil, headers = {}) ⇒ Hash
Get request
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/central/client.rb', line 32 def get(path, params = nil, headers = {}) response = http_client.get( path: request_uri(path), query: params, headers: request_headers(headers) ) if response.status == 200 parse_response(response) else handle_error_response(response) end end |
#get_stream(path, response_block, params = nil, headers = {}) ⇒ Object
Get request
51 52 53 54 55 56 57 58 59 |
# File 'lib/central/client.rb', line 51 def get_stream(path, response_block, params = nil, headers = {}) http_client.get( read_timeout: 360, path: request_uri(path), query: params, headers: request_headers(headers), response_block: response_block ) end |
#post(path, obj, params = {}, headers = {}) ⇒ Hash
Post request
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/central/client.rb', line 68 def post(path, obj, params = {}, headers = {}) request_headers = request_headers(headers) = { path: request_uri(path), headers: request_headers, body: encode_body(obj, request_headers['Content-Type']), query: params } response = http_client.post() if [200, 201].include?(response.status) parse_response(response) else handle_error_response(response) end end |
#put(path, obj, params = {}, headers = {}) ⇒ Hash
Put request
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/central/client.rb', line 92 def put(path, obj, params = {}, headers = {}) request_headers = request_headers(headers) = { path: request_uri(path), headers: request_headers, body: encode_body(obj, request_headers['Content-Type']), query: params } response = http_client.put() if [200, 201].include?(response.status) parse_response(response) else handle_error_response(response) end end |