-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature] Integrate Web Scrapper "Track resources" action with actual…
… Web Scrapper backend.
- Loading branch information
Showing
9 changed files
with
453 additions
and
6 deletions.
There are no files selected for viewing
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
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,13 @@ | ||
mod web_page_resource; | ||
mod web_page_resources_tracker; | ||
mod web_scrapper_resources_request; | ||
mod web_scrapper_resources_response; | ||
|
||
pub use self::{ | ||
web_page_resource::WebPageResource, web_page_resources_tracker::WebPageResourcesTracker, | ||
web_page_resource::WebPageResource, | ||
web_page_resources_tracker::WebPageResourcesTracker, | ||
web_scrapper_resources_request::WebScrapperResourcesRequest, | ||
web_scrapper_resources_response::{ | ||
WebScrapperResource, WebScrapperResourceBundle, WebScrapperResourcesResponse, | ||
}, | ||
}; |
85 changes: 85 additions & 0 deletions
85
src/utils/web_scrapping/resources/web_scrapper_resources_request.rs
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,85 @@ | ||
use serde::Serialize; | ||
use url::Url; | ||
|
||
/// Represents request to scrap web page resources. | ||
#[derive(Serialize, Debug, PartialEq, Eq)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct WebScrapperResourcesRequest<'a> { | ||
/// URL of the web page to scrap resources for. | ||
pub url: &'a Url, | ||
|
||
/// Number of milliseconds to wait until page enters "idle" state. | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub timeout: Option<usize>, | ||
|
||
/// Number of milliseconds to wait after page enters "idle" state. | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub delay: Option<usize>, | ||
|
||
/// Optional CSS selector to wait for before extracting resources. | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub wait_selector: Option<&'a str>, | ||
} | ||
|
||
impl<'a> WebScrapperResourcesRequest<'a> { | ||
/// Creates request with only the URL of the web page to scrap resources for, the rest of the | ||
/// parameters are omitted. | ||
pub fn with_default_parameters(url: &'a Url) -> Self { | ||
Self { | ||
url, | ||
timeout: None, | ||
delay: None, | ||
wait_selector: None, | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::WebScrapperResourcesRequest; | ||
use insta::assert_json_snapshot; | ||
use url::Url; | ||
|
||
#[test] | ||
fn serialization() -> anyhow::Result<()> { | ||
assert_json_snapshot!(WebScrapperResourcesRequest { | ||
url: &Url::parse("http://localhost:1234/my/app?q=2")?, | ||
timeout: Some(100), | ||
delay: Some(200), | ||
wait_selector: Some("body") | ||
}, @r###" | ||
{ | ||
"url": "http://localhost:1234/my/app?q=2", | ||
"timeout": 100, | ||
"delay": 200, | ||
"waitSelector": "body" | ||
} | ||
"###); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn serialization_with_default_parameters() -> anyhow::Result<()> { | ||
assert_json_snapshot!(WebScrapperResourcesRequest::with_default_parameters(&Url::parse("http://localhost:1234/my/app?q=2")?), @r###" | ||
{ | ||
"url": "http://localhost:1234/my/app?q=2" | ||
} | ||
"###); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn with_default_parameters() -> anyhow::Result<()> { | ||
let url = Url::parse("http://localhost:1234/my/app?q=2")?; | ||
let request = WebScrapperResourcesRequest::with_default_parameters(&url); | ||
|
||
assert_eq!(request.url, &url); | ||
assert!(request.wait_selector.is_none()); | ||
assert!(request.delay.is_none()); | ||
assert!(request.timeout.is_none()); | ||
|
||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.