aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Jake Swenson <jake@jakeswenson.com> 2021-08-30 08:27:10 -0700
committerGravatar Jake Swenson <jake@jakeswenson.com> 2021-08-30 08:27:10 -0700
commiteab748bf990a304063c476fb7cdeab0ec8c325f7 (patch)
tree2211af13fff6028e346641aacf3fad2a1fe3c512 /src
parenta78a8d9444426a608888755eb1537905b7d1d96a (diff)
downloadnotion-eab748bf990a304063c476fb7cdeab0ec8c325f7.tar.gz
notion-eab748bf990a304063c476fb7cdeab0ec8c325f7.tar.zst
notion-eab748bf990a304063c476fb7cdeab0ec8c325f7.zip
Improve ergonomics
Diffstat (limited to 'src')
-rw-r--r--src/ids.rs9
-rw-r--r--src/lib.rs4
-rw-r--r--src/models/paging.rs6
-rw-r--r--src/models/search.rs13
4 files changed, 29 insertions, 3 deletions
diff --git a/src/ids.rs b/src/ids.rs
index 6febe63..2ede5e0 100644
--- a/src/ids.rs
+++ b/src/ids.rs
@@ -19,6 +19,15 @@ where
}
}
+impl<T> AsIdentifier<T> for &T
+where
+ T: Identifier,
+{
+ fn as_id(&self) -> &T {
+ self
+ }
+}
+
macro_rules! identifer {
($name:ident) => {
#[derive(serde::Serialize, serde::Deserialize, Debug, Eq, PartialEq, Hash, Clone)]
diff --git a/src/lib.rs b/src/lib.rs
index fab44ef..98ea97f 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -5,6 +5,7 @@ use crate::models::{Block, Database, ListResponse, Object, Page};
use ids::AsIdentifier;
use reqwest::header::{HeaderMap, HeaderValue};
use reqwest::{header, Client, ClientBuilder, RequestBuilder};
+use tracing::Instrument;
pub mod ids;
pub mod models;
@@ -86,9 +87,11 @@ impl NotionApi {
let json = self
.client
.execute(request)
+ .instrument(tracing::trace_span!("Sending request"))
.await
.map_err(|source| Error::RequestFailed { source })?
.text()
+ .instrument(tracing::trace_span!("Reading response"))
.await
.map_err(|source| Error::ResponseIoError { source })?;
@@ -100,6 +103,7 @@ impl NotionApi {
}
let result =
serde_json::from_str(&json).map_err(|source| Error::JsonParseError { source })?;
+
match result {
Object::Error { error } => Err(Error::ApiError { error }),
response => Ok(response),
diff --git a/src/models/paging.rs b/src/models/paging.rs
index 3b97301..481c235 100644
--- a/src/models/paging.rs
+++ b/src/models/paging.rs
@@ -4,10 +4,14 @@ use serde::{Deserialize, Serialize};
#[serde(transparent)]
pub struct PagingCursor(String);
-#[derive(Serialize, Debug, Eq, PartialEq, Default)]
+#[derive(Serialize, Debug, Eq, PartialEq, Default, Clone)]
pub struct Paging {
#[serde(skip_serializing_if = "Option::is_none")]
pub start_cursor: Option<PagingCursor>,
#[serde(skip_serializing_if = "Option::is_none")]
pub page_size: Option<u8>,
}
+
+pub trait Pageable {
+ fn start_from(self, starting_point: Paging) -> Self;
+}
diff --git a/src/models/search.rs b/src/models/search.rs
index 3e5d782..22dab76 100644
--- a/src/models/search.rs
+++ b/src/models/search.rs
@@ -1,5 +1,5 @@
use crate::ids::{PageId, UserId};
-use crate::models::paging::Paging;
+use crate::models::paging::{Pageable, Paging};
use crate::models::Number;
use chrono::{DateTime, Utc};
use serde::ser::SerializeMap;
@@ -290,7 +290,7 @@ pub struct DatabaseSort {
pub direction: SortDirection,
}
-#[derive(Serialize, Debug, Eq, PartialEq, Default)]
+#[derive(Serialize, Debug, Eq, PartialEq, Default, Clone)]
pub struct DatabaseQuery {
#[serde(skip_serializing_if = "Option::is_none")]
pub sorts: Option<Vec<DatabaseSort>>,
@@ -300,6 +300,15 @@ pub struct DatabaseQuery {
pub paging: Option<Paging>,
}
+impl Pageable for DatabaseQuery {
+ fn start_from(self, starting_point: Paging) -> Self {
+ DatabaseQuery {
+ paging: Some(starting_point),
+ ..self
+ }
+ }
+}
+
#[derive(Debug, Eq, PartialEq)]
pub enum NotionSearch {
/// When supplied, limits which pages are returned by comparing the query to the page title.