Skip to main content

Client

Struct Client 

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

Threads API client. Thread-safe; wrap in Arc<Client> to share across tasks.

Implementations§

Source§

impl Client

Source

pub async fn get_post_insights( &self, post_id: &PostId, metrics: &[&str], ) -> Result<InsightsResponse>

Get insights for a specific post.

Source

pub async fn get_account_insights( &self, user_id: &UserId, metrics: &[&str], period: &str, ) -> Result<InsightsResponse>

Get insights for a user account.

Source

pub async fn get_post_insights_with_options( &self, post_id: &PostId, opts: &PostInsightsOptions, ) -> Result<InsightsResponse>

Get post insights with typed options.

Source

pub async fn get_account_insights_with_options( &self, user_id: &UserId, opts: &AccountInsightsOptions, ) -> Result<InsightsResponse>

Get account insights with typed options.

Source§

impl Client

Source

pub async fn search_locations( &self, query: Option<&str>, latitude: Option<f64>, longitude: Option<f64>, ) -> Result<LocationSearchResponse>

Search for locations by query string, coordinates, or both.

Per the API docs, you can search by query alone, latitude + longitude alone, or all three together. At least one of query or coordinates must be provided.

Source

pub async fn get_location(&self, location_id: &LocationId) -> Result<Location>

Get a location by ID.

Source§

impl Client

Source

pub async fn create_text_post(&self, content: &TextPostContent) -> Result<Post>

Create a text post. If auto_publish_text is true, skips the container+publish flow and posts directly.

Source

pub async fn create_image_post( &self, content: &ImagePostContent, ) -> Result<Post>

Create an image post.

Source

pub async fn create_video_post( &self, content: &VideoPostContent, ) -> Result<Post>

Create a video post.

Source

pub async fn create_carousel_post( &self, content: &CarouselPostContent, ) -> Result<Post>

Create a carousel post.

Source

pub async fn create_quote_post( &self, text: &str, quoted_post_id: &PostId, ) -> Result<Post>

Create a quote post — a text post that quotes another post.

Source

pub async fn create_media_container( &self, media_type: &str, media_url: &str, alt_text: Option<&str>, ) -> Result<ContainerId>

Create a media container and return its ID.

Public wrapper around the internal container creation. Useful for building carousel children or custom publishing flows.

Note: The container is returned immediately after creation. The caller must poll get_container_status until the status is FINISHED before using the container in a publish call (e.g. create_carousel_post).

Source

pub async fn repost_post(&self, post_id: &PostId) -> Result<PostId>

Repost an existing post and return the repost’s post ID.

Unlike the previous implementation, this does not make an extra API call to fetch the full post. Call get_post on the returned ID if you need the full post details.

Source

pub async fn get_container_status( &self, container_id: &ContainerId, ) -> Result<ContainerStatus>

Get the status of a media container.

Source§

impl Client

Source

pub async fn delete_post(&self, post_id: &PostId) -> Result<String>

Delete a post by ID. Returns the deleted post ID.

Source§

impl Client

Source

pub async fn get_post(&self, post_id: &PostId) -> Result<Post>

Get a single post by ID with extended fields.

Source

pub async fn get_user_posts( &self, user_id: &UserId, opts: Option<&PostsOptions>, ) -> Result<PostsResponse>

Get posts created by a user.

Source

pub async fn get_user_mentions( &self, user_id: &UserId, opts: Option<&PostsOptions>, ) -> Result<PostsResponse>

Get posts where the user is mentioned.

Supports since and until for time-range filtering in addition to cursor-based pagination.

Source

pub async fn get_user_ghost_posts( &self, user_id: &UserId, opts: Option<&PostsOptions>, ) -> Result<PostsResponse>

Get ghost posts for a user.

Supports since and until for time-range filtering in addition to cursor-based pagination.

Source

pub async fn get_publishing_limits(&self) -> Result<PublishingLimits>

Get the publishing rate limits for the authenticated user.

Source§

impl Client

Source

pub async fn get_replies( &self, post_id: &PostId, opts: Option<&RepliesOptions>, ) -> Result<RepliesResponse>

Get replies to a post.

