google-cloud-service-flows 0.1.0

Google Cloud service integration for flows.network
Documentation
use http_req::{
    request::{Method, Request},
    uri::Uri,
};

/// Detect text for the given image.
///
/// `image_base64` is the base64 encoded image content.
///
/// Return the detected text.
///
/// Example:
///
/// Take a picture from Internet, such as
/// "https://bullzeyedesign.com/wp-content/uploads/famous_textbased_logos.jpg".
/// If we pass the base64 encoded content of this image,
/// then the result should be:
/// Ok("Coca-Cola DISNEY Google\nFedEx ebay Kleenex")
/// If the content can't be recognized as an image by Cloud Vision,
/// then the result would be something like:
/// Err("Error: 13 INTERNAL: Request message serialization failure: invalid encoding")
///
pub async fn text_detection(image_base64: String) -> Result<String, String> {
    let flows_user = unsafe { crate::_get_flows_user() };

    let flow_id = unsafe { crate::_get_flow_id() };

    let mut writer = Vec::new();
    let uri = format!(
        "{}/{}/{}/cloud-vision/text-detection",
        crate::GOOGLE_CLOUD_SERVICE_API_PREFIX.as_str(),
        flows_user,
        flow_id,
    );

    let uri = Uri::try_from(uri.as_str()).unwrap();
    let body = image_base64.as_bytes();
    match Request::new(&uri)
        .method(Method::POST)
        .header("Content-Type", "text/plain")
        .header("Content-Length", &body.len())
        .body(body)
        .send(&mut writer)
    {
        Ok(res) => match res.status_code().is_success() {
            true => Ok(String::from_utf8_lossy(&writer).into_owned()),
            false => Err(String::from_utf8_lossy(&writer).into_owned()),
        },
        Err(e) => {
            return Err(e.to_string());
        }
    }
}