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
use crate::{
    client::{AuthorizedClient, GeneralErrHandler, ID},
    errors::{ErrorKind, Result},
    utils::{deserialize, serialize},
};

use chrono::{DateTime, FixedOffset};
use failure::Fail;
use log::debug;
use reqwest::{blocking::Response, StatusCode};
use serde::{self, Deserialize, Serialize};
use std::fmt;

#[derive(PartialEq, Debug)]
pub enum NamedSearch {
    None,
    PublicCollections,
}

#[derive(Debug)]
pub struct Search<'a> {
    filenames:    Option<Vec<&'a str>>,
    tags:         Option<Vec<&'a str>>,
    fulltext:     Option<&'a str>,
    named_search: NamedSearch,
}

impl<'a> Search<'a> {
    pub fn new() -> Search<'a> {
        Search {
            filenames:    None,
            tags:         None,
            fulltext:     None,
            named_search: NamedSearch::None,
        }
    }

    pub fn filenames(self, filenames: Vec<&'a str>) -> Search<'a> {
        Search {
            filenames: Some(filenames),
            ..self
        }
    }

    pub fn tags(self, tags: Vec<&'a str>) -> Search<'a> {
        Search {
            tags: Some(tags),
            ..self
        }
    }

    pub fn fulltext(self, fulltext: &'a str) -> Search<'a> {
        Search {
            fulltext: Some(fulltext),
            ..self
        }
    }

    pub fn named_searches(self, named_search: NamedSearch) -> Search<'a> { Search { named_search, ..self } }
}

impl<'a> Default for Search<'a> {
    fn default() -> Self { Self::new() }
}

pub(crate) mod internal {
    use serde::Serialize;

    #[derive(Serialize, Debug)]
    pub struct Search<'a> {
        action: &'a str,
        params: Params<'a>,
    }

    #[derive(Serialize, Debug)]
    struct Params<'a> {
        query:  Query<'a>,
        filter: Filter<'a>,
        #[serde(skip_serializing_if = "Option::is_none")]
        named:  Option<Vec<Named<'a>>>,
    }

    #[derive(Serialize, Debug)]
    struct Query<'a> {
        #[serde(skip_serializing_if = "Option::is_none")]
        text: Option<&'a str>,
    }

    #[derive(Serialize, Debug)]
    struct Filter<'a> {
        #[serde(skip_serializing_if = "Option::is_none")]
        filenames: Option<Vec<&'a str>>,
        #[serde(skip_serializing_if = "Option::is_none")]
        tags:      Option<Vec<&'a str>>,
    }

    #[derive(Serialize, Debug)]
    struct Named<'a> {
        name:   &'a str,
        params: Include,
    }

    #[derive(Serialize, Debug)]
    struct Include {
        include: bool,
    }

    impl<'a> Search<'a> {
        pub fn from_search(s: super::Search<'a>) -> Self {
            let named: Option<Vec<Named>> = match s.named_search {
                super::NamedSearch::None => None,
                super::NamedSearch::PublicCollections => {
                    let include = Include { include: true };
                    let named = vec![Named {
                        name:   "public-collections",
                        params: include,
                    }];
                    Some(named)
                }
            };

            let filter = Filter {
                filenames: s.filenames,
                tags:      s.tags,
            };
            let query = Query { text: s.fulltext };
            let params = Params { query, filter, named };

            Search {
                action: "search",
                params,
            }
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct SearchResult {
    #[serde(default)]
    pub documents: Vec<Document>,
    pub hits:      usize,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Document {
    pub author:            ID,
    // collections
    pub comments:          usize,
    #[serde(rename = "document-date")]
    pub document_date:     DateTime<FixedOffset>,
    #[serde(rename = "extended-metadata")]
    pub extended_metadata: serde_json::Value,
    pub filename:          String,
    pub hash:              String,
    pub id:                ID,
    #[serde(
        rename = "mimetype",
        serialize_with = "serialize::mime_type",
        deserialize_with = "deserialize::mime_type"
    )]
    pub mime_type:         mime::Mime,
    pub owner:             ID,
    pub pages:             Option<usize>,
    pub representations:   Representations,
    pub score:             Option<f64>,
    pub title:             String,
    #[serde(rename = "upload-date")]
    pub upload_date:       DateTime<FixedOffset>,
    pub uploader:          ID,
    // users
    pub version:           usize,
    #[serde(rename = "version-date")]
    pub version_date:      DateTime<FixedOffset>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Representations {
    pub pdf:      String,
    pub fulltext: String,
    pub jpg:      String,
    pub png:      String,
    pub mp4:      String,
}

impl fmt::Display for Representations {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut reps = Vec::new();
        if self.pdf == "yes" {
            reps.push("pdf");
        }
        if self.fulltext == "yes" {
            reps.push("fulltext");
        }
        if self.jpg == "yes" {
            reps.push("jpg");
        }
        if self.png == "yes" {
            reps.push("png");
        }
        if self.mp4 == "yes" {
            reps.push("mp4");
        }

        write!(f, "{:?}", reps)
    }
}

pub fn search_documents(authorized_client: &AuthorizedClient, search: Search) -> Result<SearchResult> {
    let url = format!("https://api.{}/v2/documents", authorized_client.base_url);

    let internal_search = internal::Search::from_search(search);

    let request = authorized_client
        .http_client
        .post(&url)
        .bearer_auth(&authorized_client.token.access_token)
        .json(&internal_search);
    debug!("Request: '{:#?}'", request);

    let response: Response = request
        .send()
        .map_err(|e| e.context(ErrorKind::HttpRequestFailed))?
        .general_err_handler(&[StatusCode::OK])?;
    debug!("Response: '{:#?}'", response);

    let status = response.status();
    let result = response.json().map_err(|e| {
        e.context(ErrorKind::FailedToProcessHttpResponse(
            status,
            "reading body".to_string(),
        ))
    })?;

    Ok(result)
}

#[cfg(test)]
mod test {
    use super::*;
    use spectral::prelude::*;

    mod document {
        use super::*;

        #[test]
        fn deserialize_ok() {
            let document_json = r#"{
    "author": "Industrie- und Handelskammer Bonn/Rhein-Sieg",
    "collections": {
        "not-visible-count": 0,
        "visible": [
            "9da0ffc7-09a5-42ee-a166-05ff13a74d91"
        ]
    },
    "comments": 0,
    "document-date": "2012-12-11T14:31:57.508Z",
    "extended-metadata": {},
    "filename": "Branchenkatalog.pdf",
    "hash": "fbf2b3b1688f94c76f10adfc82f80c1d",
    "id": "0176fc13-6dfe-40db-aca7-6b7c729e3fa3",
    "mimetype": "application/pdf",
    "owner": "ded4d798-d659-4c1c-9f2d-09e02d23e604",
    "pages": 49,
    "representations": {
        "fulltext": "yes",
        "jpg": "yes",
        "mp4": "no",
        "pdf": "yes",
        "png": "no"
    },
    "score": 15.038736,
    "size": 819693,
    "title": "NACE- Klassifikation der Wirtschaftszweige 2008",
    "upload-date": "2012-12-11T14:31:57.508Z",
    "uploader": "ded4d798-d659-4c1c-9f2d-09e02d23e604",
    "users": {
        "not-visible-count": 0,
        "visible": [
            "4161b86a-9eb8-4590-af5a-6f70b4ca0efb"
        ]
    },
    "version": 1,
    "version-date": "2012-12-11T14:31:57.508Z"
}"#;

            let document: std::result::Result<Document, _> = serde_json::from_str(document_json);

            assert_that(&document).is_ok();

            println!("Document: {:#?}", document.unwrap());
        }
    }
}