use reqwest::Method;
use serde::Serialize;
use serde::de::DeserializeOwned;
use wyrd_spec::error::WyrdError;
pub use wyrd_spec::auth::{AbsoluteUrl, PrincipalId};
pub use wyrd_spec::gateway::*;
pub use wyrd_spec::ids::{
CredentialBindingName, ModelId, ProviderCredentialName, ProviderDeploymentName, ProviderId,
RoleName, SecretBackendName,
};
use crate::WyrdClient;
const CREDENTIALS: &str = "/v1/admin/gateway/provider-credentials";
pub(crate) const DEPLOYMENTS: &str = "/v1/admin/gateway/provider-deployments";
const FALLBACK: &str = "/v1/admin/gateway/fallback-policy";
const GOVERNANCE: &str = "/v1/admin/gateway/governance-policy";
const CAPTURE: &str = "/v1/admin/gateway/capture-policy";
#[derive(Debug, Clone)]
pub struct Gateway {
client: WyrdClient,
}
impl Gateway {
pub async fn connect() -> Result<Self, WyrdError> {
Ok(Self {
client: WyrdClient::connect_ambient().await?,
})
}
pub async fn connect_with(client: &WyrdClient) -> Result<Self, WyrdError> {
Ok(Self {
client: client.connected().await?,
})
}
pub async fn credential(
&self,
name: &ProviderCredentialName,
) -> Result<ProviderCredentialView, WyrdError> {
self.send(Method::GET, &credential_path(name), None::<&()>)
.await
}
pub async fn credentials(&self) -> Result<Vec<ProviderCredentialView>, WyrdError> {
self.send(Method::GET, CREDENTIALS, None::<&()>).await
}
pub async fn put_deployment(
&self,
deployment: &ProviderDeployment,
) -> Result<ProviderDeployment, WyrdError> {
self.send(
Method::PUT,
&deployment_path(&deployment.name),
Some(deployment),
)
.await
}
pub async fn deployment(
&self,
name: &ProviderDeploymentName,
) -> Result<ProviderDeployment, WyrdError> {
self.send(Method::GET, &deployment_path(name), None::<&()>)
.await
}
pub async fn deployments(&self) -> Result<Vec<ProviderDeployment>, WyrdError> {
self.send(Method::GET, DEPLOYMENTS, None::<&()>).await
}
pub async fn delete_deployment(&self, name: &ProviderDeploymentName) -> Result<(), WyrdError> {
self.send(Method::DELETE, &deployment_path(name), None::<&()>)
.await
}
pub async fn put_fallback_policy(
&self,
policy: &GatewayFallbackPolicy,
) -> Result<GatewayFallbackPolicy, WyrdError> {
self.send(Method::PUT, FALLBACK, Some(policy)).await
}
pub async fn fallback_policy(&self) -> Result<GatewayFallbackPolicy, WyrdError> {
self.send(Method::GET, FALLBACK, None::<&()>).await
}
pub async fn delete_fallback_policy(&self) -> Result<(), WyrdError> {
self.send(Method::DELETE, FALLBACK, None::<&()>).await
}
pub async fn put_governance_policy(
&self,
policy: &GatewayGovernancePolicy,
) -> Result<GatewayGovernancePolicy, WyrdError> {
self.send(Method::PUT, GOVERNANCE, Some(policy)).await
}
pub async fn governance_policy(&self) -> Result<GatewayGovernancePolicy, WyrdError> {
self.send(Method::GET, GOVERNANCE, None::<&()>).await
}
pub async fn delete_governance_policy(&self) -> Result<(), WyrdError> {
self.send(Method::DELETE, GOVERNANCE, None::<&()>).await
}
pub async fn put_capture_policy(
&self,
policy: &GatewayCapturePolicyWrite,
) -> Result<GatewayCapturePolicy, WyrdError> {
self.send(Method::PUT, CAPTURE, Some(policy)).await
}
pub async fn capture_policy(&self) -> Result<GatewayCapturePolicy, WyrdError> {
self.send(Method::GET, CAPTURE, None::<&()>).await
}
async fn send<S: Serialize, D: DeserializeOwned>(
&self,
method: Method,
path: &str,
body: Option<&S>,
) -> Result<D, WyrdError> {
self.client.http.request_json(method, path, body).await
}
}
pub(crate) fn credential_path(name: &ProviderCredentialName) -> String {
format!("{CREDENTIALS}/{}", name.as_str())
}
fn deployment_path(name: &ProviderDeploymentName) -> String {
format!("{DEPLOYMENTS}/{}", name.as_str())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn named_routes_use_the_admin_gateway_prefix() {
let credential = ProviderCredentialName::new("primary").expect("valid name");
let deployment = ProviderDeploymentName::new("primary").expect("valid name");
assert_eq!(
credential_path(&credential),
"/v1/admin/gateway/provider-credentials/primary"
);
assert_eq!(
deployment_path(&deployment),
"/v1/admin/gateway/provider-deployments/primary"
);
}
}