// SPDX-FileCopyrightText: Politik im Blick developers
// SPDX-FileCopyrightText: Wolfgang Silbermayr <wolfgang@silbermayr.at>
//
// SPDX-License-Identifier: AGPL-3.0-or-later OR EUPL-1.2

use http_request_derive::HttpRequest;
use http_request_derive_client_reqwest::{ReqwestClient, ReqwestClientError};
use url::Url;

use crate::{AuthenticatedClient, Authorization};

#[derive(Debug, Clone)]
pub struct Client {
    inner: ReqwestClient,
}

impl Client {
    pub fn new(base_url: Url) -> Self {
        let inner = ReqwestClient::new(base_url);
        Self { inner }
    }

    pub fn into_authenticated<A: Authorization + Sync>(
        self,
        authorization: A,
    ) -> AuthenticatedClient<Self, A> {
        AuthenticatedClient::new(self, authorization)
    }
}

#[async_trait::async_trait(?Send)]
impl http_request_derive_client::Client for Client {
    type ClientError = ReqwestClientError;

    async fn execute<R: HttpRequest + Send>(
        &self,
        request: R,
    ) -> Result<R::Response, Self::ClientError> {
        self.inner.execute(request).await
    }
}
