|
| 1 | +use common::dtos::{LangDto, TaggedGroupDto}; |
| 2 | +use reqwest::Url; |
| 3 | +use rocket::{ |
| 4 | + fairing::{self, Fairing}, |
| 5 | + Build, Rocket, |
| 6 | +}; |
| 7 | +use serde::{Deserialize, Serialize}; |
| 8 | + |
| 9 | +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 10 | +pub struct HiveConfig { |
| 11 | + pub url: Url, |
| 12 | + pub secret: String, |
| 13 | +} |
| 14 | + |
| 15 | +pub struct HiveClient { |
| 16 | + client: reqwest::Client, |
| 17 | + config: HiveConfig, |
| 18 | +} |
| 19 | + |
| 20 | +impl HiveClient { |
| 21 | + pub fn new(config: HiveConfig) -> reqwest::Result<Self> { |
| 22 | + let client = reqwest::Client::builder().build()?; |
| 23 | + |
| 24 | + Ok(Self { client, config }) |
| 25 | + } |
| 26 | + |
| 27 | + /// Get group memberships for the user with the specified username that have been tagged with |
| 28 | + /// META TV's tag. |
| 29 | + /// |
| 30 | + /// On a high level this can be thought of as returning the group memberships for the user that |
| 31 | + /// this application is allowed to see. |
| 32 | + /// |
| 33 | + /// Wrapper around `/tagged/slide-manager/memberships/{username}`. |
| 34 | + pub async fn tagged_memberships( |
| 35 | + &self, |
| 36 | + username: &str, |
| 37 | + lang: LangDto, |
| 38 | + ) -> reqwest::Result<Vec<TaggedGroupDto>> { |
| 39 | + let mut url = self.config.url.clone(); |
| 40 | + url.path_segments_mut() |
| 41 | + .expect("url can be a base") |
| 42 | + .pop_if_empty() |
| 43 | + .extend(&["tagged", "slide-manager", "memberships", username]); |
| 44 | + url.query_pairs_mut() |
| 45 | + .append_pair("lang", &format!("{}", lang)); |
| 46 | + self.client |
| 47 | + .get(url) |
| 48 | + .bearer_auth(&self.config.secret) |
| 49 | + .send() |
| 50 | + .await? |
| 51 | + .error_for_status()? |
| 52 | + .json::<Vec<TaggedGroupDto>>() |
| 53 | + .await |
| 54 | + } |
| 55 | + |
| 56 | + /// Get all groups that have been tagged with META TV's tag. |
| 57 | + /// |
| 58 | + /// Wrapper around `/tagged/slide-manager/groups`. |
| 59 | + pub async fn tagged_groups(&self, lang: LangDto) -> reqwest::Result<Vec<TaggedGroupDto>> { |
| 60 | + let mut url = self.config.url.clone(); |
| 61 | + url.path_segments_mut() |
| 62 | + .expect("url can be a base") |
| 63 | + .pop_if_empty() |
| 64 | + .extend(&["tagged", "slide-manager", "groups"]); |
| 65 | + url.query_pairs_mut() |
| 66 | + .append_pair("lang", &format!("{}", lang)); |
| 67 | + self.client |
| 68 | + .get(url) |
| 69 | + .bearer_auth(&self.config.secret) |
| 70 | + .send() |
| 71 | + .await? |
| 72 | + .error_for_status()? |
| 73 | + .json::<Vec<TaggedGroupDto>>() |
| 74 | + .await |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +pub struct HiveInitializer; |
| 79 | + |
| 80 | +#[rocket::async_trait] |
| 81 | +impl Fairing for HiveInitializer { |
| 82 | + fn info(&self) -> fairing::Info { |
| 83 | + fairing::Info { |
| 84 | + name: "Hive Client", |
| 85 | + kind: fairing::Kind::Ignite, |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + async fn on_ignite(&self, rocket: Rocket<Build>) -> fairing::Result { |
| 90 | + let config = match rocket.figment().focus("hive").extract::<HiveConfig>() { |
| 91 | + Ok(config) => config, |
| 92 | + Err(e) => { |
| 93 | + error!("hive configuration incomplete: {}", e); |
| 94 | + return Err(rocket); |
| 95 | + } |
| 96 | + }; |
| 97 | + |
| 98 | + let client = match HiveClient::new(config) { |
| 99 | + Ok(client) => client, |
| 100 | + Err(e) => { |
| 101 | + error!("failed to initialize hive client: {}", e); |
| 102 | + return Err(rocket); |
| 103 | + } |
| 104 | + }; |
| 105 | + |
| 106 | + Ok(rocket.manage(client)) |
| 107 | + } |
| 108 | +} |
0 commit comments