Client

Struct Client 

Source
pub struct Client { /* private fields */ }
Expand description

A client for interacting with the Thunderstore API.

The easiest way to create a client is to use the Client::new method. If you need more control over the client’s configuration, use the Client::builder method instead.

Implementations§

Source§

impl Client

Source

pub fn new() -> Self

Creates a new client with the default configuration.

Source

pub fn builder() -> ClientBuilder

Creates a ClientBuilder to configure a new client.

Source

pub fn base_url(&self) -> &str

The base URL to use for requests. Defaults to https://thunderstore.io.

Source

pub fn set_base_url(&mut self, base_url: impl Into<String>)

Sets the base URL to use for requests.

Source

pub fn token(&self) -> Option<&str>

The API token to use for restricted endpoints.

Source

pub fn clear_token(&mut self)

Clears the API token to use for restricted endpoints.

Source

pub fn set_token(&mut self, token: impl Into<String>)

Sets the API token to use for restricted endpoints.

Source

pub async fn stream_download( &self, version: impl IntoVersionIdent<'_>, ) -> Result<impl Stream<Item = Result<Bytes>>>

Downloads a package and streams it. The result, when aggregated, is a ZIP archive containing the contents of the package.

Source

pub async fn download( &self, version: impl IntoVersionIdent<'_>, ) -> Result<Bytes>

Downloads a package and returns it as a single Bytes. The result is a ZIP archive containing the contents of the package.

Source§

impl Client

Source

pub async fn get_communities( &self, cursor: Option<impl AsRef<str>>, ) -> Result<(CursorState, Vec<Community>)>

Fetches a page of communities.

Returns a CursorState which is used to navigate between pages by passing the fields in the cursor parameter.

Source

pub async fn get_categories( &self, community: impl AsRef<str>, cursor: Option<impl AsRef<str>>, ) -> Result<(CursorState, Vec<CommunityCategory>)>

Fetches a page of categories from the given community.

Returns a CursorState which is used to navigate between pages by passing the fields in the cursor parameter.

Source§

impl Client

Source

pub async fn get_current_community(&self) -> Result<Community>

Fetches the start/main community of the client’s base URL.

For the default thunderstore repo, this is riskofrain2.

Source§

impl Client

Source

pub async fn render_markdown( &self, markdown: impl Into<String>, ) -> Result<String>

Renders a markdown string to HTML.

Source§

impl Client

Source

pub async fn create_profile(&self, data: impl AsRef<[u8]>) -> Result<Uuid>

Creates a profile with the given data and returns its key.

The data is expected to be a ZIP archive containing a export.r2x file and eventual config files or directories. However, any arbitrary data is allowed.

The returned key is used to retrieve the profile with Client::get_profile.

Source

pub async fn create_profile_raw(&self, data: Vec<u8>) -> Result<Uuid>

Creates a profile with the given data and returns its key.

The data is expected to be a base64-encoded ZIP archive containing a export.r2x file and eventual config files or directories. However, any arbitrary data is allowed.

The returned key is used to retrieve the profile with Client::get_profile.

Source

pub async fn get_profile(&self, key: Uuid) -> Result<Vec<u8>>

Downloads a profile with the given key.

The returned data is usually a ZIP archive containing a export.r2x file and eventual config files.

This assumes the profile is encoded with base64, but any arbitrary data is allowed.

Source

pub async fn get_profile_raw(&self, key: Uuid) -> Result<Bytes>

Downloads a profile with the given key.

The returned data is usually a base64-encoded ZIP archive containing a export.r2x file and eventual config files, but any arbitrary data is allowed.

Source§

impl Client

Source

pub async fn get_package( &self, ident: impl IntoPackageIdent<'_>, ) -> Result<Package>

Fetches information about a package.

§Example
let client = thunderstore::Client::new();

let a = client.get_package(("Kesomannen", "GaleModManager")).await?;
let b = client.get_package("Kesomannen-GaleModManager").await?;

assert_eq!(a, b);
Source

pub async fn get_version( &self, ident: impl IntoVersionIdent<'_>, ) -> Result<PackageVersion>

Fetches information about a specific version of a package.

§Example
let client = thunderstore::Client::new();

let a = client.get_version(("Kesomannen", "GaleModManager", "0.6.0")).await?;
let b = client.get_version("Kesomannen-GaleModManager-0.6.0").await?;

assert_eq!(a, b);
Source

pub async fn get_readme( &self, ident: impl IntoVersionIdent<'_>, ) -> Result<String>

Fetches the readme for a specific version of a package. The readme is returned as a markdown string.

Source

pub async fn get_changelog( &self, ident: impl IntoVersionIdent<'_>, ) -> Result<String>

Fetches the changelog for a specific version of a package. The changelog is returned as a markdown string.

Note that a package may not have a changelog, in which case [Error::NotFound] is returned.

Source§

impl Client

Source

pub async fn submit_package( &self, upload_uuid: Uuid, metadata: PackageMetadata, ) -> Result<PackageSubmissionResult>

Publishes a pre-uploaded package.

The contents must already have been uploaded by calling Client::initiate_upload, streaming the data to the returned URLs, and finally calling Client::finish_upload.

This method requires an API token on the client.

Source

pub async fn validate_icon(&self, data: impl AsRef<[u8]>) -> Result<bool>

Validates a package icon.

Source

pub async fn validate_manifest_v1( &self, namespace: impl Into<String>, content: impl Into<String>, ) -> Result<bool>

Validates a package manifest (v1) as if uploaded in given namespace.

Source

pub async fn validate_readme(&self, content: impl Into<String>) -> Result<bool>

Validates a package README.

Source§

impl Client

Source

pub async fn initiate_upload( &self, name: impl Into<String>, size: u64, ) -> Result<UserMediaInitiateUploadResponse>

Initiates a new package upload.

  • name is the name of the package and may only contain alphanumeric characters and underscores.

  • size must be the size of the package in bytes.

This method returns a UserMediaInitiateUploadResponse which contains a unique UUID for the upload, which is used to identify the package throughout the upload process.

The response also contains a list of URLs to which the file should be uploaded, using HTTP PUT. Each upload URL responds with an ETag header, which should be used to finalize the upload.

Alternatively, you can use Client::publish to upload and submit a package in one go.

This method requires an API token on the client.

§Example
use thunderstore::{Client, models::{UploadPartUrl, CompletedPart}};

let client = Client::builder().with_token("tss_XXX").build()?;

let path = "path/to/your/package.zip";
let size = std::fs::metadata(path)?.len();
let response = client.initiate_upload("MyCoolMod", size).await?;

let parts = Vec::new();

for UploadPartUrl { url, number, offset, length } in response.upload_urls {
   // Read `length` bytes from `offset` in the file
   // and make a PUT request to `url` with the data.

   // The response will return an ETag header, which is needed to complete the upload.
   parts.push(CompletedPart { tag: todo!(), number });

   // These requests should preferably be done concurrently to decrease upload time.
}

client.finish_upload(response.user_media.uuid, parts).await?;
Source

pub async fn abort_upload(&self, uuid: Uuid) -> Result<UserMedia>

Aborts an ongoing upload.

This method requires an API token on the client.

Source

pub async fn finish_upload( &self, uuid: Uuid, parts: Vec<CompletedPart>, ) -> Result<UserMedia>

Finalizes an upload. Requires the UUID of the upload and a list of CompletedPart objects, which contain the ETag of each part of the upload.

Note that this will not publish the package, only finish the upload process. To submit the package, use the Client::submit_package method as well.

This method requires an API token on the client.

Source

pub async fn publish( &self, name: impl Into<String>, data: impl Into<Bytes>, metadata: PackageMetadata, ) -> Result<PackageSubmissionResult>

Uploads and submits a package.

  • name may only contain alphanumeric characters and underscores.

This method requires an API token on the client.

Source§

impl Client

Source

pub async fn get_wikis(&self) -> Result<WikisResponse>

Fetches an index of all the package wikis on Thunderstore.

Source

pub async fn get_wiki(&self, package: impl IntoPackageIdent<'_>) -> Result<Wiki>

Fetches the wiki of a specific package.

Source

pub async fn create_wiki_page( &self, package: impl IntoPackageIdent<'_>, title: impl Into<String>, content: impl Into<String>, ) -> Result<WikiPage>

Creates a package wiki page.

Source

pub async fn update_wiki_page( &self, package: impl IntoPackageIdent<'_>, id: impl Into<String>, title: impl Into<String>, content: impl Into<String>, ) -> Result<WikiPage>

Updates a package wiki page.

Source

pub async fn upsert_wiki_page( &self, package: impl IntoPackageIdent<'_>, upsert: WikiPageUpsert, ) -> Result<WikiPage>

Upserts a package wiki page.

If upsert.id is None, this creates a new page. Otherwise, an existing one is updated according to its id.

Source

pub async fn delete_wiki_page( &self, package: impl IntoPackageIdent<'_>, id: impl Into<String>, ) -> Result<()>

Deletes a package wiki page.

Source

pub async fn get_wiki_page(&self, id: impl AsRef<str>) -> Result<WikiPage>

Fetches a package wiki page by its id.

Source§

impl Client

Source

pub async fn get_metrics( &self, community: impl AsRef<str>, package: impl IntoPackageIdent<'_>, ) -> Result<PackageMetrics>

Fetches PackageMetrics for a specific package.

community is the slug of the community, which is usually in kebab-case.

Source

pub async fn get_downloads( &self, community: impl AsRef<str>, version: impl IntoVersionIdent<'_>, ) -> Result<u64>

Fetches the download count for a specific version of a package.

community is the slug of the community, which is usually in kebab-case.

Source

pub async fn list_packages_v1( &self, community: impl AsRef<str>, ) -> Result<Vec<PackageV1>>

Fetches all available packages in a community and collects them in a Vec.

  • community is the slug of the community, which is usually in kebab-case.

Note that on popular communities like Lethal Company (lethal-company), this will fetch up to 170 MB of data.

Source§

impl Client

Source

pub async fn stream_packages_v1( &self, community: impl AsRef<str>, ) -> Result<impl Stream<Item = Result<PackageV1>>>

Asynchronously streams all available packages in a community.

  • community is the slug of the community, which is usually in kebab-case.

If you just want a Vec of all packages, use Client::list_packages_v1 instead.

§Examples
// provides `try_next`
use futures_util::TryStreamExt;
use futures_util::pin_mut;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = thunderstore::Client::new();

    let stream = client.stream_packages_v1("content-warning").await?;
    pin_mut!(stream); // needed for iteration

    while let Some(package) = stream.try_next().await? {
        println!("got {}!", package.name);
    }

   Ok(())
}

Trait Implementations§

Source§

impl Default for Client

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Client

§

impl !RefUnwindSafe for Client

§

impl Send for Client

§

impl Sync for Client

§

impl Unpin for Client

§

impl !UnwindSafe for Client

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> 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, 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<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,

Source§

impl<T> MaybeSendSync for T