-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(fetch): impl fetch feature variants
- Loading branch information
1 parent
555a2c4
commit 7a74d0e
Showing
7 changed files
with
132 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,3 +27,4 @@ jobs: | |
uses: actions-rs/cargo@v1 | ||
with: | ||
command: test | ||
args: --all-features |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "link-preview" | ||
version = "0.0.1" | ||
version = "0.0.2" | ||
authors = ["Esteban Borai <[email protected]>"] | ||
edition = "2018" | ||
description = "Retrieve website metadata such as title, description, preview image, author and more from OpenGraph, Google, Schema.org and Twitter compliant sites" | ||
|
@@ -21,4 +21,5 @@ url = "2.2.2" | |
tokio = { version = "1.9.0", features = ["rt", "macros"] } | ||
|
||
[features] | ||
# Provide fetch capabilities | ||
fetch = ["reqwest"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
use reqwest::get; | ||
use scraper::Html; | ||
use std::string::FromUtf8Error; | ||
use thiserror::Error; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum Error { | ||
#[error("Failed to fetch {0}. An error ocurred: {1}")] | ||
FetchFailed(String, reqwest::Error), | ||
#[error("Failed to parse response from {0}. An error ocurred: {1}")] | ||
ParseError(String, reqwest::Error), | ||
#[error("Failed to stream response chunks {0}. An error ocurred: {1}")] | ||
StreamError(String, reqwest::Error), | ||
#[error("Failed to parse bytes into UTF-8 while streaming response from {0}")] | ||
InvalidUtf8(String, FromUtf8Error), | ||
} | ||
|
||
/// Fetches the provided URL and retrieves an instance of `LinkPreview` | ||
pub async fn fetch(url: &str) -> Result<Html, Error> { | ||
let resp = get(url) | ||
.await | ||
.map_err(|err| Error::FetchFailed(url.to_string(), err))?; | ||
let html = resp | ||
.text() | ||
.await | ||
.map_err(|err| Error::ParseError(url.to_string(), err))?; | ||
|
||
Ok(Html::parse_document(&html)) | ||
} | ||
|
||
/// Fetches the provided URL and retrieves an instance of `LinkPreview` | ||
pub async fn fetch_partially(url: &str) -> Result<Html, Error> { | ||
fetch_with_limit(url, 10).await | ||
} | ||
|
||
/// Fetches the provided URL and retrieves an instance of `LinkPreview` | ||
pub async fn fetch_with_limit(url: &str, limit: usize) -> Result<Html, Error> { | ||
let mut laps = 0_usize; | ||
let mut resp = get(url) | ||
.await | ||
.map_err(|err| Error::FetchFailed(url.to_string(), err))?; | ||
let mut bytes: Vec<u8> = Vec::new(); | ||
|
||
while let Some(chunk) = resp | ||
.chunk() | ||
.await | ||
.map_err(|err| Error::StreamError(url.to_string(), err))? | ||
{ | ||
if laps >= limit { | ||
break; | ||
} | ||
|
||
let ref mut chunk = chunk.to_vec(); | ||
|
||
bytes.append(chunk); | ||
laps += 1; | ||
} | ||
|
||
let html = String::from_utf8(bytes).map_err(|err| Error::InvalidUtf8(url.to_string(), err))?; | ||
|
||
Ok(Html::parse_document(&html)) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::tests::REMOTE_FULL_FEATURED_HTML; | ||
use crate::LinkPreview; | ||
|
||
use super::{fetch, fetch_partially, fetch_with_limit}; | ||
|
||
#[tokio::test] | ||
async fn fetches() { | ||
let html = fetch(REMOTE_FULL_FEATURED_HTML).await.unwrap(); | ||
let link_preview = LinkPreview::from(&html); | ||
|
||
assert_eq!( | ||
link_preview.title.unwrap_or(String::default()), | ||
"SEO Strategies for a better web" | ||
); | ||
assert_eq!(link_preview.description.unwrap_or(String::default()), "John Appleseed tells you his secrets on SEO for a better web experience by taking advantage of OpenGraph\'s Tags!"); | ||
} | ||
|
||
#[tokio::test] | ||
async fn fetches_page_partially() { | ||
let html = fetch_partially(REMOTE_FULL_FEATURED_HTML).await.unwrap(); | ||
let link_preview = LinkPreview::from(&html); | ||
|
||
assert_eq!( | ||
link_preview.title.unwrap_or(String::default()), | ||
"SEO Strategies for a better web" | ||
); | ||
assert_eq!(link_preview.description.unwrap_or(String::default()), "John Appleseed tells you his secrets on SEO for a better web experience by taking advantage of OpenGraph\'s Tags!"); | ||
} | ||
|
||
#[tokio::test] | ||
async fn fetches_page_with_limit_of_20() { | ||
let html = fetch_with_limit(REMOTE_FULL_FEATURED_HTML, 20) | ||
.await | ||
.unwrap(); | ||
let link_preview = LinkPreview::from(&html); | ||
|
||
assert_eq!( | ||
link_preview.title.unwrap_or(String::default()), | ||
"SEO Strategies for a better web" | ||
); | ||
assert_eq!(link_preview.description.unwrap_or(String::default()), "John Appleseed tells you his secrets on SEO for a better web experience by taking advantage of OpenGraph\'s Tags!"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters