diff --git a/crates/lox-distributor/src/main.rs b/crates/lox-distributor/src/main.rs index 416184b..5d1d619 100644 --- a/crates/lox-distributor/src/main.rs +++ b/crates/lox-distributor/src/main.rs @@ -9,7 +9,7 @@ use hyper::{ }; use lox_library::bridge_table::{BridgeLine, MAX_BRIDGES_PER_BUCKET}; -use rdsys_backend::{proto::ResourceDiff, start_stream}; +use rdsys_backend::{proto::ResourceDiff, proto::Resource, request_resources, start_stream}; use serde::Deserialize; use std::{ diff --git a/crates/rdsys-backend-api/Cargo.toml b/crates/rdsys-backend-api/Cargo.toml index 46b8b91..b2a8dec 100644 --- a/crates/rdsys-backend-api/Cargo.toml +++ b/crates/rdsys-backend-api/Cargo.toml @@ -14,8 +14,8 @@ bytes = "1" hex = "0.4.3" crc64 = "2.0.0" sha1 = "0.10.6" -tokio = { version = "1", features = ["macros"]} -reqwest = { version = "0.11", features = ["stream"]} +tokio = { version = "1", features = ["full", "macros"] } +reqwest = { version = "0.11", features = ["json", "stream"]} tokio-stream = "0.1.14" futures = "0.3.28" tokio-util = "0.7.9" diff --git a/crates/rdsys-backend-api/src/lib.rs b/crates/rdsys-backend-api/src/lib.rs index 84b420a..8e2c8eb 100644 --- a/crates/rdsys-backend-api/src/lib.rs +++ b/crates/rdsys-backend-api/src/lib.rs @@ -6,7 +6,7 @@ use bytes::{self, Buf, Bytes}; use core::pin::Pin; use futures_util::{Stream, StreamExt}; -use reqwest::Client; +use reqwest::{Client, StatusCode}; use std::io::{self, BufRead}; use std::task::{ready, Context, Poll}; use tokio::sync::mpsc; @@ -19,6 +19,7 @@ pub enum Error { Reqwest(reqwest::Error), Io(io::Error), JSON(serde_json::Error), + String(StatusCode), } impl From for Error { @@ -39,6 +40,17 @@ impl From for Error { } } + +pub struct StaticResourceRequest {} + +impl StaticResourceRequest { + pub fn new(rx: mpsc::Receiver) -> StaticResourceRequest { + StaticResourceRequest { + + } + } +} + /// An iterable wrapper of ResourceDiff items for the streamed chunks of Bytes /// received from the connection to the rdsys backend pub struct ResourceStream { @@ -220,7 +232,7 @@ mod tests { /// } /// } /// ``` -/// + pub async fn start_stream( api_endpoint: String, name: String, @@ -260,3 +272,41 @@ pub async fn start_stream( }); Ok(ResourceStream::new(rx)) } + +pub async fn request_resources( api_endpoint: String, + name: String, + token: String, + resource_types: Vec, +) -> Result, Error> { + let fetched_resources: Result, Error>; + let req = proto::ResourceRequest { + request_origin: name, + resource_types, + }; + let json = serde_json::to_string(&req)?; + + let auth_value = format!("Bearer {}", token); + + let client = Client::new(); + + let response = client + .get(api_endpoint) + .header("Authorization", &auth_value) + .body(json) + .send() + .await.unwrap(); + println!("Success? {:?}", response); + match response.status() { + reqwest::StatusCode::OK => { + fetched_resources = match response.json::>().await { + Ok(fetched_resources) => Ok(fetched_resources), + Err(e) => Err(Error::Reqwest(e)), + }; + } + other => { + fetched_resources = Err(Error::String(other)) + } + }; + println!("Resources: {:?}", fetched_resources); + fetched_resources +}