From 99b4e4a67569e86438b4a42bc62ad4be4a406eb2 Mon Sep 17 00:00:00 2001 From: Bram Dingelstad Date: Mon, 20 Mar 2023 15:07:06 +0100 Subject: [PATCH] feat: added search endpoint --- src/lib.rs | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 8706f3c..093c15f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,7 @@ +use std::sync::Arc; use std::collections::HashMap; + use serde_json::json; - -// TODO: Add the ability to hack into the code or add queuing - use regex::Regex; use serde_json::Value; use chrono::{DateTime, Utc}; @@ -15,6 +14,8 @@ lazy_static! { .expect("ISO 8601 date regex to be parseable"); } +// TODO: Add the ability to hack into the code or add queuing + pub type Result = std::result::Result; #[derive(Debug)] @@ -82,7 +83,16 @@ pub struct Client { pub databases: Databases } -use std::sync::Arc; + +#[allow(unused)] +#[derive(Serialize)] +pub struct SearchOptions<'a> { + pub query: Option<&'a str>, + pub filter: Option, + pub sort: Option, + pub start_cursor: Option<&'a str>, + pub page_size: Option +} impl<'a> Client { pub fn new(notion_api_key: &'a str) -> Self { @@ -95,8 +105,28 @@ impl<'a> Client { databases: Databases { http_client: http_client.clone() } } } + + pub async fn search<'b>(self, options: SearchOptions<'b>) -> Result> { + let url = "https://api.notion.com/v1/search"; + + let request = self.http_client + .post(url) + .json(&options) + .send() + .await?; + + match request.error_for_status_ref() { + Ok(_) => Ok(request.json().await?), + Err(error) => { + println!("Error: {error:#?}"); + println!("Body: {:#?}", request.json::().await?); + Err(Error::Http(error)) + } + } + } } + pub struct PageOptions<'a> { pub page_id: &'a str }