Struct UtApi

Source
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

Source

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 - An Option<String> that holds the API key for authentication. If None, 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.

Source

pub fn from_config(config: UploadthingConfig) -> UtApi

Creates a new instance of UtApi from a given UploadthingConfig.

§Arguments
  • config - An UploadthingConfig instance 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.

Source

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 the payload that implements Serialize.
§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.

Source

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: A Vec<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.

Source

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: A Vec<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.

Source

pub async fn list_files( &self, opts: Option<ListFilesOpts>, ) -> Result<UploadthingFileResponse, Box<dyn Error>>

Lists files stored in Uploadthing service.

§Parameters
  • opts: An optional ListFilesOpts struct 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.

Source

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: A RenameFilesOpts struct 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.

Source

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.

Source

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: A PresignedUrlOpts struct 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.

Source

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.

Trait Implementations§

Source§

impl Clone for UtApi

Source§

fn clone(&self) -> UtApi

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl Freeze for UtApi

§

impl !RefUnwindSafe for UtApi

§

impl Send for UtApi

§

impl Sync for UtApi

§

impl Unpin for UtApi

§

impl !UnwindSafe for UtApi

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,