Class: FloopFloop::Client
- Inherits:
-
Object
- Object
- FloopFloop::Client
- Defined in:
- lib/floopfloop/client.rb
Overview
Main entry point. Thread-safe (each request opens its own Net::HTTP session); cheap to construct. See README for the full resource map.
Typical use:
client = FloopFloop::Client.new(api_key: ENV.fetch("FLOOP_API_KEY"))
project = client.projects.create(prompt: "a crypto RSI dashboard")
live = client.projects.wait_for_live(project.fetch("project").fetch("id"))
Constant Summary collapse
- DEFAULT_BASE_URL =
"https://www.floopfloop.com"- DEFAULT_TIMEOUT =
30- USER_AGENT_PREFIX =
"floopfloop-ruby-sdk/#{VERSION}".freeze
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
Instance Method Summary collapse
- #api_keys ⇒ Object
-
#initialize(api_key:, base_url: DEFAULT_BASE_URL, timeout: DEFAULT_TIMEOUT, user_agent_suffix: nil) ⇒ Client
constructor
A new instance of Client.
- #library ⇒ Object
-
#projects ⇒ Object
── Resource accessors ──────────────────────────────────────────.
-
#raw_put(url, body_bytes, content_type:) ⇒ Object
private
Raw PUT used by the Uploads two-step flow to push bytes to the presigned S3 URL.
-
#request(method, path, body: nil, query: nil, raw_body: nil, headers: {}) ⇒ Object
private
Performs the HTTP request, parses the data:… envelope, and raises FloopFloop::Error on non-2xx.
- #secrets ⇒ Object
- #subdomains ⇒ Object
- #uploads ⇒ Object
- #usage ⇒ Object
- #user ⇒ Object
Constructor Details
#initialize(api_key:, base_url: DEFAULT_BASE_URL, timeout: DEFAULT_TIMEOUT, user_agent_suffix: nil) ⇒ Client
Returns a new instance of Client.
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/floopfloop/client.rb', line 26 def initialize(api_key:, base_url: DEFAULT_BASE_URL, timeout: DEFAULT_TIMEOUT, user_agent_suffix: nil) raise ArgumentError, "api_key is required" if api_key.nil? || api_key.empty? @api_key = api_key @base_url = base_url.to_s.sub(%r{/+\z}, "") @timeout = timeout @user_agent = if user_agent_suffix && !user_agent_suffix.to_s.empty? "#{USER_AGENT_PREFIX} #{user_agent_suffix}" else USER_AGENT_PREFIX.dup end end |
Instance Attribute Details
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
24 25 26 |
# File 'lib/floopfloop/client.rb', line 24 def base_url @base_url end |
Instance Method Details
#api_keys ⇒ Object
47 |
# File 'lib/floopfloop/client.rb', line 47 def api_keys; @api_keys ||= ApiKeys.new(self); end |
#library ⇒ Object
45 |
# File 'lib/floopfloop/client.rb', line 45 def library; @library ||= Library.new(self); end |
#projects ⇒ Object
── Resource accessors ──────────────────────────────────────────
42 |
# File 'lib/floopfloop/client.rb', line 42 def projects; @projects ||= Projects.new(self); end |
#raw_put(url, body_bytes, content_type:) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Raw PUT used by the Uploads two-step flow to push bytes to the presigned S3 URL. Does NOT send the bearer token and does NOT go through the data:… envelope — S3 returns empty body on success, XML on error.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/floopfloop/client.rb', line 79 def raw_put(url, body_bytes, content_type:) uri = URI.parse(url) req = Net::HTTP::Put.new(uri.request_uri) req["Content-Type"] = content_type req["Content-Length"] = body_bytes.bytesize.to_s req.body = body_bytes response = open_http(uri).request(req) return if response.code.to_i < 400 snippet = (response.body || "")[0, 512] raise FloopFloop::Error.new( code: "UNKNOWN", message: "uploads: S3 rejected PUT (#{response.code}): #{snippet}", status: response.code.to_i, ) end |
#request(method, path, body: nil, query: nil, raw_body: nil, headers: {}) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Performs the HTTP request, parses the data:… envelope, and raises FloopFloop::Error on non-2xx. Resources call this; user code should not.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/floopfloop/client.rb', line 57 def request(method, path, body: nil, query: nil, raw_body: nil, headers: {}) uri = URI.parse("#{@base_url}#{path}") uri.query = URI.encode_www_form(query) if query && !query.empty? req = build_request(method, uri, body, raw_body, headers) response = perform(uri, req) body_text = response.body || "" request_id = response["x-request-id"] if response.code.to_i >= 400 raise parse_error(body_text, response.code.to_i, request_id, response["retry-after"]) end parse_success(body_text) end |
#secrets ⇒ Object
44 |
# File 'lib/floopfloop/client.rb', line 44 def secrets; @secrets ||= Secrets.new(self); end |
#subdomains ⇒ Object
43 |
# File 'lib/floopfloop/client.rb', line 43 def subdomains; @subdomains ||= Subdomains.new(self); end |
#uploads ⇒ Object
48 |
# File 'lib/floopfloop/client.rb', line 48 def uploads; @uploads ||= Uploads.new(self); end |