quick_rest: starting with building request

develop
Kinch 2023-12-12 11:06:42 +01:00
parent 7d28760614
commit ae36b37036
6 changed files with 78 additions and 11 deletions

17
.run/run rq.run.xml Normal file
View File

@ -0,0 +1,17 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="run rq" type="ShConfigurationType">
<option name="SCRIPT_TEXT" value="just build-qr run-qr" />
<option name="INDEPENDENT_SCRIPT_PATH" value="true" />
<option name="SCRIPT_PATH" value="" />
<option name="SCRIPT_OPTIONS" value="" />
<option name="INDEPENDENT_SCRIPT_WORKING_DIRECTORY" value="true" />
<option name="SCRIPT_WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="INDEPENDENT_INTERPRETER_PATH" value="true" />
<option name="INTERPRETER_PATH" value="/bin/zsh" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="EXECUTE_IN_TERMINAL" value="true" />
<option name="EXECUTE_SCRIPT_FILE" value="false" />
<envs />
<method v="2" />
</configuration>
</component>

View File

@ -1,17 +1,16 @@
print "Start quick_rest script"
local qr = require "quick_rest"
print(qr)
--local http_client = qr.http_client {
-- concurreny_limit = 10
--}
--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 {
-- username = "klara@bpa.de",
-- password = "abcdedf"
-- }
--
--
--print(request)
print(request)

View File

@ -1,7 +1,32 @@
use crate::http_request_body::HttpRequestBody;
use reqwest::Url;
use std::fmt::{Display, Formatter};
#[derive(Clone)]
pub struct HttpRequest {}
pub struct HttpRequest {
method: HttpMethod,
url: Url,
}
#[derive(Clone)]
pub enum HttpMethod {
POST,
}
impl Display for HttpRequest {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "<HttpRequest> {} {}", self.method, self.url)
}
}
impl Display for HttpMethod {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let name = match (self) {
HttpMethod::POST => "POST",
};
write!(f, "{name}")
}
}
impl HttpRequest {
pub fn get() -> HttpRequest {
todo!()
@ -9,8 +34,11 @@ impl HttpRequest {
pub fn put() -> HttpRequest {
todo!()
}
pub fn post() -> HttpRequest {
todo!()
pub fn post(url: Url) -> HttpRequest {
HttpRequest {
method: HttpMethod::POST,
url,
}
}
pub fn patch() -> HttpRequest {
todo!()

View File

@ -9,8 +9,3 @@ mod http_service_builder;
mod http_service_wrapper;
mod lua_bindings;
mod network_error;
#[mlua::lua_module]
fn quick_rest(lua: &Lua) -> LuaResult<LuaTable> {
let exports = lua.create_table()?;
Ok(exports)
}

View File

@ -0,0 +1,10 @@
use crate::http_request::HttpRequest;
use mlua::{MetaMethod, UserData, UserDataMethods};
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())
})
}
}

View File

@ -1,2 +1,20 @@
mod lua_http_request;
use crate::http_request::HttpRequest;
use mlua::prelude::{LuaResult, LuaTable};
use mlua::Lua;
use reqwest::Url;
fn post(lua: &Lua, url: String) -> LuaResult<HttpRequest> {
let url: Url = url.parse().expect("invalid url");
Ok(HttpRequest::post(url))
}
#[mlua::lua_module]
fn quick_rest(lua: &Lua) -> LuaResult<LuaTable> {
let exports = lua.create_table()?;
let request_table = lua.create_table()?;
request_table.set("post", lua.create_function(post)?)?;
exports.set("request", request_table)?;
Ok(exports)
}