alpr-sdk 0.1.1

Alpr.sh's SDK for Rust: license plate recognition from photos, cameras, readings and webhooks.
Documentation
// Code generated by sdk/generator. DO NOT EDIT.

use crate::client::{Client, Data, RequestOptions};
use crate::error::Error;
use crate::models::*;
use crate::multipart::Form;
use crate::pagination::{iterate, Page};

impl Client {
    /// The API's cameras.
    pub fn cameras(&self) -> CamerasService<'_> {
        CamerasService::new(self)
    }

    /// The API's photos.
    pub fn photos(&self) -> PhotosService<'_> {
        PhotosService::new(self)
    }

    /// The API's recognitions.
    pub fn recognitions(&self) -> RecognitionsService<'_> {
        RecognitionsService::new(self)
    }
}


/// The API's cameras: `client.cameras()`.
///
/// [`api_key`](Self::api_key) sets the key of the call made through it.
#[derive(Debug, Clone)]
pub struct CamerasService<'a> {
    client: &'a Client,
    options: RequestOptions,
}

impl<'a> CamerasService<'a> {
    pub(crate) fn new(client: &'a Client) -> Self {
        Self { client, options: RequestOptions::default() }
    }

    /// Calls with another workspace's key.
    pub fn api_key(mut self, key: impl Into<String>) -> Self {
        self.options.api_key = Some(key.into());
        self
    }

    /// Calls with these options.
    pub fn options(mut self, options: RequestOptions) -> Self {
        self.options = options;
        self
    }

    /// Listar as câmeras.
    pub fn list(&self) -> Result<Vec<Camera>, Error> {
        let out: Data<Vec<Camera>> = self.client.request("GET", "/api/v1/cameras", &[], None, &self.options)?;
        Ok(out.data)
    }
}

impl PhotosDetectParams {
    /// The body as the `multipart/form-data` it is sent as: the photo is a
    /// file part, every other field a text part, and a field left out is not
    /// sent.
    fn form(&self) -> Form<'_> {
        let mut form = Form::new();
        form.file("image", &self.image);
        if let Some(value) = &self.image_mode {
            form.text("image_mode", value.to_string());
        }
        if let Some(value) = &self.lpr_confidence {
            form.text("lpr_confidence", value.to_string());
        }
        if let Some(value) = &self.min_ocr_confidence {
            form.text("min_ocr_confidence", value.to_string());
        }
        form
    }
}

impl PhotosOCRParams {
    /// The body as the `multipart/form-data` it is sent as: the photo is a
    /// file part, every other field a text part, and a field left out is not
    /// sent.
    fn form(&self) -> Form<'_> {
        let mut form = Form::new();
        form.file("image", &self.image);
        form
    }
}

impl PhotosRecognizeParams {
    /// The body as the `multipart/form-data` it is sent as: the photo is a
    /// file part, every other field a text part, and a field left out is not
    /// sent.
    fn form(&self) -> Form<'_> {
        let mut form = Form::new();
        form.file("image", &self.image);
        if let Some(value) = &self.image_mode {
            form.text("image_mode", value.to_string());
        }
        if let Some(value) = &self.lpr_confidence {
            form.text("lpr_confidence", value.to_string());
        }
        if let Some(value) = &self.min_ocr_confidence {
            form.text("min_ocr_confidence", value.to_string());
        }
        form
    }
}

/// The API's photos: `client.photos()`.
///
/// [`api_key`](Self::api_key) sets the key of the call made through it.
#[derive(Debug, Clone)]
pub struct PhotosService<'a> {
    client: &'a Client,
    options: RequestOptions,
}

impl<'a> PhotosService<'a> {
    pub(crate) fn new(client: &'a Client) -> Self {
        Self { client, options: RequestOptions::default() }
    }

    /// Calls with another workspace's key.
    pub fn api_key(mut self, key: impl Into<String>) -> Self {
        self.options.api_key = Some(key.into());
        self
    }

    /// Calls with these options.
    pub fn options(mut self, options: RequestOptions) -> Self {
        self.options = options;
        self
    }

