From f6add08960da458852e844f3fe9a60abdb79aed8 Mon Sep 17 00:00:00 2001 From: Kinch Date: Tue, 12 Dec 2023 11:27:15 +0100 Subject: [PATCH] quick_rest: can set http headers for request --- lua/qr.example.lua | 12 ++++----- lua_modules/quick_rest/src/http_headers.rs | 27 +++++++++++++++++++ lua_modules/quick_rest/src/http_request.rs | 13 ++++++--- lua_modules/quick_rest/src/lib.rs | 1 + .../src/lua_bindings/lua_http_request.rs | 8 ++++-- 5 files changed, 50 insertions(+), 11 deletions(-) create mode 100644 lua_modules/quick_rest/src/http_headers.rs diff --git a/lua/qr.example.lua b/lua/qr.example.lua index 201f0a3..e6a2dc4 100644 --- a/lua/qr.example.lua +++ b/lua/qr.example.lua @@ -7,10 +7,10 @@ local qr = require "quick_rest" --} local request = qr.request.post("https://api.internal.insiders.cloud/1/rest/accounts/authentication/requesttoken") --- .json { --- username = "klara@bpa.de", --- password = "abcdedf" --- } --- --- + :json { + username = "klara@bpa.de", + password = "abcdedf" + } + + print(request) diff --git a/lua_modules/quick_rest/src/http_headers.rs b/lua_modules/quick_rest/src/http_headers.rs new file mode 100644 index 0000000..59cf2de --- /dev/null +++ b/lua_modules/quick_rest/src/http_headers.rs @@ -0,0 +1,27 @@ +use std::collections::HashMap; +use std::fmt::{Display, Formatter}; + +#[derive(Clone, Default)] +pub struct HttpHeaders { + map: HashMap, +} + +impl HttpHeaders { + pub fn add(&self, key: impl Into, value: impl Into) -> Self { + let mut result = self.clone(); + result.map.insert(key.into(), value.into()); + result + } +} + +impl Display for HttpHeaders { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + if self.map.is_empty() { + return Ok(()); + } + for (key, value) in self.map.iter() { + writeln!(f, "{}: {}", key, value)?; + } + writeln!(f, "") + } +} diff --git a/lua_modules/quick_rest/src/http_request.rs b/lua_modules/quick_rest/src/http_request.rs index 302fe45..381a56d 100644 --- a/lua_modules/quick_rest/src/http_request.rs +++ b/lua_modules/quick_rest/src/http_request.rs @@ -1,3 +1,4 @@ +use crate::http_headers::HttpHeaders; use crate::http_request_body::HttpRequestBody; use reqwest::Url; use std::fmt::{Display, Formatter}; @@ -6,6 +7,7 @@ use std::fmt::{Display, Formatter}; pub struct HttpRequest { method: HttpMethod, url: Url, + headers: HttpHeaders, } #[derive(Clone)] @@ -15,7 +17,9 @@ pub enum HttpMethod { impl Display for HttpRequest { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - write!(f, " {} {}", self.method, self.url) + writeln!(f, " {} {}", self.method, self.url)?; + writeln!(f, "{}", self.headers)?; + Ok(()) } } @@ -38,6 +42,7 @@ impl HttpRequest { HttpRequest { method: HttpMethod::POST, url, + headers: Default::default(), } } pub fn patch() -> HttpRequest { @@ -58,8 +63,10 @@ impl HttpRequest { pub fn add_query_param(&self, key: String, value: String) -> HttpRequest { todo!() } - pub fn add_header(&self, key: String, value: String) -> HttpRequest { - todo!() + pub fn add_header(&self, key: impl Into, value: impl Into) -> HttpRequest { + let mut result = self.clone(); + result.headers = result.headers.add(key, value); + result } pub fn body(&self, body: HttpRequestBody) -> HttpRequest { diff --git a/lua_modules/quick_rest/src/lib.rs b/lua_modules/quick_rest/src/lib.rs index b15eca2..e0bb4b5 100644 --- a/lua_modules/quick_rest/src/lib.rs +++ b/lua_modules/quick_rest/src/lib.rs @@ -9,3 +9,4 @@ mod http_service_builder; mod http_service_wrapper; mod lua_bindings; mod network_error; +mod http_headers; diff --git a/lua_modules/quick_rest/src/lua_bindings/lua_http_request.rs b/lua_modules/quick_rest/src/lua_bindings/lua_http_request.rs index 366c9d0..2e96d80 100644 --- a/lua_modules/quick_rest/src/lua_bindings/lua_http_request.rs +++ b/lua_modules/quick_rest/src/lua_bindings/lua_http_request.rs @@ -1,10 +1,14 @@ use crate::http_request::HttpRequest; -use mlua::{MetaMethod, UserData, UserDataMethods}; +use mlua::{MetaMethod, UserData, UserDataMethods, Value}; impl UserData for HttpRequest { fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) { methods.add_meta_method(MetaMethod::ToString, |_, http_request, _: ()| { Ok(http_request.to_string()) - }) + }); + methods.add_method("json", |lua, http_request, value: Value| { + let http_request = http_request.add_header("Content-Type", "application/json"); + Ok(http_request) + }); } }