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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
//! telegraph API binding in Rust
//!
//! See https://telegra.ph/api for more information
//!
//! # Examples
//!
//! ```
//! # async fn run() -> Result<(), telegraph_rs::Error> {
//! use telegraph_rs::{Telegraph, html_to_node};
//!
//! let telegraph = Telegraph::new("test_account").create().await?;
//!
//! let page = telegraph.create_page("title", &html_to_node("<p>Hello, world</p>"), false).await?;
//! # Ok(())
//! # }
//! ```
pub mod error;
pub mod types;
pub mod utils;
pub use error::*;
pub use types::*;
pub use utils::*;
use reqwest::{multipart::Form, Client, Response};
use std::collections::HashMap;
pub type Result<T> = std::result::Result<T, Error>;
macro_rules! send {
($e:expr) => {
$e.send().await.and_then(Response::error_for_status)
};
}
#[derive(Debug, Default, Clone)]
pub struct AccountBuilder {
access_token: Option<String>,
short_name: String,
author_name: Option<String>,
author_url: Option<String>,
client: Client,
}
impl AccountBuilder {
pub fn new(short_name: &str) -> Self {
AccountBuilder {
short_name: short_name.to_owned(),
..Default::default()
}
}
/// Account name, helps users with several accounts remember which they are currently using.
///
/// Displayed to the user above the "Edit/Publish" button on Telegra.ph,
///
/// other users don't see this name.
pub fn short_name(mut self, short_name: &str) -> Self {
self.short_name = short_name.to_owned();
self
}
/// Access token of the Telegraph account.
pub fn access_token(mut self, access_token: &str) -> Self {
self.access_token = Some(access_token.to_owned());
self
}
/// Default author name used when creating new articles.
pub fn author_name(mut self, author_name: &str) -> Self {
self.author_name = Some(author_name.to_owned());
self
}
/// Default profile link, opened when users click on the author's name below the title.
///
/// Can be any link, not necessarily to a Telegram profile or channel.
pub fn author_url(mut self, author_url: &str) -> Self {
self.author_url = Some(author_url.to_owned());
self
}
/// Client
pub fn client(mut self, client: Client) -> Self {
self.client = client;
self
}
/// If `access_token` is not set, an new account will be create.
///
/// Otherwise import the existing account.
pub async fn create(mut self) -> Result<Telegraph> {
if self.access_token.is_none() {
let account = Telegraph::create_account(
&self.short_name,
self.author_name.as_deref(),
self.author_url.as_deref(),
)
.await?;
self.access_token = Some(account.access_token.unwrap());
}
Ok(Telegraph {
client: self.client,
access_token: self.access_token.unwrap(),
short_name: self.short_name.to_owned(),
author_name: self.author_name.unwrap_or(self.short_name),
author_url: self.author_url,
})
}
/// Edit info of an an existing account.
pub async fn edit(self) -> Result<Telegraph> {
let response = send!(Client::new()
.get("https://api.telegra.ph/editAccountInfo")
.query(&[
("access_token", self.access_token.as_ref().unwrap()),
("short_name", &self.short_name),
("author_name", self.author_name.as_ref().unwrap()),
(
"author_url",
self.author_url.as_ref().unwrap_or(&String::new()),
),
]))?;
let json: Result<Account> = response.json::<ApiResult<Account>>().await?.into();
let json = json?;
Ok(Telegraph {
client: Client::new(),
access_token: self.access_token.unwrap(),
short_name: json.short_name.clone().unwrap(),
author_name: json.author_name.or(json.short_name).unwrap(),
author_url: json.author_url,
})
}
}
#[derive(Debug, Clone)]
pub struct Telegraph {
client: Client,
access_token: String,
short_name: String,
author_name: String,
author_url: Option<String>,
}
impl Telegraph {
/// Use this method to create a new Telegraph account or import an existing one.
///
/// Most users only need one account, but this can be useful for channel administrators who would like to keep individual author names and profile links for each of their channels.
///
/// On success, returns an Account object with the regular fields and an additional access_token field.
///
/// ```
/// # async fn run() -> Result<(), telegraph_rs::Error> {
/// use telegraph_rs::Telegraph;
///
/// let account = Telegraph::new("short_name")
/// .access_token("b968da509bb76866c35425099bc0989a5ec3b32997d55286c657e6994bbb")
/// .create()
/// .await?;
/// # Ok(())
/// # }
/// ```
pub fn new(short_name: &str) -> AccountBuilder {
AccountBuilder {
short_name: short_name.to_owned(),
..Default::default()
}
}
pub(crate) async fn create_account<'a, S, T>(
short_name: &str,
author_name: S,
author_url: T,
) -> Result<Account>
where
T: Into<Option<&'a str>>,
S: Into<Option<&'a str>>,
{
let mut params = HashMap::new();
params.insert("short_name", short_name);
if let Some(author_name) = author_name.into() {
params.insert("author_name", author_name);
}
if let Some(author_url) = author_url.into() {
params.insert("author_url", author_url);
}
let response = send!(Client::new()
.get("https://api.telegra.ph/createAccount")
.query(¶ms))?;
response.json::<ApiResult<Account>>().await?.into()
}
/// Use this method to create a new Telegraph page. On success, returns a Page object.
///
/// if `return_content` is true, a content field will be returned in the Page object.
///
/// ```
/// # async fn test() -> Result<(), telegraph_rs::Error> {
/// use telegraph_rs::{Telegraph, html_to_node};
///
/// let telegraph = Telegraph::new("author")
/// .access_token("b968da509bb76866c35425099bc0989a5ec3b32997d55286c657e6994bbb")
/// .create()
/// .await?;
///
/// let page = telegraph.create_page("title", &html_to_node("<p>Hello, world!</p>"), false).await?;
/// # Ok(())
/// # }
/// ```
pub async fn create_page(
&self,
title: &str,
content: &str,
return_content: bool,
) -> Result<Page> {
// TODO: content HTML 形式
let response = send!(self
.client
.post("https://api.telegra.ph/createPage")
.form(&[
("access_token", &*self.access_token),
("title", title),
("author_name", &*self.author_name),
("author_url", self.author_url.as_deref().unwrap_or("")),
("content", content),
("return_content", &*return_content.to_string()),
]))?;
response.json::<ApiResult<Page>>().await?.into()
}
/// Use this method to update information about a Telegraph account.
///
/// Pass only the parameters that you want to edit.
///
/// On success, returns an Account object with the default fields.
pub fn edit_account_info(self) -> AccountBuilder {
AccountBuilder {
access_token: Some(self.access_token),
short_name: self.short_name,
author_name: Some(self.author_name),
author_url: self.author_url,
client: self.client,
}
}
/// Use this method to edit an existing Telegraph page.
///
/// On success, returns a Page object.
pub async fn edit_page(
&self,
path: &str,
title: &str,
content: &str,
return_content: bool,
) -> Result<Page> {
let response = send!(self.client.post("https://api.telegra.ph/editPage").form(&[
("access_token", &*self.access_token),
("path", path),
("title", title),
("author_name", &*self.author_name),
("author_url", self.author_url.as_deref().unwrap_or("")),
("content", content),
("return_content", &*return_content.to_string()),
]))?;
response.json::<ApiResult<Page>>().await?.into()
}
/// Use this method to get information about a Telegraph account. Returns an Account object on success.
///
/// Available fields: short_name, author_name, author_url, auth_url, page_count.
pub async fn get_account_info(&self, fields: &[&str]) -> Result<Account> {
let response = send!(self
.client
.get("https://api.telegra.ph/getAccountInfo")
.query(&[
("access_token", &self.access_token),
("fields", &serde_json::to_string(fields).unwrap()),
]))?;
response.json::<ApiResult<Account>>().await?.into()
}
/// Use this method to get a Telegraph page. Returns a Page object on success.
pub async fn get_page(path: &str, return_content: bool) -> Result<Page> {
let response = Client::new()
.get(&format!("https://api.telegra.ph/getPage/{}", path))
.query(&[("return_content", return_content.to_string())])
.send()
.await?
.error_for_status()?;
response.json::<ApiResult<Page>>().await?.into()
}
/// Use this method to get a list of pages belonging to a Telegraph account.
///
/// Returns a PageList object, sorted by most recently created pages first.
///
/// - `offset` Sequential number of the first page to be returned. (suggest: 0)
/// - `limit` Limits the number of pages to be retrieved. (suggest: 50)
pub async fn get_page_list(&self, offset: i32, limit: i32) -> Result<PageList> {
let response = send!(self
.client
.get("https://api.telegra.ph/getPageList")
.query(&[
("access_token", &self.access_token),
("offset", &offset.to_string()),
("limit", &limit.to_string()),
]))?;
response.json::<ApiResult<PageList>>().await?.into()
}
/// Use this method to get the number of views for a Telegraph article.
///
/// Returns a PageViews object on success.
///
/// By default, the total number of page views will be returned.
///
/// ```rust
/// # async fn run() -> Result<(), telegraph_rs::Error> {
/// use telegraph_rs::Telegraph;
///
/// let view1 = Telegraph::get_views("Sample-Page-12-15", &vec![2016, 12]).await?;
/// let view2 = Telegraph::get_views("Sample-Page-12-15", &vec![2019, 5, 19, 12]).await?; // year-month-day-hour
/// # Ok(())
/// # }
/// ```
pub async fn get_views(path: &str, time: &[i32]) -> Result<PageViews> {
let params = ["year", "month", "day", "hour"]
.iter()
.zip(time)
.collect::<HashMap<_, _>>();
let response = send!(Client::new()
.get(&format!("https://api.telegra.ph/getViews/{}", path))
.query(¶ms))?;
response.json::<ApiResult<PageViews>>().await?.into()
}
/// Use this method to revoke access_token and generate a new one,
///
/// for example, if the user would like to reset all connected sessions,
///
/// or you have reasons to believe the token was compromised.
///
/// On success, returns an Account object with new access_token and auth_url fields.
pub async fn revoke_access_token(&mut self) -> Result<Account> {
let response = send!(self
.client
.get("https://api.telegra.ph/revokeAccessToken")
.query(&[("access_token", &self.access_token)]))?;
let json: Result<Account> = response.json::<ApiResult<Account>>().await?.into();
if json.is_ok() {
self.access_token = json
.as_ref()
.unwrap()
.access_token
.as_ref()
.unwrap()
.to_owned();
}
json
}
/// Upload files to telegraph with custom client
#[cfg(feature = "upload")]
pub async fn upload_with<T: Uploadable>(
files: &[T],
client: &Client,
) -> Result<Vec<ImageInfo>> {
let mut form = Form::new();
for (i, file) in files.iter().enumerate() {
let part = file.part()?;
form = form.part(i.to_string(), part);
}
let response = send!(client.post("https://telegra.ph/upload").multipart(form))?;
match response.json::<UploadResult>().await? {
UploadResult::Error { error } => Err(Error::ApiError(error)),
UploadResult::Source(v) => Ok(v),
}
}
/// Upload files to telegraph
#[cfg(feature = "upload")]
pub async fn upload<T: Uploadable>(files: &[T]) -> Result<Vec<ImageInfo>> {
Self::upload_with(files, &Client::new()).await
}
}
#[cfg(feature = "html")]
fn html_to_node_inner(node: &html_parser::Node) -> Option<Node> {
match node {
html_parser::Node::Text(text) => Some(Node::Text(text.to_owned())),
html_parser::Node::Element(element) => Some(Node::NodeElement(NodeElement {
tag: element.name.to_owned(),
attrs: {
(!element.attributes.is_empty()).then(|| element.attributes.clone())
},
children: {
if element.children.is_empty() {
None
} else {
element.children.iter().map(|node| html_to_node_inner(node))
.collect::<Option<Vec<_>>>()
}
},
})),
_ => None,
}
}
#[cfg(test)]
mod tests {
use crate::Telegraph;
#[test]
fn html_to_node() {
let html = r#"<a>Text</a><p>img:<img src="https://me"></p>"#;
println!("{}", super::html_to_node(html));
}
#[tokio::test]
async fn create_and_revoke_account() {
let result = Telegraph::create_account("sample", "a", None).await;
println!("{:?}", result);
assert!(result.is_ok());
let mut telegraph = Telegraph::new("test")
.access_token(&result.unwrap().access_token.unwrap().to_owned())
.create()
.await
.unwrap();
let result = telegraph.revoke_access_token().await;
println!("{:?}", result);
assert!(result.is_ok());
}
#[tokio::test]
async fn edit_account_info() {
let result = Telegraph::new("test")
.access_token("d3b25feccb89e508a9114afb82aa421fe2a9712b963b387cc5ad71e58722")
.create()
.await
.unwrap()
.edit_account_info()
.short_name("wow")
.edit()
.await;
println!("{:?}", result);
assert!(result.is_ok());
}
#[tokio::test]
async fn get_account_info() {
let result = Telegraph::new("test")
.access_token("d3b25feccb89e508a9114afb82aa421fe2a9712b963b387cc5ad71e58722")
.create()
.await
.unwrap()
.get_account_info(&["short_name"])
.await;
println!("{:?}", result);
assert!(result.is_ok());
}
#[tokio::test]
async fn create_get_edit_page() {
let telegraph = Telegraph::new("test")
.access_token("d3b25feccb89e508a9114afb82aa421fe2a9712b963b387cc5ad71e58722")
.create()
.await
.unwrap();
let page = telegraph
.create_page(
"OVO",
r#"[{"tag":"p","children":["Hello,+world!"]}]"#,
false,
)
.await;
println!("{:?}", page);
assert!(page.is_ok());
let page = Telegraph::get_page(&page.unwrap().path, true).await;
println!("{:?}", page);
assert!(page.is_ok());
let page = telegraph
.edit_page(
&page.unwrap().path,
"QAQ",
r#"[{"tag":"p","children":["Goodbye,+world!"]}]"#,
false,
)
.await;
println!("{:?}", page);
assert!(page.is_ok());
}
#[tokio::test]
async fn get_page_list() {
let telegraph = Telegraph::new("test")
.access_token("d3b25feccb89e508a9114afb82aa421fe2a9712b963b387cc5ad71e58722")
.create()
.await
.unwrap();
let page_list = telegraph.get_page_list(0, 3).await;
println!("{:?}", page_list);
assert!(page_list.is_ok());
}
#[tokio::test]
async fn get_views() {
let views = Telegraph::get_views("Sample-Page-12-15", &vec![2016, 12]).await;
println!("{:?}", views);
assert!(views.is_ok());
}
#[ignore]
#[tokio::test]
#[cfg(feature = "upload")]
async fn upload() {
let images = Telegraph::upload(&vec!["1.jpeg", "2.jpeg"]).await;
println!("{:?}", images);
assert!(images.is_ok());
}
}