Added minimal sdk, simplified test using the sdk's client

This commit is contained in:
Philip (a-0) 2024-01-07 19:13:51 +01:00
parent a75c115761
commit 84784599a7
16 changed files with 379 additions and 83 deletions

View file

@ -10,6 +10,7 @@ axum = { version = "0.7.2", features = [ "macros" ] }
chrono = "0.4.31"
itertools = "0.12.0"
jsonwebtoken = "9.2.0"
reqwest = "0.11.23"
serde = { version = "1.0.166", features = [ "derive" ] }
serde_json = "1.0.99"
serde_with = "3.3.0"

View file

@ -0,0 +1,28 @@
use reqwest::Method;
use serde::{Serialize, Deserialize};
use super::UbisyncRequest;
#[derive(Serialize, Deserialize)]
pub struct AppRegisterRequest {
pub name: String,
pub description: String,
}
#[derive(Serialize, Deserialize)]
pub struct AppRegisterResponse {
pub token: String,
}
impl UbisyncRequest for AppRegisterRequest {
type PathParameters = ();
type Response = AppRegisterResponse;
fn method(&self) -> reqwest::Method {
Method::PUT
}
fn path(&self, _: Self::PathParameters) -> String {
"/app/register".to_string()
}
}

View file

@ -0,0 +1,88 @@
use reqwest::Method;
use serde::{Serialize, Deserialize};
use crate::types::{ElementContent, ElementId, Element};
use super::UbisyncRequest;
#[derive(Serialize, Deserialize)]
pub struct ElementCreateRequest {
pub content: ElementContent,
}
#[derive(Serialize, Deserialize)]
pub struct ElementCreateResponse {
pub id: ElementId,
}
impl UbisyncRequest for ElementCreateRequest {
type PathParameters = ();
type Response = ElementCreateResponse;
fn method(&self) -> Method {
Method::PUT
}
fn path(&self, _: Self::PathParameters) -> String {
"/element".to_string()
}
}
#[derive(Serialize, Deserialize)]
pub struct ElementGetRequest;
#[derive(Serialize, Deserialize)]
pub struct ElementGetResponse {
pub element: Element,
}
impl UbisyncRequest for ElementGetRequest {
type PathParameters = ElementId;
type Response = ElementGetResponse;
fn method(&self) -> Method {
Method::GET
}
fn path(&self, params: Self::PathParameters) -> String {
format!("/element/{}", params.to_string())
}
}
#[derive(Serialize, Deserialize)]
pub struct ElementSetRequest {
pub content: ElementContent,
}
#[derive(Serialize, Deserialize)]
pub struct ElementSetResponse;
impl UbisyncRequest for ElementSetRequest {
type PathParameters = ElementId;
type Response = ElementSetResponse;
fn method(&self) -> Method {
Method::POST
}
fn path(&self, params: Self::PathParameters) -> String {
format!("/element/{}", serde_json::to_string(&params).unwrap())
}
}
#[derive(Serialize, Deserialize)]
pub struct ElementRemoveRequest;
#[derive(Serialize, Deserialize)]
pub struct ElementRemoveResponse;
impl UbisyncRequest for ElementRemoveRequest {
type PathParameters = ElementId;
type Response = ElementRemoveResponse;
fn method(&self) -> Method {
Method::DELETE
}
fn path(&self, params: Self::PathParameters) -> String {
format!("/element/{}", serde_json::to_string(&params).unwrap())
}
}

View file

@ -0,0 +1,23 @@
use async_trait::async_trait;
use reqwest::Method;
use serde::{Deserialize, Serialize};
pub mod app;
pub mod element;
/// Any struct defining a request body for the ubisync API must implement this trait
/// It is used both by the client in the SDK and by the API logic in the ubisync node
#[async_trait]
pub trait UbisyncRequest: for<'de> Deserialize<'de> + Serialize {
type PathParameters;
type Response: for<'de> Deserialize<'de> + Serialize;
fn method(&self) -> Method;
fn path(&self, params: Self::PathParameters) -> String;
async fn parse_response(resp: reqwest::Response) -> Result<Self::Response, reqwest::Error>
where
for<'de> <Self as UbisyncRequest>::Response: Deserialize<'de>,
{
resp.json().await
}
}

View file

@ -1,3 +1,4 @@
pub mod api;
pub mod messages;
pub mod types;
pub mod peer;
pub mod types;