Skip to content

Commit

Permalink
feat(endpoints): add v2 endpoints (#360)
Browse files Browse the repository at this point in the history
* feat(endpoints): add v2 api

* reorganize

---------

Co-authored-by: Galileo Daras <[email protected]>
  • Loading branch information
TheButlah and oldgalileo authored Jan 30, 2025
1 parent cb33d0e commit 8def586
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 16 deletions.
9 changes: 4 additions & 5 deletions endpoints/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ description = "Common location for all endpoints associated with the orb"
authors = ["Ryan Butler <[email protected]>"]
publish = false

# orb-core can't consume crates that use workspace inheritance :(
edition = "2021"
license = "MIT OR (Apache-2.0 WITH LLVM-exception)"
repository = "https://github.com/worldcoin/orb-software"
rust-version = "1.77.0"
edition.workspace = true
license.workspace = true
repository.workspace = true
rust-version.workspace = true

[dependencies]
hex = "0.4.3"
Expand Down
12 changes: 10 additions & 2 deletions endpoints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@
// code simpler to use it, but I didn't want the dependencies

pub mod backend;
pub mod endpoints;
pub mod orb_id;
pub mod v1;
pub mod v2;

pub use crate::backend::Backend;
pub use crate::endpoints::Endpoints;
pub use crate::orb_id::OrbId;

/// Safer way to assemble URLs involving `OrbId`
fn concat_urls(prefix: &str, orb_id: &OrbId, suffix: &str) -> url::Url {
url::Url::parse(prefix)
.and_then(|url| url.join(&format!("{}/", orb_id.as_str())))
.and_then(|url| url.join(suffix))
.expect("urls with validated orb ids should always parse")
}
10 changes: 1 addition & 9 deletions endpoints/src/endpoints.rs → endpoints/src/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub use url::ParseError as UrlParseErr;

use url::Url;

use crate::{orb_id::OrbId, Backend};
use crate::{concat_urls, orb_id::OrbId, Backend};

/// Access to all the urls that require parameterization on [`Backend`] and orb id.
#[derive(Debug, Eq, PartialEq, Hash, Clone)]
Expand All @@ -24,14 +24,6 @@ impl Endpoints {
Backend::Analysis => "analysis.ml",
};

/// Safer way to assemble URLs involving `OrbId`
fn concat_urls(prefix: &str, orb_id: &OrbId, suffix: &str) -> Url {
Url::parse(prefix)
.and_then(|url| url.join(&format!("{}/", orb_id.as_str())))
.and_then(|url| url.join(suffix))
.expect("urls with validated orb ids should always parse")
}

Self {
ai_volume: concat_urls(
&format!("https://management.{subdomain}.worldcoin.org/api/v1/orbs/"),
Expand Down
54 changes: 54 additions & 0 deletions endpoints/src/v2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use url::Url;

use crate::{concat_urls, Backend, OrbId};

#[derive(Debug, Eq, PartialEq, Hash, Clone)]
pub struct Endpoints {
pub status: Url,
}

impl Endpoints {
pub fn new(backend: Backend, orb_id: &OrbId) -> Self {
let subdomain = match backend {
Backend::Prod => "orb",
Backend::Staging => "stage.orb",
Backend::Analysis => unimplemented!(),
};

Self {
status: concat_urls(
&format!("https://management.{subdomain}.worldcoin.org/api/v2/orbs/"),
orb_id,
"status",
),
}
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_endpoints() {
let orb_id = "ea2ea744".parse().unwrap();
let stage = Endpoints::new(Backend::Staging, &orb_id);
let prod = Endpoints::new(Backend::Prod, &orb_id);

assert_eq!(
stage.status.as_str(),
"https://management.stage.orb.worldcoin.org/api/v2/orbs/ea2ea744/status"
);
assert_eq!(
prod.status.as_str(),
"https://management.orb.worldcoin.org/api/v2/orbs/ea2ea744/status"
);
}

#[test]
#[should_panic(expected = "not implemented")]
fn test_analysis_backend_unimplemented() {
let orb_id = "ea2ea744".parse().unwrap();
let _analysis = Endpoints::new(Backend::Analysis, &orb_id);
}
}

0 comments on commit 8def586

Please sign in to comment.