neocities_client/
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
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
////////       This file is part of the source code for neocities-client, a Rust           ////////
////////       library for interacting with the https://neocities.org/ API.                ////////
////////                                                                                   ////////
////////                           Copyright © 2024  André Kugland                         ////////
////////                                                                                   ////////
////////       This program is free software: you can redistribute it and/or modify        ////////
////////       it under the terms of the GNU General Public License as published by        ////////
////////       the Free Software Foundation, either version 3 of the License, or           ////////
////////       (at your option) any later version.                                         ////////
////////                                                                                   ////////
////////       This program is distributed in the hope that it will be useful,             ////////
////////       but WITHOUT ANY WARRANTY; without even the implied warranty of              ////////
////////       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the                ////////
////////       GNU General Public License for more details.                                ////////
////////                                                                                   ////////
////////       You should have received a copy of the GNU General Public License           ////////
////////       along with this program. If not, see https://www.gnu.org/licenses/.         ////////

//! Client for the Neocities API.

use crate::response::{parse_response, Info, ListEntry};
use crate::{Auth, Error, Result};
use derive_builder::Builder;
use form_data_builder::FormData;
use std::{ffi::OsStr, io::Cursor};
use tap::prelude::*;
use typed_path::Utf8UnixPath;
use ureq::{Agent, OrAnyStatus, Request};

/// Default base URL for the Neocities API.
const DEFAULT_BASE_URL: &str = "https://neocities.org/api";

/// Default user agent to use for the requests.
const DEFAULT_USER_AGENT: &str = concat!("neocities_client/", env!("CARGO_PKG_VERSION"));

/// List of file extensions allowed for free accounts.
const ALLOWED_EXTS_FOR_FREE_ACCOUNTS: &[&str] = &[
    "apng",
    "asc",
    "atom",
    "avif",
    "bin",
    "css",
    "csv",
    "dae",
    "eot",
    "epub",
    "geojson",
    "gif",
    "gltf",
    "gpg",
    "htm",
    "html",
    "ico",
    "jpeg",
    "jpg",
    "js",
    "json",
    "key",
    "kml",
    "knowl",
    "less",
    "manifest",
    "map",
    "markdown",
    "md",
    "mf",
    "mid",
    "midi",
    "mtl",
    "obj",
    "opml",
    "osdx",
    "otf",
    "pdf",
    "pgp",
    "pls",
    "png",
    "rdf",
    "resolveHandle",
    "rss",
    "sass",
    "scss",
    "svg",
    "text",
    "toml",
    "tsv",
    "ttf",
    "txt",
    "webapp",
    "webmanifest",
    "webp",
    "woff",
    "woff2",
    "xcf",
    "xml",
    "yaml",
    "yml",
];

/// Client for the Neocities API.
///
/// This struct is used to make requests to the Neocities API. It can be built using the
/// [`Client::builder()`](#method.builder) method, which returns a
/// [`ClientBuilder`](struct.ClientBuilder.html) struct.
///
/// ```
/// # use neocities_client::{Auth, Client};
/// let client = Client::builder()
///    .auth(Auth::from("username:password"))
///    .build()
///    .unwrap();
/// ```
#[derive(Debug, Builder)]
pub struct Client {
    /// Instance of [`ureq::Agent`] to use for the requests.
    ///
    /// Override this if you want to customize the [`Agent`](ureq::Agent), for example, to use a
    /// proxy, to set a timeout, to add middlewares, *&c*.
    #[builder(default = "ureq::builder().build()")]
    ureq_agent: Agent,
    /// Base URL for the Neocities API.
    ///
    /// Defaults to `https://neocities.org/api`.
    ///
    /// This is overridable for testing purposes.
    #[builder(default = "DEFAULT_BASE_URL.to_owned()")]
    base_url: String,
    /// User agent to use for the requests
    ///
    /// Defaults to `neocities_client/x.y.z`
    #[builder(default = "DEFAULT_USER_AGENT.to_owned()")]
    user_agent: String,
    /// Authorization that will be used for the requests.
    auth: Auth,
}

