Add function to start a resource-stream connection
At the moment the start_stream function very simply prints out each response as it arrives.
This commit is contained in:
parent
64247c1ceb
commit
c89aa4a9f9
|
@ -1,5 +1,5 @@
|
|||
[package]
|
||||
name = "rdsys-backend"
|
||||
name = "rdsys_backend"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
|
@ -7,4 +7,7 @@ edition = "2021"
|
|||
|
||||
[dependencies]
|
||||
serde_json = "1"
|
||||
futures-util = { version = "0.3"}
|
||||
serde = { version = "1", features = ["derive"]}
|
||||
|
||||
reqwest = { version = "0.11", features = ["stream"]}
|
||||
|
|
|
@ -0,0 +1,52 @@
|
|||
use reqwest::Client;
|
||||
use futures_util::StreamExt;
|
||||
use std::io;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
Reqwest (reqwest::Error),
|
||||
Io (io::Error),
|
||||
JSON (serde_json::Error),
|
||||
}
|
||||
|
||||
impl From<serde_json::Error> for Error {
|
||||
fn from(value: serde_json::Error) -> Self {
|
||||
Self::JSON(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<reqwest::Error> for Error {
|
||||
fn from(value: reqwest::Error) -> Self {
|
||||
Self::Reqwest(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<io::Error> for Error {
|
||||
fn from(value: io::Error) -> Self {
|
||||
Self::Io(value)
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn start_stream(api_endpoint: String, name: String, token: String, resource_types: Vec<String> ) -> Result<(), Error> {
|
||||
|
||||
let req = crate::ResourceRequest {
|
||||
request_origin: name,
|
||||
resource_types: resource_types,
|
||||
};
|
||||
let json = serde_json::to_string(&req)?;
|
||||
|
||||
let auth_value = format!("Bearer {}",token);
|
||||
|
||||
let client = Client::new();
|
||||
let mut stream = client.get(api_endpoint)
|
||||
.header("Authorization", &auth_value)
|
||||
.body(json)
|
||||
.send()
|
||||
.await?
|
||||
.bytes_stream();
|
||||
|
||||
while let Some(chunk) = stream.next().await {
|
||||
println!("Chunk: {:?}", chunk?);
|
||||
};
|
||||
Ok(())
|
||||
}
|
|
@ -2,6 +2,8 @@ use std::collections::HashMap;
|
|||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub mod http;
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct ResourceRequest {
|
||||
request_origin: String,
|
||||
|
|
Loading…
Reference in New Issue