omglol 0.0.1

A wraper for api.omg.lol for your Rust masterpieces.
Documentation
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
// SPDX-LICENSE-IDENTIFIER: MPL-2.0

/*
Client - omglol crate for Rust

Copyright © 2023 Gil Poiares-Oliveira <gil@poiares-oliveira.com>.
All rights reserved.

This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
 If a copy of the MPL was not distributed with this file,
 You can obtain one at https://mozilla.org/MPL/2.0/.
*/

use std::marker::PhantomData;

use crate::email::format_addresses_string;
use crate::structures::*;
use email_address::EmailAddress;
use reqwest::{Client, Method};
use serde::de::DeserializeOwned;
use serde_json;

macro_rules! api_endpoint (
    ($path: expr) => (
        format!("{}{}", "https://api.omg.lol/", $path)
    );
);

pub(crate) use api_endpoint;

pub struct Auth;
pub struct NoAuth;

/// Client for api.omg.lol
#[derive(Clone)]
pub struct OmglolClient<State = NoAuth> {
    client: Client,
    api_key: Option<String>,
    state: PhantomData<State>,
}

impl OmglolClient<Auth> {
    pub async fn get_dns_records(
        &self,
        address: &str,
    ) -> Result<RequestResponse<DNSrecords>, Box<dyn std::error::Error>> {
        self.send_request::<DNSrecords>(
            true,
            Method::GET,
            format!("address/{}/dns", &address).as_ref(),
            None,
        )
        .await
    }

    pub async fn delete_dns_record(
        &self,
        address: &str,
        id: &str,
    ) -> Result<RequestResponse<DNSrecords>, Box<dyn std::error::Error>> {
        self.send_request::<DNSrecords>(
            true,
            Method::DELETE,
            format!("address/{address}/dns/{id}").as_ref(),
            None,
        )
        .await
    }

    pub async fn get_status(
        &self,
        address: &str,
        id: &str,
    ) -> Result<RequestResponse<StatuslogResponseArray>, Box<dyn std::error::Error>> {
        self.send_request::<StatuslogResponseArray>(
            true,
            Method::GET,
            format!("address/{}/statuses/{}", &address, &id).as_ref(),
            None,
        )
        .await
    }

    pub async fn post_status(
        &self,
        status: &Status,
    ) -> Result<RequestResponse<StatuslogResponseArray>, Box<dyn std::error::Error>> {
        self.send_request::<StatuslogResponseArray>(
            true,
            Method::POST,
            format!("address/{}/statuses", &status.address).as_ref(),
            Some(serde_json::to_string(&status)?),
        )
        .await
    }

    pub async fn update_status(
        &self,
        status: &Status,
    ) -> Result<RequestResponse<StatuslogUpdateResponse>, Box<dyn std::error::Error>> {
        self.send_request::<StatuslogUpdateResponse>(
            true,
            Method::POST,
            format!("address/{}/status", &status.address).as_ref(),
            Some(serde_json::to_string(&status)?),
        )
        .await
    }

    pub async fn update_statuslog_bio<T: ContentAsJSON>(
        &self,
        bio: T,
        address: &str,
    ) -> Result<RequestResponse<StatuslogBio>, Box<dyn std::error::Error>> {
        self.send_request::<StatuslogBio>(
            true,
            Method::POST,
            format!("address/{}/statuses/bio", &address).as_ref(),
            Some(bio.json_content()),
        )
        .await
    }

    // Email

    pub async fn get_forwarding_addresses(
        &self,
        address: &str,
    ) -> Result<RequestResponse<ForwardingAddresses>, Box<dyn std::error::Error>> {
        self.send_request::<ForwardingAddresses>(
            true,
            Method::GET,
            format!("address/{}/email", &address).as_ref(),
            None,
        )
        .await
    }

