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
//! telegraph API binding in Rust
//!
//! See https://telegra.ph/api for more information
//!
//! # Examples
//!
//! ```
//! use telegraph_rs::Telegraph;
//!
//! let telegraph = Telegraph::new("test_account").create().unwrap();
//!
//! let page = telegraph.create_page("title", "content", false).unwrap();
//! ```
//!
pub mod error;
pub mod types;

use std::collections::HashMap;

use reqwest::Client;

pub use error::*;
pub use types::*;
use std::path::Path;
use reqwest::multipart::Form;
use reqwest::multipart::Part;

pub type Result<T> = std::result::Result<T, Error>;

#[derive(Debug, Default)]
pub struct AccountBuilder {
    access_token: Option<String>,
    short_name: String,
    author_name: Option<String>,
    author_url: Option<String>,
}

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
    }

    /// If `access_token` is not set, an new account will be create.
    ///
    /// Otherwise import the existing account.
    pub fn create(mut self) -> Result<Telegraph> {
        if self.access_token.is_none() {
            let account = Telegraph::create_account(
                &self.short_name,
                self.author_name.as_ref().map(|s| &**s),
                self.author_url.as_ref().map(|s| &**s),
            )?;
            self.access_token = Some(account.access_token.unwrap().to_owned());
        }

        Ok(Telegraph {
            client: Client::new(),
            access_token: self.access_token.unwrap().to_owned(),
            short_name: self.short_name.to_owned(),
            author_name: self.author_name.unwrap_or(self.short_name.to_owned()),
            author_url: self.author_url.clone(),
        })
    }

    /// Edit info of an an existing account.
    pub fn edit(self) -> Result<Telegraph> {
        let mut response = 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())),
            ])
            .send()?;
        let json: Result<Account> = response.json::<ApiResult<Account>>()?.into();
        let json = json?;

        Ok(Telegraph {
            client: Client::new(),
            access_token: self.access_token.clone().unwrap(),
            short_name: json.short_name.clone().unwrap(),
            author_name: json.author_name.unwrap_or(json.short_name.clone().unwrap()),
            author_url: json.author_url.clone(),
        })
    }
}