/// Client for the Neocities API.
#[allow(clippy::result_large_err)]
impl Client {
    /// Return a new [`ClientBuilder`] struct.
    pub fn builder() -> ClientBuilder {
        ClientBuilder::default()
    }

    /// Delete one or more files from the website.
    pub fn delete(&self, paths: &[&str]) -> Result<()> {
        #[cfg(debug_assertions)]
        log::trace!("Deleting files {:?}", paths);
        let form = paths
            .iter()
            .map(|path| ("filenames[]", *path))
            .collect::<Vec<_>>();
        self.make_request("POST", "delete")
            .send_form(&form)
            .or_any_status()
            .map_err(Error::from)
            .and_then(|res| parse_response::<String>("message", res))
            .tap_ok_dbg(|msg| log::trace!("{}", msg))
            .tap_err(|e| log::debug!("{}", e))
            .and(Ok(()))
    }

    /// Get the website info.
    pub fn info(&self) -> Result<Info> {
        #[cfg(debug_assertions)]
        log::trace!("Getting website info");
        self.make_request("GET", "info")
            .call()
            .or_any_status()
            .map_err(Error::from)
            .and_then(|res| parse_response::<Info>("info", res))
            .tap_ok_dbg(|info| log::trace!("{:?}", info))
            .tap_err(|e| log::debug!("{}", e))
    }

    /// Get an API key for the website.
    pub fn key(&self) -> Result<String> {
        #[cfg(debug_assertions)]
        log::trace!("Getting API key");
        self.make_request("GET", "key")
            .call()
            .or_any_status()
            .map_err(Error::from)
            .and_then(|res| parse_response::<String>("api_key", res))
            .tap_ok_dbg(|_| log::trace!("Got an API key: <redacted>"))
            .tap_err(|e| log::debug!("{}", e))
    }

    /// List the files on the website.
    pub fn list(&self) -> Result<Vec<ListEntry>> {
        #[cfg(debug_assertions)]
        log::trace!("Listing files");
        self.make_request("GET", "list")
            .call()
            .or_any_status()
            .map_err(Error::from)
            .and_then(|res| parse_response::<Vec<ListEntry>>("files", res))
            .tap_ok_dbg(|list| log::trace!("{:?}", list))
            .tap_err(|e| log::debug!("{}", e))
    }