    /// Localizar as placas de uma foto.
    pub fn detect(&self, params: &PhotosDetectParams) -> Result<PhotoDetection, Error> {
        self.client.request("POST", "/api/v1/detect", &[], Some(params.form()), &self.options)
    }

    /// Ler uma placa já recortada.
    pub fn ocr(&self, params: &PhotosOCRParams) -> Result<PlateReading, Error> {
        self.client.request("POST", "/api/v1/ocr", &[], Some(params.form()), &self.options)
    }

    /// Ler as placas de uma foto.
    pub fn recognize(&self, params: &PhotosRecognizeParams) -> Result<PhotoRecognition, Error> {
        self.client.request("POST", "/api/v1/recognize", &[], Some(params.form()), &self.options)
    }
}

/// The filters of recognitions:list, and the cursor to start from.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct RecognitionsListParams {
    /// The cursor the previous page answered as `next`; none asks for the newest page.
    pub before: Option<String>,
    /// Prefixo da placa; traço, espaço e caixa não importam.
    pub plate: Option<String>,
    /// ID da câmera.
    pub camera: Option<String>,
    /// Origem da leitura; outro valor é ignorado.
    pub source: Option<String>,
}

impl RecognitionsListParams {
    /// The newest page, with no filter.
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets the cursor to start from.
    pub fn before(mut self, before: impl Into<String>) -> Self {
        self.before = Some(before.into());
        self
    }

    /// Prefixo da placa; traço, espaço e caixa não importam.
    pub fn plate(mut self, plate: impl Into<String>) -> Self {
        self.plate = Some(plate.into());
        self
    }

    /// ID da câmera.
    pub fn camera(mut self, camera: impl Into<String>) -> Self {
        self.camera = Some(camera.into());
        self
    }

    /// Origem da leitura; outro valor é ignorado.
    pub fn source(mut self, source: impl Into<String>) -> Self {
        self.source = Some(source.into());
        self
    }

    fn query(&self) -> Vec<(&'static str, String)> {
        let mut query = Vec::new();
        if let Some(value) = &self.before {
            query.push(("before", value.clone()));
        }
        if let Some(value) = &self.plate {
            query.push(("plate", value.clone()));
        }
        if let Some(value) = &self.camera {
            query.push(("camera", value.clone()));
        }
        if let Some(value) = &self.source {
            query.push(("source", value.clone()));
        }
        query
    }
}

/// The API's recognitions: `client.recognitions()`.
///
/// [`api_key`](Self::api_key) sets the key of the call made through it.
#[derive(Debug, Clone)]
pub struct RecognitionsService<'a> {
    client: &'a Client,
    options: RequestOptions,
}

impl<'a> RecognitionsService<'a> {
    pub(crate) fn new(client: &'a Client) -> Self {
        Self { client, options: RequestOptions::default() }
    }

    /// Calls with another workspace's key.
    pub fn api_key(mut self, key: impl Into<String>) -> Self {
        self.options.api_key = Some(key.into());
        self
    }

    /// Calls with these options.
    pub fn options(mut self, options: RequestOptions) -> Self {
        self.options = options;
        self
    }

    /// Imagem de uma leitura.
    pub fn image(&self, id: &str) -> Result<DetectionImageLink, Error> {
        self.client.request("GET", &format!("/api/v1/detections/{}/image", crate::client::escape(id)), &[], None, &self.options)
    }

    /// Listar as leituras. One page; [`list_iter`](Self::list_iter) walks every page.
    pub fn list(&self, params: &RecognitionsListParams) -> Result<Page<Recognition>, Error> {
        self.client.request("GET", "/api/v1/recognitions", &params.query(), None, &self.options)
    }

    /// Walks every page of recognitions:list, item by item, from `params.before` (the newest page when none).
    pub fn list_iter(&self, params: RecognitionsListParams) -> impl Iterator<Item = Result<Recognition, Error>> + 'a {
        let client = self.client;
        let options = self.options.clone();
        let path = String::from("/api/v1/recognitions");
        let first = params.before.clone();
        iterate(first, move |cursor| {
            let mut params = params.clone();
            params.before = cursor.map(str::to_owned);
            client.request("GET", &path, &params.query(), None, &options)
        })
    }
}