Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add create page functionality #44

Merged
merged 3 commits into from
Jan 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::models::error::ErrorResponse;
use crate::models::search::{DatabaseQuery, SearchRequest};
use crate::models::{Block, Database, ListResponse, Object, Page};
use ids::{AsIdentifier, PageId};
use models::PageCreateRequest;
use reqwest::header::{HeaderMap, HeaderValue};
use reqwest::{header, Client, ClientBuilder, RequestBuilder};
use tracing::Instrument;
Expand Down Expand Up @@ -178,6 +179,22 @@ impl NotionApi {
}
}

/// Creates a new page and return the created page
pub async fn create_page<T: Into<PageCreateRequest>>(&self, page: T) -> Result<Page, Error> {
ahmetrehaseker marked this conversation as resolved.
Show resolved Hide resolved
let result = self
.make_json_request(
self.client
.post("https://api.notion.com/v1/pages")
.json(&page.into()),
)
.await?;

match result {
Object::Page { page } => Ok(page),
response => Err(Error::UnexpectedResponse { response }),
}
}

/// Query a database and return the matching pages.
pub async fn query_database<D, T>(
&self,
Expand Down
6 changes: 6 additions & 0 deletions src/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@ impl Properties {
}
}

#[derive(Serialize, Debug, Eq, PartialEq)]
pub struct PageCreateRequest {
pub parent: Parent,
pub properties: Properties,
}

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
pub struct Page {
pub id: PageId,
Expand Down
6 changes: 4 additions & 2 deletions src/models/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,10 @@ pub enum PropertyConfiguration {

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
pub struct SelectedValue {
pub id: SelectOptionId,
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<SelectOptionId>,
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
pub color: Color,
}

Expand Down
23 changes: 13 additions & 10 deletions src/models/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ pub enum FormulaCondition {
#[derive(Serialize, Debug, Eq, PartialEq, Clone)]
#[serde(rename_all = "snake_case")]
pub enum PropertyCondition {
Text(TextCondition),
RichText(TextCondition),
Number(NumberCondition),
Checkbox(CheckboxCondition),
Select(SelectCondition),
Expand Down Expand Up @@ -376,17 +376,20 @@ impl From<NotionSearch> for SearchRequest {
#[cfg(test)]
mod tests {
mod text_filters {
use crate::models::search::PropertyCondition::Text;
use crate::models::search::PropertyCondition::RichText;
use crate::models::search::{FilterCondition, TextCondition};
use serde_json::json;

#[test]
fn text_property_equals() -> Result<(), Box<dyn std::error::Error>> {
let json = serde_json::to_value(&FilterCondition {
property: "Name".to_string(),
condition: Text(TextCondition::Equals("Test".to_string())),
condition: RichText(TextCondition::Equals("Test".to_string())),
})?;
assert_eq!(json, json!({"property":"Name","text":{"equals":"Test"}}));
assert_eq!(
json,
json!({"property":"Name","rich_text":{"equals":"Test"}})
);

Ok(())
}
Expand All @@ -395,11 +398,11 @@ mod tests {
fn text_property_contains() -> Result<(), Box<dyn std::error::Error>> {
let json = serde_json::to_value(&FilterCondition {
property: "Name".to_string(),
condition: Text(TextCondition::Contains("Test".to_string())),
condition: RichText(TextCondition::Contains("Test".to_string())),
})?;
assert_eq!(
dbg!(json),
json!({"property":"Name","text":{"contains":"Test"}})
json!({"property":"Name","rich_text":{"contains":"Test"}})
);

Ok(())
Expand All @@ -409,11 +412,11 @@ mod tests {
fn text_property_is_empty() -> Result<(), Box<dyn std::error::Error>> {
let json = serde_json::to_value(&FilterCondition {
property: "Name".to_string(),
condition: Text(TextCondition::IsEmpty),
condition: RichText(TextCondition::IsEmpty),
})?;
assert_eq!(
dbg!(json),
json!({"property":"Name","text":{"is_empty":true}})
json!({"property":"Name","rich_text":{"is_empty":true}})
);

Ok(())
Expand All @@ -423,11 +426,11 @@ mod tests {
fn text_property_is_not_empty() -> Result<(), Box<dyn std::error::Error>> {
let json = serde_json::to_value(&FilterCondition {
property: "Name".to_string(),
condition: Text(TextCondition::IsNotEmpty),
condition: RichText(TextCondition::IsNotEmpty),
})?;
assert_eq!(
dbg!(json),
json!({"property":"Name","text":{"is_not_empty":true}})
json!({"property":"Name","rich_text":{"is_not_empty":true}})
);

Ok(())
Expand Down
2 changes: 2 additions & 0 deletions src/models/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ pub struct Annotations {
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
pub struct RichTextCommon {
pub plain_text: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub href: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub annotations: Option<Annotations>,
}

Expand Down