use reqwest::Method;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::{client::Sendry, error::Error};
#[derive(Debug, Clone)]
pub struct Regions {
client: Sendry,
}
impl Regions {
pub(crate) fn new(client: Sendry) -> Self {
Self { client }
}
pub async fn list(&self) -> Result<RegionList, Error> {
self.client
.request(
self.client
.build::<()>(Method::GET, "/v1/regions", &[], None),
)
.await
}
pub async fn get_org_settings(&self) -> Result<OrgRegionSettings, Error> {
self.client
.request(self.client.build::<()>(
Method::GET,
"/v1/organizations/me/region",
&[],
None,
))
.await
}
pub async fn update_org_settings(
&self,
params: UpdateOrgRegion,
) -> Result<OrgRegionSettings, Error> {
self.client
.request(self.client.build(
Method::PATCH,
"/v1/organizations/me/region",
&[],
Some(¶ms),
))
.await
}
pub async fn set_domain_region(
&self,
domain_id: &str,
params: UpdateDomainRegion,
) -> Result<Value, Error> {
self.client
.request(self.client.build(
Method::PATCH,
&format!("/v1/domains/{domain_id}/region"),
&[],
Some(¶ms),
))
.await
}
pub async fn get_region_analytics(
&self,
params: RegionAnalyticsParams,
) -> Result<RegionAnalyticsResponse, Error> {
let q = params.to_query();
self.client
.request(self.client.build::<()>(
Method::GET,
"/v1/analytics/regions",
&q,
None,
))
.await
}
}
#[derive(Debug, Clone, Deserialize)]
pub struct Region {
pub region_code: String,
pub display_name: String,
pub is_default: bool,
}
#[derive(Debug, Clone, Deserialize)]
pub struct RegionList {
pub data: Vec<Region>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct OrgRegionSettings {
pub default_region: Option<String>,
pub data_residency: String,
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct UpdateOrgRegion {
#[serde(skip_serializing_if = "Option::is_none")]
pub default_region: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub data_residency: Option<String>,
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct UpdateDomainRegion {
pub region: Option<String>,
}
#[derive(Debug, Clone, Default)]
pub struct RegionAnalyticsParams {
pub from: String,
pub to: String,
}
impl RegionAnalyticsParams {
fn to_query(&self) -> Vec<(&'static str, String)> {
vec![("from", self.from.clone()), ("to", self.to.clone())]
}
}
#[derive(Debug, Clone, Deserialize)]
pub struct RegionAnalyticsItem {
pub region: String,
pub count: u64,
pub percentage: f64,
}
#[derive(Debug, Clone, Deserialize)]
pub struct RegionAnalyticsResponse {
pub data: Vec<RegionAnalyticsItem>,
}