Class: Tina4::API
- Inherits:
-
Object
- Object
- Tina4::API
- Defined in:
- lib/tina4/api.rb
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
Instance Method Summary collapse
- #add_headers(headers) ⇒ Object
- #delete(path, body: nil) ⇒ Object
- #get(path, params: {}) ⇒ Object
-
#initialize(base_url, headers: {}, timeout: 30, bearer_token: nil, username: nil, password: nil, verify_ssl: nil) ⇒ API
constructor
3.13.1: added ergonomic kwargs (bearer_token, username, password, verify_ssl) so callers no longer need three follow-up setter calls.
- #patch(path, body: nil, content_type: "application/json") ⇒ Object
- #post(path, body: nil, content_type: "application/json") ⇒ Object
- #put(path, body: nil, content_type: "application/json") ⇒ Object
- #send_request(method = "GET", path = "", body: nil, content_type: "application/json") ⇒ Object
- #set_basic_auth(username, password) ⇒ Object
- #set_bearer_token(token) ⇒ Object
- #upload(path, file_path, field_name: "file", extra_fields: {}, headers: {}) ⇒ Object
Constructor Details
#initialize(base_url, headers: {}, timeout: 30, bearer_token: nil, username: nil, password: nil, verify_ssl: nil) ⇒ API
3.13.1: added ergonomic kwargs (bearer_token, username, password, verify_ssl) so callers no longer need three follow-up setter calls. Cross-framework parity with the Python tina4_python.api.Api kwargs.
api = Tina4::API.new("https://api.example.com", bearer_token: "sk-abc")
api = Tina4::API.new("https://api.example.com", username: "u", password: "p")
api = Tina4::API.new("https://api.example.com", headers: {"X-Tenant" => "acme"})
api = Tina4::API.new("https://self-signed.local", verify_ssl: false)
Bearer wins over basic-auth when both are passed.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/tina4/api.rb', line 21 def initialize(base_url, headers: {}, timeout: 30, bearer_token: nil, username: nil, password: nil, verify_ssl: nil) @base_url = base_url.chomp("/") @headers = { "Content-Type" => "application/json", "Accept" => "application/json" }.merge(headers) @timeout = timeout @verify_ssl = verify_ssl # Bearer wins over basic-auth when both passed if bearer_token set_bearer_token(bearer_token) elsif username && password set_basic_auth(username, password) end end |
Instance Attribute Details
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
9 10 11 |
# File 'lib/tina4/api.rb', line 9 def base_url @base_url end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
9 10 11 |
# File 'lib/tina4/api.rb', line 9 def headers @headers end |
Instance Method Details
#add_headers(headers) ⇒ Object
111 112 113 114 |
# File 'lib/tina4/api.rb', line 111 def add_headers(headers) @headers.merge!(headers) self end |
#delete(path, body: nil) ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/tina4/api.rb', line 80 def delete(path, body: nil) uri = build_uri(path) request = Net::HTTP::Delete.new(uri) request.body = body.is_a?(String) ? body : JSON.generate(body) if body apply_headers(request, {}) execute(uri, request) end |
#get(path, params: {}) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/tina4/api.rb', line 40 def get(path, params: {}) uri = build_uri(path, params) request = Net::HTTP::Get.new(uri) apply_headers(request, {}) execute(uri, request) end |
#patch(path, body: nil, content_type: "application/json") ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/tina4/api.rb', line 69 def patch(path, body: nil, content_type: "application/json") uri = build_uri(path) request = Net::HTTP::Patch.new(uri) if body request.body = body.is_a?(String) ? body : JSON.generate(body) request["Content-Type"] = content_type end apply_headers(request, {}) execute(uri, request) end |
#post(path, body: nil, content_type: "application/json") ⇒ Object
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/tina4/api.rb', line 47 def post(path, body: nil, content_type: "application/json") uri = build_uri(path) request = Net::HTTP::Post.new(uri) if body request.body = body.is_a?(String) ? body : JSON.generate(body) request["Content-Type"] = content_type end apply_headers(request, {}) execute(uri, request) end |
#put(path, body: nil, content_type: "application/json") ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/tina4/api.rb', line 58 def put(path, body: nil, content_type: "application/json") uri = build_uri(path) request = Net::HTTP::Put.new(uri) if body request.body = body.is_a?(String) ? body : JSON.generate(body) request["Content-Type"] = content_type end apply_headers(request, {}) execute(uri, request) end |
#send_request(method = "GET", path = "", body: nil, content_type: "application/json") ⇒ Object
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/tina4/api.rb', line 116 def send_request(method = "GET", path = "", body: nil, content_type: "application/json") case method.upcase when "GET" then get(path) when "POST" then post(path, body: body, content_type: content_type) when "PUT" then put(path, body: body, content_type: content_type) when "PATCH" then patch(path, body: body, content_type: content_type) when "DELETE" then delete(path, body: body) else get(path) end end |
#set_basic_auth(username, password) ⇒ Object
101 102 103 104 |
# File 'lib/tina4/api.rb', line 101 def set_basic_auth(username, password) @headers["Authorization"] = "Basic #{Base64.strict_encode64("#{username}:#{password}")}" self end |
#set_bearer_token(token) ⇒ Object
106 107 108 109 |
# File 'lib/tina4/api.rb', line 106 def set_bearer_token(token) @headers["Authorization"] = "Bearer #{token}" self end |
#upload(path, file_path, field_name: "file", extra_fields: {}, headers: {}) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/tina4/api.rb', line 88 def upload(path, file_path, field_name: "file", extra_fields: {}, headers: {}) uri = build_uri(path) boundary = "----Tina4Boundary#{SecureRandom.hex(16)}" body = build_multipart_body(boundary, file_path, field_name, extra_fields) request = Net::HTTP::Post.new(uri) request.body = body request["Content-Type"] = "multipart/form-data; boundary=#{boundary}" headers.each { |k, v| request[k] = v } execute(uri, request) end |