Struct urlshortener::UrlShortener
[−]
[src]
pub struct UrlShortener { /* fields omitted */ }
Url shortener: the way to retrieve a short url.
Methods
impl UrlShortener
[src]
fn new() -> Result<UrlShortener, Error>
Creates new UrlShortener
with default (3 seconds) timeout.
fn with_timeout(seconds: u64) -> Result<UrlShortener, Error>
Creates new UrlShortener
with custom read timeout.
fn try_generate(
&self,
url: &str,
use_providers: Option<&[Provider]>
) -> Result<String, Error>
&self,
url: &str,
use_providers: Option<&[Provider]>
) -> Result<String, Error>
Try to generate a short URL from each provider, iterating over each provider until a short URL is successfully generated. If you wish to override the list or providers or their priority, provide your own list of providers as second argument.
Examples
use urlshortener::UrlShortener; let us = UrlShortener::new().unwrap(); let long_url = "https://rust-lang.org"; let _short_url = us.try_generate(long_url, None);
use urlshortener::{UrlShortener, Provider}; let us = UrlShortener::new().unwrap(); let providers = [ Provider::GooGl { api_key: "MY_API_KEY".to_owned() }, Provider::IsGd, ]; let long_url = "https://rust-lang.org"; let _short_url = us.try_generate(long_url, Some(&providers));
Errors
Returns an Error<ErrorKind::Other>
if there is an error generating a
short URL from all providers.
fn generate<S: Into<String>>(
&self,
url: S,
provider: &Provider
) -> Result<String, Error>
&self,
url: S,
provider: &Provider
) -> Result<String, Error>
Attempts to get a short URL using the specified provider.
Examples
use urlshortener::{Provider, UrlShortener}; let us = UrlShortener::new().unwrap(); let long_url = "http://rust-lang.org"; let _short_url = us.generate(long_url, &Provider::IsGd);
use urlshortener::{Provider, UrlShortener}; let us = UrlShortener::new().unwrap(); let api_key = "MY_API_KEY".to_owned(); let long_url = "http://rust-lang.org"; let _short_url = us.generate(long_url, &Provider::GooGl { api_key: api_key });
Errors
Returns an std::io::Error
if there is an error generating a
short URL from the given provider due to either:
a. a decode error (ErrorKind::Other); b. the service being unavailable (ErrorKind::ConnectionAborted)