    pub async fn set_forwarding_addresses(
        &self,
        address: &str,
        destination: &Vec<EmailAddress>,
    ) -> Result<RequestResponse<ForwardingAddresses>, Box<dyn std::error::Error>> {
        self.send_request::<ForwardingAddresses>(
            true,
            Method::GET,
            format!("address/{}/email", &address).as_ref(),
            Some(format_addresses_string(destination)),
        )
        .await
    }

    // Pastebin

    pub async fn get_pastebin(
        &self,
        address: &str,
    ) -> Result<RequestResponse<PastebinResponse>, Box<dyn std::error::Error>> {
        self.send_request::<PastebinResponse>(
            true,
            Method::GET,
            format!("address/{}/pastebin", &address).as_ref(),
            None,
        )
        .await
    }

    pub async fn create_weblog_entry(
        &self,
        content: &str,
        entry_id: &str,
        address: &str,
    ) -> Result<RequestResponse<WeblogEntryResponse>, Box<dyn std::error::Error>> {
        self.send_request::<WeblogEntryResponse>(
            true,
            Method::POST,
            format!("address/{}/weblog/entry/{}", address, entry_id).as_ref(),
            Some(content.to_string()),
        )
        .await
    }

    pub async fn update_weblog_configuration(
        &self,
        configuration: &str,
        address: &str,
    ) -> Result<RequestResponse<WeblogEntryResponse>, Box<dyn std::error::Error>> {
        self.send_request::<WeblogEntryResponse>(
            true,
            Method::POST,
            format!("address/{}/weblog/template", address).as_ref(),
            Some(configuration.to_string()),
        )
        .await
    }
    pub async fn get_purl(
        &self,
        address: &str,
        purl_address: &str,
    ) -> Result<RequestResponse<PurlResponse>, Box<dyn std::error::Error>> {
        self.send_request::<PurlResponse>(
            true,
            Method::GET,
            format!("address/{}/purl/{purl_address}", &address).as_ref(),
            None,
        )
        .await
    }

    pub async fn get_all_purls(
        &self,
        address: &str,
    ) -> Result<RequestResponse<PurlsResponse>, Box<dyn std::error::Error>> {
        self.send_request::<PurlsResponse>(
            true,
            Method::GET,
            format!("address/{}/purls", &address).as_ref(),
            None,
        )
        .await
    }

    pub async fn delete_purl(
        &self,
        address: &str,
        purl_address: &str,
    ) -> Result<RequestResponse<MessageResponse>, Box<dyn std::error::Error>> {
        self.send_request::<MessageResponse>(
            true,
            Method::DELETE,
            format!("address/{}/purl/{purl_address}", &address).as_ref(),
            None,
        )
        .await
    }

    pub async fn get_account_info(
        &self,
        email: &EmailAddress,
    ) -> Result<RequestResponse<AccountResponse>, Box<dyn std::error::Error>> {
        self.send_request::<AccountResponse>(
            true,
            Method::GET,
            format!("account/{}/info", email).as_ref(),
            None,
        )
        .await
    }

    pub async fn get_private_address_info(
        &self,
        address: &str,
    ) -> Result<RequestResponse<Address>, Box<dyn std::error::Error>> {
        self.send_request::<Address>(
            true,
            Method::GET,
            format!("account/{}/info", address).as_ref(),
            None,
        )
        .await
    }

    pub async fn get_address_expiration(
        &self,
        address: &str,
    ) -> Result<RequestResponse<Expiration>, Box<dyn std::error::Error>> {
        self.send_request::<Expiration>(
            true,
            Method::GET,
            format!("account/{}/expiration", address).as_ref(),
            None,
        )
        .await
    }

    pub async fn get_web_page(
        &self,
        address: &str,
    ) -> Result<RequestResponse<Web>, Box<dyn std::error::Error>> {
        self.send_request::<Web>(
            true,
            Method::GET,
            format!("address/{}/web", address).as_ref(),
            None,
        )
        .await
    }

