Skip to content

Commit e95be9f

Browse files
committed
Extract morpheus-http-client into standalone crate
1 parent 6d8b783 commit e95be9f

5 files changed

Lines changed: 59 additions & 72 deletions

File tree

Cargo.toml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ version = "0.1.0"
44
authors = ["Hao Shi <shisoftgenius@gmail.com>"]
55
edition = "2021"
66

7-
[workspace]
8-
members = [
9-
"crates/morpheus-http-client",
10-
]
11-
127
[lib]
138
name = "morpheus"
149
path = "src/lib.rs"

crates/morpheus-http-client/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ unsafe_code = "warn"
1111
all = "warn"
1212
pedantic = "warn"
1313

14+
[workspace]
15+
members = ["."]
16+
1417
[dependencies]
1518
reqwest = { version = "0.12", features = ["json"] }
1619
serde = { version = "1", features = ["derive"] }
@@ -21,4 +24,4 @@ bs58 = "0.5"
2124
[dev-dependencies]
2225
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
2326
rand = "0.9"
24-
bifrost_hasher = { git = "https://github.com/shisoft/bifrost", branch = "develop" }
27+
twox-hash = "2"

crates/morpheus-http-client/src/lib.rs

Lines changed: 47 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,43 @@ use url::Url;
1111
pub struct MorpheusHttpClient {
1212
base_url: Url,
1313
client: Client,
14+
admin_token: Option<String>,
1415
}
1516

