paperless-api 0.1.0

Async Paperless ngx API client
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
use std::{collections::HashMap, path::Path, str::FromStr, sync::Arc};

use reqwest::{
    Method, StatusCode,
    header::{ACCEPT, HeaderMap, HeaderName, InvalidHeaderValue},
    multipart,
};
use serde::Deserialize;
use tracing::{debug, trace};

use crate::{
    Error, Result,
    correspondent::{Correspondent, CorrespondentId},
    custom_field::{CustomField, CustomFieldId},
    document::{Document, DocumentId},
    document_type::{DocumentType, DocumentTypeId},
    tag::{Tag, TagId},
    task::{Task, TaskId},
};

/// Selects which cached metadata to refresh.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum RefreshData {
    Tags,
    CustomFields,
    Correspondents,
    DocumentTypes,
}

/// Client to interact with Paperless.
#[derive(Debug, Clone)]
pub struct PaperlessClient {
    client: reqwest::Client,
    base_url: String,

    correspondents: HashMap<CorrespondentId, Correspondent>,
    document_types: HashMap<DocumentTypeId, DocumentType>,
    tags: HashMap<TagId, Tag>,
    custom_fields: HashMap<CustomFieldId, CustomField>,
}

#[derive(Debug, Deserialize)]
struct PaginatedResponse<T> {
    results: Vec<T>,
    next: Option<String>,
}

impl PaperlessClient {
    /// Create a new Paperless client.
    pub fn new(
        base_url: &str,
        token: &str,
        headers: Option<&HashMap<String, String>>,
    ) -> std::result::Result<Self, String> {
        let mut headers_map = HeaderMap::new();

        // Add additional headers if provided
        if let Some(headers) = headers {
            for (key, value) in headers {
                headers_map.insert(
                    HeaderName::from_str(key).map_err(|err| err.to_string())?,
                    value
                        .parse()
                        .map_err(|err: InvalidHeaderValue| err.to_string())?,
                );
            }
        }

        // Add the Paperless token header
        headers_map.insert(
            HeaderName::from_str("Authorization").map_err(|err| err.to_string())?,
            format!("Token {token}")
                .parse()
                .map_err(|err: InvalidHeaderValue| err.to_string())?,
        );

        Ok(Self {
            base_url: base_url.to_string(),
            client: reqwest::Client::builder()
                .default_headers(headers_map)
                .build()
                .map_err(|err| err.to_string())?,
            tags: HashMap::new(),
            custom_fields: HashMap::new(),
            correspondents: HashMap::new(),
            document_types: HashMap::new(),
        })
    }

    async fn load_tags(&self) -> Result<HashMap<TagId, Tag>> {
        debug!("loading tags");
        let tags: Vec<Tag> = self.fetch_all_pages("/api/tags/").await?;
        Ok(tags.into_iter().map(|tag| (tag.id, tag)).collect())
    }

    async fn load_custom_fields(&self) -> Result<HashMap<CustomFieldId, CustomField>> {
        debug!("loading custom fields");
        let custom_fields: Vec<CustomField> = self.fetch_all_pages("/api/custom_fields/").await?;
        Ok(custom_fields
            .into_iter()
            .map(|custom_field| (custom_field.id, custom_field))
            .collect())
    }

    async fn load_correspondents(&self) -> Result<HashMap<CorrespondentId, Correspondent>> {
        debug!("loading correspondents");
        let correspondents: Vec<Correspondent> =
            self.fetch_all_pages("/api/correspondents/").await?;
        Ok(correspondents
            .into_iter()
            .map(|correspondent| (correspondent.id, correspondent))
            .collect())
    }

    async fn load_document_types(&self) -> Result<HashMap<DocumentTypeId, DocumentType>> {
        debug!("loading document types");
        let document_types: Vec<DocumentType> =
            self.fetch_all_pages("/api/document_types/").await?;
        Ok(document_types
            .into_iter()
            .map(|document_type| (document_type.id, document_type))
            .collect())
    }

