Class: Yield::SDK::API::Client
- Inherits:
-
Object
- Object
- Yield::SDK::API::Client
- Defined in:
- lib/yield/sdk/api/api_client.rb
Class Method Summary collapse
Instance Method Summary collapse
- #build_signature(path, body = nil, now = Time.now) ⇒ Object
-
#initialize(api_key, base_url: nil, faraday_conn: nil) ⇒ Client
constructor
A new instance of Client.
- #run_command(path, payload) ⇒ Object
- #run_query(path, params = nil) ⇒ Object
Constructor Details
#initialize(api_key, base_url: nil, faraday_conn: nil) ⇒ Client
Returns a new instance of Client.
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/yield/sdk/api/api_client.rb', line 44 def initialize(api_key, base_url: nil, faraday_conn: nil) @base_url = base_url || "https://integrate.withyield.com/api/v1" key_parts = api_key.split("$") raise ArgumentError, "Invalid Yield API key" if key_parts.length != 3 @api_key_token = "#{key_parts[0]}$#{key_parts[1]}" @api_key_hmac_key = Base64.urlsafe_decode64(key_parts[2]) @faraday_conn = faraday_conn || Faraday.new @client_version = SDK.client_version end |
Class Method Details
.process_response(response, &from_payload_block) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/yield/sdk/api/api_client.rb', line 17 def self.process_response(response, &from_payload_block) status_code = response.status request_id = response["X-Request-Id"] if !response.success? error_type = "unexpected_error" body = nil begin body = JSON.parse(response.body) error_type = body["error"] if body["error"].is_a?(String) rescue # ignore end return Result.failure(status_code, request_id, error_type) end begin data = from_payload_block.call(JSON.parse(response.body, symbolize_names: true)) rescue return Result.failure(status_code, request_id, "unexpected_payload") end Result.success(status_code, request_id, data) end |
Instance Method Details
#build_signature(path, body = nil, now = Time.now) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/yield/sdk/api/api_client.rb', line 65 def build_signature(path, body = nil, now = Time.now) = now.utc.iso8601 parts = body.nil? ? [, path] : [, path, body] = parts.join("\n") signature_bytes = OpenSSL::HMAC.digest("sha512", @api_key_hmac_key, ) signature_b64 = Base64.urlsafe_encode64(signature_bytes, padding: false) "#{@api_key_token}$#{}$#{signature_b64}" end |
#run_command(path, payload) ⇒ Object
61 62 63 |
# File 'lib/yield/sdk/api/api_client.rb', line 61 def run_command(path, payload) call_endpoint(:post, path, payload) end |
#run_query(path, params = nil) ⇒ Object
56 57 58 59 |
# File 'lib/yield/sdk/api/api_client.rb', line 56 def run_query(path, params = nil) full_path = params.nil? ? path : "#{path}?#{URI.encode_www_form(params)}" call_endpoint(:get, full_path, nil) end |