From 1f8d1c1304491609e30f1440d69c2f189124e472 Mon Sep 17 00:00:00 2001 From: theMackabu Date: Sat, 9 Dec 2023 13:22:31 -0800 Subject: [PATCH] add complete http module --- app.routes | 26 +++++++++++++-- src/main.rs | 94 +++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 111 insertions(+), 9 deletions(-) diff --git a/app.routes b/app.routes index fd0045f..00dbfc8 100644 --- a/app.routes +++ b/app.routes @@ -7,11 +7,31 @@ hello() { } example() { - html(http::get("https://example.org")) + html(http::get("https://example.org").body) } example/json() { - text(http::get("https://httpbin.org//json")) + let res = http::get("https://httpbin.org//json"); + let body = #{ + response: res.json, + info: #{ + length: res.length, + status: res.status, + error: res.error, + body: res.body, + } + }; + + json(body) +} + +example/post() { + let body = #{ + hello: "world", + url: url, + }; + + json(http::post("https://httpbin.org//post", body).json) } test.json() { @@ -35,7 +55,7 @@ test/loadfile() { // remove to test 404 route * { - text("this is a wildcard route\ncurrently on: " + path) + text("this is a wildcard route\ncurrently on: " + path + "\n\n" + err) } 404 { diff --git a/src/main.rs b/src/main.rs index 2c3e20c..ad9c2b4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use fancy_regex::{Captures, Error, Regex}; use lazy_static::lazy_static; -use macros_rs::{crashln, ternary}; +use macros_rs::{crashln, string, ternary}; use reqwest::blocking::Client; use std::{fs, path::PathBuf}; @@ -71,19 +71,99 @@ mod status { #[export_module] mod http { - pub fn get(url: String) -> String { + #[derive(Debug, Clone)] + pub struct Http { + pub length: Option, + pub status: u16, + pub err: Option, + pub body: Option, + } + + pub fn get(url: String) -> Http { + let client = Client::new(); + let response = + match client.get(url).send() { + Ok(res) => res, + Err(err) => { + return Http { + length: Some(0), + status: 0, + err: Some(err.to_string()), + body: None, + } + } + }; + + if response.status().is_success() { + Http { + length: response.content_length(), + status: response.status().as_u16(), + err: None, + body: Some(response.text().unwrap()), + } + } else { + Http { + length: response.content_length(), + status: response.status().as_u16(), + err: Some(response.text().unwrap()), + body: None, + } + } + } + + pub fn post(url: String, data: Map) -> Http { let client = Client::new(); - let response = match client.get(url).send() { + + let data = + match serde_json::to_string(&data) { + Ok(result) => result, + Err(err) => err.to_string(), + }; + + let response = match client.post(url).body(data).send() { Ok(res) => res, - Err(err) => return err.to_string(), + Err(err) => { + return Http { + length: Some(0), + status: 0, + err: Some(err.to_string()), + body: None, + } + } }; if response.status().is_success() { - response.text().unwrap() + Http { + length: response.content_length(), + status: response.status().as_u16(), + err: None, + body: Some(response.text().unwrap()), + } } else { - format!("request failed with status code: {}", response.status()) + Http { + length: response.content_length(), + status: response.status().as_u16(), + err: Some(response.text().unwrap()), + body: None, + } } } + + // add err handling + #[rhai_fn(get = "json")] + pub fn json(res: Http) -> Map { serde_json::from_str(&res.body.unwrap()).unwrap() } + + #[rhai_fn(get = "status")] + pub fn status(res: Http) -> i64 { res.status as i64 } + + #[rhai_fn(get = "length")] + pub fn length(res: Http) -> i64 { res.length.unwrap() as i64 } + + #[rhai_fn(get = "body")] + pub fn body(res: Http) -> String { res.body.unwrap_or(string!("")) } + + #[rhai_fn(get = "error")] + pub fn error(res: Http) -> String { res.err.unwrap_or(string!("")) } } #[get("{url:.*}")] @@ -169,6 +249,8 @@ async fn handler(url: Path, req: HttpRequest) -> impl Responder { } }; + println!("{}: {} (status={}, type={})", req.method(), req.uri(), status_code, content_type); + HttpResponse::build(status_code).content_type(content_type).body(body) } -- GitLab