    pub async fn update_web_page(
        &self,
        web: &Web,
        address: &str,
    ) -> Result<RequestResponse<MessageResponse>, Box<dyn std::error::Error>> {
        self.send_request::<MessageResponse>(
            true,
            Method::POST,
            format!("address/{}/web", address).as_ref(),
            Some(serde_json::to_string(&web)?),
        )
        .await
    }

    pub async fn get_weblog_entry(
        &self,
        entry_id: &str,
        address: &str,
    ) -> Result<RequestResponse<WeblogEntryResponse>, Box<dyn std::error::Error>> {
        self.send_request::<WeblogEntryResponse>(
            true,
            Method::GET,
            format!("address/{}/weblog/entry/{}", address, entry_id).as_ref(),
            None,
        )
        .await
    }

    pub async fn delete_weblog_entry(
        &self,
        entry_id: &str,
        address: &str,
    ) -> Result<RequestResponse<MessageResponse>, Box<dyn std::error::Error>> {
        self.send_request::<MessageResponse>(
            true,
            Method::DELETE,
            format!("address/{}/weblog/delete/{}", address, entry_id).as_ref(),
            None,
        )
        .await
    }

    pub async fn get_weblog_configuration(
        &self,
        address: &str,
    ) -> Result<RequestResponse<WeblogConfigurationResponse>, Box<dyn std::error::Error>> {
        self.send_request::<WeblogConfigurationResponse>(
            true,
            Method::GET,
            format!("address/{}/weblog/configuration", address).as_ref(),
            None,
        )
        .await
    }

    pub async fn get_weblog_template(
        &self,
        address: &str,
    ) -> Result<RequestResponse<WeblogTemplateResponse>, Box<dyn std::error::Error>> {
        self.send_request::<WeblogTemplateResponse>(
            true,
            Method::GET,
            format!("address/{}/weblog/template", address).as_ref(),
            None,
        )
        .await
    }

    pub async fn update_weblog_template(
        &self,
        template: &str,
        address: &str,
    ) -> Result<RequestResponse<WeblogTemplateResponse>, Box<dyn std::error::Error>> {
        self.send_request::<WeblogTemplateResponse>(
            true,
            Method::POST,
            format!("address/{}/weblog/template", address).as_ref(),
            Some(template.to_string()),
        )
        .await
    }

    pub async fn delete_paste(
        &self,
        address: &str,
        title: &str,
    ) -> Result<RequestResponse<MessageResponse>, Box<dyn std::error::Error>> {
        self.send_request::<MessageResponse>(
            false,
            Method::DELETE,
            format!("address/{}/pastebin/{title}", &address).as_ref(),
            None,
        )
        .await
    }
}

impl OmglolClient<NoAuth> {
    /// Create an authenticated `OmglolClient`.
    ///
    /// This client is able to access private endpoints.
    ///
    /// Example:
    /// ```rust
    /// let client = OmglolClient::new();
    /// let client = client.auth("YOUR_API_KEY".to_string());
    /// ```
    pub fn auth(&self, api_key: String) -> OmglolClient<Auth> {
        OmglolClient {
            client: self.client.to_owned(),
            api_key: Some(api_key),
            state: PhantomData,
        }
    }
}

impl OmglolClient {
    /// Create a new `OmglolClient`.
    ///
    /// The client is created in unauthenticated form, i.e. restricted to
    /// methods that rely on public endpoints only.
    ///
    /// Usage:
    /// ```rust
    /// let client = OmglolClient::new();
    /// ```
    pub fn new() -> OmglolClient<NoAuth> {
        OmglolClient {
            client: Client::new(),
            api_key: None,
            state: PhantomData,
        }
    }

    pub async fn get_profile_themes(
        &self,
    ) -> Result<RequestResponse<ProfileThemes>, Box<dyn std::error::Error>> {
        self.send_request::<ProfileThemes>(false, Method::GET, "theme/list", None)
            .await
    }

