generated from veeso-dev/rust-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
285 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
mod get_agencies; | ||
mod get_contract; | ||
mod get_token; | ||
|
||
use did::{HttpRequest, HttpResponse}; | ||
|
||
use self::get_contract::{GetContractRequest, GetContractResponse}; | ||
use self::get_token::{GetTokenRequest, GetTokenResponse}; | ||
use crate::app::Deferred; | ||
|
||
pub struct HttpApi; | ||
|
||
impl HttpApi { | ||
/// Handles an HTTP request | ||
pub async fn handle_http_request(req: HttpRequest) -> HttpResponse { | ||
// must be a GET request | ||
if req.method != "GET" { | ||
return HttpResponse::bad_request("expected GET method".to_string()); | ||
} | ||
// Must be a JSON-RPC request | ||
if req.headers.get("content-type").map(|s| s.as_ref()) != Some("application/json") { | ||
return HttpResponse::bad_request( | ||
"expected content-type: application/json".to_string(), | ||
); | ||
} | ||
let method = match req.decode_method() { | ||
Ok(request) => request, | ||
Err(response) => return response, | ||
}; | ||
|
||
match method.as_str() { | ||
"getContracts" => Self::get_contracts(), | ||
"getContract" => Self::get_contract(req), | ||
"getToken" => Self::get_token(req), | ||
"getAgencies" => Self::get_agencies(), | ||
_ => HttpResponse::bad_request("unknown method".to_string()), | ||
} | ||
} | ||
|
||
fn get_contracts() -> HttpResponse { | ||
let response = get_contract::GetContractsResponse::from(Deferred::get_signed_contracts()); | ||
|
||
HttpResponse::ok(response) | ||
} | ||
|
||
fn get_contract(req: HttpRequest) -> HttpResponse { | ||
let params = match req.decode_body::<GetContractRequest>() { | ||
Ok(request) => request, | ||
Err(response) => return response, | ||
}; | ||
let response = GetContractResponse::from(Deferred::get_contract(¶ms.id)); | ||
|
||
HttpResponse::ok(response) | ||
} | ||
|
||
fn get_token(req: HttpRequest) -> HttpResponse { | ||
let params = match req.decode_body::<GetTokenRequest>() { | ||
Ok(request) => request, | ||
Err(response) => return response, | ||
}; | ||
let response = GetTokenResponse::from(Deferred::get_token(¶ms.id)); | ||
|
||
HttpResponse::ok(response) | ||
} | ||
|
||
fn get_agencies() -> HttpResponse { | ||
let response = get_agencies::GetAgenciesResponse::from(Deferred::get_agencies()); | ||
|
||
HttpResponse::ok(response) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
use did::deferred::Agency; | ||
|
||
pub type GetAgenciesResponse = Vec<Agency>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use did::deferred::Contract; | ||
use did::ID; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Clone, Debug, Deserialize)] | ||
pub struct GetContractRequest { | ||
pub id: ID, | ||
} | ||
|
||
#[derive(Clone, Debug, Serialize, Deserialize)] | ||
pub struct GetContractResponse { | ||
contract: Option<Contract>, | ||
} | ||
|
||
impl From<Option<Contract>> for GetContractResponse { | ||
fn from(contract: Option<Contract>) -> Self { | ||
Self { contract } | ||
} | ||
} | ||
|
||
pub type GetContractsResponse = Vec<ID>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use did::deferred::{TokenIdentifier, TokenInfo}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Clone, Debug, Deserialize)] | ||
pub struct GetTokenRequest { | ||
pub id: TokenIdentifier, | ||
} | ||
|
||
#[derive(Clone, Debug, Serialize, Deserialize)] | ||
pub struct GetTokenResponse { | ||
token: Option<TokenInfo>, | ||
} | ||
|
||
impl From<Option<TokenInfo>> for GetTokenResponse { | ||
fn from(token: Option<TokenInfo>) -> Self { | ||
Self { token } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
use std::borrow::Cow; | ||
use std::collections::HashMap; | ||
|
||
use candid::CandidType; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_bytes::ByteBuf; | ||
|
||
/// A HTTP response. | ||
#[derive(Clone, Debug, CandidType, Deserialize)] | ||
pub struct HttpResponse { | ||
/// The HTTP status code. | ||
pub status_code: u16, | ||
/// The response header map. | ||
pub headers: HashMap<Cow<'static, str>, Cow<'static, str>>, | ||
/// The response body. | ||
pub body: ByteBuf, | ||
/// Whether the query call should be upgraded to an update call. | ||
pub upgrade: Option<bool>, | ||
} | ||
|
||
impl HttpResponse { | ||
pub fn new( | ||
status_code: u16, | ||
headers: HashMap<Cow<'static, str>, Cow<'static, str>>, | ||
body: ByteBuf, | ||
upgrade: Option<bool>, | ||
) -> Self { | ||
Self { | ||
status_code, | ||
headers, | ||
body, | ||
upgrade, | ||
} | ||
} | ||
|
||
/// Returns a new `HttpResponse` intended to be used for internal errors. | ||
pub fn internal_error(e: String) -> Self { | ||
let body = match serde_json::to_vec(&e) { | ||
Ok(bytes) => ByteBuf::from(&bytes[..]), | ||
Err(e) => ByteBuf::from(e.to_string().as_bytes()), | ||
}; | ||
|
||
Self { | ||
status_code: 500, | ||
headers: HashMap::from([("content-type".into(), "application/json".into())]), | ||
body, | ||
upgrade: None, | ||
} | ||
} | ||
|
||
/// Returns a new `HttpResponse` intended to be used for bad request | ||
pub fn bad_request(e: String) -> Self { | ||
let body = match serde_json::to_vec(&e) { | ||
Ok(bytes) => ByteBuf::from(&bytes[..]), | ||
Err(e) => ByteBuf::from(e.to_string().as_bytes()), | ||
}; | ||
|
||
Self { | ||
status_code: 400, | ||
headers: HashMap::from([("content-type".into(), "application/json".into())]), | ||
body, | ||
upgrade: None, | ||
} | ||
} | ||
|
||
/// Returns an OK response with the given body. | ||
pub fn ok<S>(body: S) -> Self | ||
where | ||
S: Serialize, | ||
{ | ||
let body = match serde_json::to_string(&body) { | ||
Ok(body) => body, | ||
Err(e) => return HttpResponse::internal_error(e.to_string()), | ||
}; | ||
Self::new( | ||
200, | ||
HashMap::from([("content-type".into(), "application/json".into())]), | ||
ByteBuf::from(body.as_bytes()), | ||
None, | ||
) | ||
} | ||
|
||
/// Upgrade response to update call. | ||
pub fn upgrade_response() -> Self { | ||
Self::new(204, HashMap::default(), ByteBuf::default(), Some(true)) | ||
} | ||
} | ||
|
||
/// The important components of an HTTP request. | ||
#[derive(Clone, Debug, CandidType, Deserialize)] | ||
pub struct HttpRequest { | ||
/// The HTTP method string. | ||
pub method: Cow<'static, str>, | ||
/// The URL that was visited. | ||
pub url: String, | ||
/// The request headers. | ||
pub headers: HashMap<Cow<'static, str>, Cow<'static, str>>, | ||
/// The request body. | ||
pub body: ByteBuf, | ||
} | ||
|
||
impl HttpRequest { | ||
pub fn new(data: &[u8]) -> Self { | ||
let mut headers = HashMap::new(); | ||
headers.insert("content-type".into(), "application/json".into()); | ||
Self { | ||
method: "POST".into(), | ||
url: "".into(), | ||
headers, | ||
body: ByteBuf::from(data), | ||
} | ||
} | ||
|
||
pub fn decode_body<S>(&self) -> Result<S, HttpResponse> | ||
where | ||
S: serde::de::DeserializeOwned, | ||
{ | ||
serde_json::from_slice::<HttpApiRequest<S>>(&self.body) | ||
.map_err(|_| HttpResponse::bad_request("Invalid request body".to_string())) | ||
.map(|m| m.params) | ||
} | ||
|
||
pub fn decode_method(&self) -> Result<String, HttpResponse> { | ||
serde_json::from_slice::<HttpApiMethod>(&self.body) | ||
.map_err(|_| HttpResponse::bad_request("Invalid request body".to_string())) | ||
.map(|m| m.method) | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug, Deserialize)] | ||
struct HttpApiMethod { | ||
pub method: String, | ||
} | ||
|
||
/// The important components of an HTTP request. | ||
#[derive(Clone, Debug, Deserialize)] | ||
pub struct HttpApiRequest<S> { | ||
pub method: String, | ||
pub params: S, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.