Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(orb-endpoints): Add a new Analysis backend #329

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions endpoints/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub const ORB_BACKEND_ENV_VAR_NAME: &str = "ORB_BACKEND";
pub enum Backend {
Prod,
Staging,
Analysis,
}

impl Backend {
Expand Down Expand Up @@ -50,6 +51,9 @@ impl Backend {
if b == Backend::Prod && IS_STAGE_BUILD {
panic!("tried to talk to prod backend but this is a staging build!");
}
if b == Backend::Analysis && IS_STAGE_BUILD {
panic!("tried to talk to analysis backend but this is a staging build!");
}
b
}
}
Expand All @@ -61,6 +65,7 @@ impl FromStr for Backend {
match s.trim().to_lowercase().as_str() {
"prod" | "production" => Ok(Self::Prod),
"stage" | "staging" | "dev" | "development" => Ok(Self::Staging),
"analysis" | "analysis.ml" | "analysis-ml" => Ok(Self::Analysis),
_ => Err(BackendParseErr),
}
}
Expand Down Expand Up @@ -126,6 +131,7 @@ mod test {
assert_eq!(Backend::from_str("stage").unwrap(), Backend::Staging);
assert_eq!(Backend::from_str("staGe").unwrap(), Backend::Staging);
assert_eq!(Backend::from_str("dev").unwrap(), Backend::Staging);
assert_eq!(Backend::from_str("analysis").unwrap(), Backend::Analysis);
assert_eq!(Backend::from_str("foobar"), Err(BackendParseErr));
}
}
30 changes: 20 additions & 10 deletions endpoints/src/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ impl Endpoints {
/// # Errors
/// Errors if the `orb_id` would result in an invalid URL.
pub fn new(backend: Backend, orb_id: &OrbId) -> Self {
let maybe_stage = match backend {
Backend::Prod => "",
Backend::Staging => "stage.",
let subdomain = match backend {
Backend::Prod => "orb",
Backend::Staging => "stage.orb",
Backend::Analysis => "analysis.ml",
};

/// Safer way to assemble URLs involving `OrbId`
Expand All @@ -33,20 +34,16 @@ impl Endpoints {

Self {
ai_volume: concat_urls(
&format!(
"https://management.{maybe_stage}orb.worldcoin.org/api/v1/orbs/"
),
&format!("https://management.{subdomain}.worldcoin.org/api/v1/orbs/"),
orb_id,
"keys/aivolume",
),
auth: Url::parse(&format!(
"https://auth.{maybe_stage}orb.worldcoin.org/api/v1/"
"https://auth.{subdomain}.worldcoin.org/api/v1/"
))
.expect("urls with validated orb ids should always parse"),
ping: concat_urls(
&format!(
"https://management.{maybe_stage}orb.worldcoin.org/api/v1/orbs/"
),
&format!("https://management.{subdomain}.worldcoin.org/api/v1/orbs/"),
orb_id,
"",
),
Expand All @@ -62,6 +59,7 @@ mod test {
fn test_config() {
let stage = Endpoints::new(Backend::Staging, &"ea2ea744".parse().unwrap());
let prod = Endpoints::new(Backend::Prod, &"ea2ea744".parse().unwrap());
let analysis = Endpoints::new(Backend::Analysis, &"ea2ea744".parse().unwrap());

assert_eq!(
stage.ai_volume.as_str(),
Expand All @@ -71,12 +69,20 @@ mod test {
prod.ai_volume.as_str(),
"https://management.orb.worldcoin.org/api/v1/orbs/ea2ea744/keys/aivolume"
);
assert_eq!(
analysis.ai_volume.as_str(),
"https://management.analysis.ml.worldcoin.org/api/v1/orbs/ea2ea744/keys/aivolume"
);

assert_eq!(
stage.auth.as_str(),
"https://auth.stage.orb.worldcoin.org/api/v1/"
);
assert_eq!(prod.auth.as_str(), "https://auth.orb.worldcoin.org/api/v1/");
assert_eq!(
analysis.auth.as_str(),
"https://auth.analysis.ml.worldcoin.org/api/v1/"
);

assert_eq!(
stage.ping.as_str(),
Expand All @@ -86,5 +92,9 @@ mod test {
prod.ping.as_str(),
"https://management.orb.worldcoin.org/api/v1/orbs/ea2ea744/"
);
assert_eq!(
analysis.ping.as_str(),
"https://management.analysis.ml.worldcoin.org/api/v1/orbs/ea2ea744/"
);
}
}