Struct utapi_rs::config::ApiKey

source ·
pub struct ApiKey {
    pub prefix: Option<String>,
    pub key: String,
}
Expand description

Represents an API key used for authenticating with the Uploadthing service.

This struct holds the actual API key and an optional prefix. The prefix can be used to add a specific identifier before the key when sending it in the request header, but it is not required.

Fields§

§prefix: Option<String>

An optional prefix to be added to the API key. This can be used to distinguish between different types of keys.

§key: String

The API key string that is used for authentication.

Implementations§

source§

impl ApiKey

source

pub fn from_env() -> Option<ApiKey>

Constructs an ApiKey instance from an environment variable.

This function attempts to create an ApiKey by reading the value of the environment variable UPLOADTHING_SECRET. If the variable is set, it returns Some(ApiKey) with the key set to the value of the environment variable, and no prefix. If the environment variable is not set, it returns None.

§Examples
// Assuming the environment variable `UPLOADTHING_SECRET` is set to "secret123"
let api_key = ApiKey::from_env().unwrap();
assert_eq!(api_key.key, "secret123");
assert_eq!(api_key.prefix, None);

Trait Implementations§

source§

impl Clone for ApiKey

source§

fn clone(&self) -> ApiKey

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
source§

impl Debug for ApiKey

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for ApiKey

source§

fn default() -> ApiKey

Provides a default ApiKey by trying to read from the environment variable.

This implementation uses ApiKey::from_env() to attempt to create an ApiKey. If the environment variable is not set, this will panic. It is generally expected that the environment variable is set if this method is used.

§Panics

This function will panic if the environment variable UPLOADTHING_SECRET is not set.

§Examples
// Assuming the environment variable `UPLOADTHING_SECRET` is set
let api_key = ApiKey::default();
// Use the `api_key` as needed
source§

impl<'de> Deserialize<'de> for ApiKey

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Display for ApiKey

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the ApiKey for display purposes.

This implementation will only display the key field of the ApiKey. The prefix is not included in the output. This is generally used when the API key needs to be included in a header or similar context where the prefix is not required.

§Examples
let api_key = ApiKey {
    prefix: Some(String::from("Bearer")),
    key: String::from("secret123"),
};
assert_eq!(format!("{}", api_key), "secret123");
source§

impl FromStr for ApiKey

source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Creates an ApiKey instance from a string slice.

This function attempts to parse the given string slice into an ApiKey by checking for a colon which separates the optional prefix and the key. If a colon is found, the part before the colon is set as the prefix and the part after the colon is set as the key. If no colon is found, the entire string is set as the key with no prefix.

§Examples
let api_key: ApiKey = "Bearer:secret123".parse().unwrap();
assert_eq!(api_key.prefix, Some("Bearer".to_string()));
assert_eq!(api_key.key, "secret123".to_string());

let api_key: ApiKey = "secret123".parse().unwrap();
assert_eq!(api_key.prefix, None);
assert_eq!(api_key.key, "secret123".to_string());
§

type Err = ()

The associated error which can be returned from parsing.
source§

impl Serialize for ApiKey

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

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> ToOwned for T
where T: Clone,

§

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> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

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

§

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

§

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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,