quick_rest: can set http headers for request
parent
ae36b37036
commit
f6add08960
|
|
@ -7,10 +7,10 @@ local qr = require "quick_rest"
|
||||||
--}
|
--}
|
||||||
|
|
||||||
local request = qr.request.post("https://api.internal.insiders.cloud/1/rest/accounts/authentication/requesttoken")
|
local request = qr.request.post("https://api.internal.insiders.cloud/1/rest/accounts/authentication/requesttoken")
|
||||||
-- .json {
|
:json {
|
||||||
-- username = "klara@bpa.de",
|
username = "klara@bpa.de",
|
||||||
-- password = "abcdedf"
|
password = "abcdedf"
|
||||||
-- }
|
}
|
||||||
--
|
|
||||||
--
|
|
||||||
print(request)
|
print(request)
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,27 @@
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::fmt::{Display, Formatter};
|
||||||
|
|
||||||
|
#[derive(Clone, Default)]
|
||||||
|
pub struct HttpHeaders {
|
||||||
|
map: HashMap<String, String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl HttpHeaders {
|
||||||
|
pub fn add(&self, key: impl Into<String>, value: impl Into<String>) -> 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, "")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
use crate::http_headers::HttpHeaders;
|
||||||
use crate::http_request_body::HttpRequestBody;
|
use crate::http_request_body::HttpRequestBody;
|
||||||
use reqwest::Url;
|
use reqwest::Url;
|
||||||
use std::fmt::{Display, Formatter};
|
use std::fmt::{Display, Formatter};
|
||||||
|
|
@ -6,6 +7,7 @@ use std::fmt::{Display, Formatter};
|
||||||
pub struct HttpRequest {
|
pub struct HttpRequest {
|
||||||
method: HttpMethod,
|
method: HttpMethod,
|
||||||
url: Url,
|
url: Url,
|
||||||
|
headers: HttpHeaders,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
|
|
@ -15,7 +17,9 @@ pub enum HttpMethod {
|
||||||
|
|
||||||
impl Display for HttpRequest {
|
impl Display for HttpRequest {
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||||
write!(f, "<HttpRequest> {} {}", self.method, self.url)
|
writeln!(f, "<HttpRequest> {} {}", self.method, self.url)?;
|
||||||
|
writeln!(f, "{}", self.headers)?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -38,6 +42,7 @@ impl HttpRequest {
|
||||||
HttpRequest {
|
HttpRequest {
|
||||||
method: HttpMethod::POST,
|
method: HttpMethod::POST,
|
||||||
url,
|
url,
|
||||||
|
headers: Default::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn patch() -> HttpRequest {
|
pub fn patch() -> HttpRequest {
|
||||||
|
|
@ -58,8 +63,10 @@ impl HttpRequest {
|
||||||
pub fn add_query_param(&self, key: String, value: String) -> HttpRequest {
|
pub fn add_query_param(&self, key: String, value: String) -> HttpRequest {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
pub fn add_header(&self, key: String, value: String) -> HttpRequest {
|
pub fn add_header(&self, key: impl Into<String>, value: impl Into<String>) -> HttpRequest {
|
||||||
todo!()
|
let mut result = self.clone();
|
||||||
|
result.headers = result.headers.add(key, value);
|
||||||
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn body(&self, body: HttpRequestBody) -> HttpRequest {
|
pub fn body(&self, body: HttpRequestBody) -> HttpRequest {
|
||||||
|
|
|
||||||
|
|
@ -9,3 +9,4 @@ mod http_service_builder;
|
||||||
mod http_service_wrapper;
|
mod http_service_wrapper;
|
||||||
mod lua_bindings;
|
mod lua_bindings;
|
||||||
mod network_error;
|
mod network_error;
|
||||||
|
mod http_headers;
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,14 @@
|
||||||
use crate::http_request::HttpRequest;
|
use crate::http_request::HttpRequest;
|
||||||
use mlua::{MetaMethod, UserData, UserDataMethods};
|
use mlua::{MetaMethod, UserData, UserDataMethods, Value};
|
||||||
|
|
||||||
impl UserData for HttpRequest {
|
impl UserData for HttpRequest {
|
||||||
fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) {
|
fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) {
|
||||||
methods.add_meta_method(MetaMethod::ToString, |_, http_request, _: ()| {
|
methods.add_meta_method(MetaMethod::ToString, |_, http_request, _: ()| {
|
||||||
Ok(http_request.to_string())
|
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)
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue