aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/lib.rs50
-rw-r--r--src/models.rs16
-rw-r--r--src/models/search.rs10
-rw-r--r--src/models/tests/query_result.json43
4 files changed, 104 insertions, 15 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 3636ab7..7c4e454 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,5 +1,5 @@
-use crate::models::search::SearchRequest;
-use crate::models::{Database, DatabaseId, ListResponse};
+use crate::models::search::{DatabaseQuery, SearchRequest};
+use crate::models::{Database, DatabaseId, ListResponse, Object, Page};
use reqwest::header::{HeaderMap, HeaderValue};
use reqwest::{header, Client, ClientBuilder, RequestBuilder};
use serde::de::DeserializeOwned;
@@ -54,6 +54,7 @@ impl NotionApi {
T: DeserializeOwned,
{
let json = request.send().await?.text().await?;
+ println!("JSON: {}", json);
dbg!(serde_json::from_str::<serde_json::Value>(&json)?);
let result = serde_json::from_str(&json)?;
Ok(result)
@@ -71,7 +72,7 @@ impl NotionApi {
pub async fn search<T: Into<SearchRequest>>(
&self,
query: T,
- ) -> Result<ListResponse<Database>, Box<dyn std::error::Error>> {
+ ) -> Result<ListResponse<Database>, NotionApiClientError> {
Ok(NotionApi::make_json_request(
self.client
.post("https://api.notion.com/v1/search")
@@ -83,7 +84,7 @@ impl NotionApi {
pub async fn get_database<T: Identifiable<Type = DatabaseId>>(
&self,
database_id: T,
- ) -> Result<Database, Box<dyn std::error::Error>> {
+ ) -> Result<Database, NotionApiClientError> {
Ok(NotionApi::make_json_request(self.client.get(format!(
"https://api.notion.com/v1/databases/{}",
database_id.id().id()
@@ -95,9 +96,9 @@ impl NotionApi {
&self,
database: D,
query: T,
- ) -> Result<ListResponse<Database>, NotionApiClientError>
+ ) -> Result<ListResponse<Page>, NotionApiClientError>
where
- T: Into<SearchRequest>,
+ T: Into<DatabaseQuery>,
D: Identifiable<Type = DatabaseId>,
{
Ok(NotionApi::make_json_request(
@@ -114,8 +115,12 @@ impl NotionApi {
#[cfg(test)]
mod tests {
- use crate::models::search::{FilterProperty, FilterValue, NotionSearch};
+ use crate::models::search::PropertyCondition::Text;
+ use crate::models::search::{
+ DatabaseQuery, FilterCondition, FilterProperty, FilterValue, NotionSearch, TextCondition,
+ };
use crate::NotionApi;
+
const TEST_TOKEN: &'static str = include_str!(".api_token");
fn test_client() -> NotionApi {
@@ -165,4 +170,35 @@ mod tests {
Ok(())
}
+
+ #[tokio::test]
+ async fn query_database() -> Result<(), Box<dyn std::error::Error>> {
+ let api = test_client();
+
+ let response = api
+ .search(NotionSearch::Filter {
+ value: FilterValue::Database,
+ property: FilterProperty::Object,
+ })
+ .await?;
+
+ let db = dbg!(response.results()[0].clone());
+
+ let pages = api
+ .query_database(
+ db,
+ DatabaseQuery {
+ filter: Some(FilterCondition {
+ property: "Name".to_string(),
+ condition: Text(TextCondition::Contains("First".to_string())),
+ }),
+ ..Default::default()
+ },
+ )
+ .await?;
+
+ assert_eq!(pages.results().len(), 1);
+
+ Ok(())
+ }
}
diff --git a/src/models.rs b/src/models.rs
index bb1ba42..2581e13 100644
--- a/src/models.rs
+++ b/src/models.rs
@@ -104,9 +104,13 @@ impl PageId {
#[serde(rename_all = "snake_case")]
pub enum Parent {
#[serde(rename = "database_id")]
- Database(#[serde(rename = "database_id")] DatabaseId),
+ Database {
+ database_id: DatabaseId,
+ },
#[serde(rename = "page_id")]
- Page(#[serde(rename = "page_id")] PageId),
+ Page {
+ page_id: PageId,
+ },
Workspace,
}
@@ -190,10 +194,16 @@ pub enum User {
#[cfg(test)]
mod tests {
- use crate::models::Page;
+ use crate::models::{ListResponse, Page};
#[test]
fn deserialize_page() {
let _page: Page = serde_json::from_str(include_str!("models/tests/page.json")).unwrap();
}
+
+ #[test]
+ fn deserialize_query_result() {
+ let _page: ListResponse<Page> =
+ serde_json::from_str(include_str!("models/tests/query_result.json")).unwrap();
+ }
}
diff --git a/src/models/search.rs b/src/models/search.rs
index 09a74b4..7b14aa2 100644
--- a/src/models/search.rs
+++ b/src/models/search.rs
@@ -72,19 +72,19 @@ pub enum PropertyCondition {
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
pub struct FilterCondition {
- property: String,
+ pub property: String,
#[serde(flatten)]
- condition: PropertyCondition,
+ pub condition: PropertyCondition,
}
#[derive(Serialize, Debug, Eq, PartialEq, Default)]
pub struct DatabaseQuery {
#[serde(skip_serializing_if = "Option::is_none")]
- sorts: Option<Sort>,
+ pub sorts: Option<Sort>,
#[serde(skip_serializing_if = "Option::is_none")]
- filter: Option<FilterCondition>,
+ pub filter: Option<FilterCondition>,
#[serde(flatten)]
- paging: Option<Paging>,
+ pub paging: Option<Paging>,
}
#[derive(Debug, Eq, PartialEq)]
diff --git a/src/models/tests/query_result.json b/src/models/tests/query_result.json
new file mode 100644
index 0000000..56b8834
--- /dev/null
+++ b/src/models/tests/query_result.json
@@ -0,0 +1,43 @@
+{
+ "object": "list",
+ "results": [
+ {
+ "object": "page",
+ "id": "bb85a889-3eb3-4146-9325-80508fb5e23d",
+ "created_time": "2021-05-15T17:16:51.364Z",
+ "last_edited_time": "2021-05-15T17:16:00.000Z",
+ "parent": {
+ "type": "database_id",
+ "database_id": "5d794de0-2224-49d3-86f9-3540db13d884"
+ },
+ "archived": false,
+ "properties": {
+ "Name": {
+ "id": "title",
+ "type": "title",
+ "title": [
+ {
+ "type": "text",
+ "text": {
+ "content": "First",
+ "link": null
+ },
+ "annotations": {
+ "bold": false,
+ "italic": false,
+ "strikethrough": false,
+ "underline": false,
+ "code": false,
+ "color": "default"
+ },
+ "plain_text": "First",
+ "href": null
+ }
+ ]
+ }
+ }
+ }
+ ],
+ "next_cursor": null,
+ "has_more": false
+}