Skip to content

Commit

Permalink
封装部分openApi
Browse files Browse the repository at this point in the history
  • Loading branch information
RocsSun committed Jan 12, 2024
1 parent 558c9f7 commit 0a1e8b5
Show file tree
Hide file tree
Showing 5 changed files with 414 additions and 14 deletions.
77 changes: 77 additions & 0 deletions src/apis/ing/comment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//! 闪存评论相关
//!
use anyhow::{Ok, Result};
use reqwest::{Client, Response};
use serde::{Deserialize, Serialize};

use crate::{infra::http::RequestBuilderExt, openapi};

/// 闪存评论及评论回复
///
/// replay_to: 在web端有一个ReplyToUserId,这里盲猜是这个
/// parent_comment_id: 0 是对某条闪存评论,如果对闪存评论要回应,这里则是闪存评论的id
/// content: 评论内容。 如果是对闪存评论回应,则应加上`@用户名称`
///
#[derive(Debug, Default, Deserialize, Serialize)]
#[serde(rename_all = "PascalCase")]
#[serde(default)]
pub struct StatusComment {
#[serde(skip)]
pub status_id: String,
pub replay_to: u64,
pub parent_comment_id: u64,
pub content: String,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct IngCommentEntry {
pub id: usize,
pub content: String,
#[serde(rename = "DateAdded")]
pub create_time: String,
pub status_id: usize,
pub user_alias: String,
#[serde(rename = "UserDisplayName")]
pub user_name: String,
pub user_icon_url: String,
pub user_id: usize,
pub user_guid: String,
}

/// 根据闪存ID发表一个评论
pub async fn post(token: String, sc: StatusComment) -> Result<Response> {
let r = Client::new()
.post(openapi!("/statuses/{}/comments", sc.parent_comment_id))
.pat_auth(token.as_str())
.form(&sc)
.send()
.await?
.error_for_status()?;
Ok(r)
}

/// 根据闪存ID获取评论
pub async fn get(token: &str, status_id: &str) -> Result<Vec<IngCommentEntry>> {
let r = Client::new()
.get(openapi!("/statuses/{}/comments", status_id))
.pat_auth(token)
.send()
.await?
.error_for_status()?
.json()
.await?;
Ok(r)
}

/// 根据闪存ID和commentid删除评论
pub async fn delete(token: &str, status_id: &str, comment_id: &str) -> Result<()> {
Client::new()
.delete(openapi!("/statuses/{}/comments/{}", status_id, comment_id))
.pat_auth(token)
.send()
.await?
.error_for_status()?;
Ok(())
}
197 changes: 197 additions & 0 deletions src/apis/ing/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
//! cnblogs 闪存接口模块
//!
//! 实现封装[cnblogs Api](https://api.cnblogs.com/Help#0aee001a01835c83a3277a500ffc9040)中的`Statuses`。
//!
//! - 获取自己最新一条闪存内容 https://api.cnblogs.com/api/statuses/recent
//! - 发布闪存评论 https://api.cnblogs.com/api/statuses/{statusId}/comments
//! - 获取闪存评论 https://api.cnblogs.com/api/statuses/{statusId}/comments
//! - 删除闪存评论 https://api.cnblogs.com/api/statuses/{statusId}/comments/{id}
//! - 发布闪存 https://api.cnblogs.com/api/statuses
//! - 删除闪存 https://api.cnblogs.com/api/statuses/{id}
//! - 根据类型获取闪存列表 https://api.cnblogs.com/api/statuses/@{type}?pageIndex={pageIndex}&pageSize={pageSize}&tag={tag}
//! - 根据Id获取闪存 https://api.cnblogs.com/api/statuses/{id}
//!
pub mod comment;

use anyhow::{Ok, Result};
use reqwest::{Client, Response};
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};

use crate::{infra::http::RequestBuilderExt, openapi};

#[derive(Debug, Default, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
#[serde(default)]
pub struct IngContent {
pub content: String,
pub is_private: bool,
pub client_type: IngSendFrom,
}

#[derive(Clone, Debug, Serialize_repr, Deserialize_repr)]
#[repr(u8)]
pub enum IngSendFrom {
None = 0,
Ms = 1,
GTalk = 2,
Qq = 3,
Sms = 5,
CellPhone = 6,
Web = 8,
VsCode = 9,
Cli = 13,
}

