Class: Tryiton::Client
- Inherits:
-
Object
- Object
- Tryiton::Client
- Defined in:
- lib/tryiton/client.rb
Overview
Client for the TryItOn virtual try-on API.
client = Tryiton::Client.new(api_key: ENV["TRYITON_API_KEY"])
job_id = client.try_on_clothes(
model_image: "https://example.com/model.jpg",
garment_image: "https://example.com/tshirt.jpg",
category: "clothing", subcategory: "tops"
)
urls = client.wait_for_result(job_id)
See docs.tryiton.now
Constant Summary collapse
- DEFAULT_BASE_URL =
"https://tryiton.now/api/v1"
Instance Method Summary collapse
-
#get_credits ⇒ Object
Fetch your current credit balance.
-
#get_status(job_id) ⇒ Object
Fetch the current status of a job.
-
#initialize(api_key:, base_url: DEFAULT_BASE_URL, timeout: 60) ⇒ Client
constructor
A new instance of Client.
-
#try_on_clothes(**params) ⇒ Object
Put a garment or accessory on a person.
-
#try_on_hairstyle(**params) ⇒ Object
Restyle a person’s hair.
-
#try_on_tattoo(**params) ⇒ Object
Ink a design onto skin.
-
#wait_for_result(job_id, poll_interval: 2.0, timeout: 120.0) ⇒ Object
Poll a job until it completes, then return the output image URLs.
Constructor Details
#initialize(api_key:, base_url: DEFAULT_BASE_URL, timeout: 60) ⇒ Client
Returns a new instance of Client.
45 46 47 48 49 50 51 |
# File 'lib/tryiton/client.rb', line 45 def initialize(api_key:, base_url: DEFAULT_BASE_URL, timeout: 60) raise Error.new("An api_key is required.", error_name: "ConfigError") if api_key.nil? || api_key.empty? @api_key = api_key @base_url = base_url.sub(%r{/+\z}, "") @timeout = timeout end |
Instance Method Details
#get_credits ⇒ Object
Fetch your current credit balance.
83 84 85 |
# File 'lib/tryiton/client.rb', line 83 def get_credits request(:get, "/credits")["credits"] end |
#get_status(job_id) ⇒ Object
Fetch the current status of a job. Returns { “status” => …, “output” => […], “error” => … }.
77 78 79 80 |
# File 'lib/tryiton/client.rb', line 77 def get_status(job_id) data = request(:get, "/status/#{URI.encode_www_form_component(job_id)}") { "status" => data["status"] || "processing", "output" => data["output"] || [], "error" => data["error"] } end |
#try_on_clothes(**params) ⇒ Object
Put a garment or accessory on a person. Returns the job id. Keys: model_image (required), garment_image (required), category, subcategory, mode, num_samples (1-4), output_format (“png”/“jpeg”), moderation_level (“conservative”/“permissive”/“none”).
57 58 59 |
# File 'lib/tryiton/client.rb', line 57 def try_on_clothes(**params) request(:post, "/tryon/clothes", params)["jobId"] end |
#try_on_hairstyle(**params) ⇒ Object
Restyle a person’s hair. Returns the job id. Keys: face_image (required), haircut (required), hair_color, num_samples (1-4), output_format (“png”/“jpeg”).
64 65 66 |
# File 'lib/tryiton/client.rb', line 64 def try_on_hairstyle(**params) request(:post, "/tryon/hairstyle", params)["jobId"] end |
#try_on_tattoo(**params) ⇒ Object
Ink a design onto skin. Returns the job id. Keys: body_image (required), design_image (required), placement, num_samples (1-4), output_format (“png”/“jpeg”).
71 72 73 |
# File 'lib/tryiton/client.rb', line 71 def try_on_tattoo(**params) request(:post, "/tryon/tattoo", params)["jobId"] end |
#wait_for_result(job_id, poll_interval: 2.0, timeout: 120.0) ⇒ Object
Poll a job until it completes, then return the output image URLs. Raises Tryiton::Error if the job fails or the timeout is reached.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/tryiton/client.rb', line 89 def wait_for_result(job_id, poll_interval: 2.0, timeout: 120.0) deadline = monotonic + timeout loop do status = get_status(job_id) return status["output"] if status["status"] == "completed" if status["status"] == "failed" err = status["error"] || {} raise Error.new(err["message"] || "Try-on failed.", error_name: err["name"] || "ProcessingError") end raise Error.new("Timed out waiting for job #{job_id}.", error_name: "Timeout") if monotonic > deadline sleep(poll_interval) end end |