Skip to main content

alpr/
services.rs

1// Code generated by sdk/generator. DO NOT EDIT.
2
3use crate::client::{Client, Data, RequestOptions};
4use crate::error::Error;
5use crate::models::*;
6use crate::multipart::Form;
7use crate::pagination::{iterate, Page};
8
9impl Client {
10    /// The API's cameras.
11    pub fn cameras(&self) -> CamerasService<'_> {
12        CamerasService::new(self)
13    }
14
15    /// The API's photos.
16    pub fn photos(&self) -> PhotosService<'_> {
17        PhotosService::new(self)
18    }
19
20    /// The API's recognitions.
21    pub fn recognitions(&self) -> RecognitionsService<'_> {
22        RecognitionsService::new(self)
23    }
24}
25
26
27/// The API's cameras: `client.cameras()`.
28///
29/// [`api_key`](Self::api_key) sets the key of the call made through it.
30#[derive(Debug, Clone)]
31pub struct CamerasService<'a> {
32    client: &'a Client,
33    options: RequestOptions,
34}
35
36impl<'a> CamerasService<'a> {
37    pub(crate) fn new(client: &'a Client) -> Self {
38        Self { client, options: RequestOptions::default() }
39    }
40
41    /// Calls with another workspace's key.
42    pub fn api_key(mut self, key: impl Into<String>) -> Self {
43        self.options.api_key = Some(key.into());
44        self
45    }
46
47    /// Calls with these options.
48    pub fn options(mut self, options: RequestOptions) -> Self {
49        self.options = options;
50        self
51    }
52
53    /// Listar as câmeras.
54    pub fn list(&self) -> Result<Vec<Camera>, Error> {
55        let out: Data<Vec<Camera>> = self.client.request("GET", "/api/v1/cameras", &[], None, &self.options)?;
56        Ok(out.data)
57    }
58}
59
60impl PhotosDetectParams {
61    /// The body as the `multipart/form-data` it is sent as: the photo is a
62    /// file part, every other field a text part, and a field left out is not
63    /// sent.
64    fn form(&self) -> Form<'_> {
65        let mut form = Form::new();
66        form.file("image", &self.image);
67        if let Some(value) = &self.image_mode {
68            form.text("image_mode", value.to_string());
69        }
70        if let Some(value) = &self.lpr_confidence {
71            form.text("lpr_confidence", value.to_string());
72        }
73        if let Some(value) = &self.min_ocr_confidence {
74            form.text("min_ocr_confidence", value.to_string());
75        }
76        form
77    }
78}
79
80impl PhotosOCRParams {
81    /// The body as the `multipart/form-data` it is sent as: the photo is a
82    /// file part, every other field a text part, and a field left out is not
83    /// sent.
84    fn form(&self) -> Form<'_> {
85        let mut form = Form::new();
86        form.file("image", &self.image);
87        form
88    }
89}
90
91impl PhotosRecognizeParams {
92    /// The body as the `multipart/form-data` it is sent as: the photo is a
93    /// file part, every other field a text part, and a field left out is not
94    /// sent.
95    fn form(&self) -> Form<'_> {
96        let mut form = Form::new();
97        form.file("image", &self.image);
98        if let Some(value) = &self.image_mode {
99            form.text("image_mode", value.to_string());
100        }
101        if let Some(value) = &self.lpr_confidence {
102            form.text("lpr_confidence", value.to_string());
103        }
104        if let Some(value) = &self.min_ocr_confidence {
105            form.text("min_ocr_confidence", value.to_string());
106        }
107        form
108    }
109}
110
111/// The API's photos: `client.photos()`.
112///
113/// [`api_key`](Self::api_key) sets the key of the call made through it.
114#[derive(Debug, Clone)]
115pub struct PhotosService<'a> {
116    client: &'a Client,
117    options: RequestOptions,
118}
119
120impl<'a> PhotosService<'a> {
121    pub(crate) fn new(client: &'a Client) -> Self {
122        Self { client, options: RequestOptions::default() }
123    }
124
125    /// Calls with another workspace's key.
126    pub fn api_key(mut self, key: impl Into<String>) -> Self {
127        self.options.api_key = Some(key.into());
128        self
129    }
130
131    /// Calls with these options.
132    pub fn options(mut self, options: RequestOptions) -> Self {
133        self.options = options;
134        self
135    }
136
137    /// Localizar as placas de uma foto.
138    pub fn detect(&self, params: &PhotosDetectParams) -> Result<PhotoDetection, Error> {
139        self.client.request("POST", "/api/v1/detect", &[], Some(params.form()), &self.options)
140    }
141
142    /// Ler uma placa já recortada.
143    pub fn ocr(&self, params: &PhotosOCRParams) -> Result<PlateReading, Error> {
144        self.client.request("POST", "/api/v1/ocr", &[], Some(params.form()), &self.options)
145    }
146
147    /// Ler as placas de uma foto.
148    pub fn recognize(&self, params: &PhotosRecognizeParams) -> Result<PhotoRecognition, Error> {
149        self.client.request("POST", "/api/v1/recognize", &[], Some(params.form()), &self.options)
150    }
151}
152
153/// The filters of recognitions:list, and the cursor to start from.
154#[derive(Debug, Clone, Default, PartialEq, Eq)]
155#[non_exhaustive]
156pub struct RecognitionsListParams {
157    /// The cursor the previous page answered as `next`; none asks for the newest page.
158    pub before: Option<String>,
159    /// Prefixo da placa; traço, espaço e caixa não importam.
160    pub plate: Option<String>,
161    /// ID da câmera.
162    pub camera: Option<String>,
163    /// Origem da leitura; outro valor é ignorado.
164    pub source: Option<String>,
165}
166
167impl RecognitionsListParams {
168    /// The newest page, with no filter.
169    pub fn new() -> Self {
170        Self::default()
171    }
172
173    /// Sets the cursor to start from.
174    pub fn before(mut self, before: impl Into<String>) -> Self {
175        self.before = Some(before.into());
176        self
177    }
178
179    /// Prefixo da placa; traço, espaço e caixa não importam.
180    pub fn plate(mut self, plate: impl Into<String>) -> Self {
181        self.plate = Some(plate.into());
182        self
183    }
184
185    /// ID da câmera.
186    pub fn camera(mut self, camera: impl Into<String>) -> Self {
187        self.camera = Some(camera.into());
188        self
189    }
190
191    /// Origem da leitura; outro valor é ignorado.
192    pub fn source(mut self, source: impl Into<String>) -> Self {
193        self.source = Some(source.into());
194        self
195    }
196
197    fn query(&self) -> Vec<(&'static str, String)> {
198        let mut query = Vec::new();
199        if let Some(value) = &self.before {
200            query.push(("before", value.clone()));
201        }
202        if let Some(value) = &self.plate {
203            query.push(("plate", value.clone()));
204        }
205        if let Some(value) = &self.camera {
206            query.push(("camera", value.clone()));
207        }
208        if let Some(value) = &self.source {
209            query.push(("source", value.clone()));
210        }
211        query
212    }
213}
214
215/// The API's recognitions: `client.recognitions()`.
216///
217/// [`api_key`](Self::api_key) sets the key of the call made through it.
218#[derive(Debug, Clone)]
219pub struct RecognitionsService<'a> {
220    client: &'a Client,
221    options: RequestOptions,
222}
223
224impl<'a> RecognitionsService<'a> {
225    pub(crate) fn new(client: &'a Client) -> Self {
226        Self { client, options: RequestOptions::default() }
227    }
228
229    /// Calls with another workspace's key.
230    pub fn api_key(mut self, key: impl Into<String>) -> Self {
231        self.options.api_key = Some(key.into());
232        self
233    }
234
235    /// Calls with these options.
236    pub fn options(mut self, options: RequestOptions) -> Self {
237        self.options = options;
238        self
239    }
240
241    /// Imagem de uma leitura.
242    pub fn image(&self, id: &str) -> Result<DetectionImageLink, Error> {
243        self.client.request("GET", &format!("/api/v1/detections/{}/image", crate::client::escape(id)), &[], None, &self.options)
244    }
245
246    /// Listar as leituras. One page; [`list_iter`](Self::list_iter) walks every page.
247    pub fn list(&self, params: &RecognitionsListParams) -> Result<Page<Recognition>, Error> {
248        self.client.request("GET", "/api/v1/recognitions", &params.query(), None, &self.options)
249    }
250
251    /// Walks every page of recognitions:list, item by item, from `params.before` (the newest page when none).
252    pub fn list_iter(&self, params: RecognitionsListParams) -> impl Iterator<Item = Result<Recognition, Error>> + 'a {
253        let client = self.client;
254        let options = self.options.clone();
255        let path = String::from("/api/v1/recognitions");
256        let first = params.before.clone();
257        iterate(first, move |cursor| {
258            let mut params = params.clone();
259            params.before = cursor.map(str::to_owned);
260            client.request("GET", &path, &params.query(), None, &options)
261        })
262    }
263}
264