diff options
author | 2021-12-11 20:23:11 +0200 | |
---|---|---|
committer | 2021-12-11 10:23:11 -0800 | |
commit | f48378ec16af1035af32ed16592099104a552d90 (patch) | |
tree | 5407e21cb7e99d379c5c6f5474def43ad7e99388 | |
parent | e28f581e1d91bbe40074c47a90b0bdf6528107c2 (diff) | |
download | notion-f48378ec16af1035af32ed16592099104a552d90.tar.gz notion-f48378ec16af1035af32ed16592099104a552d90.tar.zst notion-f48378ec16af1035af32ed16592099104a552d90.zip |
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
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | examples/todo/main.rs | 6 | ||||
-rw-r--r-- | src/ids.rs | 10 | ||||
-rw-r--r-- | src/lib.rs | 17 | ||||
-rw-r--r-- | src/models/properties.rs | 4 |
5 files changed, 32 insertions, 7 deletions
@@ -34,7 +34,7 @@ features = ["derive"] cargo-husky = "1" wiremock = "0.5.2" anyhow = "1.0.40" -clap = "3.0.0-beta.2" +clap = { version = "3.0.0-rc.0", features = ["derive"] } skim = "0.9.4" crossbeam-channel = "0.5" toml = "0.5.8" diff --git a/examples/todo/main.rs b/examples/todo/main.rs index 3a01af3..0832f68 100644 --- a/examples/todo/main.rs +++ b/examples/todo/main.rs @@ -1,20 +1,20 @@ mod commands; use anyhow::{Context, Result}; -use clap::Clap; +use clap::Parser; use notion::ids::DatabaseId; use notion::NotionApi; use serde::{Deserialize, Serialize}; // From <https://docs.rs/clap/3.0.0-beta.2/clap/> -#[derive(Clap)] +#[derive(Parser, Debug)] #[clap(version = "1.0", author = "Jake Swenson")] struct Opts { #[clap(subcommand)] command: SubCommand, } -#[derive(Clap)] +#[derive(Parser, Debug)] enum SubCommand { /// Configure what database this notion-todo example uses Config, @@ -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<ById: Identifier> { @@ -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<Self, Self::Err> { + Ok($name(s.to_string())) + } + } }; } @@ -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<T: AsIdentifier<PageId>>(&self, page_id: T) -> Result<Page, Error> { + 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<D, T>( &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, }, /// <https://developers.notion.com/reference/page#relation-property-values> + /// It is actually an array of relations Relation { id: PropertyId, - relation: RelationValue, + relation: Option<Vec<RelationValue>>, }, Rollup { id: PropertyId, |