    pub async fn service_status(
        &self,
    ) -> Result<RequestResponse<ServiceStatus>, Box<dyn std::error::Error>> {
        self.send_request::<ServiceStatus>(false, Method::GET, "service/info", None)
            .await
    }

    pub async fn get_statuslog_bio(
        &self,
        address: &str,
    ) -> Result<RequestResponse<StatuslogBio>, Box<dyn std::error::Error>> {
        self.send_request::<StatuslogBio>(
            false,
            Method::GET,
            format!("address/{}/statuses/bio", &address).as_ref(),
            None,
        )
        .await
    }

    pub async fn get_listed_pastes(
        &self,
        address: &str,
    ) -> Result<RequestResponse<PastebinResponse>, Box<dyn std::error::Error>> {
        self.send_request::<PastebinResponse>(
            false,
            Method::GET,
            format!("address/{}/pastebin", &address).as_ref(),
            None,
        )
        .await
    }

    pub async fn get_paste(
        &self,
        address: &str,
        title: &str,
    ) -> Result<RequestResponse<PasteResponse>, Box<dyn std::error::Error>> {
        self.send_request::<PasteResponse>(
            false,
            Method::GET,
            format!("address/{}/pastebin/{title}", &address).as_ref(),
            None,
        )
        .await
    }

    pub async fn upload_paste(
        &self,
        address: &str,
        paste: Paste,
    ) -> Result<RequestResponse<PasteResponse>, Box<dyn std::error::Error>> {
        self.send_request::<PasteResponse>(
            false,
            Method::POST,
            format!("address/{}/pastebin", &address).as_ref(),
            Some(serde_json::to_string(&paste)?),
        )
        .await
    }

    pub async fn get_public_address_info(
        &self,
        address: &str,
    ) -> Result<RequestResponse<Address>, Box<dyn std::error::Error>> {
        self.send_request::<Address>(
            false,
            Method::GET,
            format!("account/{}/info", address).as_ref(),
            None,
        )
        .await
    }

    pub async fn get_latest_weblog_post(
        &self,
        address: &str,
    ) -> Result<RequestResponse<WeblogEntryResponse>, Box<dyn std::error::Error>> {
        self.send_request::<WeblogEntryResponse>(
            false,
            Method::GET,
            format!("address/{}/weblog/post/latest", address).as_ref(),
            None,
        )
        .await
    }

    pub async fn get_all_statuses(
        &self,
        address: &str,
    ) -> Result<RequestResponse<StatuslogAllStatuses>, Box<dyn std::error::Error>> {
        self.send_request::<StatuslogAllStatuses>(
            false,
            Method::GET,
            format!("address/{}/statuses", &address).as_ref(),
            None,
        )
        .await
    }
}

/// OmglolClient allows you to make authenticated or unauthenticated REST API
/// requests.
impl<State> OmglolClient<State> {
    async fn send_request<T>(
        &self,
        authenticate: bool,
        method: Method,
        uri: &str,
        body: Option<String>,
    ) -> Result<RequestResponse<T>, Box<dyn std::error::Error>>
    where
        T: DeserializeOwned,
    {
        let reqwest_client = &self.client;
        let mut req = reqwest_client.request(method, api_endpoint!(uri));

        if authenticate {
            req = req.bearer_auth(&self.api_key.as_ref().unwrap().to_string());
        }

        if body.is_some() {
            req = req.body(body.unwrap());
        }

        #[cfg(debug_assertions)]
        dbg!(&req);

        let resp = req.send().await?;

        let raw_res = match &resp.status().as_u16() {
            _status_code @ 200 => resp.text().await?,
            status_code => {
                return Err(Box::new(RequestError {
                    status_code: *status_code,
                }))
            }
        };

        #[cfg(debug_assertions)]
        dbg!(&raw_res);

        let res: RequestResponse<T> = serde_json::from_str(&raw_res).unwrap();

        Ok(res)
    }
}