Source

pub async fn get_conversation( &self, post_id: &PostId, opts: Option<&RepliesOptions>, ) -> Result<RepliesResponse>

Get the full conversation thread for a post.

Source

pub async fn get_pending_replies( &self, post_id: &PostId, opts: Option<&PendingRepliesOptions>, ) -> Result<RepliesResponse>

Get pending replies awaiting moderation.

Source

pub async fn approve_pending_reply(&self, reply_id: &PostId) -> Result<()>

Approve a pending reply.

Source

pub async fn ignore_pending_reply(&self, reply_id: &PostId) -> Result<()>

Ignore a pending reply.

Source

pub async fn hide_reply(&self, reply_id: &PostId) -> Result<()>

Hide a reply.

Source

pub async fn reply_to_post(&self, post_id: &PostId, text: &str) -> Result<Post>

Reply to a post with text.

Convenience wrapper: builds a TextPostContent with reply_to_id set and delegates to create_text_post.

Source

pub async fn create_reply( &self, content: &TextPostContent, apply_reply_delay: bool, ) -> Result<Post>

Create a text reply.

Validates that reply_to_id is set, then delegates to create_text_post. Set apply_reply_delay to true to apply the API-recommended 10-second delay before creating the reply.

Source

pub async fn create_image_reply( &self, content: &ImagePostContent, ) -> Result<Post>

Create an image reply.

Validates that reply_to_id is set, then delegates to create_image_post.

Source

pub async fn create_video_reply( &self, content: &VideoPostContent, ) -> Result<Post>

Create a video reply.

Validates that reply_to_id is set, then delegates to create_video_post.

Source

pub async fn unhide_reply(&self, reply_id: &PostId) -> Result<()>

Unhide a reply.

Source§

impl Client

Search for posts by keyword.

Search for posts by hashtag.

Convenience wrapper around keyword_search with search_mode set to TAG.

Source§

impl Client

Source

pub async fn get_user(&self, user_id: &UserId) -> Result<User>

Get a user profile by ID.

Source

pub async fn get_me(&self) -> Result<User>

Get the authenticated user’s profile.

Source

pub async fn lookup_public_profile(&self, username: &str) -> Result<PublicUser>

Look up a public user profile by username.

Source

pub async fn get_user_with_fields( &self, user_id: &UserId, fields: &[&str], ) -> Result<User>

Get a user profile with custom field selection.

Source

pub async fn get_public_profile_posts( &self, username: &str, opts: Option<&PostsOptions>, ) -> Result<PostsResponse>

Get posts from a public user profile by username.

Source

pub async fn get_user_replies( &self, user_id: &UserId, opts: Option<&PostsOptions>, ) -> Result<RepliesResponse>

Get replies posted by a user.

Source

pub async fn get_my_posts( &self, opts: Option<&PostsOptions>, ) -> Result<PostsResponse>

Get the authenticated user’s posts.

Source

pub async fn get_my_replies( &self, opts: Option<&PostsOptions>, ) -> Result<RepliesResponse>

Get the authenticated user’s replies.

Source

pub async fn get_my_mentions( &self, opts: Option<&PostsOptions>, ) -> Result<PostsResponse>

Get posts where the authenticated user is mentioned.

Source

pub async fn get_my_ghost_posts( &self, opts: Option<&PostsOptions>, ) -> Result<PostsResponse>

Get the authenticated user’s ghost posts.

Source

pub async fn get_recently_searched_keywords(&self) -> Result<Vec<RecentSearch>>

Get the authenticated user’s recently searched keywords.

Source§

impl Client

Source

pub fn get_auth_url(&self, scopes: &[String]) -> (String, String)

Build the OAuth authorization URL that the user should visit.

scopes overrides the scopes from the client config. Pass an empty slice to use the config defaults. Returns (url, state) — the caller must store state and verify it matches the state query parameter on the OAuth callback to prevent CSRF.

Source

pub async fn get_app_access_token(&self) -> Result<AppAccessTokenResponse>

Get an app access token using client credentials.

This does NOT store the token in the client (matches Go behavior). The caller should use the returned token as needed.

Source