1617
impl MorpheusHttpClient {
1718
pub fn new(base_url: Url) -> Self {
1819
Self {
1920
base_url,
2021
client: Client::new(),
22+
admin_token: None,
2123
}
2224
}
2325

2426
pub fn base_url(&self) -> &Url {
2527
&self.base_url
2628
}
2729

30+
pub fn with_admin_token(mut self, token: impl Into<String>) -> Self {
31+
self.admin_token = Some(token.into());
32+
self
33+
}
34+
2835
fn endpoint(&self, path: &str) -> Result<Url, ClientError> {
2936
self.base_url.join(path).map_err(ClientError::invalid_url)
3037
}
3138

39+
fn request(&self, method: reqwest::Method, url: Url) -> reqwest::RequestBuilder {
40+
let request = self.client.request(method, url);
41+
match &self.admin_token {
42+
Some(token) => request.bearer_auth(token),
43+
None => request,
44+
}
45+
}
46+
3247
pub async fn health(&self) -> Result<ApiResponse<()>, ClientError> {
3348
let url = self.endpoint("/v1/health")?;
3449
let res = self
35-
.client
36-
.get(url)
50+
.request(reqwest::Method::GET, url)
3751
.send()
3852
.await
3953
.map_err(ClientError::http)?;
@@ -58,8 +72,7 @@ impl MorpheusHttpClient {
5872
pub async fn nuke(&self) -> Result<ApiResponse<NukeResult>, ClientError> {
5973
let url = self.endpoint("/v1/admin/nuke")?;
6074
let res = self
61-
.client
62-
.post(url)
75+
.request(reqwest::Method::POST, url)
6376
.send()
6477
.await
6578
.map_err(ClientError::http)?;
@@ -73,8 +86,7 @@ impl MorpheusHttpClient {
7386
pub async fn cell_get(&self, cell_id_b58: &str) -> Result<ApiResponse<CellDto>, ClientError> {
7487
let url = self.endpoint(&format!("/v1/cells/{cell_id_b58}"))?;
7588
let res = self
76-
.client
77-
.get(url)
89+
.request(reqwest::Method::GET, url)
7890
.send()
7991
.await
8092
.map_err(ClientError::http)?;
@@ -88,8 +100,7 @@ impl MorpheusHttpClient {
88100
) -> Result<ApiResponse<CellWriteResult>, ClientError> {
89101
let url = self.endpoint(&format!("/v1/cells/{cell_id_b58}"))?;
90102
let res = self
91-
.client
92-
.post(url)
103+
.request(reqwest::Method::POST, url)
93104
.json(req)
94105
.send()
95106
.await
@@ -104,8 +115,7 @@ impl MorpheusHttpClient {
104115
) -> Result<ApiResponse<CellWriteResult>, ClientError> {
105116
let url = self.endpoint(&format!("/v1/cells/{cell_id_b58}"))?;
106117
let res = self
107-
.client
108-
.put(url)
118+
.request(reqwest::Method::PUT, url)
109119
.json(req)
110120
.send()
111121
.await
@@ -116,8 +126,7 @@ impl MorpheusHttpClient {
116126
pub async fn cell_delete(&self, cell_id_b58: &str) -> Result<ApiResponse<()>, ClientError> {
117127
let url = self.endpoint(&format!("/v1/cells/{cell_id_b58}"))?;
118128
let res = self
119-
.client
120-
.delete(url)
129+
.request(reqwest::Method::DELETE, url)
121130
.send()
122131
.await
123132
.map_err(ClientError::http)?;
@@ -131,8 +140,7 @@ impl MorpheusHttpClient {
131140
pub async fn graph_schema_list(&self) -> Result<ApiResponse<Vec<GraphSchema>>, ClientError> {
132141
let url = self.endpoint("/v1/graph/schemas")?;
133142
let res = self
134-
.client
135-
.get(url)
143+
.request(reqwest::Method::GET, url)
136144
.send()
137145
.await
138146
.map_err(ClientError::http)?;
@@ -145,8 +153,7 @@ impl MorpheusHttpClient {
145153
) -> Result<ApiResponse<GraphSchema>, ClientError> {
146154
let url = self.endpoint(&format!("/v1/graph/schemas/{name}"))?;
147155
let res = self
148-
.client
149-
.get(url)
156+
.request(reqwest::Method::GET, url)
150157
.send()
151158
.await
152159
.map_err(ClientError::http)?;
@@ -159,8 +166,7 @@ impl MorpheusHttpClient {
159166
) -> Result<ApiResponse<GraphSchemaCreateResponse>, ClientError> {
160167
let url = self.endpoint("/v1/graph/schemas")?;
161168
let res = self
162-
.client
163-
.post(url)
169+
.request(reqwest::Method::POST, url)
164170
.json(req)
165171
.send()
166172
.await
@@ -171,8 +177,7 @@ impl MorpheusHttpClient {
171177
pub async fn graph_schema_delete(&self, name: &str) -> Result<ApiResponse<()>, ClientError> {
172178
let url = self.endpoint(&format!("/v1/graph/schemas/{name}"))?;
173179
let res = self
174-
.client
175-
.delete(url)
180+
.request(reqwest::Method::DELETE, url)
176181
.send()
177182
.await
178183
.map_err(ClientError::http)?;
@@ -182,8 +187,7 @@ impl MorpheusHttpClient {
182187
pub async fn schema_list_legacy(&self) -> Result<ApiResponse<Vec<GraphSchema>>, ClientError> {
183188
let url = self.endpoint("/v1/schemas")?;
184189
let res = self
185-
.client
186-
.get(url)
190+
.request(reqwest::Method::GET, url)
187191
.send()
188192
.await
189193
.map_err(ClientError::http)?;
@@ -196,8 +200,7 @@ impl MorpheusHttpClient {
196200
) -> Result<ApiResponse<GraphSchema>, ClientError> {
197201
let url = self.endpoint(&format!("/v1/schemas/{name}"))?;
198202
let res = self
199-
.client
200-
.get(url)
203+
.request(reqwest::Method::GET, url)
201204
.send()
202205
.await
203206
.map_err(ClientError::http)?;
@@ -211,8 +214,7 @@ impl MorpheusHttpClient {
211214
pub async fn cell_schema_list(&self) -> Result<ApiResponse<Vec<CellSchema>>, ClientError> {
212215
let url = self.endpoint("/v1/cell/schemas")?;
213216
let res = self
214-
.client
215-
.get(url)
217+
.request(reqwest::Method::GET, url)
216218
.send()
217219
.await
218220
.map_err(ClientError::http)?;
@@ -225,8 +227,7 @@ impl MorpheusHttpClient {
225227
) -> Result<ApiResponse<CellSchema>, ClientError> {
226228
let url = self.endpoint(&format!("/v1/cell/schemas/{name}"))?;
227229
let res = self
228-
.client
229-
.get(url)
230+
.request(reqwest::Method::GET, url)
230231
.send()
231232
.await
232233
.map_err(ClientError::http)?;
@@ -239,8 +240,7 @@ impl MorpheusHttpClient {
239240
) -> Result<ApiResponse<CellSchemaCreateResponse>, ClientError> {
240241
let url = self.endpoint("/v1/cell/schemas")?;
241242
let res = self
242-
.client
243-
.post(url)
243+
.request(reqwest::Method::POST, url)
244244
.json(req)
245245
.send()
246246
.await
@@ -251,8 +251,7 @@ impl MorpheusHttpClient {
251251
pub async fn cell_schema_delete(&self, name: &str) -> Result<ApiResponse<()>, ClientError> {
252252
let url = self.endpoint(&format!("/v1/cell/schemas/{name}"))?;
253253
let res = self
254-
.client
255-
.delete(url)
254+
.request(reqwest::Method::DELETE, url)
256255
.send()
257256
.await
258257
.map_err(ClientError::http)?;
@@ -269,8 +268,7 @@ impl MorpheusHttpClient {
269268
) -> Result<ApiResponse<VertexDto>, ClientError> {
270269
let url = self.endpoint(&format!("/v1/graph/vertices/{vertex_id_b58}"))?;
271270
let res = self
272-
.client
273-
.get(url)
271+
.request(reqwest::Method::GET, url)
274272
.send()
275273
.await
276274
.map_err(ClientError::http)?;
@@ -283,8 +281,7 @@ impl MorpheusHttpClient {
283281
) -> Result<ApiResponse<VertexDto>, ClientError> {
284282
let url = self.endpoint("/v1/graph/vertices")?;
285283
let res = self
286-
.client
287-
.post(url)
284+
.request(reqwest::Method::POST, url)
288285
.json(req)
289286
.send()
290287
.await
@@ -295,8 +292,7 @@ impl MorpheusHttpClient {
295292
pub async fn vertex_delete(&self, vertex_id_b58: &str) -> Result<ApiResponse<()>, ClientError> {
296293
let url = self.endpoint(&format!("/v1/graph/vertices/{vertex_id_b58}"))?;
297294
let res = self
298-
.client
299-
.delete(url)
295+
.request(reqwest::Method::DELETE, url)
300296
.send()
301297
.await
302298
.map_err(ClientError::http)?;
@@ -310,8 +306,7 @@ impl MorpheusHttpClient {
310306
) -> Result<ApiResponse<VertexDto>, ClientError> {
311307
let url = self.endpoint(&format!("/v1/graph/vertices/{vertex_id_b58}"))?;
312308
let res = self
313-
.client
314-
.patch(url)
309+
.request(reqwest::Method::PATCH, url)
315310
.json(req)
316311
.send()
317312
.await
@@ -325,8 +320,7 @@ impl MorpheusHttpClient {
325320
) -> Result<ApiResponse<EdgeDto>, ClientError> {
326321
let url = self.endpoint("/v1/graph/edges")?;
327322
let res = self
328-
.client
329-
.post(url)
323+
.request(reqwest::Method::POST, url)
330324
.json(req)
331325
.send()
332326
.await
@@ -340,8 +334,7 @@ impl MorpheusHttpClient {
340334
) -> Result<ApiResponse<()>, ClientError> {
341335
let url = self.endpoint("/v1/graph/edges")?;
342336
let res = self
343-
.client
344-
.delete(url)
337+
.request(reqwest::Method::DELETE, url)
345338
.json(req)
346339
.send()
347340
.await
@@ -355,8 +348,7 @@ impl MorpheusHttpClient {
355348
) -> Result<ApiResponse<()>, ClientError> {
356349
let url = self.endpoint(&format!("/v1/graph/edges/{edge_cell_id_b58}"))?;
357350
let res = self
358-
.client
359-
.delete(url)
351+
.request(reqwest::Method::DELETE, url)
360352
.send()
361353
.await
362354
.map_err(ClientError::http)?;
@@ -377,8 +369,7 @@ impl MorpheusHttpClient {
377369
}
378370
}
379371
let res = self
380-
.client
381-
.get(url)
372+
.request(reqwest::Method::GET, url)
382373
.send()
383374
.await
384375
.map_err(ClientError::http)?;
@@ -391,8 +382,7 @@ impl MorpheusHttpClient {
391382
) -> Result<ApiResponse<QueryResult>, ClientError> {
392383
let url = self.endpoint("/v1/query")?;
393384
let res = self
394-
.client
395-
.post(url)
385+
.request(reqwest::Method::POST, url)
396386
.json(req)
397387
.send()
398388
.await
@@ -406,8 +396,7 @@ impl MorpheusHttpClient {
406396
) -> Result<ApiResponse<Option<QueryPlanExplainDto>>, ClientError> {
407397
let url = self.endpoint("/v1/query/explain")?;
408398
let res = self
409-
.client
410-
.post(url)
399+
.request(reqwest::Method::POST, url)
411400
.json(req)
412401
.send()
413402
.await
@@ -421,8 +410,7 @@ impl MorpheusHttpClient {
421410
) -> Result<ApiResponse<Vec<CellDto>>, ClientError> {
422411
let url = self.endpoint("/v1/graph/query/vertices")?;
423412
let res = self
424-
.client
425-
.post(url)
413+
.request(reqwest::Method::POST, url)
426414
.json(req)
427415
.send()
428416
.await
@@ -436,8 +424,7 @@ impl MorpheusHttpClient {
436424
) -> Result<ApiResponse<Option<QueryPlanExplainDto>>, ClientError> {
437425
let url = self.endpoint("/v1/graph/query/vertices/explain")?;
438426
let res = self
439-
.client
440-
.post(url)
427+
.request(reqwest::Method::POST, url)
441428
.json(req)
442429
.send()
443430
.await
@@ -451,8 +438,7 @@ impl MorpheusHttpClient {
451438
) -> Result<ApiResponse<Vec<GraphLookupEdgeDto>>, ClientError> {
452439
let url = self.endpoint("/v1/graph/query/edges")?;
453440
let res = self
454-
.client
455-
.post(url)
441+
.request(reqwest::Method::POST, url)
456442
.json(req)
457443
.send()
458444
.await
@@ -466,8 +452,7 @@ impl MorpheusHttpClient {
466452
) -> Result<ApiResponse<GraphTraverseResult>, ClientError> {
467453
let url = self.endpoint("/v1/graph/query/traverse")?;
468454
let res = self
469-
.client
470-
.post(url)
455+
.request(reqwest::Method::POST, url)
471456
.json(req)
472457
.send()
473458
.await
@@ -509,8 +494,7 @@ impl MorpheusHttpClient {
509494
}
510495
}
511496
let res = self
512-
.client
513-
.get(url)
497+
.request(reqwest::Method::GET, url)
514498
.send()
515499
.await
516500
.map_err(ClientError::http)?;
@@ -523,8 +507,7 @@ impl MorpheusHttpClient {
523507
) -> Result<ApiResponse<GraphPathResult>, ClientError> {
524508
let url = self.endpoint("/v1/graph/query/path")?;
525509
let res = self
526-
.client
527-
.post(url)
510+
.request(reqwest::Method::POST, url)
528511
.json(req)
529512
.send()
530513
.await

0 commit comments

Comments
 (0)