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, max_retries: 0, retry_backoff: 0.5) ⇒ 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, max_retries: 0, retry_backoff: 0.5) ⇒ 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.
3.13.39: max_retries / retry_backoff enable opt-in automatic retry with exponential backoff (default max_retries: 0 = off, non-breaking) on a transport error (APIResponse#status == 0) or a retryable status (429/5xx). A retried non-idempotent request (POST/PUT/PATCH/DELETE) may be re-sent — retries are opt-in for exactly that reason. Parity with the Python master.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/tina4/api.rb', line 35 def initialize(base_url, headers: {}, timeout: 30, bearer_token: nil, username: nil, password: nil, verify_ssl: nil, max_retries: 0, retry_backoff: 0.5) @base_url = base_url.chomp("/") @headers = { "Content-Type" => "application/json", "Accept" => "application/json" }.merge(headers) @timeout = timeout @verify_ssl = verify_ssl @max_retries = [0, max_retries.to_i].max @retry_backoff = retry_backoff.to_f # 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.
16 17 18 |
# File 'lib/tina4/api.rb', line 16 def base_url @base_url end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
16 17 18 |
# File 'lib/tina4/api.rb', line 16 def headers @headers end |
Instance Method Details
#add_headers(headers) ⇒ Object
127 128 129 130 |
# File 'lib/tina4/api.rb', line 127 def add_headers(headers) @headers.merge!(headers) self end |
#delete(path, body: nil) ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/tina4/api.rb', line 96 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
56 57 58 59 60 61 |
# File 'lib/tina4/api.rb', line 56 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
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/tina4/api.rb', line 85 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
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/tina4/api.rb', line 63 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
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/tina4/api.rb', line 74 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
132 133 134 135 136 137 138 139 140 141 |
# File 'lib/tina4/api.rb', line 132 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
117 118 119 120 |
# File 'lib/tina4/api.rb', line 117 def set_basic_auth(username, password) @headers["Authorization"] = "Basic #{Base64.strict_encode64("#{username}:#{password}")}" self end |
#set_bearer_token(token) ⇒ Object
122 123 124 125 |
# File 'lib/tina4/api.rb', line 122 def set_bearer_token(token) @headers["Authorization"] = "Bearer #{token}" self end |
#upload(path, file_path, field_name: "file", extra_fields: {}, headers: {}) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/tina4/api.rb', line 104 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 |