    /// Upload one or more files to the website.
    ///
    /// This method receives a list of tuples, each containing the path of the file and the
    /// contents of the file.
    ///
    /// ```no_run
    /// # use neocities_client::{Auth, Client, Result};
    /// # fn main() -> Result<()> {
    /// # let client = Client::builder().auth(Auth::from("faketoken")).build().unwrap();
    /// client.upload(&[
    ///     ("/1st_file.txt", b"Contents of the first file"),
    ///     ("/2nd_file.txt", b"Contents of the second file"),
    /// ])?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn upload(&self, files: &[(&str, &[u8])]) -> Result<()> {
        #[cfg(debug_assertions)]
        log::trace!(
            "Uploading files {:?}",
            files.iter().map(|(name, _)| name).collect::<Vec<_>>()
        );
        let mut form = FormData::new(Vec::new());
        for (name, content) in files {
            form.write_file(
                name,
                Cursor::new(content),
                Some(OsStr::new("file")),
                "application/octet-stream",
            )
            .tap_err(|e| log::debug!("{}", e))
            // The only occasion where in-memory fake I/O can fail is when we run out of memory,
            // and in that case, we're screwed anyway. Having this possible panic here allows us to
            // avoid having a variant for [`std::io::Error`] in our [`Error`] enum.
            .expect("Failed to write file contents to form data");
        }
        let post_body = form
            .finish()
            .tap_err(|e| log::debug!("{}", e))
            .expect("Failed to finish form data"); // Same as above.
        let content_type = form.content_type_header();
        self.make_request("POST", "upload")
            .set("Content-Type", &content_type)
            .send_bytes(&post_body)
            .or_any_status()
            .map_err(Error::from)
            .and_then(|res| parse_response::<String>("message", res))
            .tap_ok_dbg(|list| log::trace!("{:?}", list))
            .tap_err(|e| log::debug!("{}", e))
            .and(Ok(()))
    }

    /// Check whether the given path has an allowed extension for this account.
    ///
    /// If the [`free_account`](ClientBuilder::free_account) field is set to `true`, this method
    /// will check that the file extension of the given path is in the list of allowed extensions.
    /// If the field is set to `false`, this method will always return `true`.
    ///
    /// For more information, see <https://neocities.org/site_files/allowed_types>.
    ///
    /// ```
    /// # use neocities_client::{Auth, Client};
    /// assert!(Client::has_allowed_extension(true, "hello.txt"));
    /// assert!(!Client::has_allowed_extension(true, "hello.exe"));
    /// ```
    pub fn has_allowed_extension(free_account: bool, path: &str) -> bool {
        if !free_account {
            true
        } else {
            let unix_path = Utf8UnixPath::new(path);
            let ext = unix_path
                .extension()
                .unwrap_or_default()
                .to_ascii_lowercase();
            ALLOWED_EXTS_FOR_FREE_ACCOUNTS.contains(&ext.as_str())
        }
    }

    // ------------------------------------ Private methods ------------------------------------ //

    /// Build a new request with the given method and path.
    ///
    /// This method will set the appropriate headers, including the `Authorization` header if
    /// the the `auth` field is set.
    fn make_request(&self, method: &str, path: &str) -> Request {
        let path = format!("{}/{}", self.base_url, path);
        self.ureq_agent
            .request(method, &path)
            .set("User-Agent", &self.user_agent)
            .set("Accept", "application/json")
            .set("Accept-Charset", "utf-8")
            .set("Authorization", &self.auth.header())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ErrorKind;
    use indoc::indoc;
    use mockito::{Matcher, Server};

    #[test]
    fn delete_ok() {
        let mut server = Server::new();
        let mock = server
            .mock("POST", "/delete")
            .match_header("Accept", "application/json")
            .match_header("Accept-Charset", "utf-8")
            .match_header("Authorization", "Basic dXNlcm5hbWU6cGFzc3dvcmQ=")
            .match_body(Matcher::UrlEncoded(
                "filenames[]".to_owned(),
                "hello.txt".to_owned(),
            ))
            .with_status(200)
            .with_header("Content-Type", "application/json")
            .with_body(r#"{ "result": "success", "message": "file(s) have been deleted" }"#)
            .create();
        let client = Client::builder()
            .base_url(server.url())
            .auth(Auth::from("username:password"))
            .build()
            .unwrap();
        client.delete(&["hello.txt"]).unwrap();
        mock.assert();
    }

    #[test]
    fn delete_err() {
        let mut server = Server::new();
        let mock = server
            .mock("POST", "/delete")
            .match_header("Accept", "application/json")
            .match_header("Accept-Charset", "utf-8")
            .match_header("Authorization", "Basic dXNlcm5hbWU6cGFzc3dvcmQ=")
            .match_body(Matcher::UrlEncoded(
                "filenames[]".to_owned(),
                "hello.txt".to_owned(),
            ))
            .with_status(200)
            .with_header("Content-Type", "application/json")
            .with_body(
                r#"{
                    "result": "error",
                    "error_type": "missing_files",
                    "message": "img1.jpg was not found on your site, canceled deleting"
                }"#,
            )
            .create();
        let client = Client::builder()
            .base_url(server.url())
            .auth(Auth::from("username:password"))
            .build()
            .unwrap();
        let err = client.delete(&["hello.txt"]).unwrap_err();
        mock.assert();
        assert!(matches!(
            err,
            Error::Api {
                kind: ErrorKind::MissingFiles,
                ..
            }
        ));
    }

    #[test]
    fn info() {
        let mut server = Server::new();
        let mock = server
            .mock("GET", "/info")
            .match_header("Accept", "application/json")
            .match_header("Accept-Charset", "utf-8")
            .match_header("Authorization", "Basic dXNlcm5hbWU6cGFzc3dvcmQ=")
            .with_status(200)
            .with_header("Content-Type", "application/json")
            .with_body(
                r#"{
                    "result": "success",
                    "info": {
                        "sitename": "youpi",
                        "views": 235684,
                        "hits": 1487423,
                        "created_at": "Sat, 29 Jun 2013 10:11:38 -0000",
                        "last_updated": "Fri, 01 Dec 2017 18:47:51 -0000",
                        "domain": null,
                        "tags": ["anime", "music", "videogames", "personal", "art"],
                        "latest_ipfs_hash": null
                    }
                }"#,
            )
            .create();
        let client = Client::builder()
            .base_url(server.url())
            .auth(Auth::from("username:password"))
            .build()
            .unwrap();
        let info = client.info().unwrap();
        mock.assert();
        assert_eq!(info.sitename, "youpi");
        assert_eq!(info.views, 235684);
        assert_eq!(info.hits, 1487423);
        assert_eq!(info.created_at, "Sat, 29 Jun 2013 10:11:38 -0000");
        assert_eq!(
            info.last_updated.unwrap(),
            "Fri, 01 Dec 2017 18:47:51 -0000"
        );
        assert_eq!(info.domain, None);
        assert_eq!(
            info.tags,
            vec!["anime", "music", "videogames", "personal", "art"]
        );
        assert_eq!(info.latest_ipfs_hash, None);
    }

    #[test]
    fn key_ok() {
        let mut server = Server::new();
        let mock = server
            .mock("GET", "/key")
            .match_header("Accept", "application/json")
            .match_header("Accept-Charset", "utf-8")
            .match_header("Authorization", "Basic dXNlcm5hbWU6cGFzc3dvcmQ=")
            .with_status(200)
            .with_header("Content-Type", "application/json")
            .with_body(r#"{ "result": "success", "api_key": "c6275ca833ac06c83926ccb00dff4c82" }"#)
            .create();
        let client = Client::builder()
            .base_url(server.url())
            .auth(Auth::from("username:password"))
            .build()
            .unwrap();
        let key = client.key().unwrap();
        mock.assert();
        assert_eq!(key, "c6275ca833ac06c83926ccb00dff4c82");
    }

    #[test]
    fn key_err() {
        let mut server = Server::new();
        let mock = server
            .mock("GET", "/key")
            .match_header("Accept", "application/json")
            .match_header("Accept-Charset", "utf-8")
            .match_header("Authorization", "Basic dXNlcm5hbWU6cGFzc3dvcmQ=")
            .with_status(200)
            .with_header("Content-Type", "application/json")
            .with_body(r#"{
                "result": "error",
                "error_type": "invalid_auth",
                "message": "invalid credentials - please check your username and password (or your api key)"
            }"#)
            .create();
        let client = Client::builder()
            .base_url(server.url())
            .auth(Auth::from("username:password"))
            .build()
            .unwrap();
        let key = client.key().unwrap_err();
        mock.assert();
        assert!(matches!(
            key,
            Error::Api {
                kind: ErrorKind::InvalidAuth,
                ..
            }
        ));
    }

    #[test]
    fn list() {
        let mut server = Server::new();
        let mock = server
            .mock("GET", "/list")
            .match_header("Accept", "application/json")
            .match_header("Accept-Charset", "utf-8")
            .match_header("Authorization", "Basic dXNlcm5hbWU6cGFzc3dvcmQ=")
            .with_status(200)
            .with_header("Content-Type", "application/json")
            .with_body(
                r#"{
                    "result": "success",
                    "files": [{
                        "path": "index.html",
                        "is_directory": false,
                        "size": 1023,
                        "updated_at": "Sat, 13 Feb 2016 03:04:00 -0000",
                        "sha1_hash": "c8aac06f343c962a24a7eb111aad739ff48b7fb1"
                    }, {
                        "path": "not_found.html",
                        "is_directory": false,
                        "size": 271,
                        "updated_at": "Sat, 13 Feb 2016 03:04:00 -0000",
                        "sha1_hash": "cfdf0bda2557c322be78302da23c32fec72ffc0b"
                    }, {
                        "path": "images",
                        "is_directory": true,
                        "updated_at": "Sat, 13 Feb 2016 03:04:00 -0000"
                    }, {
                        "path": "images/cat.png",
                        "is_directory": false,
                        "size": 16793,
                        "updated_at": "Sat, 13 Feb 2016 03:04:00 -0000",
                        "sha1_hash": "41fe08fc0dd44e79f799d03ece903e62be25dc7d"
                    }]
                }"#,
            )
            .create();
        let client = Client::builder()
            .base_url(server.url())
            .auth(Auth::from("username:password"))
            .build()
            .unwrap();
        let list = client.list().unwrap();
        mock.assert();
        assert_eq!(list.len(), 4);
        assert_eq!(list[0].path, "index.html");
        assert!(!list[0].is_directory);
        assert_eq!(list[0].size, Some(1023));
        assert_eq!(list[0].updated_at, "Sat, 13 Feb 2016 03:04:00 -0000");
        assert_eq!(
            list[0].sha1_hash.clone().unwrap(),
            "c8aac06f343c962a24a7eb111aad739ff48b7fb1"
        );
        assert_eq!(list[1].path, "not_found.html");
        assert!(!list[1].is_directory);
        assert_eq!(list[1].size, Some(271));
        assert_eq!(list[1].updated_at, "Sat, 13 Feb 2016 03:04:00 -0000");
        assert_eq!(
            list[1].sha1_hash.clone().unwrap(),
            "cfdf0bda2557c322be78302da23c32fec72ffc0b"
        );
        assert_eq!(list[2].path, "images");
        assert!(list[2].is_directory);
        assert_eq!(list[2].size, None);
        assert_eq!(list[2].updated_at, "Sat, 13 Feb 2016 03:04:00 -0000");
        assert_eq!(list[2].sha1_hash, None);
        assert_eq!(list[3].path, "images/cat.png");
        assert!(!list[3].is_directory);
        assert_eq!(list[3].size, Some(16793));
        assert_eq!(list[3].updated_at, "Sat, 13 Feb 2016 03:04:00 -0000");
        assert_eq!(
            list[3].sha1_hash.clone().unwrap(),
            "41fe08fc0dd44e79f799d03ece903e62be25dc7d"
        );
    }

    #[test]
    fn upload_ok() {
        let content_type =
            Matcher::Regex("multipart/form-data; boundary=--------+[-A-Za-z0-9_]{32}".to_owned());
        let body = Matcher::Regex(
            indoc! {"
                --------+[-A-Za-z0-9_]{32}\r\n\
                Content-Disposition: form-data; name=\"hello.txt\"; filename=\"file\"\r\n\
                Content-Type: application/octet-stream\r\n\
                \r\n\
                Hello, world!\n\r\n\
                --------+[-A-Za-z0-9_]{32}\r\n\
                Content-Disposition: form-data; name=\"hello1.txt\"; filename=\"file\"\r\n\
                Content-Type: application/octet-stream\r\n\
                \r\n\
                Hello, world!\n\r\n\
                --------+[-A-Za-z0-9_]{32}--\r\n\
            "}
            .to_owned(),
        );
        let mut server = Server::new();
        let mock = server
            .mock("POST", "/upload")
            .match_header("Accept", "application/json")
            .match_header("Accept-Charset", "utf-8")
            .match_header("Authorization", "Basic dXNlcm5hbWU6cGFzc3dvcmQ=")
            .match_header("Content-Type", content_type)
            .match_body(body)
            .with_status(200)
            .with_header("Content-Type", "application/json")
            .with_body(
                r#"{
                    "result": "success",
                    "message": "your file(s) have been successfully uploaded"
                }"#,
            )
            .create();
        let content = b"Hello, world!\n";
        let client = Client::builder()
            .base_url(server.url())
            .auth(Auth::from("username:password"))
            .build()
            .unwrap();
        client
            .upload(&[("hello.txt", content), ("hello1.txt", content)])
            .unwrap();
        mock.assert();
    }
}