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
impl ApiKey
sourcepub fn from_env() -> Option<ApiKey>
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 Default for ApiKey
impl Default for ApiKey
source§fn default() -> ApiKey
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
impl<'de> Deserialize<'de> for ApiKey
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
source§impl Display for ApiKey
impl Display for ApiKey
source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
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
impl FromStr for ApiKey
source§fn from_str(s: &str) -> Result<Self, Self::Err>
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());