1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
//! The `spider-client` module provides the primary interface and
//! functionalities for the Spider web crawler library, which is
//! designed for rapid and efficient crawling of web pages to gather
//! links using isolated contexts.
//!
//! ### Features
//!
//! - **Multi-threaded Crawling:** Spider can utilize multiple
//! threads to parallelize the crawling process, drastically
//! improving performance and allowing the ability to gather
//! millions of pages in a short time.
//!
//! - **Configurable:** The library provides various options to
//! configure the crawling behavior, such as setting the depth
//! of crawling, user-agent strings, delays between requests,
//! and more.
//!
//! - **Link Gathering:** The primary objective of Spider is to
//! gather and manage links from the web pages it crawls,
//! compiling them into a structured format for further use.
//!
//! ### Examples
//!
//! Basic usage of the Spider client might look like this:
//!
//! ```rust
//! use spider_client::{Spider, RequestType, RequestParams};
//! use tokio;
//!
//! # #[ignore]
//! #[tokio::main]
//! async fn main() {
//! let spider = Spider::new(Some("myspiderapikey".into())).expect("API key must be provided");
//!
//! let url = "https://spider.cloud";
//!
//! // Scrape a single URL
//! let scraped_data = spider.scrape_url(url, None, "application/json").await.expect("Failed to scrape the URL");
//!
//! println!("Scraped Data: {:?}", scraped_data);
//!
//! // Crawl a website
//! let crawler_params = RequestParams {
//! limit: Some(1),
//! proxy_enabled: Some(true),
//! store_data: Some(false),
//! metadata: Some(false),
//! request: Some(RequestType::Http),
//! ..Default::default()
//! };
//!
//! let crawl_result = spider.crawl_url(url, Some(crawler_params), false, "application/json", None::<fn(serde_json::Value)>).await.expect("Failed to crawl the URL");
//!
//! println!("Crawl Result: {:?}", crawl_result);
//! }
//! ```
//!
//! ### Modules
//!
//! - `config`: Contains the configuration options for the Spider client.
//! - `utils`: Utility functions used by the Spider client.
//!
use reqwest::Client;
use reqwest::{Error, Response};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use tokio_stream::StreamExt;
/// Structure representing the Chunking algorithm dictionary.
#[derive(Debug, Deserialize, Serialize)]
pub struct ChunkingAlgDict {
/// The chunking algorithm to use, defined as a specific type.
r#type: ChunkingType,
/// The amount to chunk by.
value: i32,
}
/// Enum representing different types of Chunking.
#[derive(Default, Debug, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum ChunkingType {
#[default]
/// By the word count.
ByWords,
/// By the line count.
ByLines,
/// By the char length.
ByCharacterLength,
/// By sentence.
BySentence,
}
/// Structure representing request parameters.
#[derive(Debug, Default, Deserialize, Serialize)]
pub struct RequestParams {
/// The URL to be crawled.
pub url: Option<String>,
/// The type of request to be made.
pub request: Option<RequestType>,
/// The maximum number of pages the crawler should visit.
pub limit: Option<u32>,
/// The format in which the result should be returned.
pub return_format: Option<ReturnFormat>,
/// Specifies whether to only visit the top-level domain.
pub tld: Option<bool>,
/// The depth of the crawl.
pub depth: Option<u32>,
/// Specifies whether the request should be cached.
pub cache: Option<bool>,
/// The budget for various resources.
pub budget: Option<HashMap<String, u32>>,
/// The blacklist routes to ignore. This can be a Regex string pattern.
pub black_list: Option<Vec<String>>,
/// The whitelist routes to only crawl. This can be a Regex string pattern and used with black_listing.
pub white_list: Option<Vec<String>>,
/// The locale to be used during the crawl.
pub locale: Option<String>,
/// The cookies to be set for the request, formatted as a single string.
pub cookies: Option<String>,
/// Specifies whether to use stealth techniques to avoid detection.
pub stealth: Option<bool>,
/// The headers to be used for the request.
pub headers: Option<HashMap<String, String>>,
/// Specifies whether anti-bot measures should be used.
pub anti_bot: Option<bool>,
/// Specifies whether to include metadata in the response.
pub metadata: Option<bool>,
/// The dimensions of the viewport.
pub viewport: Option<HashMap<String, i32>>,
/// The encoding to be used for the request.
pub encoding: Option<String>,
/// Specifies whether to include subdomains in the crawl.
pub subdomains: Option<bool>,
/// The user agent string to be used for the request.
pub user_agent: Option<String>,
/// Specifies whether the response data should be stored.
pub store_data: Option<bool>,
/// Configuration settings for GPT (general purpose texture mappings).
pub gpt_config: Option<Vec<String>>,
/// Specifies whether to use fingerprinting protection.
pub fingerprint: Option<bool>,
/// Specifies whether to perform the request without using storage.
pub storageless: Option<bool>,
/// Specifies whether readability optimizations should be applied.
pub readability: Option<bool>,
/// Specifies whether to use a proxy for the request.
pub proxy_enabled: Option<bool>,
/// Specifies whether to respect the site's robots.txt file.
pub respect_robots: Option<bool>,
/// CSS selector to be used to filter the content.
pub query_selector: Option<String>,
/// Specifies whether to load all resources of the crawl target.
pub full_resources: Option<bool>,
/// Specifies whether to use the sitemap links.
pub sitemap: Option<bool>,
/// Get page insights to determine information like request duration, accessibility, and other web vitals. Requires the `metadata` parameter to be set to `true`.
pub page_insights: Option<bool>,
/// Returns the OpenAI embeddings for the title and description. Other values, such as keywords, may also be included. Requires the `metadata` parameter to be set to `true`.
pub return_embeddings: Option<bool>,
/// The timeout for the request, in milliseconds.
pub request_timeout: Option<u32>,
/// Specifies whether to run the request in the background.
pub run_in_background: Option<bool>,
/// Specifies whether to skip configuration checks.
pub skip_config_checks: Option<bool>,
/// The chunking algorithm to use.
pub chunking_alg: Option<ChunkingAlgDict>,
}
/// The structure representing request parameters for a search request.
#[derive(Debug, Default, Deserialize, Serialize)]
pub struct SearchRequestParams {
/// The base request parameters.
#[serde(flatten, skip)]
pub base: RequestParams,
/// The search query string.
pub search: String,
/// The limit amount of URLs to fetch or crawl from the search results.
pub search_limit: Option<u32>,
/// Fetch all the content of the websites by performing crawls.
pub fetch_page_content: Option<bool>,
/// The country code to use for the search. It's a two-letter country code (e.g., 'us' for the United States).
pub country: Option<String>,
/// The location from where you want the search to originate.
pub location: Option<String>,
/// The language to use for the search. It's a two-letter language code (e.g., 'en' for English).
pub language: Option<String>,
/// The maximum number of results to return for the search.
pub num: Option<u32>,
}
/// Enum representing different types of Requests.
#[derive(Default, Debug, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum RequestType {
#[default]
Http,
Chrome,
Smart,
}
/// Enum representing different return formats.
#[derive(Default, Debug, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum ReturnFormat {
#[default]
Raw,
Markdown,
Commonmark,
Html2text,
Text,
Bytes,
}
/// Represents a Spider with API key and HTTP client.
#[derive(Debug)]
pub struct Spider {
/// The Spider API key.
api_key: String,
/// The Spider Client to re-use.
client: Client,
}
impl Spider {
/// Creates a new instance of Spider.
///
/// # Arguments
///
/// * `api_key` - An optional API key.
///
/// # Returns
///
/// A new instance of Spider or an error string if no API key is provided.
pub fn new(api_key: Option<String>) -> Result<Self, &'static str> {
let api_key = api_key.or_else(|| std::env::var("SPIDER_API_KEY").ok());
match api_key {
Some(key) => Ok(Self {
api_key: key,
client: Client::new(),
}),
None => Err("No API key provided"),
}
}
/// Sends a POST request to the API.
///
/// # Arguments
///
/// * `endpoint` - The API endpoint.
/// * `data` - The request data as a HashMap.
/// * `stream` - Whether streaming is enabled.
/// * `content_type` - The content type of the request.
///
/// # Returns
///
/// The response from the API.
async fn api_post(
&self,
endpoint: &str,
data: impl Serialize,
content_type: &str,
) -> Result<Response, Error> {
let url: String = format!("https://api.spider.cloud/{}", endpoint);
self.client
.post(&url)
.header(
"User-Agent",
format!("Spider-Client/{}", env!("CARGO_PKG_VERSION")),
)
.header("Content-Type", content_type)
.header("Authorization", format!("Bearer {}", self.api_key))
.json(&data)
.send()
.await
}
/// Sends a GET request to the API.
///
/// # Arguments
///
/// * `endpoint` - The API endpoint.
///
/// # Returns
///
/// The response from the API as a JSON value.
async fn api_get(&self, endpoint: &str) -> Result<serde_json::Value, reqwest::Error> {
let url = format!("https://api.spider.cloud/{}", endpoint);
let res = self
.client
.get(&url)
.header(
"User-Agent",
format!("Spider-Client/{}", env!("CARGO_PKG_VERSION")),
)
.header("Content-Type", "application/json")
.header("Authorization", format!("Bearer {}", self.api_key))
.send()
.await?;
res.json().await
}
/// Scrapes a URL.
///
/// # Arguments
///
/// * `url` - The URL to scrape.
/// * `params` - Optional request parameters.
/// * `stream` - Whether streaming is enabled.
/// * `content_type` - The content type of the request.
///
/// # Returns
///
/// The response from the API as a JSON value.
pub async fn scrape_url(
&self,
url: &str,
params: Option<RequestParams>,
content_type: &str,
) -> Result<serde_json::Value, reqwest::Error> {
let mut data = HashMap::new();
data.insert(
"url".to_string(),
serde_json::Value::String(url.to_string()),
);
data.insert("limit".to_string(), serde_json::Value::Number(1.into()));
if let Ok(params) = serde_json::to_value(params) {
match params.as_object() {
Some(ref p) => {
let params_collect = p.iter().map(|(k, v)| (k.to_string(), v.clone()));
data.extend(params_collect);
}
_ => (),
}
}
let res = self.api_post("crawl", data, content_type).await?;
res.json().await
}
/// Sends a DELETE request to the API.
///
/// # Arguments
///
/// * `endpoint` - The API endpoint.
/// * `params` - Optional request parameters.
/// * `stream` - Whether streaming is enabled.
/// * `content_type` - The content type of the request.
///
/// # Returns
///
/// The response from the API.
async fn api_delete(
&self,
endpoint: &str,
params: Option<HashMap<String, serde_json::Value>>,
) -> Result<Response, Error> {
let url = format!("https://api.spider.cloud/v1/{}", endpoint);
let request_builder = self
.client
.delete(&url)
.header(
"User-Agent",
format!("Spider-Client/{}", env!("CARGO_PKG_VERSION")),
)
.header("Content-Type", "application/json")
.header("Authorization", format!("Bearer {}", self.api_key));
let request_builder = if let Some(params) = params {
request_builder.json(¶ms)
} else {
request_builder
};
request_builder.send().await
}
/// Crawls a URL.
///
/// # Arguments
///
/// * `url` - The URL to crawl.
/// * `params` - Optional request parameters.
/// * `stream` - Whether streaming is enabled.
/// * `content_type` - The content type of the request.
/// * `callback` - Optional callback function to handle each streamed chunk.
///
/// # Returns
///
/// The response from the API as a JSON value.
pub async fn crawl_url(
&self,
url: &str,
params: Option<RequestParams>,
stream: bool,
content_type: &str,
callback: Option<impl Fn(serde_json::Value) + Send>,
) -> Result<serde_json::Value, reqwest::Error> {
let mut data = HashMap::new();
data.insert("url".into(), serde_json::Value::String(url.to_string()));
if let Ok(params) = serde_json::to_value(params) {
match params.as_object() {
Some(ref p) => {
data.extend(p.iter().map(|(k, v)| (k.to_string(), v.clone())));
}
_ => (),
}
}
let res = self.api_post("crawl", data, content_type).await?;
if stream {
if let Some(callback) = callback {
let stream = res.bytes_stream();
tokio::pin!(stream);
while let Some(item) = stream.next().await {
match item {
Ok(chunk) => match serde_json::from_slice(&chunk) {
Ok(json_obj) => {
callback(json_obj);
}
_ => (),
},
Err(e) => {
eprintln!("Error in streaming response: {}", e);
}
}
}
Ok(serde_json::Value::Null)
} else {
Ok(serde_json::Value::Null)
}
} else {
res.json().await
}
}
/// Fetches links from a URL.
///
/// # Arguments
///
/// * `url` - The URL to fetch links from.
/// * `params` - Optional request parameters.
/// * `stream` - Whether streaming is enabled.
/// * `content_type` - The content type of the request.
///
/// # Returns
///
/// The response from the API as a JSON value.
pub async fn links(
&self,
url: &str,
params: Option<RequestParams>,
stream: bool,
content_type: &str,
) -> Result<serde_json::Value, reqwest::Error> {
let mut data = HashMap::new();
data.insert("url".into(), serde_json::Value::String(url.to_string()));
if let Ok(params) = serde_json::to_value(params) {
match params.as_object() {
Some(ref p) => {
data.extend(p.iter().map(|(k, v)| (k.to_string(), v.clone())));
}
_ => (),
}
}
let res = self.api_post("links", data, content_type).await?;
res.json().await
}
/// Takes a screenshot of a URL.
///
/// # Arguments
///
/// * `url` - The URL to take a screenshot of.
/// * `params` - Optional request parameters.
/// * `stream` - Whether streaming is enabled.
/// * `content_type` - The content type of the request.
///
/// # Returns
///
/// The response from the API as a JSON value.
pub async fn screenshot(
&self,
url: &str,
params: Option<RequestParams>,
stream: bool,
content_type: &str,
) -> Result<serde_json::Value, reqwest::Error> {
let mut data = HashMap::new();
data.insert("url".into(), serde_json::Value::String(url.to_string()));
if let Ok(params) = serde_json::to_value(params) {
match params.as_object() {
Some(ref p) => {
data.extend(p.iter().map(|(k, v)| (k.to_string(), v.clone())));
}
_ => (),
}
}
let res = self.api_post("screenshot", data, content_type).await?;
res.json().await
}
/// Searches for a query.
///
/// # Arguments
///
/// * `q` - The query to search for.
/// * `params` - Optional request parameters.
/// * `stream` - Whether streaming is enabled.
/// * `content_type` - The content type of the request.
///
/// # Returns
///
/// The response from the API as a JSON value.
pub async fn search(
&self,
q: &str,
params: Option<SearchRequestParams>,
stream: bool,
content_type: &str,
) -> Result<serde_json::Value, reqwest::Error> {
let body = match params {
Some(mut params) => {
params.search = q.to_string();
params
}
_ => {
let mut params = SearchRequestParams::default();
params.search = q.to_string();
params
}
};
let res = self.api_post("search", body, content_type).await?;
res.json().await
}
/// Transforms data.
///
/// # Arguments
///
/// * `data` - The data to transform.
/// * `params` - Optional request parameters.
/// * `stream` - Whether streaming is enabled.
/// * `content_type` - The content type of the request.
///
/// # Returns
///
/// The response from the API as a JSON value.
pub async fn transform(
&self,
data: Vec<HashMap<&str, serde_json::Value>>,
params: Option<RequestParams>,
stream: bool,
content_type: &str,
) -> Result<serde_json::Value, reqwest::Error> {
let mut payload = HashMap::new();
payload.insert("data".into(), serde_json::to_value(data).unwrap());
if let Ok(params) = serde_json::to_value(params) {
match params.as_object() {
Some(ref p) => {
payload.extend(p.iter().map(|(k, v)| (k.to_string(), v.clone())));
}
_ => (),
}
}
let res = self.api_post("transform", payload, content_type).await?;
res.json().await
}
/// Extracts contacts from a URL.
///
/// # Arguments
///
/// * `url` - The URL to extract contacts from.
/// * `params` - Optional request parameters.
/// * `stream` - Whether streaming is enabled.
/// * `content_type` - The content type of the request.
///
/// # Returns
///
/// The response from the API as a JSON value.
pub async fn extract_contacts(
&self,
url: &str,
params: Option<RequestParams>,
stream: bool,
content_type: &str,
) -> Result<serde_json::Value, reqwest::Error> {
let mut data = HashMap::new();
data.insert("url".into(), serde_json::to_value(url).unwrap());
if let Ok(params) = serde_json::to_value(params) {
match params.as_object() {
Some(ref p) => {
data.extend(p.iter().map(|(k, v)| (k.to_string(), v.clone())));
}
_ => (),
}
}
let res = self
.api_post("pipeline/extract-contacts", data, content_type)
.await?;
res.json().await
}
/// Labels data from a URL.
///
/// # Arguments
///
/// * `url` - The URL to label data from.
/// * `params` - Optional request parameters.
/// * `stream` - Whether streaming is enabled.
/// * `content_type` - The content type of the request.
///
/// # Returns
///
/// The response from the API as a JSON value.
pub async fn label(
&self,
url: &str,
params: Option<RequestParams>,
stream: bool,
content_type: &str,
) -> Result<serde_json::Value, reqwest::Error> {
let mut data = HashMap::new();
data.insert("url".into(), serde_json::Value::String(url.to_string()));
if let Ok(params) = serde_json::to_value(params) {
match params.as_object() {
Some(ref p) => {
data.extend(p.iter().map(|(k, v)| (k.to_string(), v.clone())));
}
_ => (),
}
}
let res = self.api_post("pipeline/label", data, content_type).await?;
res.json().await
}
/// Creates a signed URL.
///
/// # Arguments
///
/// * `domain` - Optional domain.
/// * `options` - Optional options.
/// * `stream` - Whether streaming is enabled.
///
/// # Returns
///
/// The response from the API.
pub async fn create_signed_url(
&self,
domain: Option<&str>,
options: Option<HashMap<&str, i32>>,
) -> Result<reqwest::Response, reqwest::Error> {
let mut params = HashMap::new();
if let Some(domain) = domain {
params.insert("domain".to_string(), domain.to_string());
}
if let Some(options) = options {
for (key, value) in options {
params.insert(key.to_string(), value.to_string());
}
}
let url = format!("https://api.spider.cloud/v1/data/storage");
let request = self
.client
.get(&url)
.header(
"User-Agent",
format!("Spider-Client/{}", env!("CARGO_PKG_VERSION")),
)
.header("Content-Type", "application/octet-stream")
.header("Authorization", format!("Bearer {}", self.api_key))
.query(¶ms);
let res = request.send().await?;
Ok(res)
}
/// Gets the crawl state of a URL.
///
/// # Arguments
///
/// * `url` - The URL to get the crawl state of.
/// * `params` - Optional request parameters.
/// * `stream` - Whether streaming is enabled.
/// * `content_type` - The content type of the request.
///
/// # Returns
///
pub async fn get_crawl_state(
&self,
url: &str,
params: Option<RequestParams>,
stream: bool,
content_type: &str,
) -> Result<serde_json::Value, reqwest::Error> {
let mut payload = HashMap::new();
payload.insert("url".into(), serde_json::Value::String(url.to_string()));
payload.insert(
"contentType".into(),
serde_json::Value::String(content_type.to_string()),
);
if let Ok(params) = serde_json::to_value(params) {
match params.as_object() {
Some(ref p) => {
payload.extend(p.iter().map(|(k, v)| (k.to_string(), v.clone())));
}
_ => (),
}
}
let res = self
.api_post("data/crawl_state", payload, content_type)
.await?;
res.json().await
}
pub async fn get_credits(&self) -> Result<serde_json::Value, reqwest::Error> {
self.api_get("data/credits").await
}
pub async fn data_post(
&self,
table: &str,
data: Option<RequestParams>,
) -> Result<serde_json::Value, reqwest::Error> {
let res = self
.api_post(&format!("data/{}", table), data, "application/json")
.await?;
res.json().await
}
pub async fn data_get(
&self,
table: &str,
params: Option<RequestParams>,
) -> Result<serde_json::Value, reqwest::Error> {
let mut payload = HashMap::new();
if let Some(params) = params {
let params = serde_json::to_value(params).unwrap();
payload.extend(
params
.as_object()
.unwrap()
.iter()
.map(|(k, v)| (k.as_str(), v.clone())),
);
}
let res = self.api_get(&format!("data/{}", table)).await?;
Ok(res)
}
pub async fn data_delete(
&self,
table: &str,
params: Option<RequestParams>,
) -> Result<serde_json::Value, reqwest::Error> {
let mut payload = HashMap::new();
if let Ok(params) = serde_json::to_value(params) {
match params.as_object() {
Some(ref p) => {
let params_collect = p.iter().map(|(k, v)| (k.to_string(), v.clone()));
payload.extend(params_collect);
}
_ => (),
}
}
let res = self
.api_delete(&format!("data/{}", table), Some(payload))
.await?;
res.json().await
}
}
#[cfg(test)]
mod tests {
use super::*;
use dotenv::dotenv;
use lazy_static::lazy_static;
lazy_static! {
static ref SPIDER_CLIENT: Spider = {
dotenv().ok();
Spider::new(None).unwrap()
};
}
#[tokio::test]
async fn test_scrape_url() {
let response = SPIDER_CLIENT
.scrape_url("https://example.com", None, "application/json")
.await;
assert!(response.is_ok());
}
#[tokio::test]
async fn test_crawl_url() {
let response = SPIDER_CLIENT
.crawl_url(
"https://example.com",
None,
false,
"application/json",
None::<fn(serde_json::Value)>,
)
.await;
assert!(response.is_ok());
}
#[tokio::test]
async fn test_links() {
let response = SPIDER_CLIENT
.links("https://example.com", None, false, "application/json")
.await;
assert!(response.is_ok());
}
#[tokio::test]
async fn test_screenshot() {
let response = SPIDER_CLIENT
.screenshot("https://example.com", None, false, "application/json")
.await;
assert!(response.is_ok());
}
// #[tokio::test]
// async fn test_search() {
// let mut params = SearchRequestParams::default();
// params.search_limit = Some(1);
// params.num = Some(1);
// let response = SPIDER_CLIENT
// .search("a sports website", Some(params), false, "application/json")
// .await;
// assert!(response.is_ok());
// }
#[tokio::test]
async fn test_transform() {
let data = vec![HashMap::new()];
let response = SPIDER_CLIENT
.transform(data, None, false, "application/json")
.await;
assert!(response.is_ok());
}
#[tokio::test]
async fn test_extract_contacts() {
let response = SPIDER_CLIENT
.extract_contacts("https://example.com", None, false, "application/json")
.await;
assert!(response.is_ok());
}
#[tokio::test]
async fn test_label() {
let response = SPIDER_CLIENT
.label("https://example.com", None, false, "application/json")
.await;
assert!(response.is_ok());
}
#[tokio::test]
async fn test_create_signed_url() {
let response = SPIDER_CLIENT
.create_signed_url(Some("example.com"), None)
.await;
assert!(response.is_ok());
}
#[tokio::test]
async fn test_get_crawl_state() {
let response = SPIDER_CLIENT
.get_crawl_state("https://example.com", None, false, "application/json")
.await;
assert!(response.is_ok());
}
#[tokio::test]
async fn test_get_credits() {
let response = SPIDER_CLIENT.get_credits().await;
assert!(response.is_ok());
}
}