use reqwest::{Client, Url};
use serde::de::DeserializeOwned;
use crate::error::Result;
#[derive(Debug, Clone)]
pub struct AzureSearchClient {
endpoint: Url,
api_version: String,
api_key: String,
http_client: Client,
}
impl AzureSearchClient {
pub fn new(
endpoint: impl Into<String>,
api_key: impl Into<String>,
api_version: impl Into<String>,
http_client: Option<Client>,
) -> Result<Self> {
let endpoint = Url::parse(&endpoint.into())?;
Ok(Self {
endpoint,
api_version: api_version.into(),
api_key: api_key.into(),
http_client: http_client.unwrap_or_else(Client::new),
})
}
pub async fn send_request<T: DeserializeOwned, B: serde::Serialize>(
&self,
method: reqwest::Method,
path: &str,
body: Option<&B>,
) -> Result<T> {
let url = self.endpoint.join(path)?;
let mut request = self
.http_client
.request(method, url)
.header("api-key", &self.api_key)
.header("Content-Type", "application/json")
.query(&[("api-version", &self.api_version)]);
if let Some(body) = body {
request = request.json(body);
}
let response = request.send().await?;
let status = response.status();
let body = response.text().await?;
if !status.is_success() {
return Err(crate::error::Error::RequestFailed { status, body });
}
serde_json::from_str(&body).map_err(Into::into)
}
}