stof_http/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
//
// Copyright 2024 Formata, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
use std::{collections::BTreeMap, io::Read, ops::Deref, time::Duration};
use bytes::Bytes;
use stof::{lang::SError, Library, SDoc, SNodeRef, SUnits, SVal};
use ureq::Agent;
pub mod server;
#[derive(Debug)]
pub struct HTTPLibrary {
pub agent: Agent,
}
impl Default for HTTPLibrary {
fn default() -> Self {
Self {
agent: ureq::AgentBuilder::new()
.timeout_read(Duration::from_secs(5))
.timeout_write(Duration::from_secs(5))
.build(),
}
}
}
impl Library for HTTPLibrary {
/// Scope of this library.
/// This is how this library is invoked from Stof.
/// Ex. `HTTP.get('https://example.com')`
fn scope(&self) -> String {
"HTTP".to_string()
}
/// Call an HTTP method in this library.
///
/// Supported functions:
/// - HTTP.get
/// - HTTP.head
/// - HTTP.patch
/// - HTTP.post
/// - HTTP.put
/// - HTTP.delete
///
/// Parameters (in order) for each call:
/// - url: str - The HTTP request path (REQUIRED)
/// - headers: vec[(str, str)] | map - The request headers (OPTIONAL)
/// - body: str | blob - The request body (OPTIONAL)
/// - timeout: float | units - The overall timeout for the request (OPTIONAL) (default 5 seconds - use time units as needed)
/// - response_obj: obj - A response object to parse the response into via doc.header_import with the content type (OPTIONAL)
///
/// Basic GET request: `HTTP.get('https://example.com')`
///
/// POST request with a body: `HTTP.post('https://example.com', 'this is a string body to send')`
///
/// POST request json body and a timeout: `HTTP.post('https://example.com', map(('content-type', 'application/json')), stringify(self, 'json'), 10s)`
fn call(&self, pid: &str, doc: &mut SDoc, name: &str, parameters: &mut Vec<SVal>) -> Result<SVal, SError> {
let url;
if parameters.len() > 0 {
match ¶meters[0] {
SVal::String(val) => {
url = val.clone();
},
SVal::Boxed(val) => {
let val = val.lock().unwrap();
let val = val.deref();
match val {
SVal::String(val) => {
url = val.clone();
},
_ => {
return Err(SError::custom(pid, &doc, "HTTPError", "url must be a string"));
}
}
},
_ => {
return Err(SError::custom(pid, &doc, "HTTPError", "url must be a string"));
}
}
} else {
return Err(SError::custom(pid, &doc, "HTTPError", "must provide a URL as the first parameter when calling into the HTTP library"));
}
let mut request;
match name {
"get" => request = self.agent.get(&url),
"head" => request = self.agent.head(&url),
"patch" => request = self.agent.patch(&url),
"post" => request = self.agent.post(&url),
"put" => request = self.agent.put(&url),
"delete" => request = self.agent.delete(&url),
_ => {
return Err(SError::custom(pid, &doc, "HTTPError", &format!("unrecognized HTTP library function: {}", name)));
}
}
let mut headers = Vec::new();
let mut str_body: Option<String> = None;
let mut blob_body: Option<Vec<u8>> = None;
let mut timeout = Duration::from_secs(5);
let mut response_obj: Option<SNodeRef> = None;
if parameters.len() > 1 {
match ¶meters[1] {
SVal::Array(vals) => {
for val in vals {
match val {
SVal::Tuple(vals) => {
if vals.len() == 2 {
headers.push((vals[0].to_string(), vals[1].to_string()));
}
},
_ => {}
}
}
},
SVal::Map(map) => {
for (k, v) in map {
headers.push((k.to_string(), v.to_string()));
}
},
SVal::String(body) => {
str_body = Some(body.clone());
},
SVal::Blob(body) => {
blob_body = Some(body.clone());
},
SVal::Number(num) => {
let seconds = num.float_with_units(SUnits::Seconds);
timeout = Duration::from_secs(seconds as u64);
},
SVal::Object(nref) => {
response_obj = Some(nref.clone());
},
SVal::Boxed(val) => {
let val = val.lock().unwrap();
let val = val.deref();
match val {
SVal::Array(vals) => {
for val in vals {
match val {
SVal::Tuple(vals) => {
if vals.len() == 2 {
headers.push((vals[0].to_string(), vals[1].to_string()));
}
},
_ => {}
}
}
},
SVal::Map(map) => {
for (k, v) in map {
headers.push((k.to_string(), v.to_string()));
}
},
SVal::String(body) => {
str_body = Some(body.clone());
},
SVal::Blob(body) => {
blob_body = Some(body.clone());
},
SVal::Number(num) => {
let seconds = num.float_with_units(SUnits::Seconds);
timeout = Duration::from_secs(seconds as u64);
},
SVal::Object(nref) => {
response_obj = Some(nref.clone());
},
_ => {
return Err(SError::custom(pid, &doc, "HTTPError", "second parameter for an HTTP request must be either headers (vec), a body (str | blob), a timeout (float | units), or response object (obj)"));
}
}
},
_ => {
return Err(SError::custom(pid, &doc, "HTTPError", "second parameter for an HTTP request must be either headers (vec), a body (str | blob), a timeout (float | units), or response object (obj)"));
}
}
}
if parameters.len() > 2 {
match ¶meters[2] {
SVal::String(body) => {
str_body = Some(body.clone());
},
SVal::Blob(body) => {
blob_body = Some(body.clone());
},
SVal::Number(num) => {
let seconds = num.float_with_units(SUnits::Seconds);
timeout = Duration::from_secs(seconds as u64);
},
SVal::Object(nref) => {
response_obj = Some(nref.clone());
},
SVal::Boxed(val) => {
let val = val.lock().unwrap();
let val = val.deref();
match val {
SVal::String(body) => {
str_body = Some(body.clone());
},
SVal::Blob(body) => {
blob_body = Some(body.clone());
},
SVal::Number(num) => {
let seconds = num.float_with_units(SUnits::Seconds);
timeout = Duration::from_secs(seconds as u64);
},
SVal::Object(nref) => {
response_obj = Some(nref.clone());
},
_ => {
return Err(SError::custom(pid, &doc, "HTTPError", "third parameter for an HTTP request must be either a body (str | blob), a timeout (float | units), or a response object (obj)"));
}
}
},
_ => {
return Err(SError::custom(pid, &doc, "HTTPError", "third parameter for an HTTP request must be either a body (str | blob), a timeout (float | units), or a response object (obj)"));
}
}
}
if parameters.len() > 3 {
match ¶meters[3] {
SVal::Number(num) => {
let seconds = num.float_with_units(SUnits::Seconds);
timeout = Duration::from_secs(seconds as u64);
},
SVal::Object(nref) => {
response_obj = Some(nref.clone());
},
SVal::Boxed(val) => {
let val = val.lock().unwrap();
let val = val.deref();
match val {
SVal::Number(num) => {
let seconds = num.float_with_units(SUnits::Seconds);
timeout = Duration::from_secs(seconds as u64);
},
SVal::Object(nref) => {
response_obj = Some(nref.clone());
},
_ => {
return Err(SError::custom(pid, &doc, "HTTPError", "fourth parameter for an HTTP request must be a timeout (float | units) or a response object (obj)"));
}
}
},
_ => {
return Err(SError::custom(pid, &doc, "HTTPError", "fourth parameter for an HTTP request must be a timeout (float | units) or a response object (obj)"));
}
}
}
if parameters.len() > 4 {
match ¶meters[4] {
SVal::Object(nref) => {
response_obj = Some(nref.clone());
},
SVal::Boxed(val) => {
let val = val.lock().unwrap();
let val = val.deref();
match val {
SVal::Object(nref) => {
response_obj = Some(nref.clone());
},
_ => {
return Err(SError::custom(pid, &doc, "HTTPError", "fifth parameter for an HTTP request must be a response object (obj)"));
}
}
},
_ => {
return Err(SError::custom(pid, &doc, "HTTPError", "fifth parameter for an HTTP request must be a response object (obj)"));
}
}
}
// Set headers and timeout
for header in headers {
request = request.set(header.0.as_str(), header.1.as_str());
}
request = request.timeout(timeout);
// Send with body or call without
let response_res;
if let Some(body) = str_body {
response_res = request.send_string(&body);
} else if let Some(body) = blob_body {
response_res = request.send_bytes(&body);
} else {
response_res = request.call();
}
let response;
match response_res {
Ok(res) => response = res,
Err(error) => return Err(SError::custom(pid, &doc, "HTTPError", &format!("error sending request: {}", error.to_string()))),
}
// Get content type and headers from the response
let content_type = response.content_type().to_owned();
let mut response_headers = BTreeMap::new();
for name in response.headers_names() {
if let Some(value) = response.header(&name) {
response_headers.insert(SVal::String(name), SVal::String(value.to_owned()));
}
}
// Read response body into a blob
let mut buf: Vec<u8> = vec![];
let res = response.into_reader()
.take(((10 * 1_024 * 1_024) + 1) as u64)
.read_to_end(&mut buf);
if res.is_err() {
return Err(SError::custom(pid, &doc, "HTTPError", &format!("error reading response into buffer: {}", res.err().unwrap().to_string())));
}
if buf.len() > (10 * 1_024 * 1_024) {
return Err(SError::custom(pid, &doc, "HTTPError", "response is too large to be read into a buffer"));
}
// Import the response into a response object if one was provided
if let Some(response_obj) = response_obj {
let mut bytes = Bytes::from(buf.clone());
let as_name = response_obj.path(&doc.graph);
doc.header_import(pid, &content_type, &content_type, &mut bytes, &as_name)?;
}
// Return the response content type, headers, and body
return Ok(SVal::Tuple(vec![SVal::String(content_type), SVal::Map(response_headers), SVal::Blob(buf)]));
}
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use stof::SDoc;
use crate::HTTPLibrary;
#[test]
fn get() {
let stof = r#"
fn main(): str {
let url = 'https://restcountries.com/v3.1/name/germany';
// Using a response object, we are telling the document to call header_import using the responses 'content-type' as a format,
// parsing the response into this object. The object can be created like so, or be an already created obj in the document somewhere.
let obj = new {};
let resp = HTTP.get(url, obj);
// return resp[2] as str; // This will convert the blob body to a string using utf-8, returning the entire response body
let first = obj.field[0];
return `${first.altSpellings[1]} has an area of ${first.area}`;
}
"#;
let mut doc = SDoc::src(stof, "stof").unwrap();
doc.load_lib(Arc::new(HTTPLibrary::default()));
let res = doc.call_func("main", None, vec![]).unwrap();
assert_eq!(res.to_string(), "Federal Republic of Germany has an area of 357114");
}
}