From f48378ec16af1035af32ed16592099104a552d90 Mon Sep 17 00:00:00 2001 From: Dmitry Gavrovsky Date: Sat, 11 Dec 2021 20:23:11 +0200 Subject: fix(example): Example wasn't compling + lib fixes (#19) * get page + query database bugfixes * fixed wrong error type * clap integration fix * comments cleaning * source formatting using cargo format --- src/ids.rs | 10 +++++++++- src/lib.rs | 17 ++++++++++++++++- src/models/properties.rs | 4 +++- 3 files changed, 28 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/ids.rs b/src/ids.rs index 2ede5e0..beffd06 100644 --- a/src/ids.rs +++ b/src/ids.rs @@ -1,9 +1,9 @@ use std::fmt::Display; +use std::fmt::Error; pub trait Identifier: Display { fn value(&self) -> &str; } - /// Meant to be a helpful trait allowing anything that can be /// identified by the type specified in `ById`. pub trait AsIdentifier { @@ -45,6 +45,14 @@ macro_rules! identifer { self.0.fmt(f) } } + + impl std::str::FromStr for $name { + type Err = Error; + + fn from_str(s: &str) -> Result { + Ok($name(s.to_string())) + } + } }; } diff --git a/src/lib.rs b/src/lib.rs index 98ea97f..ac45a35 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,7 +2,7 @@ use crate::ids::{BlockId, DatabaseId}; use crate::models::error::ErrorResponse; use crate::models::search::{DatabaseQuery, SearchRequest}; use crate::models::{Block, Database, ListResponse, Object, Page}; -use ids::AsIdentifier; +use ids::{AsIdentifier, PageId}; use reqwest::header::{HeaderMap, HeaderValue}; use reqwest::{header, Client, ClientBuilder, RequestBuilder}; use tracing::Instrument; @@ -161,6 +161,21 @@ impl NotionApi { } } + /// Get a page by [PageId]. + pub async fn get_page>(&self, page_id: T) -> Result { + let result = self + .make_json_request(self.client.get(format!( + "https://api.notion.com/v1/pages/{}", + page_id.as_id() + ))) + .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( &self, diff --git a/src/models/properties.rs b/src/models/properties.rs index b46b7f7..f7e3897 100644 --- a/src/models/properties.rs +++ b/src/models/properties.rs @@ -103,6 +103,7 @@ pub enum RollupFunction { Min, Max, Range, + ShowOriginal, } #[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)] @@ -284,9 +285,10 @@ pub enum PropertyValue { formula: FormulaResultValue, }, /// + /// It is actually an array of relations Relation { id: PropertyId, - relation: RelationValue, + relation: Option>, }, Rollup { id: PropertyId, -- cgit v1.2.3