pub struct UtApi { /* private fields */ }Expand description
The UtApi struct represents the client for interacting with the Uploadthing API.
It contains the configuration for the service and the HTTP client used to make requests.
Implementations§
Source§impl UtApi
impl UtApi
Sourcepub fn new(api_key: Option<String>) -> UtApi
pub fn new(api_key: Option<String>) -> UtApi
Creates a new instance of UtApi.
This constructor initializes the UtApi struct with the provided API key
or, if none is provided, attempts to retrieve the API key from the environment.
It sets up the UploadthingConfig and the internal Client for HTTP requests.
§Arguments
api_key- AnOption<String>that holds the API key for authentication. IfNone, the API key is retrieved from the environment.
§Examples
// Create a new API client with a provided API key.
let api_with_key = UtApi::new(Some("your_api_key".to_string()));
// Create a new API client using the API key from the environment.
let api_with_env_key = UtApi::new(None);§Returns
Returns a new UtApi struct initialized with the provided or environment API key
and a new Client.
§Panics
Panics if the API key is not provided and is also not set in the environment.
Sourcepub fn from_config(config: UploadthingConfig) -> UtApi
pub fn from_config(config: UploadthingConfig) -> UtApi
Creates a new instance of UtApi from a given UploadthingConfig.
§Arguments
config- AnUploadthingConfiginstance containing the configuration for the service.
§Examples
let config = UploadthingConfig::builder().api_key("your_api_key").build();
let api = UtApi::from_config(config);§Returns
Returns a new UtApi struct initialized with the provided configuration and a new Client.
Sourcepub async fn request_uploadthing<T: Serialize>(
&self,
pathname: &str,
payload: &T,
) -> Result<Response, Error>
pub async fn request_uploadthing<T: Serialize>( &self, pathname: &str, payload: &T, ) -> Result<Response, Error>
Sends a POST request to the Uploadthing service.
This method constructs a URL using the pathname and the host from the configuration,
then sends a POST request with the provided payload serialized as JSON.
It also sets necessary headers, including the user agent, API key, and version,
as well as a Cache-Control header to prevent caching.
§Type Parameters
T: The type of thepayloadthat implementsSerialize.
§Parameters
pathname: The endpoint path to which the request will be sent.payload: The data to be serialized and sent as the request body.
§Returns
A Result with the HTTP Response if the request was successful,
or an Error boxed in a Box<dyn Error> if the request failed.
§Errors
If the response status is not a success, this function will return an Error
containing the HTTP error returned by the server.
Sourcepub async fn delete_files(
&self,
file_keys: Vec<String>,
) -> Result<DeleteFileResponse, Box<dyn Error>>
pub async fn delete_files( &self, file_keys: Vec<String>, ) -> Result<DeleteFileResponse, Box<dyn Error>>
Sends a DELETE request to the Uploadthing service to delete a list of files.
This method accepts a list of file keys and constructs a payload to send to the
/api/deleteFile endpoint. It then calls the request_uploadthing method to
perform the actual request.
§Parameters
file_keys: AVec<String>containing the keys of the files to be deleted.
§Returns
A Result with a DeleteFileResponse if the deletion was successful,
or an Error boxed in a Box<dyn Error> if the request failed.
§Errors
If the response status is not a success, or if the response cannot be deserialized
into a DeleteFileResponse, this function will return an Error.
Sourcepub async fn get_file_urls(
&self,
file_keys: Vec<String>,
) -> Result<UploadthingUrlsResponse, Box<dyn Error>>
pub async fn get_file_urls( &self, file_keys: Vec<String>, ) -> Result<UploadthingUrlsResponse, Box<dyn Error>>
Retrieves the URLs for a list of file keys from the Uploadthing service.
§Parameters
file_keys: AVec<String>containing the keys of the files whose URLs are to be retrieved.
§Returns
A Result with a UploadthingUrlsResponse if the retrieval was successful,
or an Error boxed in a Box<dyn Error> if the request failed.
§Errors
If the response status is not a success, or if the response cannot be deserialized
into a UploadthingUrlsResponse, this function will return an Error.
Sourcepub async fn list_files(
&self,
opts: Option<ListFilesOpts>,
) -> Result<UploadthingFileResponse, Box<dyn Error>>
pub async fn list_files( &self, opts: Option<ListFilesOpts>, ) -> Result<UploadthingFileResponse, Box<dyn Error>>
Lists files stored in Uploadthing service.
§Parameters
opts: An optionalListFilesOptsstruct with parameters to control pagination.
§Returns
A Result with a UploadthingFileResponse if the retrieval was successful,
or an Error boxed in a Box<dyn Error> if the request failed.
§Errors
If the response status is not a success, or if the response cannot be deserialized
into a UploadthingFileResponse, this function will return an Error.
Sourcepub async fn rename_files(
&self,
files: RenameFilesOpts,
) -> Result<(), Box<dyn Error>>
pub async fn rename_files( &self, files: RenameFilesOpts, ) -> Result<(), Box<dyn Error>>
Renames files in the Uploadthing service according to the given options.
§Parameters
files: ARenameFilesOptsstruct with the file keys and new names for renaming.
§Returns
An Ok result if the renaming operation was successful,
or an Error boxed in a Box<dyn Error> if the request failed.
§Errors
If the response status is not a success, this function will return an Error.
Sourcepub async fn get_usage_info(
&self,
) -> Result<UploadthingUsageInfo, Box<dyn Error>>
pub async fn get_usage_info( &self, ) -> Result<UploadthingUsageInfo, Box<dyn Error>>
Gets usage information for the current Uploadthing account.
§Returns
A Result with a UploadthingUsageInfo if the retrieval was successful,
or an Error boxed in a Box<dyn Error> if the request failed.
§Errors
If the response status is not a success, or if the response cannot be deserialized
into an UploadthingUsageInfo, this function will return an Error.
Sourcepub async fn get_presigned_url(
&self,
opts: PresignedUrlOpts,
) -> Result<String, Box<dyn Error>>
pub async fn get_presigned_url( &self, opts: PresignedUrlOpts, ) -> Result<String, Box<dyn Error>>
Generates a presigned URL for a file.
The maximum value for expires_in is 604800 (7 days).
This function assumes that you must accept overrides on the UploadThing dashboard
for expires_in to be accepted.
§Parameters
opts: APresignedUrlOptsstruct containing options for the presigned URL, including the file key and the expiration time in seconds.
§Returns
A Result with a String presigned URL if the operation was successful,
or an Error boxed in a Box<dyn Error> if the request failed, including
scenarios where expires_in is greater than the allowed maximum.
§Errors
If expires_in is greater than 604800 or if an error occurs during the request,
an Error is returned.
Sourcepub async fn upload_files(
&self,
files: Vec<FileObj>,
opts: Option<UploadFileOpts>,
wait_until_done: bool,
) -> Result<Vec<FileUpload>, Box<dyn Error>>
pub async fn upload_files( &self, files: Vec<FileObj>, opts: Option<UploadFileOpts>, wait_until_done: bool, ) -> Result<Vec<FileUpload>, Box<dyn Error>>
Uploads files to the Uploadthing service.