#api #telegraph #page #account #token #access-token #views

telegraph-api-rs

Rust对Telegraph API的实现

4个版本

0.2.0 2023年4月9日
0.1.2 2022年11月5日
0.1.1 2022年10月20日
0.1.0 2022年10月20日

9 in #views

23 每月下载量

MIT 协议

63KB
1K SLoC

telegraph-api-rs

Crates.io Crates.io CI docs.rs

Rust对Telegraph API的实现

快速开始

Cargo.toml中添加依赖

[dependencies]
telegraph-api-rs = "0.2.0"

创建账户

use telegraph_api_rs::{Telegraph, Request};

let telegraph = Telegraph::new();
let account = telegraph.create_account()
    .short_name("Short name")
    .author_name("Author name")
    .send()
    .unwrap();

编辑账户

let edited_account = telegraph.edit_account_info()
    .access_token(token)
    .author_name("Author name 2")
    .send()
    .unwrap();

获取账户信息

let account_info = telegraph.get_account_info()
    .access_token(token)
    .fields(vec![AccountField::ShortName, AccountField::AuthorUrl])
    .send()
    .unwrap();

撤销访问令牌

let account = telegraph.revoke_access_token()
    .access_token(token)
    .send()
    .unwrap();

创建页面

let content = r#"
[
    {
        "tag": "h3",
        "children": ["Hello world"]
    },
    {
        "tag": "h4",
        "children": ["Title"]
    },
    {
        "tag": "p",
        "children": [
            {
                "tag": "ul",
                "children": ["Some text"]
            }
        ]
    }
]
"#;

let cont = build_content(content).unwrap();

let page = telegraph.create_page()
    .access_token(token)
    .title("Hello world")
    .content(cont)
    .return_content(true)
    .send()
    .unwrap();

编辑页面

let new_content = r#"
[
    {
    "tag": "h3",
        "children": ["Hello world"]
    },
    {
        "tag": "h4",
        "children": ["Title"]
    },
    {
        "tag": "p",
        "children": [
            {
                "tag": "ul",
                "children": ["Some text"]
            },
            {
                "tag": "ul",
                "children": ["Some text 2"]
            }
        ]
    }
]
"#;

let new_cont = build_content(new_content).unwrap();

let edited_page = telegraph.edit_page()
    .access_token(token)
    .title(&page.title)
    .path(&page.path)
    .content(new_cont)
    .return_content(true)
    .send()
    .unwrap();

获取页面

let get_page = telegraph.get_page()
    .path(&page.path)
    .send()
    .unwrap();

获取页面列表

let page_list = telegraph.get_page_list()
    .access_token(token)
    .limit(2)
    .send()
    .unwrap();

获取页面查看次数

let count = telegraph.get_views()
    .path(&page_list.pages[0].path)
    .year(2023)
    .month(10)
    .day(15)
    .hour(24)
    .send()
    .unwrap();

上传媒体文件

use telegraph_api_rs::Telegraph;

let telegraph = Telegraph::new();
let files = vec!["1.jpg", "2.png"];
let media = telegraph.upload(&files);

您可以使用自定义客户端上传媒体

use telegraph_api_rs::Telegraph;
use reqwest::blocking::Client;
 
let client = Client::new();
let files = vec!["1.jpg", "2.png"];
let media = Telegraph::upload_with(&client, &files);

更多示例请参考文档

依赖

~4–19MB
~253K SLoC