nexgenomics 0.2.7

The official Rust crate for NexGenomics
Documentation

// src/ping.rs

use crate::utils;
use reqwest::{Client,Error};
use reqwest::header::{AUTHORIZATION, HeaderValue};

use serde::Deserialize;


#[derive(Debug,Deserialize)]
#[allow(dead_code)]
pub struct Ping {
    api: String,
    host: String,
}

async fn inner_ping() -> Result<Ping, Error> {
    let auth_token = utils::get_api_auth_token().unwrap();
    let url_stem = utils::get_api_url_stem().unwrap();

    let token = format!("Bearer {}", auth_token);
    let url = format!("{}/api/v0/ping",url_stem);
    //println!("{}, {}", token, url);

    let client = Client::new();
    let response = client
        .get(url)
        .header (AUTHORIZATION, HeaderValue::from_str(&token).unwrap())
        .send()
        .await?
        .error_for_status()?;

    let p = response.json::<Ping>().await?;
    Ok(p)
    
}

#[cfg(feature = "async")]
pub async fn ping() -> Result<Ping,Error> {
    inner_ping().await
}


#[cfg(feature = "blocking")]
pub fn ping() -> Result<Ping,Error> {
    let rt = tokio::runtime::Runtime::new()
        .expect("Failed to create Tokio runtime");
    rt.block_on(inner_ping())
}



//////////// Unit tests:



#[tokio::test]
#[cfg(feature="async")]
async fn test_ping() {
    println!("Async ping test");
    let r = ping().await.unwrap();
    assert_eq! (r.api, "v0");
    assert_eq! (r.host, "nexgenomics.ai");

}

#[test]
#[cfg(feature="blocking")]
fn test_ping_blocking() {
    println!("Blocking ping test");
    let r = ping().unwrap();
    assert_eq! (r.api, "v0");
    assert_eq! (r.host, "nexgenomics.ai");
}