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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
use std::{collections::HashMap, sync::Arc, time::Duration};
use reqwest::multipart;
use serde::Deserialize;
use tokio::sync::Mutex;
use url::Url;
use crate::{
auth::{AuthClient, TokenSet},
channel::DocumentChannel,
error::ScribeError,
model::{
AccountInfo, AppStoreProduct, AppStoreProductsResponse, BillingStatus, BrailleTable,
BrailleTablesResponse, CreatedDocument, Dialect, DialectsResponse, DocumentList,
DocumentListResponse, DocumentSummary, Download, Language, LanguagesResponse,
NotificationSettings, OutputFormat, OutputList, OutputListResponse, Settings,
SettingsUpdate, TrashedDocument, TrashedDocumentListResponse, Voice, VoicesResponse,
},
};
/// How early to proactively refresh a token before it actually expires.
/// 5 minutes gives enough headroom for slow mobile networks and cold starts.
const REFRESH_SKEW: Duration = Duration::from_secs(300);
/// What to create a document from.
pub enum DocumentSource {
/// Upload file bytes directly.
File { file_name: String, bytes: Vec<u8> },
/// Have the server fetch the document from a URL.
Url(String),
}
#[derive(Debug, Deserialize)]
struct ApiErrorResponse {
error: ApiErrorBody,
}
#[derive(Debug, Deserialize)]
struct ApiErrorBody {
code: String,
#[serde(default)]
message: Option<String>,
}
/// A client for the document-conversion endpoints
/// (`/api/documents*`). Holds a [`TokenSet`] and refreshes it
/// automatically as needed, so construct one [`AuthClient`]/[`ScribeClient`]
/// pair per authenticated user session and reuse it across requests.
/// Path of the web page where a user requests, or cancels, deletion of their own
/// account.
///
/// It is under `/settings`, not at a top-level `/delete_account`. Reach it through [`ScribeClient::delete_account_url`] so the
/// browser arrives already signed in. The same page starts a deletion and cancels
/// a pending one, so one path serves both.
pub const DELETE_ACCOUNT_PATH: &str = "/settings/delete_account";
pub struct ScribeClient {
http: reqwest::Client,
base_url: Url,
auth: AuthClient,
tokens: Arc<Mutex<TokenSet>>,
}
impl ScribeClient {
pub fn new(
http: reqwest::Client,
base_url: Url,
client_id: impl Into<String>,
tokens: TokenSet,
) -> Self {
let auth = AuthClient::new(http.clone(), base_url.clone(), client_id);
ScribeClient {
http,
base_url,
auth,
tokens: Arc::new(Mutex::new(tokens)),
}
}
/// The current access token, refreshing first if it's missing or about
/// to expire and a refresh token is available.
async fn access_token(&self) -> Result<String, ScribeError> {
let mut tokens = self.tokens.lock().await;
if tokens.needs_refresh(REFRESH_SKEW) {
if let Some(refresh_token) = tokens.refresh_token.clone() {
*tokens = self.auth.refresh(&refresh_token).await?;
}
}
Ok(tokens.access_token.clone())
}
/// Returns a snapshot of the current token set. Useful for persisting
/// the session after the client has auto-refreshed it.
pub async fn current_tokens(&self) -> TokenSet {
self.tokens.lock().await.clone()
}
/// Force-refreshes and replaces the current token set. Also used after
/// a request unexpectedly comes back `401` even though our local
/// expiry tracking thought the token was still good.
async fn force_refresh(&self) -> Result<String, ScribeError> {
let mut tokens = self.tokens.lock().await;
let refresh_token =
tokens
.refresh_token
.clone()
.ok_or_else(|| ScribeError::InvalidGrant {
message: "Your session has expired. Please sign in again.".to_string(),
})?;
*tokens = self.auth.refresh(&refresh_token).await?;
Ok(tokens.access_token.clone())
}
/// Creates a document from `source`.
///
/// # Errors
///
/// Returns [`ScribeError::Http`]/[`ScribeError::Api`] if the request
/// fails or the server rejects it.
/// `password` unlocks a password-protected source file. Supplying it up
/// front saves a round trip when the caller already knows the document
/// is protected; otherwise leave it `None` and handle the
/// `password_required` signal on the document channel.
pub async fn create_document(
&self,
source: DocumentSource,
password: Option<&str>,
) -> Result<CreatedDocument, ScribeError> {
let mut url = self.base_url.clone();
url.set_path("/api/documents");
self.with_auth_retry(|token| {
let form = match &source {
DocumentSource::File { file_name, bytes } => multipart::Form::new().part(
"document[file]",
multipart::Part::bytes(bytes.clone()).file_name(file_name.clone()),
),
DocumentSource::Url(source_url) => {
multipart::Form::new().text("document[url]", source_url.clone())
}
};
let form = match password {
Some(password) => form.text("document[password]", password.to_string()),
None => form,
};
self.http
.post(url.clone())
.bearer_auth(token)
.multipart(form)
})
.await
}
/// Lists every document owned by the current user, each with its
/// outputs embedded (so this alone is enough to show a document list
/// with per-document conversion status, no follow-up `list_outputs`
/// calls needed).
///
/// # Errors
///
/// Returns [`ScribeError::Http`]/[`ScribeError::Api`] on request failure.
pub async fn list_documents(&self) -> Result<DocumentList, ScribeError> {
let mut url = self.base_url.clone();
url.set_path("/api/documents");
let response: DocumentListResponse = self
.with_auth_retry(|token| self.http.get(url.clone()).bearer_auth(token))
.await?;
Ok(DocumentList {
documents: response.documents,
pages_remaining: response.pages_remaining,
suggest_buying_pages: response.suggest_buying_pages,
})
}
/// Moves a document to the trash. It's permanently deleted 7 days later,
/// or sooner per the owner's org retention policy, unless recovered
/// first with [`recover_document`](Self::recover_document).
///
/// # Errors
///
/// Returns [`ScribeError::NotFound`]/[`ScribeError::Forbidden`] if the
/// document doesn't exist or isn't owned by the caller, or
/// [`ScribeError::Http`]/[`ScribeError::Api`] on other request failures.
pub async fn trash_document(&self, document_id: &str) -> Result<(), ScribeError> {
let mut url = self.base_url.clone();
url.set_path(&format!("/api/documents/{document_id}/trash"));
self.with_auth_retry_raw(|token| self.http.post(url.clone()).bearer_auth(token))
.await?;
Ok(())
}
/// Permanently deletes a document and all of its outputs. The document
/// must already be in the trash (see
/// [`trash_document`](Self::trash_document)).
///
/// # Errors
///
/// Returns [`ScribeError::NotFound`]/[`ScribeError::Forbidden`] if the
/// document doesn't exist or isn't owned by the caller,
/// [`ScribeError::NotTrashed`] if it hasn't been moved to the trash yet,
/// or [`ScribeError::Http`]/[`ScribeError::Api`] on other request
/// failures.
pub async fn delete_document_permanently(&self, document_id: &str) -> Result<(), ScribeError> {
let mut url = self.base_url.clone();
url.set_path(&format!("/api/documents/{document_id}"));
self.with_auth_retry_raw(|token| self.http.delete(url.clone()).bearer_auth(token))
.await?;
Ok(())
}
/// Restores a trashed document, clearing its trash state.
///
/// # Errors
///
/// Returns [`ScribeError::NotFound`]/[`ScribeError::Forbidden`] if the
/// document doesn't exist or isn't owned by the caller, or
/// [`ScribeError::Http`]/[`ScribeError::Api`] on other request failures.
pub async fn recover_document(&self, document_id: &str) -> Result<(), ScribeError> {
let mut url = self.base_url.clone();
url.set_path(&format!("/api/documents/{document_id}/recover"));
self.with_auth_retry_raw(|token| self.http.post(url.clone()).bearer_auth(token))
.await?;
Ok(())
}
/// Lists the caller's trashed documents, most recently trashed first.
///
/// # Errors
///
/// Returns [`ScribeError::Http`]/[`ScribeError::Api`] on request failure.
pub async fn list_trashed_documents(&self) -> Result<Vec<TrashedDocument>, ScribeError> {
let mut url = self.base_url.clone();
url.set_path("/api/documents/trash");
let response: TrashedDocumentListResponse = self
.with_auth_retry(|token| self.http.get(url.clone()).bearer_auth(token))
.await?;
Ok(response.documents)
}
/// Submits a document for human review, attaching `comment` as the
/// reason / description of the problem.
pub async fn submit_document_feedback(
&self,
document_id: &str,
comment: &str,
) -> Result<(), ScribeError> {
let mut url = self.base_url.clone();
url.set_path(&format!("/api/documents/{document_id}/feedback"));
let body = serde_json::json!({ "comment": comment });
self.with_auth_retry_raw(|token| {
self.http.post(url.clone()).bearer_auth(token).json(&body)
})
.await?;
Ok(())
}
/// Opens a real-time channel for `document_id`, subscribing to
/// progress on whatever formats are already in progress (or their
/// incomplete parent formats) and allowing new conversions to be
/// started with [`DocumentChannel::start_conversion`]. This is the
/// only way to start a conversion for a format other than the
/// `html_stream` preview `create_document` already starts — it's no
/// longer possible over plain REST, so the server can guarantee it's
/// subscribed to whatever it just started converting.
///
/// # Errors
///
/// Returns [`ScribeError::NotFound`]/[`ScribeError::Forbidden`] if the
/// document doesn't exist or isn't owned by the caller, or
/// [`ScribeError::WebSocket`] if the connection fails.
pub async fn open_document_channel(
&self,
document_id: &str,
) -> Result<DocumentChannel, ScribeError> {
let token = self.access_token().await?;
match self.connect_ws_channel(document_id, &token).await {
// WebSocket handshake failure likely means the token was rejected
// (the server answers 403 when it refuses the token).
// Retry once with a force-refreshed token before giving up.
Err(ScribeError::WebSocket(_)) => {
let token = self.force_refresh().await?;
self.connect_ws_channel(document_id, &token).await
}
other => other,
}
}
async fn connect_ws_channel(
&self,
document_id: &str,
token: &str,
) -> Result<DocumentChannel, ScribeError> {
let mut url = self.base_url.clone();
let ws_scheme = if url.scheme() == "https" { "wss" } else { "ws" };
url.set_scheme(ws_scheme)
.map_err(|()| ScribeError::Channel {
message: "Something went wrong. Please try again.".to_string(),
})?;
url.set_path("/socket/websocket");
url.query_pairs_mut()
.append_pair("vsn", "2.0.0")
.append_pair("token", token);
let (ws, _response) = tokio_tungstenite::connect_async(url.as_str())
.await
.map_err(|e| ScribeError::WebSocket(Box::new(e)))?;
DocumentChannel::join(ws, document_id).await
}
/// # Errors
///
/// Returns [`ScribeError::NotFound`]/[`ScribeError::Forbidden`] if the
/// document doesn't exist or isn't owned by the caller, or
/// [`ScribeError::Http`]/[`ScribeError::Api`] on other request failures.
pub async fn list_outputs(&self, document_id: &str) -> Result<OutputList, ScribeError> {
let mut url = self.base_url.clone();
url.set_path(&format!("/api/documents/{document_id}/outputs"));
let response: OutputListResponse = self
.with_auth_retry(|token| self.http.get(url.clone()).bearer_auth(token))
.await?;
Ok(OutputList {
outputs: response.outputs,
is_password_needed: response.is_password_needed,
})
}
/// # Errors
///
/// Returns [`ScribeError::ConversionNotComplete`] if that format hasn't
/// finished converting yet, [`ScribeError::NotFound`]/[`ScribeError::Forbidden`]
/// if the document doesn't exist or isn't owned by the caller, or
/// [`ScribeError::Http`]/[`ScribeError::Api`] on other request failures.
pub async fn download_output(
&self,
document_id: &str,
format: OutputFormat,
) -> Result<Download, ScribeError> {
self.download_output_with_progress(document_id, format, |_, _| {})
.await
}
/// Where the file can be fetched from directly, when the server answers
/// with a redirect rather than the bytes.
///
/// The API redirects to storage by default, and that URL carries a real
/// `Content-Length` and accepts range requests, which is what a media
/// player needs to start before the whole file has arrived and to seek
/// afterwards. `None` means this server sends the file itself, so the
/// caller has to download it first.
///
/// # Errors
///
/// The same as [`download_output`](Self::download_output).
pub async fn download_url(
&self,
document_id: &str,
format: OutputFormat,
) -> Result<Option<String>, ScribeError> {
let mut url = self.base_url.clone();
url.set_path(&format!(
"/api/documents/{document_id}/outputs/{}/download",
format.as_str()
));
// Its own client: the shared one follows redirects, which would fetch
// the whole file here instead of reporting where it lives.
let client = reqwest::Client::builder()
.redirect(reqwest::redirect::Policy::none())
.build()?;
let token = self.access_token().await?;
let mut response = client.get(url.clone()).bearer_auth(&token).send().await?;
if response.status() == reqwest::StatusCode::UNAUTHORIZED {
let token = self.force_refresh().await?;
response = client.get(url.clone()).bearer_auth(&token).send().await?;
}
if response.status().is_redirection() {
return Ok(response
.headers()
.get(reqwest::header::LOCATION)
.and_then(|value| value.to_str().ok())
.map(str::to_string));
}
// Not a redirect: either this server streams the file itself, or
// something is wrong and the body says what.
response.error_for_status_or_json_error().await?;
Ok(None)
}
/// As [`download_output`](Self::download_output), reporting bytes received
/// as they arrive: `(downloaded, total)`, where `total` is `None` when the
/// server doesn't say how big the file is.
///
/// Audio of a long document is tens of megabytes, and the whole download
/// used to be one silent wait.
///
/// # Errors
///
/// The same as [`download_output`](Self::download_output).
pub async fn download_output_with_progress<F>(
&self,
document_id: &str,
format: OutputFormat,
mut on_progress: F,
) -> Result<Download, ScribeError>
where
F: FnMut(u64, Option<u64>) + Send,
{
let mut url = self.base_url.clone();
url.set_path(&format!(
"/api/documents/{document_id}/outputs/{}/download",
format.as_str()
));
let mut response = self
.with_auth_retry_raw(|token| self.http.get(url.clone()).bearer_auth(token))
.await?;
let file_name = response
.headers()
.get(reqwest::header::CONTENT_DISPOSITION)
.and_then(|value| value.to_str().ok())
.and_then(content_disposition_file_name);
let total = response_total_bytes(&response);
let mut data: Vec<u8> = Vec::with_capacity(total.unwrap_or(0) as usize);
on_progress(0, total);
while let Some(chunk) = response.chunk().await? {
data.extend_from_slice(&chunk);
on_progress(data.len() as u64, total);
}
Ok(Download { data, file_name })
}
/// # Errors
///
/// Returns [`ScribeError::NotFound`]/[`ScribeError::Forbidden`] if the
/// document doesn't exist or isn't owned by the caller, or
/// [`ScribeError::Http`]/[`ScribeError::Api`] on other request failures.
pub async fn get_settings(&self, document_id: &str) -> Result<Settings, ScribeError> {
let mut url = self.base_url.clone();
url.set_path(&format!("/api/documents/{document_id}/settings"));
self.with_auth_retry(|token| self.http.get(url.clone()).bearer_auth(token))
.await
}
/// # Errors
///
/// Returns [`ScribeError::NotFound`]/[`ScribeError::Forbidden`] if the
/// document doesn't exist or isn't owned by the caller, or
/// [`ScribeError::Http`]/[`ScribeError::Api`] on other request failures
/// (including validation errors on the settings themselves).
pub async fn update_settings(
&self,
document_id: &str,
update: &SettingsUpdate,
) -> Result<Settings, ScribeError> {
let mut url = self.base_url.clone();
url.set_path(&format!("/api/documents/{document_id}/settings"));
let body = serde_json::json!({ "settings": update });
self.with_auth_retry(|token| self.http.patch(url.clone()).bearer_auth(token).json(&body))
.await
}
/// Changes a document's title.
///
/// Titles start life as the uploaded file name, which for a photo or a
/// share-sheet import is something like "IMG_6746783276432", so this is how
/// a caller fixes one after the fact. Returns the document as the list
/// endpoint describes it, so a caller can refresh its row from the result.
///
/// # Errors
///
/// Returns [`ScribeError::NotFound`]/[`ScribeError::Forbidden`] if the
/// document doesn't exist or isn't owned by the caller, or
/// [`ScribeError::Api`] if the title is rejected (it may not be blank and
/// is capped at 255 characters).
pub async fn rename_document(
&self,
document_id: &str,
title: &str,
) -> Result<DocumentSummary, ScribeError> {
let mut url = self.base_url.clone();
url.set_path(&format!("/api/documents/{document_id}"));
let body = serde_json::json!({ "document": { "title": title } });
self.with_auth_retry(|token| self.http.patch(url.clone()).bearer_auth(token).json(&body))
.await
}
/// A URL that signs the system browser in as this client's user, then lands
/// on `return_to_path`.
///
/// The app and the browser do not share a session, so a bare link to a
/// settings page would greet the user with a login form. This hands the
/// app's access token to `/auth/browser_from_oauth`, which trades it for a
/// browser session and redirects.
///
/// Refreshes the token first when it is near expiry. The server rejects a
/// stale one with a "Failed to log in" page, and that is the page the user
/// would land on.
///
/// # Errors
///
/// Returns [`ScribeError::InvalidGrant`] if the token has expired and cannot
/// be refreshed, or [`ScribeError::Http`] if the refresh request fails.
pub async fn browser_entry_url(&self, return_to_path: &str) -> Result<String, ScribeError> {
let token = self.access_token().await?;
let state = serde_json::json!({ "return_to": return_to_path }).to_string();
let mut url = self.base_url.clone();
url.set_path("/auth/browser_from_oauth");
url.query_pairs_mut()
.append_pair("token", &token)
.append_pair("state", &state);
Ok(url.into())
}
/// The account-deletion page, signed in, for an app that lets users
/// start deleting their account from inside it.
///
/// # Errors
///
/// As for [`ScribeClient::browser_entry_url`].
pub async fn delete_account_url(&self) -> Result<String, ScribeError> {
self.browser_entry_url(DELETE_ACCOUNT_PATH).await
}
/// The caller's default conversion settings, which every new document's
/// settings are seeded from.
///
/// Account-scoped, so there is no id: a user has exactly one set, and the
/// server creates it on first read rather than returning a 404.
///
/// # Errors
///
/// Returns [`ScribeError::Http`]/[`ScribeError::Api`] on request failure.
pub async fn default_settings(&self) -> Result<Settings, ScribeError> {
let mut url = self.base_url.clone();
url.set_path("/api/document_settings");
self.with_auth_retry(|token| self.http.get(url.clone()).bearer_auth(token))
.await
}
/// Updates the caller's default conversion settings. Partial: fields left
/// `None` on `update` are not sent, so they keep their current value.
///
/// Changing these does not touch documents that already exist.
///
/// # Errors
///
/// Returns [`ScribeError::Http`]/[`ScribeError::Api`] on request failure,
/// including validation errors on the settings themselves.
pub async fn update_default_settings(
&self,
update: &SettingsUpdate,
) -> Result<Settings, ScribeError> {
let mut url = self.base_url.clone();
url.set_path("/api/document_settings");
let body = serde_json::json!({ "settings": update });
self.with_auth_retry(|token| self.http.patch(url.clone()).bearer_auth(token).json(&body))
.await
}
/// Registers `token` (the hex-encoded APNs device token) so the server
/// can send this device push notifications. Registration is an upsert —
/// re-registering the same token re-points it at the current user (e.g.
/// after signing out and into a different account on the same device).
///
/// # Errors
///
/// Returns [`ScribeError::Http`]/[`ScribeError::Api`] on request failure.
pub async fn register_device(&self, token: &str, platform: &str) -> Result<(), ScribeError> {
let mut url = self.base_url.clone();
url.set_path("/api/devices");
let body = serde_json::json!({ "token": token, "platform": platform });
self.with_auth_retry_raw(|token| {
self.http.post(url.clone()).bearer_auth(token).json(&body)
})
.await?;
Ok(())
}
/// Unregisters `token`, e.g. on sign-out, so this device stops receiving push.
///
/// # Errors
///
/// Returns [`ScribeError::Http`]/[`ScribeError::Api`] on request failure.
pub async fn unregister_device(&self, token: &str) -> Result<(), ScribeError> {
let mut url = self.base_url.clone();
url.set_path(&format!("/api/devices/{token}"));
self.with_auth_retry_raw(|t| self.http.delete(url.clone()).bearer_auth(t))
.await?;
Ok(())
}
/// # Errors
///
/// Returns [`ScribeError::Http`]/[`ScribeError::Api`] on request failure.
pub async fn get_notification_settings(&self) -> Result<NotificationSettings, ScribeError> {
let mut url = self.base_url.clone();
url.set_path("/api/notification_settings");
self.with_auth_retry(|token| self.http.get(url.clone()).bearer_auth(token))
.await
}
/// # Errors
///
/// Returns [`ScribeError::Http`]/[`ScribeError::Api`] on request failure.
pub async fn update_notification_settings(
&self,
push_notify_when_complete: bool,
) -> Result<NotificationSettings, ScribeError> {
let mut url = self.base_url.clone();
url.set_path("/api/notification_settings");
let body = serde_json::json!({ "push_notify_when_complete": push_notify_when_complete });
self.with_auth_retry(|token| self.http.patch(url.clone()).bearer_auth(token).json(&body))
.await
}
/// # Errors
///
/// Returns [`ScribeError::Http`]/[`ScribeError::Api`] on request failure.
pub async fn get_account_info(&self) -> Result<AccountInfo, ScribeError> {
let mut url = self.base_url.clone();
url.set_path("/api/account_info");
self.with_auth_retry(|token| self.http.get(url.clone()).bearer_auth(token))
.await
}
/// The pages the account has left and its active subscription, if any.
///
/// # Errors
///
/// Returns [`ScribeError::Http`]/[`ScribeError::Api`] on request failure.
pub async fn billing(&self) -> Result<BillingStatus, ScribeError> {
let mut url = self.base_url.clone();
url.set_path("/api/billing");
self.with_auth_retry(|token| self.http.get(url.clone()).bearer_auth(token))
.await
}
/// Lists the App Store products on sale, in the order to show them.
///
/// # Errors
///
/// Returns [`ScribeError::Http`]/[`ScribeError::Api`] on request failure.
pub async fn app_store_products(&self) -> Result<Vec<AppStoreProduct>, ScribeError> {
let mut url = self.base_url.clone();
url.set_path("/api/app_store/products");
let response: AppStoreProductsResponse = self
.with_auth_retry(|token| self.http.get(url.clone()).bearer_auth(token))
.await?;
Ok(response.products)
}
/// Credits a completed App Store purchase to the account. `signed_transaction`
/// is `Transaction.jwsRepresentation` from `StoreKit` 2. Reporting the same
/// transaction again is harmless, so finish the transaction in `StoreKit`
/// only once this succeeds. Returns the billing status afterwards.
///
/// # Errors
///
/// Returns [`ScribeError::Api`] with code `other_account` if the purchase
/// was made while signed in to a different Scribe account, or
/// `invalid_transaction` if the server couldn't verify it.
pub async fn submit_app_store_transaction(
&self,
signed_transaction: &str,
) -> Result<BillingStatus, ScribeError> {
let mut url = self.base_url.clone();
url.set_path("/api/app_store/transactions");
let body = serde_json::json!({ "signed_transaction": signed_transaction });
self.with_auth_retry(|token| self.http.post(url.clone()).bearer_auth(token).json(&body))
.await
}
/// # Errors
///
/// Returns [`ScribeError::Http`]/[`ScribeError::Api`] on request failure.
pub async fn languages(&self) -> Result<Vec<Language>, ScribeError> {
let mut url = self.base_url.clone();
url.set_path("/api/settings/languages");
let response: LanguagesResponse = self
.with_auth_retry(|token| self.http.get(url.clone()).bearer_auth(token))
.await?;
Ok(response.languages)
}
/// # Errors
///
/// Returns [`ScribeError::Http`]/[`ScribeError::Api`] on request failure.
pub async fn dialects(&self) -> Result<HashMap<String, Vec<Dialect>>, ScribeError> {
let mut url = self.base_url.clone();
url.set_path("/api/settings/dialects");
let response: DialectsResponse = self
.with_auth_retry(|token| self.http.get(url.clone()).bearer_auth(token))
.await?;
Ok(response.dialects)
}
/// # Errors
///
/// Returns [`ScribeError::Http`]/[`ScribeError::Api`] on request failure.
pub async fn braille_tables(&self) -> Result<Vec<BrailleTable>, ScribeError> {
let mut url = self.base_url.clone();
url.set_path("/api/settings/braille_tables");
let response: BrailleTablesResponse = self
.with_auth_retry(|token| self.http.get(url.clone()).bearer_auth(token))
.await?;
Ok(response.braille_tables)
}
/// # Errors
///
/// Returns [`ScribeError::Http`]/[`ScribeError::Api`] on request failure.
pub async fn voices(&self) -> Result<HashMap<String, Vec<Voice>>, ScribeError> {
let mut url = self.base_url.clone();
url.set_path("/api/settings/voices");
let response: VoicesResponse = self
.with_auth_retry(|token| self.http.get(url.clone()).bearer_auth(token))
.await?;
Ok(response.voices)
}
/// Sends a request built by `build`, retrying once with a
/// force-refreshed token if the server returns `401`. Returns the
/// validated response for callers that need to read the body themselves.
async fn with_auth_retry_raw<F>(&self, build: F) -> Result<reqwest::Response, ScribeError>
where
F: Fn(&str) -> reqwest::RequestBuilder,
{
let token = self.access_token().await?;
let response = build(&token).send().await?;
let response = if response.status() == reqwest::StatusCode::UNAUTHORIZED {
let token = self.force_refresh().await?;
build(&token).send().await?
} else {
response
};
response.error_for_status_or_json_error().await
}
/// Like [`with_auth_retry_raw`] but deserializes the response body as JSON.
async fn with_auth_retry<T, F>(&self, build: F) -> Result<T, ScribeError>
where
T: serde::de::DeserializeOwned,
F: Fn(&str) -> reqwest::RequestBuilder,
{
self.with_auth_retry_raw(build)
.await?
.json()
.await
.map_err(Into::into)
}
}
/// Small helper trait so response-status handling reads the same way at every call site.
/// How many bytes the body will be, if the server says.
///
/// A download streamed from object storage goes out chunked, which has no
/// `Content-Length`, so the API repeats the size it already knows in a header
/// of its own. Without either, the caller only learns the total at the end.
fn response_total_bytes(response: &reqwest::Response) -> Option<u64> {
response.content_length().or_else(|| {
response
.headers()
.get("x-scribe-content-length")
.and_then(|value| value.to_str().ok())
.and_then(|value| value.trim().parse().ok())
})
}
/// The file name in a `Content-Disposition` header. Prefers the UTF-8
/// `filename*` (RFC 6266) over the ASCII-only `filename` fallback, and keeps
/// only the last path component so a name can never point outside the folder
/// it is saved in.
fn content_disposition_file_name(header: &str) -> Option<String> {
let mut fallback = None;
let mut extended = None;
for param in header.split(';').map(str::trim) {
if let Some(value) = param.strip_prefix("filename*=") {
// `charset'language'value`. The server always sends UTF-8, and
// its value keeps a literal "+", so this must not be decoded as a
// form, which would turn it into a space.
let mut parts = value.splitn(3, '\'');
if let (Some(charset), Some(_), Some(encoded)) =
(parts.next(), parts.next(), parts.next())
{
if charset.eq_ignore_ascii_case("utf-8") {
extended = percent_encoding::percent_decode_str(encoded)
.decode_utf8()
.ok()
.map(|name| name.into_owned());
}
}
} else if let Some(value) = param.strip_prefix("filename=") {
fallback = Some(value.trim_matches('"').to_string());
}
}
extended
.or(fallback)
.and_then(|name| name.rsplit(['/', '\\']).next().map(str::to_string))
.filter(|name| !name.is_empty())
}
trait ResponseExt {
async fn error_for_status_or_json_error(self) -> Result<reqwest::Response, ScribeError>;
}
impl ResponseExt for reqwest::Response {
async fn error_for_status_or_json_error(self) -> Result<reqwest::Response, ScribeError> {
let status = self.status();
if status.is_success() {
return Ok(self);
}
let text = self.text().await.unwrap_or_default();
// A parse failure here means the body wasn't our JSON envelope at
// all (an HTML error page, a proxy's plain-text response, etc.) —
// never surface that raw text, fall back to a generic message.
let Ok(body) = serde_json::from_str::<ApiErrorResponse>(&text) else {
return Err(ScribeError::Api {
status: status.as_u16(),
code: "server_error".to_string(),
message: "Something went wrong. Please try again.".to_string(),
});
};
let message = body
.error
.message
.unwrap_or_else(|| "Something went wrong. Please try again.".to_string());
Err(match body.error.code.as_str() {
"not_found" => ScribeError::NotFound { message },
"forbidden" => ScribeError::Forbidden { message },
"not_trashed" => ScribeError::NotTrashed { message },
"conversion_not_complete" => ScribeError::ConversionNotComplete { message },
_ => ScribeError::Api {
status: status.as_u16(),
code: body.error.code,
message,
},
})
}
}