#[derive(Debug)]
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.
    ///
    /// ```
    /// use telegraph_rs::Telegraph;
    ///
    /// let account = Telegraph::new("short_name")
    ///     .access_token("b968da509bb76866c35425099bc0989a5ec3b32997d55286c657e6994bbb")
    ///     .create();
    /// ```
    pub fn new(short_name: &str) -> AccountBuilder {
        AccountBuilder {
            short_name: short_name.to_owned(),
            ..Default::default()
        }
    }

    pub(crate) fn create_account(
        short_name: &str,
        author_name: Option<&str>,
        author_url: Option<&str>,
    ) -> Result<Account> {
        let mut params = HashMap::new();
        params.insert("short_name", short_name);
        if let Some(author_name) = author_name {
            params.insert("author_name", author_name);
        }
        if let Some(author_url) = author_url {
            params.insert("author_url", author_url);
        }
        let mut response = Client::new()
            .get("https://api.telegra.ph/createAccount")
            .query(&params)
            .send()?;
        response.json::<ApiResult<Account>>()?.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.
    ///
    /// ```
    /// use telegraph_rs::Telegraph;
    ///
    /// let telegraph = Telegraph::new("author")
    ///     .access_token("b968da509bb76866c35425099bc0989a5ec3b32997d55286c657e6994bbb")
    ///     .create()
    ///     .unwrap();
    ///
    /// let page = telegraph.create_page("title", r#"[{"tag":"p","children":["Hello, world!"]}]"#, false);
    /// ```
    pub fn create_page(&self, title: &str, content: &str, return_content: bool) -> Result<Page> {
        // TODO: content HTML 形式
        let mut response = self
            .client
            .post("https://api.telegra.ph/createPage")
            .form(&[
                ("access_token", &*self.access_token),
                ("title", title),
                ("author_name", &*self.author_name),
                ("content", content),
                ("return_content", &*return_content.to_string()),
            ])
            .send()?;
        response.json::<ApiResult<Page>>()?.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.clone()),
            short_name: self.short_name.clone(),
            author_name: Some(self.author_name.clone()),
            author_url: self.author_url.clone(),
        }
    }

    /// Use this method to edit an existing Telegraph page.
    ///
    /// On success, returns a Page object.
    pub fn edit_page(
        &self,
        path: &str,
        title: &str,
        content: &str,
        return_content: bool,
    ) -> Result<Page> {
        let mut response = self
            .client
            .post("https://api.telegra.ph/editPage")
            .form(&[
                ("access_token", &*self.access_token),
                ("path", path),
                ("title", title),
                ("author_name", &*self.author_name),
                ("content", content),
                ("return_content", &*return_content.to_string()),
            ])
            .send()?;
        response.json::<ApiResult<Page>>()?.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 fn get_account_info(&self, fields: &[&str]) -> Result<Account> {
        let mut response = self
            .client
            .get("https://api.telegra.ph/getAccountInfo")
            .query(&[
                ("access_token", &self.access_token),
                ("fields", &serde_json::to_string(fields).unwrap()),
            ])
            .send()?;
        response.json::<ApiResult<Account>>()?.into()
    }

    /// Use this method to get a Telegraph page. Returns a Page object on success.
    pub fn get_page(path: &str, return_content: bool) -> Result<Page> {
        let mut response = Client::new()
            .get(&format!("https://api.telegra.ph/getPage/{}", path))
            .query(&[("return_content", return_content.to_string())])
            .send()?;
        response.json::<ApiResult<Page>>()?.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 fn get_page_list(&self, offset: i32, limit: i32) -> Result<PageList> {
        let mut response = self
            .client
            .get("https://api.telegra.ph/getPageList")
            .query(&[
                ("access_token", &self.access_token),
                ("offset", &offset.to_string()),
                ("limit", &limit.to_string()),
            ])
            .send()?;
        response.json::<ApiResult<PageList>>()?.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
    /// use telegraph_rs::Telegraph;
    ///
    /// let view1 = Telegraph::get_views("Sample-Page-12-15", &vec![2016, 12]);
    /// let view2 = Telegraph::get_views("Sample-Page-12-15", &vec![2019, 5, 19, 12]); // year-month-day-hour
    /// ```
    pub fn get_views(path: &str, time: &[i32]) -> Result<PageViews> {
        let params = ["year", "month", "day", "hour"].iter().zip(time)
            .collect::<HashMap<_, _>>();

        let mut response = Client::new()
            .get(&format!("https://api.telegra.ph/getViews/{}", path))
            .query(&params)
            .send()?;
        response.json::<ApiResult<PageViews>>()?.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 fn revoke_access_token(&mut self) -> Result<Account> {
        let mut response = self
            .client
            .get("https://api.telegra.ph/revokeAccessToken")
            .query(&[("access_token", &self.access_token)])
            .send()?;
        let json: Result<Account> = response.json::<ApiResult<Account>>()?.into();
        if json.is_ok() {
            self.access_token = json
                .as_ref()
                .unwrap()
                .access_token
                .as_ref()
                .unwrap()
                .to_owned();
        }
        json
    }

    /// Upload files to telegraph
    pub fn upload<P: AsRef<Path>>(files: &[P]) -> Result<Vec<UploadResult>> {
        let mut form = Form::new();
        for (idx, file) in files.into_iter().enumerate() {
            let part = Part::file(file)?;
            form = form.part(idx.to_string(), part);
        }
        let mut response = Client::new()
            .post("https://telegra.ph/upload")
            .multipart(form)
            .send()?;

        Ok(response.json::<Vec<UploadResult>>()?)
    }
}

#[cfg(test)]
mod tests {
    use crate::Telegraph;

    #[test]
    fn create_and_revoke_account() {
        let result = Telegraph::create_account("sample", Some("a"), None);
        println!("{:?}", result);
        assert!(result.is_ok());

        let mut telegraph = Telegraph::new("test")
            .access_token(&result.unwrap().access_token.unwrap().to_owned())
            .create()
            .unwrap();
        let result = telegraph.revoke_access_token();
        println!("{:?}", result);
        assert!(result.is_ok());
    }

    #[test]
    fn edit_account_info() {
        let result = Telegraph::new("test")
            .access_token("b968da509bb76866c35425099bc0989a5ec3b32997d55286c657e6994bbb")
            .create()
            .unwrap()
            .edit_account_info()
            .short_name("wow")
            .edit();
        println!("{:?}", result);
        assert!(result.is_ok());
    }

    #[test]
    fn get_account_info() {
        let result = Telegraph::new("test")
            .access_token("b968da509bb76866c35425099bc0989a5ec3b32997d55286c657e6994bbb")
            .create()
            .unwrap()
            .get_account_info(&["short_name"]);
        println!("{:?}", result);
        assert!(result.is_ok());
    }

    #[test]
    fn create_get_edit_page() {
        let telegraph = Telegraph::new("test")
            .access_token("b968da509bb76866c35425099bc0989a5ec3b32997d55286c657e6994bbb")
            .create()
            .unwrap();
        let page = telegraph.create_page(
            "OVO",
            r#"[{"tag":"p","children":["Hello,+world!"]}]"#,
            false,
        );
        println!("{:?}", page);
        assert!(page.is_ok());

        let page = Telegraph::get_page(&page.unwrap().path, true);
        println!("{:?}", page);
        assert!(page.is_ok());

        let page = telegraph.edit_page(
            &page.unwrap().path,
            "QAQ",
            r#"[{"tag":"p","children":["Goodbye,+world!"]}]"#,
            false,
        );
        println!("{:?}", page);
        assert!(page.is_ok());
    }

    #[test]
    fn get_page_list() {
        let telegraph = Telegraph::new("test")
            .access_token("b968da509bb76866c35425099bc0989a5ec3b32997d55286c657e6994bbb")
            .create()
            .unwrap();
        let page_list = telegraph.get_page_list(0, 3);
        println!("{:?}", page_list);
        assert!(page_list.is_ok());
    }

    #[test]
    fn get_views() {
        let views = Telegraph::get_views("Sample-Page-12-15", &vec![2016, 12]);
        println!("{:?}", views);
        assert!(views.is_ok());
    }

    #[ignore]
    #[test]
    fn upload() {
        let images = Telegraph::upload(&vec!["1.jpeg", "2.jpeg"]);
        println!("{:?}", images);
    }
}