Skip to content

Commit

Permalink
refactor request files
Browse files Browse the repository at this point in the history
  • Loading branch information
kayhhh committed Dec 19, 2023
1 parent 6ed43d9 commit 7ac815a
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 21 deletions.
38 changes: 24 additions & 14 deletions dwn-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ use axum::{
use base64::engine::general_purpose::URL_SAFE_NO_PAD;
use base64::Engine;
use dwn::{
data::{Data, JsonData},
features::{FeatureDetection, Records},
request::{
data::{Data, JsonData},
media_types::{Application, MediaType},
Message, Method, RecordIdGenerator, RequestBody,
Interface, Message, Method, RecordIdGenerator, RequestBody,
},
response::{MessageResult, ResponseBody, Status},
};
Expand Down Expand Up @@ -129,20 +129,30 @@ fn process_message(message: &Message) -> Result<MessageResult, Box<dyn std::erro
// Process message
info!("Received message: {:?}", message);

match message.descriptor.method {
Method::FeatureDetectionRead => {
let mut features = FeatureDetection::default();
if message.descriptor.method == Method::FeatureDetectionRead {
let mut features = FeatureDetection::default();

features.interfaces.records = Some(BTreeMap::from_iter(vec![
(Records::RecordsCommit.to_string(), true),
(Records::RecordsQuery.to_string(), true),
(Records::RecordsWrite.to_string(), true),
]));
features.interfaces.records = Some(BTreeMap::from_iter(vec![
(Records::RecordsCommit.to_string(), true),
(Records::RecordsQuery.to_string(), true),
(Records::RecordsWrite.to_string(), true),
]));

let value = serde_json::to_value(features)?;
let value = serde_json::to_value(features)?;

Ok(MessageResult::new(vec![value]))
}
_ => Err("Method not supported".into()),
return Ok(MessageResult::new(vec![value]));
};

match message.descriptor.interface {
Interface::Records => match message.descriptor.method {
Method::Write => {
info!("Writing record");

Ok(MessageResult::default())
}

_ => Err("Method not supported".into()),
},
_ => Err("Interface not supported".into()),
}
}
18 changes: 16 additions & 2 deletions dwn-server/tests/messages.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use dwn::{
data::JsonData,
features::FeatureDetection,
request::{DescriptorBuilder, Interface, Message, MessageBuilder, Method, RequestBody},
request::{
data::JsonData, DescriptorBuilder, Interface, Message, MessageBuilder, Method, RequestBody,
},
response::ResponseBody,
};
use dwn_server::StartOptions;
Expand Down Expand Up @@ -174,3 +175,16 @@ async fn requires_data_descriptors() {

expect_status(body, port, StatusCode::INTERNAL_SERVER_ERROR).await;
}

#[tokio::test]
async fn records_write() {
let port = spawn_server();

let msg = MessageBuilder::<JsonData>::new(
Interface::Records,
Method::Write,
JsonData(json!({
"foo": "bar",
})),
);
}
1 change: 0 additions & 1 deletion dwn/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod data;
pub mod features;
pub mod request;
pub mod response;
Expand Down
File renamed without changes.
9 changes: 6 additions & 3 deletions dwn/src/request.rs → dwn/src/request/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
pub use iana_media_types as media_types;
use crate::util::cid_from_bytes;
use data::Data;
use libipld_cbor::DagCborCodec;
use media_types::MediaType;
use serde::{Deserialize, Serialize};
use std::fmt::Display;

use crate::{data::Data, util::cid_from_bytes};
pub mod data;

pub use iana_media_types as media_types;

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct RequestBody {
Expand Down Expand Up @@ -107,7 +110,7 @@ pub enum Interface {
Permissions,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
pub enum Method {
Commit,
Configure,
Expand Down
11 changes: 10 additions & 1 deletion dwn/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,20 @@ pub struct MessageResult {
pub entries: Option<Vec<Value>>,
}

impl Default for MessageResult {
fn default() -> Self {
Self {
status: Status::new(200, Some("OK")),
entries: None,
}
}
}

impl MessageResult {
pub fn new(entries: Vec<Value>) -> Self {
Self {
status: Status::new(200, Some("OK")),
entries: Some(entries),
..Default::default()
}
}
}

0 comments on commit 7ac815a

Please sign in to comment.