#![doc = "Generated reqwest client with typed errors and streaming bodies."]
use async_trait::async_trait;
use futures::{Stream, TryStreamExt, stream};
use reqwest::Url;
use reqwest_middleware::ClientWithMiddleware;
use spiral_contract::auth::AuthHeader;
use spiral_contract::call::{CallError, ServiceFailure};
use spiral_contract::error::ErrorEnvelope;
use crate::api::apps::AppsApi;
use crate::api::apps::objects::*;
use crate::http::default_http_client;
#[derive(Clone)]
pub struct AppsClient {
http: ClientWithMiddleware,
base: Url,
}
impl std::fmt::Debug for AppsClient {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("AppsClient")
.field("base", &self.base)
.finish_non_exhaustive()
}
}
impl AppsClient {
pub fn new(base: &str) -> Result<Self, CallError<AppsError>> {
let http = default_http_client().map_err(CallError::transport)?;
Self::from_http(base, http)
}
pub fn from_http(
base: &str,
http: impl Into<ClientWithMiddleware>,
) -> Result<Self, CallError<AppsError>> {
Self::from_transport(base, http.into())
}
pub fn from_transport(
base: &str,
http: ClientWithMiddleware,
) -> Result<Self, CallError<AppsError>> {
let base =
Url::parse(base).map_err(|_| CallError::<AppsError>::protocol("invalid base URL"))?;
if !matches!(base.scheme(), "http" | "https")
|| base.host_str().is_none()
|| !base.username().is_empty()
|| base.password().is_some()
|| base.query().is_some()
|| base.fragment().is_some()
{
return Err(CallError::<AppsError>::protocol("invalid base URL"));
}
Ok(Self { http, base })
}
pub fn with_transport(mut self, http: ClientWithMiddleware) -> Self {
self.http = http;
self
}
pub fn with_http(mut self, http: impl Into<ClientWithMiddleware>) -> Self {
self.http = http.into();
self
}
pub async fn approve_installation_http(
&self,
auth_header: AuthHeader,
installation_id: String,
) -> Result<Installation, CallError<AppsError>> {
let mut url = self.base.clone();
{
let mut path = url.path_segments_mut().map_err(|_| {
CallError::<AppsError>::protocol("base URL cannot contain path segments")
})?;
path.pop_if_empty();
path.push("api");
path.push("apps");
path.push("installations");
let segment = installation_id.to_string();
if matches!(segment.as_str(), "." | "..") {
return Err(CallError::<AppsError>::protocol("dot path segment"));
}
path.push(&segment);
path.push("approve");
}
let builder = self.http.post(url).header("accept", "application/json");
let builder = builder.header("authorization", auth_header.as_str());
let response = self.send(builder, "application/json").await?;
let status = response.status().as_u16();
let bytes = bounded_body(response, 8 * 1024 * 1024).await?;
serde_json::from_slice(&bytes)
.map_err(|_| CallError::protocol_response(status, "invalid JSON response"))
}
pub async fn create_application_http(
&self,
auth_header: AuthHeader,
project_id: String,
request: ApplicationConfiguration,
) -> Result<Application, CallError<AppsError>> {
let mut url = self.base.clone();
{
let mut path = url.path_segments_mut().map_err(|_| {
CallError::<AppsError>::protocol("base URL cannot contain path segments")
})?;
path.pop_if_empty();
path.push("api");
path.push("apps");
path.push("projects");
let segment = project_id.to_string();
if matches!(segment.as_str(), "." | "..") {
return Err(CallError::<AppsError>::protocol("dot path segment"));
}
path.push(&segment);
}
let builder = self.http.post(url).header("accept", "application/json");
let builder = builder.header("authorization", auth_header.as_str());
let builder = builder
.header("content-type", "application/json")
.json(&request);
let response = self.send(builder, "application/json").await?;
let status = response.status().as_u16();
let bytes = bounded_body(response, 8 * 1024 * 1024).await?;
serde_json::from_slice(&bytes)
.map_err(|_| CallError::protocol_response(status, "invalid JSON response"))
}
pub async fn decide_consent_http(
&self,
auth_header: AuthHeader,
challenge: String,
request: ConsentDecision,
) -> Result<ConsentRedirect, CallError<AppsError>> {
let mut url = self.base.clone();
{
let mut path = url.path_segments_mut().map_err(|_| {
CallError::<AppsError>::protocol("base URL cannot contain path segments")
})?;
path.pop_if_empty();
path.push("api");
path.push("apps");
path.push("consent");
let segment = challenge.to_string();
if matches!(segment.as_str(), "." | "..") {
return Err(CallError::<AppsError>::protocol("dot path segment"));
}
path.push(&segment);
}
let builder = self.http.post(url).header("accept", "application/json");
let builder = builder.header("authorization", auth_header.as_str());
let builder = builder
.header("content-type", "application/json")
.json(&request);
let response = self.send(builder, "application/json").await?;
let status = response.status().as_u16();
let bytes = bounded_body(response, 8 * 1024 * 1024).await?;
serde_json::from_slice(&bytes)
.map_err(|_| CallError::protocol_response(status, "invalid JSON response"))
}
pub async fn delete_application_http(
&self,
auth_header: AuthHeader,
application_id: String,
) -> Result<Application, CallError<AppsError>> {
let mut url = self.base.clone();
{
let mut path = url.path_segments_mut().map_err(|_| {
CallError::<AppsError>::protocol("base URL cannot contain path segments")
})?;
path.pop_if_empty();
path.push("api");
path.push("apps");
let segment = application_id.to_string();
if matches!(segment.as_str(), "." | "..") {
return Err(CallError::<AppsError>::protocol("dot path segment"));
}
path.push(&segment);
}
let builder = self.http.delete(url).header("accept", "application/json");
let builder = builder.header("authorization", auth_header.as_str());
let response = self.send(builder, "application/json").await?;
let status = response.status().as_u16();
let bytes = bounded_body(response, 8 * 1024 * 1024).await?;
serde_json::from_slice(&bytes)
.map_err(|_| CallError::protocol_response(status, "invalid JSON response"))
}
pub async fn get_application_http(
&self,
auth_header: AuthHeader,
application_id: String,
) -> Result<Application, CallError<AppsError>> {
let mut url = self.base.clone();
{
let mut path = url.path_segments_mut().map_err(|_| {
CallError::<AppsError>::protocol("base URL cannot contain path segments")
})?;
path.pop_if_empty();
path.push("api");
path.push("apps");
let segment = application_id.to_string();
if matches!(segment.as_str(), "." | "..") {
return Err(CallError::<AppsError>::protocol("dot path segment"));
}
path.push(&segment);
}
let builder = self.http.get(url).header("accept", "application/json");
let builder = builder.header("authorization", auth_header.as_str());
let response = self.send(builder, "application/json").await?;
let status = response.status().as_u16();
let bytes = bounded_body(response, 8 * 1024 * 1024).await?;
serde_json::from_slice(&bytes)
.map_err(|_| CallError::protocol_response(status, "invalid JSON response"))
}
pub async fn get_consent_request_http(
&self,
auth_header: AuthHeader,
challenge: String,
) -> Result<ConsentRequest, CallError<AppsError>> {
let mut url = self.base.clone();
{
let mut path = url.path_segments_mut().map_err(|_| {
CallError::<AppsError>::protocol("base URL cannot contain path segments")
})?;
path.pop_if_empty();
path.push("api");
path.push("apps");
path.push("consent");
let segment = challenge.to_string();
if matches!(segment.as_str(), "." | "..") {
return Err(CallError::<AppsError>::protocol("dot path segment"));
}
path.push(&segment);
}
let builder = self.http.get(url).header("accept", "application/json");
let builder = builder.header("authorization", auth_header.as_str());
let response = self.send(builder, "application/json").await?;
let status = response.status().as_u16();
let bytes = bounded_body(response, 8 * 1024 * 1024).await?;
serde_json::from_slice(&bytes)
.map_err(|_| CallError::protocol_response(status, "invalid JSON response"))
}
pub async fn get_current_installation_http(
&self,
auth_header: AuthHeader,
) -> Result<InstallationDetails, CallError<AppsError>> {
let mut url = self.base.clone();
{
let mut path = url.path_segments_mut().map_err(|_| {
CallError::<AppsError>::protocol("base URL cannot contain path segments")
})?;
path.pop_if_empty();
path.push("api");
path.push("apps");
path.push("installation");
}
let builder = self.http.get(url).header("accept", "application/json");
let builder = builder.header("authorization", auth_header.as_str());
let response = self.send(builder, "application/json").await?;
let status = response.status().as_u16();
let bytes = bounded_body(response, 8 * 1024 * 1024).await?;
serde_json::from_slice(&bytes)
.map_err(|_| CallError::protocol_response(status, "invalid JSON response"))
}
pub async fn get_installable_application_http(
&self,
auth_header: AuthHeader,
application_id: String,
) -> Result<InstallableApplication, CallError<AppsError>> {
let mut url = self.base.clone();
{
let mut path = url.path_segments_mut().map_err(|_| {
CallError::<AppsError>::protocol("base URL cannot contain path segments")
})?;
path.pop_if_empty();
path.push("api");
path.push("apps");
path.push("installable");
let segment = application_id.to_string();
if matches!(segment.as_str(), "." | "..") {
return Err(CallError::<AppsError>::protocol("dot path segment"));
}
path.push(&segment);
}
let builder = self.http.get(url).header("accept", "application/json");
let builder = builder.header("authorization", auth_header.as_str());
let response = self.send(builder, "application/json").await?;
let status = response.status().as_u16();
let bytes = bounded_body(response, 8 * 1024 * 1024).await?;
serde_json::from_slice(&bytes)
.map_err(|_| CallError::protocol_response(status, "invalid JSON response"))
}
pub async fn get_installation_http(
&self,
auth_header: AuthHeader,
installation_id: String,
) -> Result<Installation, CallError<AppsError>> {
let mut url = self.base.clone();
{
let mut path = url.path_segments_mut().map_err(|_| {
CallError::<AppsError>::protocol("base URL cannot contain path segments")
})?;
path.pop_if_empty();
path.push("api");
path.push("apps");
path.push("installations");
let segment = installation_id.to_string();
if matches!(segment.as_str(), "." | "..") {
return Err(CallError::<AppsError>::protocol("dot path segment"));
}
path.push(&segment);
}
let builder = self.http.get(url).header("accept", "application/json");
let builder = builder.header("authorization", auth_header.as_str());
let response = self.send(builder, "application/json").await?;
let status = response.status().as_u16();
let bytes = bounded_body(response, 8 * 1024 * 1024).await?;
serde_json::from_slice(&bytes)
.map_err(|_| CallError::protocol_response(status, "invalid JSON response"))
}
pub async fn get_organization_app_access_http(
&self,
auth_header: AuthHeader,
) -> Result<OrganizationAppAccess, CallError<AppsError>> {
let mut url = self.base.clone();
{
let mut path = url.path_segments_mut().map_err(|_| {
CallError::<AppsError>::protocol("base URL cannot contain path segments")
})?;
path.pop_if_empty();
path.push("api");
path.push("apps");
path.push("organization-access");
}
let builder = self.http.get(url).header("accept", "application/json");
let builder = builder.header("authorization", auth_header.as_str());
let response = self.send(builder, "application/json").await?;
let status = response.status().as_u16();
let bytes = bounded_body(response, 8 * 1024 * 1024).await?;
serde_json::from_slice(&bytes)
.map_err(|_| CallError::protocol_response(status, "invalid JSON response"))
}
pub async fn get_self_http(
&self,
auth_header: AuthHeader,
) -> Result<Application, CallError<AppsError>> {
let mut url = self.base.clone();
{
let mut path = url.path_segments_mut().map_err(|_| {
CallError::<AppsError>::protocol("base URL cannot contain path segments")
})?;
path.pop_if_empty();
path.push("api");
path.push("apps");
path.push("self");
}
let builder = self.http.get(url).header("accept", "application/json");
let builder = builder.header("authorization", auth_header.as_str());
let response = self.send(builder, "application/json").await?;
let status = response.status().as_u16();
let bytes = bounded_body(response, 8 * 1024 * 1024).await?;
serde_json::from_slice(&bytes)
.map_err(|_| CallError::protocol_response(status, "invalid JSON response"))
}
pub async fn get_self_installation_http(
&self,
auth_header: AuthHeader,
installation_id: String,
) -> Result<InstallationDetails, CallError<AppsError>> {
let mut url = self.base.clone();
{
let mut path = url.path_segments_mut().map_err(|_| {
CallError::<AppsError>::protocol("base URL cannot contain path segments")
})?;
path.pop_if_empty();
path.push("api");
path.push("apps");
path.push("self");
path.push("installations");
let segment = installation_id.to_string();
if matches!(segment.as_str(), "." | "..") {
return Err(CallError::<AppsError>::protocol("dot path segment"));
}
path.push(&segment);
}
let builder = self.http.get(url).header("accept", "application/json");
let builder = builder.header("authorization", auth_header.as_str());
let response = self.send(builder, "application/json").await?;
let status = response.status().as_u16();
let bytes = bounded_body(response, 8 * 1024 * 1024).await?;
serde_json::from_slice(&bytes)
.map_err(|_| CallError::protocol_response(status, "invalid JSON response"))
}
pub async fn get_user_installation_http(
&self,
auth_header: AuthHeader,
) -> Result<UserInstallation, CallError<AppsError>> {
let mut url = self.base.clone();
{
let mut path = url.path_segments_mut().map_err(|_| {
CallError::<AppsError>::protocol("base URL cannot contain path segments")
})?;
path.pop_if_empty();
path.push("api");
path.push("apps");
path.push("user");
path.push("installation");
}
let builder = self.http.get(url).header("accept", "application/json");
let builder = builder.header("authorization", auth_header.as_str());
let response = self.send(builder, "application/json").await?;
let status = response.status().as_u16();
let bytes = bounded_body(response, 8 * 1024 * 1024).await?;
serde_json::from_slice(&bytes)
.map_err(|_| CallError::protocol_response(status, "invalid JSON response"))
}
pub async fn list_application_installations_http(
&self,
auth_header: AuthHeader,
application_id: String,
request: ListApplicationsRequest,
) -> Result<InstallationPage, CallError<AppsError>> {
let mut url = self.base.clone();
{
let mut path = url.path_segments_mut().map_err(|_| {
CallError::<AppsError>::protocol("base URL cannot contain path segments")
})?;
path.pop_if_empty();
path.push("api");
path.push("apps");
let segment = application_id.to_string();
if matches!(segment.as_str(), "." | "..") {
return Err(CallError::<AppsError>::protocol("dot path segment"));
}
path.push(&segment);
path.push("installations");
}
let builder = self.http.post(url).header("accept", "application/json");
let builder = builder.header("authorization", auth_header.as_str());
let builder = builder
.header("content-type", "application/json")
.json(&request);
let response = self.send(builder, "application/json").await?;
let status = response.status().as_u16();
let bytes = bounded_body(response, 8 * 1024 * 1024).await?;
serde_json::from_slice(&bytes)
.map_err(|_| CallError::protocol_response(status, "invalid JSON response"))
}
pub fn list_application_installations_pages<'a>(
&'a self,
auth_header: &'a AuthHeader,
application_id: String,
request: ListApplicationsRequest,
) -> impl Stream<Item = Result<InstallationPage, CallError<AppsError>>> + 'a {
stream::try_unfold(Some(request), move |request| {
#[allow(
clippy::clone_on_copy,
reason = "Hosts may implement Copy for generated aliases; pagination also supports Clone aliases."
)]
let application_id = application_id.clone();
async move {
let Some(mut request) = request else {
return Ok(None);
};
let page = self
.list_application_installations_http(
auth_header.clone(),
application_id,
request.clone(),
)
.await?;
let next = page.next_page_token.clone();
if next.as_ref().is_some_and(|token| {
token.is_empty() || Some(token) == request.page_token.as_ref()
}) {
return Err(CallError::<AppsError>::protocol("invalid pagination token"));
}
request.page_token = next;
let next_request = request.page_token.is_some().then_some(request);
Ok(Some((page, next_request)))
}
})
}
pub fn list_application_installations_items<'a>(
&'a self,
auth_header: &'a AuthHeader,
application_id: String,
request: ListApplicationsRequest,
) -> impl Stream<Item = Result<Installation, CallError<AppsError>>> + 'a {
self.list_application_installations_pages(auth_header, application_id, request)
.map_ok(|page| {
stream::iter(
page.installations
.into_iter()
.map(Ok::<_, CallError<AppsError>>),
)
})
.try_flatten()
}
pub async fn list_applications_http(
&self,
auth_header: AuthHeader,
project_id: String,
request: ListApplicationsRequest,
) -> Result<ApplicationPage, CallError<AppsError>> {
let mut url = self.base.clone();
{
let mut path = url.path_segments_mut().map_err(|_| {
CallError::<AppsError>::protocol("base URL cannot contain path segments")
})?;
path.pop_if_empty();
path.push("api");
path.push("apps");
path.push("projects");
let segment = project_id.to_string();
if matches!(segment.as_str(), "." | "..") {
return Err(CallError::<AppsError>::protocol("dot path segment"));
}
path.push(&segment);
path.push("list");
}
let builder = self.http.post(url).header("accept", "application/json");
let builder = builder.header("authorization", auth_header.as_str());
let builder = builder
.header("content-type", "application/json")
.json(&request);
let response = self.send(builder, "application/json").await?;
let status = response.status().as_u16();
let bytes = bounded_body(response, 8 * 1024 * 1024).await?;
serde_json::from_slice(&bytes)
.map_err(|_| CallError::protocol_response(status, "invalid JSON response"))
}
pub fn list_applications_pages<'a>(
&'a self,
auth_header: &'a AuthHeader,
project_id: String,
request: ListApplicationsRequest,
) -> impl Stream<Item = Result<ApplicationPage, CallError<AppsError>>> + 'a {
stream::try_unfold(Some(request), move |request| {
#[allow(
clippy::clone_on_copy,
reason = "Hosts may implement Copy for generated aliases; pagination also supports Clone aliases."
)]
let project_id = project_id.clone();
async move {
let Some(mut request) = request else {
return Ok(None);
};
let page = self
.list_applications_http(auth_header.clone(), project_id, request.clone())
.await?;
let next = page.next_page_token.clone();
if next.as_ref().is_some_and(|token| {
token.is_empty() || Some(token) == request.page_token.as_ref()
}) {
return Err(CallError::<AppsError>::protocol("invalid pagination token"));
}
request.page_token = next;
let next_request = request.page_token.is_some().then_some(request);
Ok(Some((page, next_request)))
}
})
}
pub fn list_applications_items<'a>(
&'a self,
auth_header: &'a AuthHeader,
project_id: String,
request: ListApplicationsRequest,
) -> impl Stream<Item = Result<Application, CallError<AppsError>>> + 'a {
self.list_applications_pages(auth_header, project_id, request)
.map_ok(|page| {
stream::iter(
page.applications
.into_iter()
.map(Ok::<_, CallError<AppsError>>),
)
})
.try_flatten()
}
pub async fn list_installation_projects_http(
&self,
auth_header: AuthHeader,
request: ListApplicationsRequest,
) -> Result<InstallationProjectPage, CallError<AppsError>> {
let mut url = self.base.clone();
{
let mut path = url.path_segments_mut().map_err(|_| {
CallError::<AppsError>::protocol("base URL cannot contain path segments")
})?;
path.pop_if_empty();
path.push("api");
path.push("apps");
path.push("installation");
path.push("projects");
path.push("list");
}
let builder = self.http.post(url).header("accept", "application/json");
let builder = builder.header("authorization", auth_header.as_str());
let builder = builder
.header("content-type", "application/json")
.json(&request);
let response = self.send(builder, "application/json").await?;
let status = response.status().as_u16();
let bytes = bounded_body(response, 8 * 1024 * 1024).await?;
serde_json::from_slice(&bytes)
.map_err(|_| CallError::protocol_response(status, "invalid JSON response"))
}
pub fn list_installation_projects_pages<'a>(
&'a self,
auth_header: &'a AuthHeader,
request: ListApplicationsRequest,
) -> impl Stream<Item = Result<InstallationProjectPage, CallError<AppsError>>> + 'a {
stream::try_unfold(Some(request), move |request| async move {
let Some(mut request) = request else {
return Ok(None);
};
let page = self
.list_installation_projects_http(auth_header.clone(), request.clone())
.await?;
let next = page.next_page_token.clone();
if next
.as_ref()
.is_some_and(|token| token.is_empty() || Some(token) == request.page_token.as_ref())
{
return Err(CallError::<AppsError>::protocol("invalid pagination token"));
}
request.page_token = next;
let next_request = request.page_token.is_some().then_some(request);
Ok(Some((page, next_request)))
})
}
pub fn list_installation_projects_items<'a>(
&'a self,
auth_header: &'a AuthHeader,
request: ListApplicationsRequest,
) -> impl Stream<Item = Result<InstallationProject, CallError<AppsError>>> + 'a {
self.list_installation_projects_pages(auth_header, request)
.map_ok(|page| {
stream::iter(page.projects.into_iter().map(Ok::<_, CallError<AppsError>>))
})
.try_flatten()
}
pub async fn list_installations_http(
&self,
auth_header: AuthHeader,
request: ListApplicationsRequest,
) -> Result<InstallationPage, CallError<AppsError>> {
let mut url = self.base.clone();
{
let mut path = url.path_segments_mut().map_err(|_| {
CallError::<AppsError>::protocol("base URL cannot contain path segments")
})?;
path.pop_if_empty();
path.push("api");
path.push("apps");
path.push("installations");
path.push("list");
}
let builder = self.http.post(url).header("accept", "application/json");
let builder = builder.header("authorization", auth_header.as_str());
let builder = builder
.header("content-type", "application/json")
.json(&request);
let response = self.send(builder, "application/json").await?;
let status = response.status().as_u16();
let bytes = bounded_body(response, 8 * 1024 * 1024).await?;
serde_json::from_slice(&bytes)
.map_err(|_| CallError::protocol_response(status, "invalid JSON response"))
}
pub fn list_installations_pages<'a>(
&'a self,
auth_header: &'a AuthHeader,
request: ListApplicationsRequest,
) -> impl Stream<Item = Result<InstallationPage, CallError<AppsError>>> + 'a {
stream::try_unfold(Some(request), move |request| async move {
let Some(mut request) = request else {
return Ok(None);
};
let page = self
.list_installations_http(auth_header.clone(), request.clone())
.await?;
let next = page.next_page_token.clone();
if next
.as_ref()
.is_some_and(|token| token.is_empty() || Some(token) == request.page_token.as_ref())
{
return Err(CallError::<AppsError>::protocol("invalid pagination token"));
}
request.page_token = next;
let next_request = request.page_token.is_some().then_some(request);
Ok(Some((page, next_request)))
})
}
pub fn list_installations_items<'a>(
&'a self,
auth_header: &'a AuthHeader,
request: ListApplicationsRequest,
) -> impl Stream<Item = Result<Installation, CallError<AppsError>>> + 'a {
self.list_installations_pages(auth_header, request)
.map_ok(|page| {
stream::iter(
page.installations
.into_iter()
.map(Ok::<_, CallError<AppsError>>),
)
})
.try_flatten()
}
pub async fn list_scopes_http(
&self,
auth_header: AuthHeader,
) -> Result<ApplicationScopes, CallError<AppsError>> {
let mut url = self.base.clone();
{
let mut path = url.path_segments_mut().map_err(|_| {
CallError::<AppsError>::protocol("base URL cannot contain path segments")
})?;
path.pop_if_empty();
path.push("api");
path.push("apps");
path.push("scopes");
}
let builder = self.http.get(url).header("accept", "application/json");
let builder = builder.header("authorization", auth_header.as_str());
let response = self.send(builder, "application/json").await?;
let status = response.status().as_u16();
let bytes = bounded_body(response, 8 * 1024 * 1024).await?;
serde_json::from_slice(&bytes)
.map_err(|_| CallError::protocol_response(status, "invalid JSON response"))
}
pub async fn list_self_installations_http(
&self,
auth_header: AuthHeader,
request: ListApplicationsRequest,
) -> Result<InstallationPage, CallError<AppsError>> {
let mut url = self.base.clone();
{
let mut path = url.path_segments_mut().map_err(|_| {
CallError::<AppsError>::protocol("base URL cannot contain path segments")
})?;
path.pop_if_empty();
path.push("api");
path.push("apps");
path.push("self");
path.push("installations");
path.push("list");
}
let builder = self.http.post(url).header("accept", "application/json");
let builder = builder.header("authorization", auth_header.as_str());
let builder = builder
.header("content-type", "application/json")
.json(&request);
let response = self.send(builder, "application/json").await?;
let status = response.status().as_u16();
let bytes = bounded_body(response, 8 * 1024 * 1024).await?;
serde_json::from_slice(&bytes)
.map_err(|_| CallError::protocol_response(status, "invalid JSON response"))
}
pub fn list_self_installations_pages<'a>(
&'a self,
auth_header: &'a AuthHeader,
request: ListApplicationsRequest,
) -> impl Stream<Item = Result<InstallationPage, CallError<AppsError>>> + 'a {
stream::try_unfold(Some(request), move |request| async move {
let Some(mut request) = request else {
return Ok(None);
};
let page = self
.list_self_installations_http(auth_header.clone(), request.clone())
.await?;
let next = page.next_page_token.clone();
if next
.as_ref()
.is_some_and(|token| token.is_empty() || Some(token) == request.page_token.as_ref())
{
return Err(CallError::<AppsError>::protocol("invalid pagination token"));
}
request.page_token = next;
let next_request = request.page_token.is_some().then_some(request);
Ok(Some((page, next_request)))
})
}
pub fn list_self_installations_items<'a>(
&'a self,
auth_header: &'a AuthHeader,
request: ListApplicationsRequest,
) -> impl Stream<Item = Result<Installation, CallError<AppsError>>> + 'a {
self.list_self_installations_pages(auth_header, request)
.map_ok(|page| {
stream::iter(
page.installations
.into_iter()
.map(Ok::<_, CallError<AppsError>>),
)
})
.try_flatten()
}
pub async fn list_user_installation_projects_http(
&self,
auth_header: AuthHeader,
installation_id: String,
request: ListApplicationsRequest,
) -> Result<InstallationProjectPage, CallError<AppsError>> {
let mut url = self.base.clone();
{
let mut path = url.path_segments_mut().map_err(|_| {
CallError::<AppsError>::protocol("base URL cannot contain path segments")
})?;
path.pop_if_empty();
path.push("api");
path.push("apps");
path.push("user");
path.push("installations");
let segment = installation_id.to_string();
if matches!(segment.as_str(), "." | "..") {
return Err(CallError::<AppsError>::protocol("dot path segment"));
}
path.push(&segment);
path.push("projects");
path.push("list");
}
let builder = self.http.post(url).header("accept", "application/json");
let builder = builder.header("authorization", auth_header.as_str());
let builder = builder
.header("content-type", "application/json")
.json(&request);
let response = self.send(builder, "application/json").await?;
let status = response.status().as_u16();
let bytes = bounded_body(response, 8 * 1024 * 1024).await?;
serde_json::from_slice(&bytes)
.map_err(|_| CallError::protocol_response(status, "invalid JSON response"))
}
pub fn list_user_installation_projects_pages<'a>(
&'a self,
auth_header: &'a AuthHeader,
installation_id: String,
request: ListApplicationsRequest,
) -> impl Stream<Item = Result<InstallationProjectPage, CallError<AppsError>>> + 'a {
stream::try_unfold(Some(request), move |request| {
#[allow(
clippy::clone_on_copy,
reason = "Hosts may implement Copy for generated aliases; pagination also supports Clone aliases."
)]
let installation_id = installation_id.clone();
async move {
let Some(mut request) = request else {
return Ok(None);
};
let page = self
.list_user_installation_projects_http(
auth_header.clone(),
installation_id,
request.clone(),
)
.await?;
let next = page.next_page_token.clone();
if next.as_ref().is_some_and(|token| {
token.is_empty() || Some(token) == request.page_token.as_ref()
}) {
return Err(CallError::<AppsError>::protocol("invalid pagination token"));
}
request.page_token = next;
let next_request = request.page_token.is_some().then_some(request);
Ok(Some((page, next_request)))
}
})
}
pub fn list_user_installation_projects_items<'a>(
&'a self,
auth_header: &'a AuthHeader,
installation_id: String,
request: ListApplicationsRequest,
) -> impl Stream<Item = Result<InstallationProject, CallError<AppsError>>> + 'a {
self.list_user_installation_projects_pages(auth_header, installation_id, request)
.map_ok(|page| {
stream::iter(page.projects.into_iter().map(Ok::<_, CallError<AppsError>>))
})
.try_flatten()
}
pub async fn put_application_http(
&self,
auth_header: AuthHeader,
application_id: String,
request: ApplicationConfiguration,
) -> Result<Application, CallError<AppsError>> {
let mut url = self.base.clone();
{
let mut path = url.path_segments_mut().map_err(|_| {
CallError::<AppsError>::protocol("base URL cannot contain path segments")
})?;
path.pop_if_empty();
path.push("api");
path.push("apps");
let segment = application_id.to_string();
if matches!(segment.as_str(), "." | "..") {
return Err(CallError::<AppsError>::protocol("dot path segment"));
}
path.push(&segment);
}
let builder = self.http.put(url).header("accept", "application/json");
let builder = builder.header("authorization", auth_header.as_str());
let builder = builder
.header("content-type", "application/json")
.json(&request);
let response = self.send(builder, "application/json").await?;
let status = response.status().as_u16();
let bytes = bounded_body(response, 8 * 1024 * 1024).await?;
serde_json::from_slice(&bytes)
.map_err(|_| CallError::protocol_response(status, "invalid JSON response"))
}
pub async fn put_organization_app_access_http(
&self,
auth_header: AuthHeader,
request: OrganizationAppAccess,
) -> Result<OrganizationAppAccess, CallError<AppsError>> {
let mut url = self.base.clone();
{
let mut path = url.path_segments_mut().map_err(|_| {
CallError::<AppsError>::protocol("base URL cannot contain path segments")
})?;
path.pop_if_empty();
path.push("api");
path.push("apps");
path.push("organization-access");
}
let builder = self.http.put(url).header("accept", "application/json");
let builder = builder.header("authorization", auth_header.as_str());
let builder = builder
.header("content-type", "application/json")
.json(&request);
let response = self.send(builder, "application/json").await?;
let status = response.status().as_u16();
let bytes = bounded_body(response, 8 * 1024 * 1024).await?;
serde_json::from_slice(&bytes)
.map_err(|_| CallError::protocol_response(status, "invalid JSON response"))
}
pub async fn request_installation_http(
&self,
auth_header: AuthHeader,
request: InstallationRequest,
) -> Result<Installation, CallError<AppsError>> {
let mut url = self.base.clone();
{
let mut path = url.path_segments_mut().map_err(|_| {
CallError::<AppsError>::protocol("base URL cannot contain path segments")
})?;
path.pop_if_empty();
path.push("api");
path.push("apps");
path.push("installations");
}
let builder = self.http.post(url).header("accept", "application/json");
let builder = builder.header("authorization", auth_header.as_str());
let builder = builder
.header("content-type", "application/json")
.json(&request);
let response = self.send(builder, "application/json").await?;
let status = response.status().as_u16();
let bytes = bounded_body(response, 8 * 1024 * 1024).await?;
serde_json::from_slice(&bytes)
.map_err(|_| CallError::protocol_response(status, "invalid JSON response"))
}
pub async fn review_installation_http(
&self,
auth_header: AuthHeader,
installation_id: String,
request: InstallationReview,
) -> Result<Installation, CallError<AppsError>> {
let mut url = self.base.clone();
{
let mut path = url.path_segments_mut().map_err(|_| {
CallError::<AppsError>::protocol("base URL cannot contain path segments")
})?;
path.pop_if_empty();
path.push("api");
path.push("apps");
path.push("installations");
let segment = installation_id.to_string();
if matches!(segment.as_str(), "." | "..") {
return Err(CallError::<AppsError>::protocol("dot path segment"));
}
path.push(&segment);
path.push("review");
}
let builder = self.http.post(url).header("accept", "application/json");
let builder = builder.header("authorization", auth_header.as_str());
let builder = builder
.header("content-type", "application/json")
.json(&request);
let response = self.send(builder, "application/json").await?;
let status = response.status().as_u16();
let bytes = bounded_body(response, 8 * 1024 * 1024).await?;
serde_json::from_slice(&bytes)
.map_err(|_| CallError::protocol_response(status, "invalid JSON response"))
}
pub async fn set_installation_status_http(
&self,
auth_header: AuthHeader,
installation_id: String,
request: InstallationStatusChange,
) -> Result<Installation, CallError<AppsError>> {
let mut url = self.base.clone();
{
let mut path = url.path_segments_mut().map_err(|_| {
CallError::<AppsError>::protocol("base URL cannot contain path segments")
})?;
path.pop_if_empty();
path.push("api");
path.push("apps");
path.push("installations");
let segment = installation_id.to_string();
if matches!(segment.as_str(), "." | "..") {
return Err(CallError::<AppsError>::protocol("dot path segment"));
}
path.push(&segment);
path.push("status");
}
let builder = self.http.put(url).header("accept", "application/json");
let builder = builder.header("authorization", auth_header.as_str());
let builder = builder
.header("content-type", "application/json")
.json(&request);
let response = self.send(builder, "application/json").await?;
let status = response.status().as_u16();
let bytes = bounded_body(response, 8 * 1024 * 1024).await?;
serde_json::from_slice(&bytes)
.map_err(|_| CallError::protocol_response(status, "invalid JSON response"))
}
pub async fn uninstall_http(
&self,
auth_header: AuthHeader,
installation_id: String,
) -> Result<Installation, CallError<AppsError>> {
let mut url = self.base.clone();
{
let mut path = url.path_segments_mut().map_err(|_| {
CallError::<AppsError>::protocol("base URL cannot contain path segments")
})?;
path.pop_if_empty();
path.push("api");
path.push("apps");
path.push("installations");
let segment = installation_id.to_string();
if matches!(segment.as_str(), "." | "..") {
return Err(CallError::<AppsError>::protocol("dot path segment"));
}
path.push(&segment);
}
let builder = self.http.delete(url).header("accept", "application/json");
let builder = builder.header("authorization", auth_header.as_str());
let response = self.send(builder, "application/json").await?;
let status = response.status().as_u16();
let bytes = bounded_body(response, 8 * 1024 * 1024).await?;
serde_json::from_slice(&bytes)
.map_err(|_| CallError::protocol_response(status, "invalid JSON response"))
}
async fn send(
&self,
builder: reqwest_middleware::RequestBuilder,
expected_media: &str,
) -> Result<reqwest::Response, CallError<AppsError>> {
let response = builder
.timeout(std::time::Duration::from_secs(60))
.send()
.await
.map_err(CallError::transport)?;
let status = response.status();
if !status.is_success() {
let status = status.as_u16();
let content_type = response
.headers()
.get("content-type")
.and_then(|value| value.to_str().ok())
.map(str::to_owned);
let (bytes, truncated) =
bounded_error_body::<AppsError>(response, 64 * 1024, status, content_type.clone())
.await?;
if truncated {
return Err(CallError::unknown_remote(status, content_type, bytes, true));
}
return match serde_json::from_slice::<ErrorEnvelope>(&bytes) {
Ok(envelope) => Err(CallError::Service(ServiceFailure::remote(status, envelope))),
Err(_) => Err(CallError::unknown_remote(
status,
content_type,
bytes,
false,
)),
};
}
if !media_matches(&response, expected_media) {
return Err(CallError::protocol_response(
status.as_u16(),
"unexpected response media type",
));
}
Ok(response)
}
}
fn media_matches(response: &reqwest::Response, expected: &str) -> bool {
response
.headers()
.get("content-type")
.and_then(|value| value.to_str().ok())
.and_then(|value| value.split(';').next())
.is_some_and(|value| value.trim().eq_ignore_ascii_case(expected))
}
async fn bounded_body(
mut response: reqwest::Response,
limit: usize,
) -> Result<Vec<u8>, CallError<AppsError>> {
let status = response.status().as_u16();
let mut result = Vec::new();
while let Some(chunk) = response
.chunk()
.await
.map_err(|error| CallError::transport_response(status, error))?
{
if chunk.len() > limit.saturating_sub(result.len()) {
return Err(CallError::protocol_response(
status,
"response exceeds JSON size limit",
));
}
result.extend_from_slice(&chunk);
}
Ok(result)
}
async fn bounded_error_body<E>(
mut response: reqwest::Response,
limit: usize,
status: u16,
content_type: Option<String>,
) -> Result<(Vec<u8>, bool), CallError<E>> {
let mut result = Vec::new();
loop {
let chunk = response.chunk().await.map_err(|error| {
CallError::unreadable_remote(status, content_type.clone(), result.clone(), error)
})?;
let Some(chunk) = chunk else {
return Ok((result, false));
};
let remaining = limit.saturating_sub(result.len());
if chunk.len() > remaining {
result.extend_from_slice(&chunk[..remaining]);
return Ok((result, true));
}
result.extend_from_slice(&chunk);
}
}
#[async_trait]
impl AppsApi for AppsClient {
async fn approve_installation(
&self,
auth_header: AuthHeader,
installation_id: String,
) -> Result<Installation, CallError<AppsError>> {
let response = Self::approve_installation_http(self, auth_header, installation_id).await?;
Ok(response)
}
async fn create_application(
&self,
auth_header: AuthHeader,
project_id: String,
request: ApplicationConfiguration,
) -> Result<Application, CallError<AppsError>> {
let response =
Self::create_application_http(self, auth_header, project_id, request).await?;
Ok(response)
}
async fn decide_consent(
&self,
auth_header: AuthHeader,
challenge: String,
request: ConsentDecision,
) -> Result<ConsentRedirect, CallError<AppsError>> {
let response = Self::decide_consent_http(self, auth_header, challenge, request).await?;
Ok(response)
}
async fn delete_application(
&self,
auth_header: AuthHeader,
application_id: String,
) -> Result<Application, CallError<AppsError>> {
let response = Self::delete_application_http(self, auth_header, application_id).await?;
Ok(response)
}
async fn get_application(
&self,
auth_header: AuthHeader,
application_id: String,
) -> Result<Application, CallError<AppsError>> {
let response = Self::get_application_http(self, auth_header, application_id).await?;
Ok(response)
}
async fn get_consent_request(
&self,
auth_header: AuthHeader,
challenge: String,
) -> Result<ConsentRequest, CallError<AppsError>> {
let response = Self::get_consent_request_http(self, auth_header, challenge).await?;
Ok(response)
}
async fn get_current_installation(
&self,
auth_header: AuthHeader,
) -> Result<InstallationDetails, CallError<AppsError>> {
let response = Self::get_current_installation_http(self, auth_header).await?;
Ok(response)
}
async fn get_installable_application(
&self,
auth_header: AuthHeader,
application_id: String,
) -> Result<InstallableApplication, CallError<AppsError>> {
let response =
Self::get_installable_application_http(self, auth_header, application_id).await?;
Ok(response)
}
async fn get_installation(
&self,
auth_header: AuthHeader,
installation_id: String,
) -> Result<Installation, CallError<AppsError>> {
let response = Self::get_installation_http(self, auth_header, installation_id).await?;
Ok(response)
}
async fn get_organization_app_access(
&self,
auth_header: AuthHeader,
) -> Result<OrganizationAppAccess, CallError<AppsError>> {
let response = Self::get_organization_app_access_http(self, auth_header).await?;
Ok(response)
}
async fn get_self(&self, auth_header: AuthHeader) -> Result<Application, CallError<AppsError>> {
let response = Self::get_self_http(self, auth_header).await?;
Ok(response)
}
async fn get_self_installation(
&self,
auth_header: AuthHeader,
installation_id: String,
) -> Result<InstallationDetails, CallError<AppsError>> {
let response = Self::get_self_installation_http(self, auth_header, installation_id).await?;
Ok(response)
}
async fn get_user_installation(
&self,
auth_header: AuthHeader,
) -> Result<UserInstallation, CallError<AppsError>> {
let response = Self::get_user_installation_http(self, auth_header).await?;
Ok(response)
}
async fn list_application_installations(
&self,
auth_header: AuthHeader,
application_id: String,
request: ListApplicationsRequest,
) -> Result<InstallationPage, CallError<AppsError>> {
let response =
Self::list_application_installations_http(self, auth_header, application_id, request)
.await?;
Ok(response)
}
async fn list_applications(
&self,
auth_header: AuthHeader,
project_id: String,
request: ListApplicationsRequest,
) -> Result<ApplicationPage, CallError<AppsError>> {
let response = Self::list_applications_http(self, auth_header, project_id, request).await?;
Ok(response)
}
async fn list_installation_projects(
&self,
auth_header: AuthHeader,
request: ListApplicationsRequest,
) -> Result<InstallationProjectPage, CallError<AppsError>> {
let response = Self::list_installation_projects_http(self, auth_header, request).await?;
Ok(response)
}
async fn list_installations(
&self,
auth_header: AuthHeader,
request: ListApplicationsRequest,
) -> Result<InstallationPage, CallError<AppsError>> {
let response = Self::list_installations_http(self, auth_header, request).await?;
Ok(response)
}
async fn list_scopes(
&self,
auth_header: AuthHeader,
) -> Result<ApplicationScopes, CallError<AppsError>> {
let response = Self::list_scopes_http(self, auth_header).await?;
Ok(response)
}
async fn list_self_installations(
&self,
auth_header: AuthHeader,
request: ListApplicationsRequest,
) -> Result<InstallationPage, CallError<AppsError>> {
let response = Self::list_self_installations_http(self, auth_header, request).await?;
Ok(response)
}
async fn list_user_installation_projects(
&self,
auth_header: AuthHeader,
installation_id: String,
request: ListApplicationsRequest,
) -> Result<InstallationProjectPage, CallError<AppsError>> {
let response =
Self::list_user_installation_projects_http(self, auth_header, installation_id, request)
.await?;
Ok(response)
}
async fn put_application(
&self,
auth_header: AuthHeader,
application_id: String,
request: ApplicationConfiguration,
) -> Result<Application, CallError<AppsError>> {
let response =
Self::put_application_http(self, auth_header, application_id, request).await?;
Ok(response)
}
async fn put_organization_app_access(
&self,
auth_header: AuthHeader,
request: OrganizationAppAccess,
) -> Result<OrganizationAppAccess, CallError<AppsError>> {
let response = Self::put_organization_app_access_http(self, auth_header, request).await?;
Ok(response)
}
async fn request_installation(
&self,
auth_header: AuthHeader,
request: InstallationRequest,
) -> Result<Installation, CallError<AppsError>> {
let response = Self::request_installation_http(self, auth_header, request).await?;
Ok(response)
}
async fn review_installation(
&self,
auth_header: AuthHeader,
installation_id: String,
request: InstallationReview,
) -> Result<Installation, CallError<AppsError>> {
let response =
Self::review_installation_http(self, auth_header, installation_id, request).await?;
Ok(response)
}
async fn set_installation_status(
&self,
auth_header: AuthHeader,
installation_id: String,
request: InstallationStatusChange,
) -> Result<Installation, CallError<AppsError>> {
let response =
Self::set_installation_status_http(self, auth_header, installation_id, request).await?;
Ok(response)
}
async fn uninstall(
&self,
auth_header: AuthHeader,
installation_id: String,
) -> Result<Installation, CallError<AppsError>> {
let response = Self::uninstall_http(self, auth_header, installation_id).await?;
Ok(response)
}
}