Skip to content

Commit

Permalink
fix(example): Example wasn't compling + lib fixes (#19)
Browse files Browse the repository at this point in the history
* get page + query database bugfixes

* fixed wrong error type

* clap integration fix

* comments cleaning

* source formatting using cargo format
  • Loading branch information
flamedmg authored Dec 11, 2021
1 parent e28f581 commit f48378e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions examples/todo/main.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
10 changes: 9 additions & 1 deletion src/ids.rs
Original file line number Diff line number Diff line change
@@ -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> {
Expand Down Expand Up @@ -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()))
}
}
};
}

Expand Down
17 changes: 16 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion src/models/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ pub enum RollupFunction {
Min,
Max,
Range,
ShowOriginal,
}

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit f48378e

Please sign in to comment.