/
hatena_blog_client.rs
64 lines (57 loc) · 2.23 KB
/
hatena_blog_client.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use crate::hatena_blog::HatenaBlogEntryId;
use hatena_blog_api::{
Client, Config, CreateEntryResponse, EntryId, EntryParams, GetEntryResponse,
UpdateEntryResponse,
};
use crate::hatena_blog::HatenaBlogListEntriesResponse;
#[derive(Clone, Debug)]
pub struct HatenaBlogClient {
config: Config,
}
impl HatenaBlogClient {
pub fn new(hatena_id: String, hatena_blog_id: String, hatena_api_key: String) -> Self {
let config = Config::new(&hatena_id, None, &hatena_blog_id, &hatena_api_key);
Self { config }
}
pub async fn create_entry(&self, params: EntryParams) -> anyhow::Result<CreateEntryResponse> {
let client = Client::new(&self.config);
Ok(client.create_entry(params).await?)
}
pub async fn update_entry(
&self,
hatena_blog_entry_id: &HatenaBlogEntryId,
params: EntryParams,
) -> anyhow::Result<UpdateEntryResponse> {
let client = Client::new(&self.config);
let entry_id = EntryId::from(hatena_blog_entry_id);
Ok(client.update_entry(&entry_id, params).await?)
}
pub async fn get_entry(
&self,
hatena_blog_entry_id: &HatenaBlogEntryId,
) -> anyhow::Result<Option<GetEntryResponse>> {
let client = Client::new(&self.config);
let entry_id = EntryId::from(hatena_blog_entry_id);
Ok(match client.get_entry(&entry_id).await {
Ok(response) => Ok(Some(response)),
Err(err) => match err {
hatena_blog_api::ClientError::NotFound => Ok(None),
hatena_blog_api::ClientError::RequestError(_)
| hatena_blog_api::ClientError::BadRequest
| hatena_blog_api::ClientError::Unauthorized
| hatena_blog_api::ClientError::MethodNotAllowed
| hatena_blog_api::ClientError::InternalServerError
| hatena_blog_api::ClientError::UnknownStatusCode => Err(err),
},
}?)
}
pub async fn list_entries_in_page(
&self,
page: Option<&str>,
) -> anyhow::Result<HatenaBlogListEntriesResponse> {
let client = Client::new(&self.config);
Ok(HatenaBlogListEntriesResponse::from(
client.list_entries_in_page(page).await?,
))
}
}