tin-nacos-wrapper 0.1.0

A Rust library for Nacos service discovery and configuration management
Documentation
use reqwest::{Client, Error, Response};
use std::collections::HashMap;

pub struct HttpClient {
    client: Client,
}

impl Default for HttpClient {
    fn default() -> Self {
        Self::new()
    }
}

impl HttpClient {
    pub fn new() -> Self {
        HttpClient {
            client: Client::new(),
        }
    }

    pub async fn get(
        &self,
        url: &str,
        headers: Option<HashMap<String, String>>,
    ) -> Result<Response, Error> {
        let mut req = self.client.get(url);
        if let Some(hdrs) = headers {
            for (k, v) in hdrs {
                req = req.header(&k, &v);
            }
        }
        req.send().await
    }

    pub async fn post<T: serde::Serialize + ?Sized>(
        &self,
        url: &str,
        body: &T,
        headers: Option<HashMap<String, String>>,
    ) -> Result<Response, Error> {
        let mut req = self.client.post(url).json(body);
        if let Some(hdrs) = headers {
            for (k, v) in hdrs {
                req = req.header(&k, &v);
            }
        }
        req.send().await
    }
}