Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
misha-krainik committed Jan 9, 2024
1 parent 8f08968 commit 19dba39
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 70 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
[package]
name = "getpocket"
version = "0.2.5-rc1"
version = "0.2.5-rc2"
edition = "2021"
description = "Rust API for https://getpocket.com/"
repository = "https://github.com/misha-krainik/GetPocket.rs"
license = "AGPL-3.0-only"
exclude = [
".gitignore",
Expand All @@ -11,6 +12,7 @@ exclude = [
[features]
default = []
unstable = []
extended = []

[dependencies]
anyhow = "1"
Expand All @@ -34,6 +36,5 @@ name = "modify"
path = "examples/modify.rs"

[dev-dependencies]
rocksdb = "0.19"
webbrowser = "0.8.3"
lazy_static = "1.4.0"
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,13 +280,30 @@ cargo run --example list
cargo run --example add
```

```shell
cargo run --example modify
```

### Dependencies

* tokio
* reqwest
* async-trait0
* async-trait
* serde
* serde_json
* anyhow
* thiserror

### Features

This upcoming feature utilizes a new API and may be UNSTABLE, with the API subject to CHANGE. Please use it at your own risk
```
[dependencies]
getpocket = { version = "*", features = ["unstable"] }
```

[Article View](https://getpocket.com/developer/docs/v3/article-view) API and [Preferences](https://getpocket.com/developer/docs/v3/preferences-api) API (WIP)
```
[dependencies]
getpocket = { version = "*", features = ["extended"] }
```
2 changes: 1 addition & 1 deletion examples/add.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
extern crate getpocket;

use getpocket::{adding::AddingExt, GetPocket};
use getpocket::{adding::AddingExt, GetPocket};

#[path = "../tests/test_helper.rs"]
mod lib;
Expand Down
4 changes: 2 additions & 2 deletions examples/list.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
extern crate getpocket;

use getpocket::{retrieving::RetrievingExt, GetPocket};
use getpocket::{retrieving::RetrievingExt, GetPocket};

#[path = "../tests/test_helper.rs"]
mod lib;
Expand All @@ -10,4 +10,4 @@ async fn main() {
let get_pocket: GetPocket = lib::init_get_pocket().await;

dbg!(get_pocket.list_of_items().await.unwrap());
}
}
4 changes: 2 additions & 2 deletions examples/modify.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
extern crate getpocket;

use getpocket::{modifying::ModifyingExt, GetPocket};
use getpocket::{modifying::ModifyingExt, GetPocket};

#[path = "../tests/test_helper.rs"]
mod lib;
Expand All @@ -23,4 +23,4 @@ async fn main() {
.bulk_modify_raw_params("actions=%5B%7B%22action%22%3A%22archive%22%2C%22time%22%3A1348853312%2C%22item_id%22%3A229279689%7D%5D")
.await
.unwrap());
}
}
39 changes: 29 additions & 10 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,23 @@ use crate::ApiRequestError;
#[cfg(feature = "unstable")]
const RATE_LIMIT_HEADERS: [(&str, &str); 6] = [
("X-Limit-User-Limit", "Current rate limit enforced per user"),
("X-Limit-User-Remaining", "Number of calls remaining before hitting user's rate limit"),
("X-Limit-User-Reset", "Seconds until user's rate limit resets"),
("X-Limit-Key-Limit", "Current rate limit enforced per consumer key"),
("X-Limit-Key-Remaining", "Number of calls remaining before hitting consumer key's rate limit"),
("X-Limit-Key-Reset: Seconds until consumer key rate limit resets")
(
"X-Limit-User-Remaining",
"Number of calls remaining before hitting user's rate limit",
),
(
"X-Limit-User-Reset",
"Seconds until user's rate limit resets",
),
(
"X-Limit-Key-Limit",
"Current rate limit enforced per consumer key",
),
(
"X-Limit-Key-Remaining",
"Number of calls remaining before hitting consumer key's rate limit",
),
("X-Limit-Key-Reset: Seconds until consumer key rate limit resets"),
];

#[derive(Error, Debug)]
Expand Down Expand Up @@ -84,7 +96,9 @@ impl GetPocket {
token,
};

get_pocket.get_access_token_manual_open(opener_fn, None).await?;
get_pocket
.get_access_token_manual_open(opener_fn, None)
.await?;

if let Some(ref access_token) = get_pocket.token.access_token {
store_fn(access_token);
Expand Down Expand Up @@ -152,18 +166,24 @@ impl GetPocket {
.map_err(Into::into)
}

async fn get_access_token_manual_open<F>(&mut self, f: F, redirect_uri: Option<&str>) -> Result<&mut Self>
async fn get_access_token_manual_open<F>(
&mut self,
f: F,
redirect_uri: Option<&str>,
) -> Result<&mut Self>
where
F: for<'b> FnOnce(&'b str) -> Result<bool>,
{
let code = self.token_code().await?;

let redirect_uri = match redirect_uri {
Some(redirect_uri) => redirect_uri,
None => "https://getpocket.com"
None => "https://getpocket.com",
};

let is_save = f(&format!("https://getpocket.com/auth/authorize?request_token={code}&redirect_uri={redirect_uri}"))?;
let is_save = f(&format!(
"https://getpocket.com/auth/authorize?request_token={code}&redirect_uri={redirect_uri}"
))?;

if is_save {
self.token.set_code(&code);
Expand Down Expand Up @@ -286,4 +306,3 @@ pub enum RecordItemDetailType {
Simple,
Complete,
}

9 changes: 3 additions & 6 deletions src/ext/adding.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
use crate::{
client::GetPocket,
ApiRequestError,
};
use std::collections::BTreeMap as Map;
use crate::{client::GetPocket, ApiRequestError};
use anyhow::{bail, format_err, Result};
use async_trait::async_trait;
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap as Map;
use thiserror::Error;

static ENDPOINT: &'static str = "https://getpocket.com/v3/add";
Expand Down
2 changes: 1 addition & 1 deletion src/ext/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ pub mod adding;
pub use adding::AddingExt;

pub mod modifying;
pub use modifying::ModifyingExt;
pub use modifying::ModifyingExt;
9 changes: 3 additions & 6 deletions src/ext/modifying.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
};
use anyhow::{bail, format_err, Result};
use async_trait::async_trait;
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};
use thiserror::Error;

static ENDPOINT: &'static str = "https://getpocket.com/v3/send";
Expand Down Expand Up @@ -137,10 +137,7 @@ pub trait ModifyingExt {

// NOTE: function signature and code can be changed.
#[cfg(feature = "unstable")]
async fn bulk_unfavorite(
&self,
_params: &[BulkRecUnfovorite],
) -> Result<BulkRecUnfovorited> {
async fn bulk_unfavorite(&self, _params: &[BulkRecUnfovorite]) -> Result<BulkRecUnfovorited> {
unimplemented!()
}

Expand Down Expand Up @@ -220,4 +217,4 @@ impl ModifyingExt for GetPocket {

Ok(res_ser)
}
}
}
4 changes: 2 additions & 2 deletions src/ext/retrieving.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use crate::{
ApiRequestError,
};
use anyhow::{bail, format_err, Result};
use std::collections::BTreeMap as Map;
use async_trait::async_trait;
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap as Map;
use thiserror::Error;

static ENDPOINT: &'static str = "https://getpocket.com/v3/get";
Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ mod client;
pub use client::GetPocket;

mod ext;
pub use ext::retrieving;
pub use ext::adding;
pub use ext::modifying;
pub use ext::retrieving;

mod request;
pub use request::ApiRequestError;

pub use client::{
RecordItemContentType, RecordItemDetailType, RecordItemFavorite, RecordItemSort, RecordItemState, RecordItemTag,
RecordItemContentType, RecordItemDetailType, RecordItemFavorite, RecordItemSort,
RecordItemState, RecordItemTag,
};
37 changes: 20 additions & 17 deletions tests/list_tests.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
mod test_helper;

use getpocket::{
retrieving::RetrievingExt, adding::AddingExt, GetPocket, RecordItemContentType, RecordItemDetailType,
RecordItemFavorite, RecordItemSort, RecordItemState, RecordItemTag,
adding::AddingExt, retrieving::RetrievingExt, GetPocket, RecordItemContentType,
RecordItemDetailType, RecordItemFavorite, RecordItemSort, RecordItemState, RecordItemTag,
};
use tokio::test;

Expand Down Expand Up @@ -148,12 +148,14 @@ async fn test_retrieve_untagged_items() {
async fn test_retrieve_tagged_items() {
let get_pocket: GetPocket = test_helper::init_get_pocket().await;

let _ = get_pocket.add_item_with_params(
"https://www.rust-lang.org/",
Some("Rust Programming Language"),
Some(&["rust", "programming", "language"]),
None,
).await;
let _ = get_pocket
.add_item_with_params(
"https://www.rust-lang.org/",
Some("Rust Programming Language"),
Some(&["rust", "programming", "language"]),
None,
)
.await;

// Test case: Retrieve tagged items
let resp = get_pocket
Expand All @@ -172,8 +174,7 @@ async fn test_retrieve_tagged_items() {
)
.await;


dbg!(&resp);
dbg!(&resp);

assert!(resp.is_ok());
}
Expand Down Expand Up @@ -251,12 +252,14 @@ async fn test_retrieve_article_content_type_items() {
async fn test_retrieve_image_content_type_items() {
let get_pocket: GetPocket = test_helper::init_get_pocket().await;

let _ = get_pocket.add_item_with_params(
"https://www.mozilla.org/media/img/pocket/pocket-logo-light-mode.9a20614bbcba.svg",
None,
None,
None,
).await;
let _ = get_pocket
.add_item_with_params(
"https://www.mozilla.org/media/img/pocket/pocket-logo-light-mode.9a20614bbcba.svg",
None,
None,
None,
)
.await;

// Test case: Retrieve image content type items
let r = get_pocket
Expand Down Expand Up @@ -344,4 +347,4 @@ async fn test_retrieve_all_items() {
)
.await;
assert!(r.is_ok());
}
}
34 changes: 16 additions & 18 deletions tests/test_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ lazy_static! {
static ref GETPOCKET_INSTANCE: Mutex<Option<GetPocket>> = Mutex::new(None);
}

use std::{path, fs, env};
use std::{env, fs, path};

pub async fn init_get_pocket() -> GetPocket {
let consumer_key = env::var("GET_POCKET_CONSUMER_KEY").expect("ENV must be set");
Expand All @@ -22,24 +22,22 @@ pub async fn init_get_pocket() -> GetPocket {
.await
.unwrap()
}
false => {
GetPocket::init(
consumer_key,
redirect_url,
|access_token| {
fs::write(cfg_path, access_token).unwrap();
},
|auth_url| {
let ret = webbrowser::open(auth_url).is_ok();
false => GetPocket::init(
consumer_key,
redirect_url,
|access_token| {
fs::write(cfg_path, access_token).unwrap();
},
|auth_url| {
let ret = webbrowser::open(auth_url).is_ok();

let wait_time = time::Duration::from_millis(6000);
thread::sleep(wait_time);
let wait_time = time::Duration::from_millis(6000);
thread::sleep(wait_time);

Ok(ret)
},
)
.await
.unwrap()
}
Ok(ret)
},
)
.await
.unwrap(),
}
}

0 comments on commit 19dba39

Please sign in to comment.