    /// Refresh selected cached metadata concurrently.
    pub async fn refresh(&mut self, data: impl IntoIterator<Item = RefreshData>) -> Result<()> {
        let mut refresh_tags = false;
        let mut refresh_custom_fields = false;
        let mut refresh_correspondents = false;
        let mut refresh_document_types = false;

        for item in data {
            match item {
                RefreshData::Tags => refresh_tags = true,
                RefreshData::CustomFields => refresh_custom_fields = true,
                RefreshData::Correspondents => refresh_correspondents = true,
                RefreshData::DocumentTypes => refresh_document_types = true,
            }
        }

        let (tags, custom_fields, correspondents, document_types) = futures_util::try_join!(
            async {
                if refresh_tags {
                    Ok::<Option<HashMap<TagId, Tag>>, Error>(Some(self.load_tags().await?))
                } else {
                    Ok::<Option<HashMap<TagId, Tag>>, Error>(None)
                }
            },
            async {
                if refresh_custom_fields {
                    Ok::<Option<HashMap<CustomFieldId, CustomField>>, Error>(Some(
                        self.load_custom_fields().await?,
                    ))
                } else {
                    Ok::<Option<HashMap<CustomFieldId, CustomField>>, Error>(None)
                }
            },
            async {
                if refresh_correspondents {
                    Ok::<Option<HashMap<CorrespondentId, Correspondent>>, Error>(Some(
                        self.load_correspondents().await?,
                    ))
                } else {
                    Ok::<Option<HashMap<CorrespondentId, Correspondent>>, Error>(None)
                }
            },
            async {
                if refresh_document_types {
                    Ok::<Option<HashMap<DocumentTypeId, DocumentType>>, Error>(Some(
                        self.load_document_types().await?,
                    ))
                } else {
                    Ok::<Option<HashMap<DocumentTypeId, DocumentType>>, Error>(None)
                }
            },
        )?;

        if let Some(tags) = tags {
            self.tags = tags;
        }

        if let Some(custom_fields) = custom_fields {
            self.custom_fields = custom_fields;
        }

        if let Some(correspondents) = correspondents {
            self.correspondents = correspondents;
        }

        if let Some(document_types) = document_types {
            self.document_types = document_types;
        }

        Ok(())
    }

    /// List all tags.
    #[inline]
    pub async fn refresh_tags(&mut self) -> Result<()> {
        self.refresh([RefreshData::Tags]).await
    }

    /// List all custom fields.
    #[inline]
    pub async fn refresh_custom_fields(&mut self) -> Result<()> {
        self.refresh([RefreshData::CustomFields]).await
    }

    /// List all correspondents.
    #[inline]
    pub async fn refresh_correspondents(&mut self) -> Result<()> {
        self.refresh([RefreshData::Correspondents]).await
    }

    /// List all document types.
    #[inline]
    pub async fn refresh_document_types(&mut self) -> Result<()> {
        self.refresh([RefreshData::DocumentTypes]).await
    }

    /// Get all documents with any of the given tags.
    pub async fn get_documents_by_tags(
        &self,
        tag_ids: &[TagId],
        truncate_content: bool,
    ) -> Result<Vec<Document>> {
        let tag_id_str = tag_ids
            .iter()
            .map(|tag_id| tag_id.0.to_string())
            .collect::<Vec<_>>()
            .join(",");
        let mut documents: Vec<Document> = self
            .fetch_all_pages(&format!(
                "/api/documents/?truncate_content={truncate_content}&tags__id__in={tag_id_str}"
            ))
            .await?;

        for doc in &mut documents {
            doc.client = Some(Arc::new(self.clone()));
            doc.content_is_truncated = truncate_content;
        }

        Ok(documents)
    }

    /// Get a document by its ID.
    pub async fn get_document_by_id(&self, id: DocumentId) -> Result<Document> {
        let resp = self
            .request(Method::GET, &format!("/api/documents/{}/", id.0), None)
            .await?;

        let mut document: Document = resp
            .json()
            .await
            .map_err(|e| Error::Other(format!("Failed to parse document: {e}")))?;

        document.client = Some(Arc::new(self.clone()));
        Ok(document)
    }

    pub(crate) async fn request(
        &self,
        method: Method,
        endpoint: &str,
        body: Option<&serde_json::Value>,
    ) -> Result<reqwest::Response> {
        let mut req = self
            .client
            .request(method, format!("{}{endpoint}", self.base_url))
            .header(ACCEPT, "application/json");

        if let Some(json_body) = body {
            req = req.json(json_body);
        }

        let resp = req
            .send()
            .await
            .map_err(|e| Error::Other(format!("Failed to send request: {e}")))?;

        if resp.status() == StatusCode::NOT_FOUND {
            return Err(Error::NotFound);
        }

        if !resp.status().is_success() {
            return Err(Error::Response {
                status_code: resp.status().as_u16(),
                body: resp.text().await.unwrap_or_default(),
            });
        }

        Ok(resp)
    }

