use atrium_xrpc::HttpClient;
use reqwest::Client;

pub struct DefaultHttpClient {
    client: Client,
}

impl HttpClient for DefaultHttpClient {
    async fn send_http(
        &self,
        request: atrium_xrpc::http::Request<Vec<u8>>,
    ) -> core::result::Result<
        atrium_xrpc::http::Response<Vec<u8>>,
        Box<dyn std::error::Error + Send + Sync + 'static>,
    > {
        let response = self.client.execute(request.try_into()?).await?;
        let mut builder = atrium_xrpc::http::Response::builder().status(response.status());
        for (k, v) in response.headers() {
            builder = builder.header(k, v);
        }
        builder.body(response.bytes().await?.to_vec()).map_err(Into::into)
    }
}

impl Default for DefaultHttpClient {
    fn default() -> Self {
        Self { client: reqwest::Client::new() }
    }
}