pub fn get_app_access_token_shorthand(&self) -> String

Get an app access token in shorthand format.

Returns "TH|{client_id}|{client_secret}" or an empty string if client_id or client_secret are empty.

Source

pub async fn exchange_code_for_token(&self, code: &str) -> Result<()>

Exchange an authorization code for a short-lived access token.

On success the token is stored via set_token_info.

Source

pub async fn get_long_lived_token(&self) -> Result<()>

Convert the current short-lived token into a long-lived token (60 days).

Requires that the client already holds a valid short-lived token.

Source

pub async fn refresh_token(&self) -> Result<()>

Refresh the current long-lived token, extending its expiry.

The token must still be valid (not expired) to be refreshed.

Source

pub async fn debug_token(&self, input_token: &str) -> Result<DebugTokenResponse>

Inspect a token via the /debug_token endpoint.

Source

pub async fn validate_token(&self) -> Result<()>

Validate the current token locally: non-empty and not expired.

Source

pub async fn ensure_valid_token(&self) -> Result<()>

Validate the current token and auto-refresh if expired.

Only attempts a refresh when the token exists but has expired. Returns the original error for other failures (no token, empty token).

Source

pub async fn get_token_debug_info(&self) -> HashMap<String, String>

Return debug information about the current token.

The access token is masked (first 4 + last 4 characters shown).

Source

pub async fn load_token_from_storage(&self) -> Result<()>

Explicitly reload the token from storage.

Source

pub async fn set_token_from_debug_info( &self, access_token: &str, debug_resp: &DebugTokenResponse, ) -> Result<()>

Store a token built from a previous debug_token response.

Useful for bootstrapping the client from a known-valid token without going through the full OAuth flow again.

Source§

impl Client

Source

pub async fn new(config: Config) -> Result<Self>

Create a new client with the given configuration.

Source

pub async fn with_token_storage( config: Config, token_storage: Box<dyn TokenStorage>, ) -> Result<Self>

Create a new client with custom token storage.

Source

pub async fn with_token(config: Config, access_token: &str) -> Result<Self>

Create a client with a pre-existing access token.

Calls the debug_token endpoint to resolve the user ID and exact expiry from the token. Useful for scripts and tests where the token is already known.

Source

pub async fn from_env() -> Result<Self>

Create a client from environment variables.

Source

pub async fn set_token_info(&self, token_info: TokenInfo) -> Result<()>

Set token information (thread-safe).

Source

pub async fn get_token_info(&self) -> Option<TokenInfo>

Get a copy of the current token info.

Source

pub async fn is_authenticated(&self) -> bool

Returns true if the client has a valid access token.

Source

pub async fn is_token_expired(&self) -> bool

Returns true if the current token has expired.

Source

pub async fn is_token_expiring_soon(&self, within: Duration) -> bool

Returns true if the token expires within the given duration.

Source

pub async fn clear_token(&self) -> Result<()>

Clear the current token from the client and storage.

Source

pub async fn access_token(&self) -> String

Get the current access token.

Source

pub fn config(&self) -> &Config

Returns a reference to the client configuration.

Source

pub async fn update_config(self, new_config: Config) -> Result<Client>

Consume this client and create a new one with the given config, preserving the current token state.

Source

pub async fn rate_limit_status(&self) -> Option<RateLimitStatus>

Returns the current rate limit status.

Source

pub async fn is_near_rate_limit(&self, threshold: f64) -> bool

Returns true if we’re close to the rate limit.

Source

pub async fn is_rate_limited(&self) -> bool

Returns true if we’re currently rate-limited by the API.

Source

pub async fn disable_rate_limiting(&self)

Disable rate limiting. Requests will not be throttled.

Source

pub async fn enable_rate_limiting(&self)

Enable rate limiting.

Source

pub async fn wait_for_rate_limit(&self) -> Result<()>

Wait until the rate limiter allows a request.

Auto Trait Implementations§

§

impl !Freeze for Client

§

impl !RefUnwindSafe for Client

§

impl Send for Client

§

impl Sync for Client

§

impl Unpin for Client

§

impl UnsafeUnpin 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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. 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