pub struct Client<S = HyperTransport> { /* private fields */ }Expand description
A client of the TypeSafe API.
Build one with Client::builder, or with Client::from_env when the
environment holds everything. Cloning a client is cheap - the settings and
the transport are shared behind one reference count - so a clone per task
is the way to use one from many tasks, and all of them share one
connection pool.
S is the transport. The default, HyperTransport, is an HTTP/2 client
over TLS; any tower service over http requests is accepted through
ClientBuilder::build_with_service.
Every request runs on the caller’s Tokio runtime, which needs its time driver enabled: each attempt has a deadline, and HTTP/2 keep-alive pings run on a timer.
Implementations§
Source§impl Client<HyperTransport>
impl Client<HyperTransport>
Sourcepub fn builder() -> ClientBuilder
pub fn builder() -> ClientBuilder
A builder for a client; every setting it leaves unset comes from the environment, then from the SDK’s default.
Source§impl<S> Client<S>where
S: HttpService,
impl<S> Client<S>where
S: HttpService,
Sourcepub fn system_one<'a, T>(
&'a self,
state: &'a T,
questions: &'a PreparedQuestions,
) -> SystemOne<'a, S, T>
pub fn system_one<'a, T>( &'a self, state: &'a T, questions: &'a PreparedQuestions, ) -> SystemOne<'a, S, T>
A System One request asking questions about state.
state is anything that serializes to a JSON string, object or array;
it is encoded when the request is sent, straight into the body. The
request is configured with the builder’s methods and sent with
send.
Sourcepub async fn warm_up(&self) -> Result<(), Error>
pub async fn warm_up(&self) -> Result<(), Error>
Lists the models once and drops the answer.
That checks the API key and leaves an open connection in the pool, so requests started together afterwards share it instead of each opening one. Call it before a burst of concurrent requests.
§Errors
Returns what ListModels::send
returns: an authentication failure, for a key the API refuses.
Source§impl<S> Client<S>where
S: HttpService,
Asking a QuestionSet.
impl<S> Client<S>where
S: HttpService,
Asking a QuestionSet.
Sourcepub fn ask<'a, Q>(
&'a self,
state: &'a (impl Serialize + ?Sized),
) -> SystemOne<'a, S, impl Serialize + ?Sized, Q>where
Q: QuestionSet,
pub fn ask<'a, Q>(
&'a self,
state: &'a (impl Serialize + ?Sized),
) -> SystemOne<'a, S, impl Serialize + ?Sized, Q>where
Q: QuestionSet,
A System One request that asks the questions of Q about state and
decodes the answers into a Q.
It is system_one with Q’s prepared questions,
made typed as Q: the same builder, configured
and sent the same way.
The state’s type is not a type parameter of this method, so that
ask::<Ticket>(&state) names only the question set. The price is that
the state’s type is opaque in the returned builder’s type: a function
that takes the builder takes it as a generic. Where the type must be
named,
client.system_one(&state, Ticket::prepared()).typed::<Ticket>() is
the same request with the state’s own type.
use std::time::Duration;
use typesafe_sdk::{Client, NoulAnswer, QuestionSet};
#[derive(QuestionSet)]
struct Spam {
#[noul(instructions = "Is this message spam?")]
spam: NoulAnswer,
}
let client = Client::builder().api_key("your-api-key").build()?;
let request = client.ask::<Spam>("Buy now!").timeout(Duration::from_secs(2));
// `request.send().await?` needs a Tokio runtime and returns a
// `SystemOneResponse<Spam>`, whose `answers().spam` is the answer.
drop(request);