Skip to content

Commit db7c131

Browse files
committed
Split SupergraphFetcher
1 parent 63ff3eb commit db7c131

File tree

3 files changed

+94
-60
lines changed

3 files changed

+94
-60
lines changed

packages/libraries/router/src/registry.rs

Lines changed: 7 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
use crate::registry_logger::Logger;
22
use anyhow::{anyhow, Result};
3+
use hive_console_sdk::supergraph_fetcher::SupergraphFetcher;
34
use sha2::Digest;
45
use sha2::Sha256;
56
use std::env;
67
use std::io::Write;
78
use std::thread;
89

9-
#[derive(Debug, Clone)]
10+
#[derive(Debug)]
1011
pub struct HiveRegistry {
11-
endpoint: String,
12-
key: String,
1312
file_name: String,
14-
etag: Option<String>,
15-
accept_invalid_certs: bool,
13+
fetcher: SupergraphFetcher,
1614
pub logger: Logger,
1715
}
1816

@@ -24,8 +22,6 @@ pub struct HiveRegistryConfig {
2422
schema_file_path: Option<String>,
2523
}
2624

27-
static COMMIT: Option<&'static str> = option_env!("GITHUB_SHA");
28-
2925
impl HiveRegistry {
3026
#[allow(clippy::new_ret_no_self)]
3127
pub fn new(user_config: Option<HiveRegistryConfig>) -> Result<()> {
@@ -133,11 +129,9 @@ impl HiveRegistry {
133129
env::set_var("APOLLO_ROUTER_HOT_RELOAD", "true");
134130

135131
let mut registry = HiveRegistry {
136-
endpoint,
137-
key,
132+
fetcher: SupergraphFetcher::try_new(endpoint, key, accept_invalid_certs)
133+
.map_err(|e| anyhow!(e))?,
138134
file_name,
139-
etag: None,
140-
accept_invalid_certs,
141135
logger,
142136
};
143137

@@ -161,52 +155,9 @@ impl HiveRegistry {
161155
Ok(())
162156
}
163157

164-
fn fetch_supergraph(&mut self, etag: Option<String>) -> Result<Option<String>, String> {
165-
let client = reqwest::blocking::Client::builder()
166-
.danger_accept_invalid_certs(self.accept_invalid_certs)
167-
.build()
168-
.map_err(|err| err.to_string())?;
169-
let mut headers = reqwest::header::HeaderMap::new();
170-
171-
headers.insert(
172-
reqwest::header::USER_AGENT,
173-
reqwest::header::HeaderValue::from_str(
174-
format!("hive-apollo-router/{}", COMMIT.unwrap_or("local")).as_str(),
175-
)
176-
.unwrap(),
177-
);
178-
headers.insert("X-Hive-CDN-Key", self.key.parse().unwrap());
179-
180-
if let Some(checksum) = etag {
181-
headers.insert("If-None-Match", checksum.parse().unwrap());
182-
}
183-
184-
let resp = client
185-
.get(self.endpoint.as_str())
186-
.headers(headers)
187-
.send()
188-
.map_err(|e| e.to_string())?;
189-
190-
match resp.headers().get("etag") {
191-
Some(checksum) => {
192-
let etag = checksum.to_str().map_err(|e| e.to_string())?;
193-
self.update_latest_etag(Some(etag.to_string()));
194-
}
195-
None => {
196-
self.update_latest_etag(None);
197-
}
198-
}
199-
200-
if resp.status().as_u16() == 304 {
201-
return Ok(None);
202-
}
203-
204-
Ok(Some(resp.text().map_err(|e| e.to_string())?))
205-
}
206-
207158
fn initial_supergraph(&mut self) -> Result<(), String> {
208159
let mut file = std::fs::File::create(self.file_name.clone()).map_err(|e| e.to_string())?;
209-
let resp = self.fetch_supergraph(None)?;
160+
let resp = self.fetcher.fetch_supergraph()?;
210161

211162
match resp {
212163
Some(supergraph) => {
@@ -221,12 +172,8 @@ impl HiveRegistry {
221172
Ok(())
222173
}
223174

224-
fn update_latest_etag(&mut self, etag: Option<String>) {
225-
self.etag = etag;
226-
}
227-
228175
fn poll(&mut self) {
229-
match self.fetch_supergraph(self.etag.clone()) {
176+
match self.fetcher.fetch_supergraph() {
230177
Ok(new_supergraph) => {
231178
if let Some(new_supergraph) = new_supergraph {
232179
let current_file = std::fs::read_to_string(self.file_name.clone())
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pub mod agent;
22
pub mod graphql;
33
pub mod persisted_documents;
4+
pub mod supergraph_fetcher;
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#[derive(Debug)]
2+
pub struct SupergraphFetcher {
3+
client: reqwest::blocking::Client,
4+
endpoint: String,
5+
key: String,
6+
etag: Option<String>,
7+
}
8+
9+
static COMMIT: Option<&'static str> = option_env!("GITHUB_SHA");
10+
11+
impl SupergraphFetcher {
12+
pub fn try_new(
13+
endpoint: String,
14+
key: String,
15+
accept_invalid_certs: bool,
16+
) -> Result<Self, String> {
17+
if endpoint.is_empty() {
18+
return Err("Supergraph endpoint is required".into());
19+
}
20+
if key.is_empty() {
21+
return Err("Supergraph key is required".into());
22+
}
23+
let mut endpoint = endpoint;
24+
if !endpoint.ends_with("/supergraph") {
25+
if endpoint.ends_with("/") {
26+
endpoint.push_str("supergraph")
27+
} else {
28+
endpoint.push_str("/supergraph")
29+
}
30+
}
31+
let client = reqwest::blocking::Client::builder()
32+
.danger_accept_invalid_certs(accept_invalid_certs)
33+
.build()
34+
.map_err(|err| err.to_string())?;
35+
Ok(Self {
36+
client,
37+
endpoint,
38+
key,
39+
etag: None,
40+
})
41+
}
42+
43+
pub fn fetch_supergraph(&mut self) -> Result<Option<String>, String> {
44+
let mut headers = reqwest::header::HeaderMap::new();
45+
46+
headers.insert(
47+
reqwest::header::USER_AGENT,
48+
reqwest::header::HeaderValue::from_str(
49+
format!("hive-apollo-router/{}", COMMIT.unwrap_or("local")).as_str(),
50+
)
51+
.unwrap(),
52+
);
53+
headers.insert("X-Hive-CDN-Key", self.key.parse().unwrap());
54+
55+
if let Some(checksum) = &self.etag {
56+
headers.insert("If-None-Match", checksum.parse().unwrap());
57+
}
58+
59+
let resp = self
60+
.client
61+
.get(self.endpoint.as_str())
62+
.headers(headers)
63+
.send()
64+
.map_err(|e| e.to_string())?;
65+
66+
match resp.headers().get("etag") {
67+
Some(checksum) => {
68+
let etag = checksum.to_str().map_err(|e| e.to_string())?;
69+
self.update_latest_etag(Some(etag.to_string()));
70+
}
71+
None => {
72+
self.update_latest_etag(None);
73+
}
74+
}
75+
76+
if resp.status().as_u16() == 304 {
77+
return Ok(None);
78+
}
79+
80+
Ok(Some(resp.text().map_err(|e| e.to_string())?))
81+
}
82+
83+
fn update_latest_etag(&mut self, etag: Option<String>) {
84+
self.etag = etag;
85+
}
86+
}

0 commit comments

Comments
 (0)