    pub(crate) async fn fetch_all_pages<T: for<'de> Deserialize<'de>>(
        &self,
        endpoint: &str,
    ) -> Result<Vec<T>> {
        let mut results = Vec::new();
        let mut current_url = Some(endpoint.to_string());

        while let Some(url) = current_url {
            let resp = self.request(Method::GET, &url, None).await?;

            let page: PaginatedResponse<T> = resp.json().await.map_err(|e| {
                Error::Other(format!(
                    "Failed to parse paginated response for {endpoint}: {e}"
                ))
            })?;

            results.extend(page.results);

            current_url = page.next.and_then(|next_url| {
                // Extract just the path from the full URL
                next_url
                    .trim_start_matches(&self.base_url)
                    .to_string()
                    .into()
            });
        }

        Ok(results)
    }

    /// Get all tasks with optional filtering by ID, name, or acknowledged status.
    pub async fn get_task_status(
        &self,
        task_id: Option<&TaskId>,
        task_name: Option<&str>,
        acknowledged: Option<bool>,
    ) -> Result<Vec<Task>> {
        let mut query = Vec::new();

        if let Some(id) = task_id {
            query.push(("task_id", id.to_string()));
        }

        if let Some(name) = task_name {
            query.push(("task_name", name.to_string()));
        }

        if let Some(ack) = acknowledged {
            query.push(("acknowledged", ack.to_string()));
        }

        let resp = self
            .request(
                Method::GET,
                &format!(
                    "/api/tasks/?{}",
                    serde_urlencoded::to_string(&query)
                        .map_err(|e| Error::Other(format!("Failed to serialize query: {e}")))?
                ),
                None,
            )
            .await?;

        trace!("get_task_status response: {:?}", resp);

        let body = resp
            .text()
            .await
            .map_err(|e| Error::Other(format!("Failed to read response body: {e}")))?;

        let tasks: Vec<Task> = match serde_json::from_str(&body) {
            Ok(t) => t,
            Err(e) => {
                return Err(Error::InvalidJson(format!(
                    "Failed to parse response body: {e}"
                )));
            }
        };

        if tasks.is_empty() {
            return Err(Error::NotFound);
        }

        Ok(tasks)
    }

    /// Upload a document to Paperless.
    ///
    /// Returns the task ID on success.
    pub async fn upload_document(&self, file_path: &Path, filename: &str) -> Result<TaskId> {
        let file_bytes = std::fs::read(file_path)
            .map_err(|e| Error::Other(format!("Failed to read file: {e}")))?;

        let form = multipart::Form::new().part(
            "document",
            multipart::Part::bytes(file_bytes).file_name(filename.to_string()),
        );

        let url = format!("{}/api/documents/post_document/", self.base_url);

        let resp = self
            .client
            .post(&url)
            .multipart(form)
            .send()
            .await
            .map_err(|e| Error::Other(format!("Failed to send request: {e}")))?;

        let status = resp.status();
        if !resp.status().is_success() {
            return Err(Error::Response {
                status_code: status.as_u16(),
                body: resp.text().await.unwrap_or_default(),
            });
        }

        let task_id: String = resp
            .json()
            .await
            .map_err(|e| Error::Other(format!("Failed to parse task ID: {e}")))?;
        Ok(TaskId(task_id))
    }

    pub fn tags(&self) -> &HashMap<TagId, Tag> {
        &self.tags
    }

    pub fn find_tag_by_name(&self, name: &str) -> Option<&Tag> {
        self.tags.values().find(|tag| tag.name == name)
    }

    pub fn document_types(&self) -> &HashMap<DocumentTypeId, DocumentType> {
        &self.document_types
    }

    pub fn correspondents(&self) -> &HashMap<CorrespondentId, Correspondent> {
        &self.correspondents
    }

    pub fn custom_fields(&self) -> &HashMap<CustomFieldId, CustomField> {
        &self.custom_fields
    }

    pub fn find_custom_field_by_name(&self, name: &str) -> Option<&CustomField> {
        self.custom_fields.values().find(|field| field.name == name)
    }
}