impl Default for IngSendFrom {
fn default() -> Self {
return IngSendFrom::Cli;
}
}

/// 查询条件,用于根据类别查询
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct QeurySet {
#[serde(skip)]
pub types: QueryIngType,
pub page_index: u64,
pub page_size: u64,
#[serde(skip_serializing_if = "String::is_empty")]
pub tag: String,
}

impl Default for QeurySet {
fn default() -> Self {
return Self {
types: QueryIngType::default(),
page_index: 1,
page_size: 30,
tag: "".to_string(),
};
}
}

///
/// Follow = 1, 关注
/// Myself = 4, 我的
/// Public = 5,
/// RecentComment = 6, //新回应
/// MyComment = 7, 我回应
/// Tag = 10, tag 必填
/// Comment = 13 回复我
/// Mention = 14,
#[derive(Debug)]
pub enum QueryIngType {
Following,
My,
MyComment,
RecentComment,
Mention,
Comment,
All,
}

impl Default for QueryIngType {
fn default() -> Self {
return Self::All;
}
}

impl QueryIngType {
fn as_u8(&self) -> u8 {
match self {
QueryIngType::Following => 1,
QueryIngType::My => 4,
QueryIngType::All => 5,
QueryIngType::RecentComment => 6,
QueryIngType::MyComment => 7,
QueryIngType::Mention => 14,
QueryIngType::Comment => 13,
}
}
}

/// 闪存详细内容。
///
/// 用于根据ID查询闪存的结果解析。
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct IngEntry {
pub id: u64,
pub content: String,
pub is_private: bool,
pub is_lucky: bool,
pub comment_count: u64,
pub date_added: String,
pub user_alias: String,
pub user_display_name: String,
pub user_icon_url: String,
pub user_id: u64,
pub user_guid: String,
pub send_from: u8,
pub icons: String,
}

pub async fn lastest(token: &str) -> Result<Response> {
let c = Client::new()
.get(openapi!("/statuses/recent"))
.pat_auth(token)
.send()
.await?
.error_for_status()?;
Ok(c)
}

/// 根据条件查询
///
/// 如果是tag是,一定要传入Tag,tag是自己想查询的比如Linux,Debian,Python等等。
/// 页数是从1开始的
pub async fn query(token: &str, q: &QeurySet) -> Result<Vec<IngEntry>> {
let r = Client::new()
.get(openapi!("/statuses/@{}", q.types.as_u8()))
.pat_auth(token)
.query(&q)
.send()
.await?
.error_for_status()?
.json::<Vec<IngEntry>>()
.await?;
Ok(r)
}

/// 根据ID查询
pub async fn query_by_id(token: &str, id: &str) -> Result<IngEntry> {
let r = Client::new()
.get(openapi!("/statuses/{}", id))
.pat_auth(token)
.send()
.await?
.error_for_status()?
.json::<IngEntry>()
.await?;
Ok(r)
}

/// 发布一条闪存
pub async fn post(token: &str, c: &IngContent) -> Result<Response> {
let r = Client::new()
.post(openapi!("/statuses"))
.pat_auth(token)
.json(c)
.send()
.await?;
Ok(r)
}

/// 删除一条闪存
pub async fn delete(token: &str, id: String) -> Result<Response> {
let r = Client::new()
.post(openapi!("/statuses/{}", id))
.pat_auth(token)
.send()
.await?;
Ok(r)
}
7 changes: 6 additions & 1 deletion src/apis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@
//! - token: 认证相关
//! - marks: 收藏相关
pub mod statuses;
pub mod ing;
pub mod token;

pub const OAUTH_CLIENT: &str = "https://api.cnblogs.com/token";
pub const OAUTH_TOKEN: &str = "https://oauth.cnblogs.com/connect/token";
pub const OAUTHORIZE: &str = "https://oauth.cnblogs.com/connect/authorize";
13 changes: 0 additions & 13 deletions src/apis/statuses/mod.rs

This file was deleted.

Loading

0 comments on commit 0a1e8b5

Please sign in to comment.