Skip to content

Commit

Permalink
add http body
Browse files Browse the repository at this point in the history
  • Loading branch information
xmxu committed Jan 12, 2022
1 parent bee2423 commit dbeac15
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 25 deletions.
1 change: 1 addition & 0 deletions anole/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ impl Engine {
}

debug!("store:{:?}", self.ctx.store);
self.ctx.store.clear();
info!("execute completed! cost_time:{:?}", cost.elapsed());
}
}
Expand Down
10 changes: 6 additions & 4 deletions anole/src/task/http.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{collections::HashMap, time::Duration};
use reqwest::Response;

use crate::{value::{Value, self}, capture::Capture, context::Context};
use crate::{value::{Value, self, Body}, capture::Capture, context::Context};

#[derive(Debug)]
pub enum Method {
Expand Down Expand Up @@ -124,7 +124,9 @@ impl HttpTask {
}
//body
if let Some(b) = &self.config.body {
request_builder = request_builder.body(b.to_owned());
if let Some(bb) = b.as_bytes(ctx) {
request_builder = request_builder.body(bb);
}
}
let rsp = request_builder.send().await.unwrap();
let is_success = (&rsp.status()).is_success();
Expand Down Expand Up @@ -174,7 +176,7 @@ pub struct HttpTaskBuilder {
pub(crate) header: Option<HashMap<String, Value>>,
pub(crate) query: Option<HashMap<String, Value>>,
pub(crate) form: Option<HashMap<String, Value>>,
pub(crate) body: Option<bytes::Bytes>,
pub(crate) body: Option<Body>,
pub(crate) capture: Option<Vec<Capture>>,
pub(crate) verbose: bool,
}
Expand Down Expand Up @@ -239,7 +241,7 @@ impl HttpTaskBuilder {
self
}

pub fn body(mut self, body: bytes::Bytes) ->Self {
pub fn body(mut self, body: Body) ->Self {
self.body = Some(body);
self
}
Expand Down
58 changes: 45 additions & 13 deletions anole/src/value.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use serde::Serialize;

use crate::error;

use crate::{context::Context, error};

#[derive(Debug, Clone)]
pub enum Value {
Expand All @@ -21,7 +20,7 @@ impl Value {
Self::Bool(v) => Ok(*v as i32),
Self::Str(v) => match v.parse::<i32>() {
Ok(i) => Ok(i),
Err(e) => Err(error::parse_value(e.into()))
Err(e) => Err(error::parse_value(e.into())),
},
}
}
Expand All @@ -34,7 +33,7 @@ impl Value {
Self::Bool(v) => Ok(*v as u32),
Self::Str(v) => match v.parse::<u32>() {
Ok(u) => Ok(u),
Err(e) => Err(error::parse_value(e.into()))
Err(e) => Err(error::parse_value(e.into())),
},
}
}
Expand All @@ -49,11 +48,11 @@ impl Value {
return Ok(1.0);
}
Ok(0.0)
},
}
Self::Str(v) => match v.parse::<f64>() {
Ok(f) => Ok(f),
Err(e) => Err(error::parse_value(e.into()))
}
Err(e) => Err(error::parse_value(e.into())),
},
}
}

Expand All @@ -65,7 +64,7 @@ impl Value {
Self::Bool(b) => Ok(*b),
Self::Str(s) => match s.parse::<bool>() {
Ok(b) => Ok(b),
Err(e) => Err(error::parse_value(e.into()))
Err(e) => Err(error::parse_value(e.into())),
},
}
}
Expand All @@ -80,15 +79,15 @@ impl Value {
}
}

pub fn as_wildcard(&self)-> Option<String> {
pub fn as_wildcard(&self) -> Option<String> {
match self {
Self::Str(s) => {
if s.starts_with(':') {
return Some(s.as_str()[1..s.len()].to_string());
}
None
},
_ => None
}
_ => None,
}
}
}
Expand Down Expand Up @@ -183,7 +182,8 @@ fn parse_number(s: &str) -> Option<usize> {
impl Serialize for Value {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer {
S: serde::Serializer,
{
match self {
Value::Bool(b) => serializer.serialize_bool(*b),
Value::I32(v) => serializer.serialize_i32(*v),
Expand All @@ -192,4 +192,36 @@ impl Serialize for Value {
Value::Str(s) => serializer.serialize_str(s),
}
}
}
}

#[derive(Debug)]
pub enum Body {
File(String),
Raw(bytes::Bytes),
Replace(String, Vec<Value>),
}

impl Body {
pub fn as_bytes(&self, ctx: &mut Context) -> Option<bytes::Bytes> {
match self {
Self::Raw(b) => Some(b.to_owned()),
Self::File(path) => {
if let Ok(file_content) = std::fs::read(path) {
return Some(bytes::Bytes::from(file_content));
}
None
}
Self::Replace(tmpl, values) => {
let mut tmpl = tmpl.to_owned();
for value in values {
if let Some(wildcard) = value.as_wildcard() {
if let Some(vv) = ctx.store.get(wildcard) {
tmpl = tmpl.replace(&value.as_str(), &vv.as_str());
}
}
}
Some(tmpl.into())
}
}
}
}
11 changes: 3 additions & 8 deletions examples/http.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anole::{engine::Engine, task::http::{HttpTaskBuilder, Method}, capture, value::Value};
use anole::{engine::Engine, task::http::{HttpTaskBuilder, Method}, capture};

#[macro_use]
extern crate log;
Expand All @@ -12,18 +12,13 @@ async fn main() {
.url("https://tvapi.dykkan.com/v1/tags".to_string())
.method(Method::Get)
.capture(vec![
capture::header("host".to_string(), "host".to_string()),
capture::header("date".to_string(), "header_date".to_string()),
capture::header("content-type".to_string(), "header_content-type".to_string()),
capture::json("code".to_string(), "code".to_string()),
capture::json("data.0".to_string(), "tag".to_string()),
capture::json("data.1".to_string(), "tag".to_string()),
])
.build())
.with_http(HttpTaskBuilder::new()
.url("https://tvapi.dykkan.com/v1/tag/:tag".to_string())
.method(Method::Get)
.header(("_tag_".to_string(), Value::Str(":tag".to_string())))
.query(("tag".to_string(), Value::Str(":tag".to_string())))
.verbose(false)
.build())
.run().await;

Expand Down

0 comments on commit dbeac15

Please sign in to comment.