nushell/src/commands/post.rs

389 lines
16 KiB
Rust
Raw Normal View History

2019-08-31 06:08:59 +02:00
use crate::commands::UnevaluatedCallInfo;
2019-08-30 20:27:15 +02:00
use crate::context::SpanSource;
use crate::errors::ShellError;
2019-08-31 06:08:59 +02:00
use crate::object::Value;
2019-08-30 20:27:15 +02:00
use crate::parser::hir::SyntaxType;
use crate::parser::registry::Signature;
use crate::prelude::*;
use base64::encode;
use mime::Mime;
2019-08-31 06:08:59 +02:00
use std::path::PathBuf;
2019-08-30 20:27:15 +02:00
use std::str::FromStr;
use surf::mime;
use uuid::Uuid;
pub struct Post;
impl PerItemCommand for Post {
fn name(&self) -> &str {
"post"
}
fn signature(&self) -> Signature {
Signature::build(self.name())
.required("path", SyntaxType::Any)
.required("body", SyntaxType::Any)
.named("user", SyntaxType::Any)
.named("password", SyntaxType::Any)
.switch("raw")
}
fn usage(&self) -> &str {
"Post content to a url and retrieve data as a table if possible."
}
2019-08-30 20:27:15 +02:00
fn run(
&self,
call_info: &CallInfo,
2019-08-31 06:08:59 +02:00
registry: &CommandRegistry,
raw_args: &RawCommandArgs,
2019-08-30 20:27:15 +02:00
_input: Tagged<Value>,
) -> Result<OutputStream, ShellError> {
2019-08-31 06:08:59 +02:00
run(call_info, registry, raw_args)
2019-08-30 20:27:15 +02:00
}
}
2019-08-31 06:08:59 +02:00
fn run(
call_info: &CallInfo,
registry: &CommandRegistry,
raw_args: &RawCommandArgs,
) -> Result<OutputStream, ShellError> {
let call_info = call_info.clone();
2019-08-30 20:27:15 +02:00
let path = match call_info
.args
.nth(0)
2019-08-31 06:08:59 +02:00
.ok_or_else(|| ShellError::string(&format!("No url specified")))?
2019-08-30 20:27:15 +02:00
{
2019-08-31 06:08:59 +02:00
file => file.clone(),
2019-08-30 20:27:15 +02:00
};
let body = match call_info
.args
.nth(1)
.ok_or_else(|| ShellError::string(&format!("No body specified")))?
{
2019-08-31 06:08:59 +02:00
file => file.clone(),
2019-08-30 20:27:15 +02:00
};
let path_str = path.as_string()?;
let path_span = path.span();
let has_raw = call_info.args.has("raw");
let user = call_info.args.get("user").map(|x| x.as_string().unwrap());
let password = call_info
.args
.get("password")
.map(|x| x.as_string().unwrap());
2019-08-31 06:08:59 +02:00
let registry = registry.clone();
let raw_args = raw_args.clone();
2019-08-30 20:27:15 +02:00
let stream = async_stream_block! {
let (file_extension, contents, contents_tag, span_source) =
2019-08-31 06:08:59 +02:00
post(&path_str, &body, user, password, path_span, &registry, &raw_args).await.unwrap();
let file_extension = if has_raw {
None
} else {
// If the extension could not be determined via mimetype, try to use the path
// extension. Some file types do not declare their mimetypes (such as bson files).
file_extension.or(path_str.split('.').last().map(String::from))
};
if let Some(uuid) = contents_tag.origin {
// If we have loaded something, track its source
yield ReturnSuccess::action(CommandAction::AddSpanSource(
uuid,
span_source,
));
}
2019-08-30 20:27:15 +02:00
2019-08-31 06:08:59 +02:00
let tagged_contents = contents.tagged(contents_tag);
2019-08-30 20:27:15 +02:00
2019-08-31 06:08:59 +02:00
if let Some(extension) = file_extension {
let command_name = format!("from-{}", extension);
if let Some(converter) = registry.get_command(&command_name) {
let new_args = RawCommandArgs {
host: raw_args.host,
shell_manager: raw_args.shell_manager,
call_info: UnevaluatedCallInfo {
args: crate::parser::hir::Call {
head: raw_args.call_info.args.head,
positional: None,
named: None
},
source: raw_args.call_info.source,
source_map: raw_args.call_info.source_map,
name_span: raw_args.call_info.name_span,
}
};
let mut result = converter.run(new_args.with_input(vec![tagged_contents]), &registry);
let result_vec: Vec<Result<ReturnSuccess, ShellError>> = result.drain_vec().await;
for res in result_vec {
match res {
Ok(ReturnSuccess::Value(Tagged { item: Value::List(list), ..})) => {
for l in list {
yield Ok(ReturnSuccess::Value(l));
}
}
Ok(ReturnSuccess::Value(Tagged { item, .. })) => {
yield Ok(ReturnSuccess::Value(Tagged { item, tag: contents_tag }));
2019-08-31 06:08:59 +02:00
}
x => yield x,
}
}
} else {
yield ReturnSuccess::value(tagged_contents);
}
} else {
yield ReturnSuccess::value(tagged_contents);
}
2019-08-30 20:27:15 +02:00
};
Ok(stream.to_output_stream())
}
pub async fn post(
location: &str,
2019-08-31 06:08:59 +02:00
body: &Tagged<Value>,
2019-08-30 20:27:15 +02:00
user: Option<String>,
password: Option<String>,
span: Span,
2019-08-31 06:08:59 +02:00
registry: &CommandRegistry,
raw_args: &RawCommandArgs,
2019-08-30 20:27:15 +02:00
) -> Result<(Option<String>, Value, Tag, SpanSource), ShellError> {
2019-08-31 06:08:59 +02:00
let registry = registry.clone();
let raw_args = raw_args.clone();
2019-08-30 20:27:15 +02:00
if location.starts_with("http:") || location.starts_with("https:") {
2019-09-01 08:44:56 +02:00
let login = match (user, password) {
(Some(user), Some(password)) => Some(encode(&format!("{}:{}", user, password))),
(Some(user), _) => Some(encode(&format!("{}:", user))),
_ => None,
};
2019-08-31 06:08:59 +02:00
let response = match body {
Tagged {
item: Value::Primitive(Primitive::String(body_str)),
..
} => {
2019-09-01 08:44:56 +02:00
let mut s = surf::post(location).body_string(body_str.to_string());
if let Some(login) = login {
s = s.set_header("Authorization", format!("Basic {}", login));
}
s.await
2019-08-31 06:08:59 +02:00
}
Tagged {
item: Value::Binary(b),
..
} => {
2019-09-01 08:44:56 +02:00
let mut s = surf::post(location).body_bytes(b);
if let Some(login) = login {
s = s.set_header("Authorization", format!("Basic {}", login));
}
s.await
2019-08-31 06:08:59 +02:00
}
Tagged { item, tag } => {
if let Some(converter) = registry.get_command("to-json") {
let new_args = RawCommandArgs {
host: raw_args.host,
shell_manager: raw_args.shell_manager,
call_info: UnevaluatedCallInfo {
args: crate::parser::hir::Call {
head: raw_args.call_info.args.head,
positional: None,
named: None,
},
source: raw_args.call_info.source,
source_map: raw_args.call_info.source_map,
name_span: raw_args.call_info.name_span,
},
};
let mut result = converter.run(
new_args.with_input(vec![item.clone().tagged(tag.clone())]),
&registry,
);
let result_vec: Vec<Result<ReturnSuccess, ShellError>> =
result.drain_vec().await;
let mut result_string = String::new();
for res in result_vec {
match res {
Ok(ReturnSuccess::Value(Tagged {
item: Value::Primitive(Primitive::String(s)),
..
})) => {
result_string.push_str(&s);
}
_ => {
return Err(ShellError::labeled_error(
"Save could not successfully save",
"unexpected data during save",
span,
));
}
}
}
2019-09-01 08:44:56 +02:00
let mut s = surf::post(location).body_string(result_string);
if let Some(login) = login {
s = s.set_header("Authorization", format!("Basic {}", login));
}
s.await
2019-08-31 06:08:59 +02:00
} else {
return Err(ShellError::labeled_error(
"Could not automatically convert table",
"needs manual conversion",
tag.span,
));
}
}
};
2019-08-30 20:27:15 +02:00
match response {
Ok(mut r) => match r.headers().get("content-type") {
Some(content_type) => {
let content_type = Mime::from_str(content_type).unwrap();
match (content_type.type_(), content_type.subtype()) {
(mime::APPLICATION, mime::XML) => Ok((
Some("xml".to_string()),
Value::string(r.body_string().await.map_err(|_| {
ShellError::labeled_error(
"Could not load text from remote url",
"could not load",
span,
)
})?),
Tag {
span,
origin: Some(Uuid::new_v4()),
},
SpanSource::Url(location.to_string()),
)),
(mime::APPLICATION, mime::JSON) => Ok((
Some("json".to_string()),
Value::string(r.body_string().await.map_err(|_| {
ShellError::labeled_error(
"Could not load text from remote url",
"could not load",
span,
)
})?),
Tag {
span,
origin: Some(Uuid::new_v4()),
},
SpanSource::Url(location.to_string()),
)),
(mime::APPLICATION, mime::OCTET_STREAM) => {
let buf: Vec<u8> = r.body_bytes().await.map_err(|_| {
ShellError::labeled_error(
"Could not load binary file",
"could not load",
span,
)
})?;
Ok((
None,
Value::Binary(buf),
Tag {
span,
origin: Some(Uuid::new_v4()),
},
SpanSource::Url(location.to_string()),
))
}
(mime::IMAGE, image_ty) => {
let buf: Vec<u8> = r.body_bytes().await.map_err(|_| {
ShellError::labeled_error(
"Could not load image file",
"could not load",
span,
)
})?;
Ok((
Some(image_ty.to_string()),
Value::Binary(buf),
Tag {
span,
origin: Some(Uuid::new_v4()),
},
SpanSource::Url(location.to_string()),
))
}
(mime::TEXT, mime::HTML) => Ok((
Some("html".to_string()),
Value::string(r.body_string().await.map_err(|_| {
ShellError::labeled_error(
"Could not load text from remote url",
"could not load",
span,
)
})?),
Tag {
span,
origin: Some(Uuid::new_v4()),
},
SpanSource::Url(location.to_string()),
)),
(mime::TEXT, mime::PLAIN) => {
let path_extension = url::Url::parse(location)
.unwrap()
.path_segments()
.and_then(|segments| segments.last())
.and_then(|name| if name.is_empty() { None } else { Some(name) })
.and_then(|name| {
PathBuf::from(name)
.extension()
.map(|name| name.to_string_lossy().to_string())
});
Ok((
path_extension,
Value::string(r.body_string().await.map_err(|_| {
ShellError::labeled_error(
"Could not load text from remote url",
"could not load",
span,
)
})?),
Tag {
span,
origin: Some(Uuid::new_v4()),
},
SpanSource::Url(location.to_string()),
))
}
(ty, sub_ty) => Ok((
None,
Value::string(format!(
"Not yet supported MIME type: {} {}",
ty, sub_ty
)),
Tag {
span,
origin: Some(Uuid::new_v4()),
},
SpanSource::Url(location.to_string()),
)),
}
}
None => Ok((
None,
Value::string(format!("No content type found")),
Tag {
span,
origin: Some(Uuid::new_v4()),
},
SpanSource::Url(location.to_string()),
)),
},
Err(_) => {
return Err(ShellError::labeled_error(
"URL could not be opened",
"url not found",
span,
));
}
}
} else {
Err(ShellError::labeled_error(
"Expected a url",
"needs a url",
span,
))
}
}