[−][src]Struct shorty::Shortener
Shortener
is the struct exposing methods lookup
and shorten
.
lookup
attempts to resolve an ID to a URL. If no URL is found or an error occurs, it returns
None
, otherwise it returns Some(url)
.
shorten
takes an optional API key and a URL to shorten. If the API key is present, it will
validate it and shorten the URL only if validation passes. Otherwise, it will just shorten the
URL.
Shortener
interacts with a RedisFacade
, which makes it easier to work with the redis
crate
and simplifies testing.
Methods
impl Shortener
[src]
pub fn new(
id_length: usize,
id_alphabet: Vec<char>,
id_generation_max_attempts: u8,
redis: RedisFacade,
rate_limit_period: usize,
rate_limit: i64
) -> Shortener
[src]
id_length: usize,
id_alphabet: Vec<char>,
id_generation_max_attempts: u8,
redis: RedisFacade,
rate_limit_period: usize,
rate_limit: i64
) -> Shortener
Creates a new Shortener
id_length
is the length of the generated ID.
id_alphabet
is the alphabet used in the ID: a decent one is a-zA-Z0-9
as each entry has
62 possible values and is ASCII
id_generation_max_attempts
is the number of attempts to generate an unique ID when a
conflict is detected.
redis
is a RedisFacade
instance.
rate_limit_period
is the amount of seconds during which calls to shorten
will be counted.
rate_limit
is the max number of calls that can be made to shorten
in a period.
pub fn lookup(&self, id: &str) -> Option<String>
[src]
Looks up a URL by the given ID. If no URL is found or an error occurs, it returns None
,
otherwise it returns Some(url)
.
pub fn shorten(
&self,
api_key: &Option<&str>,
host: Option<&str>,
url: &str
) -> Result<ShortenerResult, ShortenerError>
[src]
&self,
api_key: &Option<&str>,
host: Option<&str>,
url: &str
) -> Result<ShortenerResult, ShortenerError>
Shortens an URL, returning a ShortenerResult
holding the provided URL and the generated ID.
If the optional API key is present, it will validate it and shorten the URL only if validation passes.
If the optional host is present, it will ensure that the url to shorten is not a url from the same host that's running shorty (which would create a link loop)
Otherwise, it will just shorten the URL.
Auto Trait Implementations
impl Send for Shortener
impl Unpin for Shortener
impl !Sync for Shortener
impl UnwindSafe for Shortener
impl !RefUnwindSafe for Shortener
Blanket Implementations
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,