Skip to content

Commit

Permalink
update edn to fit new FFI; tag 0.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Nov 3, 2021
1 parent 37b784a commit 8a8e1d1
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 29 deletions.
18 changes: 13 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "calcit_http"
version = "0.0.2"
version = "0.0.3"
authors = ["jiyinyiyong <jiyinyiyong@gmail.com>"]
edition = "2018"

Expand All @@ -13,6 +13,6 @@ crate-type = ["dylib"] # Creates dynamic lib
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
cirru_edn = "0.1.12"
cirru_parser = "0.1.8"
cirru_edn = "0.2.8"
cirru_parser = "0.1.12"
reqwest = { version = "0.11", features = ["blocking"] }
2 changes: 1 addition & 1 deletion calcit.cirru

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion compact.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{} (:package |fetch)
:configs $ {} (:init-fn |fetch.test/main!) (:reload-fn |fetch.test/reload!)
:modules $ []
:version |0.0.2
:version |0.0.3
:files $ {}
|fetch.core $ {}
:ns $ quote
Expand Down
49 changes: 30 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ use std::sync::Arc;
use std::thread::spawn;

pub fn wrap_ok(x: Edn) -> Edn {
Edn::List(vec![Edn::Keyword("ok".to_owned()), x])
Edn::List(vec![Edn::kwd("ok"), x])
}
pub fn wrap_err(x: Edn) -> Edn {
Edn::List(vec![Edn::Keyword("err".to_owned()), x])
Edn::List(vec![Edn::kwd("err"), x])
}

struct RequestSkeleton {
method: Method,
headers: HeaderMap,
body: String,
query: Vec<(String, String)>,
query: Vec<(Box<str>, Box<str>)>,
}

#[no_mangle]
pub fn abi_version() -> String {
String::from("0.0.1")
String::from("0.0.5")
}

#[no_mangle]
Expand All @@ -39,11 +39,11 @@ pub fn fetch(
let client = reqwest::blocking::Client::new();
let options = parse_request_options(&a1)?;
let builder = match options.method {
Method::GET => client.get(url),
Method::POST => client.post(url),
Method::PUT => client.put(url),
Method::PATCH => client.patch(url),
Method::DELETE => client.delete(url),
Method::GET => client.get(&*url),
Method::POST => client.post(&*url),
Method::PUT => client.put(&*url),
Method::PATCH => client.patch(&*url),
Method::DELETE => client.delete(&*url),
a => return Err(format!("unexpected method: {}", a)),
};

Expand All @@ -54,8 +54,8 @@ pub fn fetch(

let ret = match b.send() {
Ok(res) => match res.text() {
Ok(s) => handler(vec![wrap_ok(Edn::Str(s.to_string()))]),
Err(e) => handler(vec![wrap_err(Edn::Str(format!(
Ok(s) => handler(vec![wrap_ok(Edn::Str(s.to_string().into_boxed_str()))]),
Err(e) => handler(vec![wrap_err(Edn::str(format!(
"failed to turn body into text: {}",
e
)))]),
Expand Down Expand Up @@ -85,26 +85,32 @@ fn parse_request_options(info: &Edn) -> Result<RequestSkeleton, String> {

match info {
Edn::Map(m) => {
req.method = match m.get(&Edn::Keyword(String::from("method"))) {
Some(Edn::Keyword(k)) => k.parse::<Method>().map_err(|x| x.to_string())?,
req.method = match m.get(&Edn::kwd("method")) {
Some(Edn::Keyword(k)) => k.to_str().parse::<Method>().map_err(|x| x.to_string())?,
None => Method::GET,
Some(a) => return Err(format!("invalid method name: {}", a)),
};
req.body = match m.get(&Edn::Keyword(String::from("body"))) {
Some(Edn::Str(s)) => s.to_owned(),
req.body = match m.get(&Edn::kwd("body")) {
Some(Edn::Str(s)) => (*s).to_string(),
None => "".to_owned(),
Some(a) => a.to_string(),
};
match m.get(&Edn::Keyword(String::from("headers"))) {
match m.get(&Edn::kwd("headers")) {
Some(Edn::Map(xs)) => {
for (k, v) in xs {
match (k, v) {
(Edn::Str(k2), Edn::Str(v2)) | (Edn::Keyword(k2), Edn::Str(v2)) => {
(Edn::Str(k2), Edn::Str(v2)) => {
req.headers.insert(
k2.parse::<HeaderName>().unwrap(),
v2.parse::<HeaderValue>().unwrap(),
);
}
(Edn::Keyword(k2), Edn::Str(v2)) => {
req.headers.insert(
k2.to_str().parse::<HeaderName>().unwrap(),
v2.parse::<HeaderValue>().unwrap(),
);
}
_ => return Err(format!("expected strings for headers: {}, {}", k, v)),
}
}
Expand All @@ -115,17 +121,22 @@ fn parse_request_options(info: &Edn) -> Result<RequestSkeleton, String> {
Some(a) => return Err(format!("expected list of pairs for queries: {}", a)),
}

match m.get(&Edn::Keyword(String::from("query"))) {
match m.get(&Edn::kwd("query")) {
Some(Edn::List(xs)) => {
for x in xs {
if let Edn::List(ys) = x {
if ys.len() == 2 {
match (&ys[0], &ys[1]) {
(Edn::Str(k), Edn::Str(v)) | (Edn::Keyword(k), Edn::Str(v)) => {
(Edn::Str(k), Edn::Str(v)) => {
req.query.push((k.to_owned(), v.to_owned()));
// quit jump to next call
continue;
}
(Edn::Keyword(k), Edn::Str(v)) => {
req.query.push((k.to_str().to_owned(), v.to_owned()));
// quit jump to next call
continue;
}
(a, b) => return Err(format!("expected strings, got: {} {}", a, b)),
}
}
Expand Down

0 comments on commit 8a8e1d1

Please sign in to comment.