use crate::client::{Client, Data, RequestOptions};
use crate::error::Error;
use crate::models::*;
use crate::multipart::Form;
use crate::pagination::{iterate, Page};
impl Client {
pub fn cameras(&self) -> CamerasService<'_> {
CamerasService::new(self)
}
pub fn photos(&self) -> PhotosService<'_> {
PhotosService::new(self)
}
pub fn recognitions(&self) -> RecognitionsService<'_> {
RecognitionsService::new(self)
}
}
#[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() }
}
pub fn api_key(mut self, key: impl Into<String>) -> Self {
self.options.api_key = Some(key.into());
self
}
pub fn options(mut self, options: RequestOptions) -> Self {
self.options = options;
self
}
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 {
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 {
fn form(&self) -> Form<'_> {
let mut form = Form::new();
form.file("image", &self.image);
form
}
}
impl PhotosRecognizeParams {
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
}
}
#[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() }
}
pub fn api_key(mut self, key: impl Into<String>) -> Self {
self.options.api_key = Some(key.into());
self
}
pub fn options(mut self, options: RequestOptions) -> Self {
self.options = options;
self
}
pub fn detect(&self, params: &PhotosDetectParams) -> Result<PhotoDetection, Error> {
self.client.request("POST", "/api/v1/detect", &[], Some(params.form()), &self.options)
}
pub fn ocr(&self, params: &PhotosOCRParams) -> Result<PlateReading, Error> {
self.client.request("POST", "/api/v1/ocr", &[], Some(params.form()), &self.options)
}
pub fn recognize(&self, params: &PhotosRecognizeParams) -> Result<PhotoRecognition, Error> {
self.client.request("POST", "/api/v1/recognize", &[], Some(params.form()), &self.options)
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct RecognitionsListParams {
pub before: Option<String>,
pub plate: Option<String>,
pub camera: Option<String>,
pub source: Option<String>,
}
impl RecognitionsListParams {
pub fn new() -> Self {
Self::default()
}
pub fn before(mut self, before: impl Into<String>) -> Self {
self.before = Some(before.into());
self
}
pub fn plate(mut self, plate: impl Into<String>) -> Self {
self.plate = Some(plate.into());
self
}
pub fn camera(mut self, camera: impl Into<String>) -> Self {
self.camera = Some(camera.into());
self
}
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
}
}
#[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() }
}
pub fn api_key(mut self, key: impl Into<String>) -> Self {
self.options.api_key = Some(key.into());
self
}
pub fn options(mut self, options: RequestOptions) -> Self {
self.options = options;
self
}
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)
}
pub fn list(&self, params: &RecognitionsListParams) -> Result<Page<Recognition>, Error> {
self.client.request("GET", "/api/v1/recognitions", ¶ms.query(), None, &self.options)
}
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, ¶ms.query(), None, &options)
})
}
}