Skip to content

Commit 630b1a0

Browse files
committed
http lib update + v0.8.27
1 parent 211d137 commit 630b1a0

File tree

7 files changed

+423
-113
lines changed

7 files changed

+423
-113
lines changed

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,15 @@ age_encrypt = ["dep:age"]
101101
basic = ["stof_std", "system", "pkg"]
102102
default = ["basic"]
103103
docx = ["dep:docx-rs"]
104-
full = ["stof_std", "system", "pkg", "image", "docx", "pdf", "http", "age_encrypt"]
105-
http = ["dep:reqwest", "dep:tokio"]
104+
full = ["stof_std", "system", "pkg", "image", "docx", "pdf", "http", "tokio", "age_encrypt"]
105+
http = ["dep:reqwest", "tokio"]
106106
image = ["dep:image"]
107107
js = ["stof_std", "dep:js-sys", "dep:wasm-bindgen", "dep:serde-wasm-bindgen", "dep:getrandom"]
108108
pdf = ["dep:lopdf", "image"]
109109
pkg = ["dep:zip", "dep:regex", "dep:walkdir"]
110110
stof_std = []
111111
system = []
112+
tokio = ["dep:tokio"]
112113

113114
[lib]
114115
bench = false

docs/libs/Http.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,40 @@ Functions for working with HTTP calls over a system network connection (async fe
44
## Thread Pool
55
This library adds a thread pool in the background for processing HTTP requests, allowing Stof to keep running while requests are executed separately. Asyncronous fetch requests will create a new Stof process, which will wait for the thread pool to execute the request before returning a map with the response data. You can then await this response map when you need it, which significantly increases performance by enabling parallel HTTP requests.
66

7+
# Http.client_error(response: map) -> bool
8+
Was the request a client error? Meaning, is the response 'status' between [400, 499]?
9+
```rust
10+
const resp = await Http.fetch("https://restcountries.com/v3.1/region/europe");
11+
assert_not(Http.client_error(resp));
12+
```
13+
714
# async Http.fetch(url: str, method: str = "get", body: str | blob = null, headers: map = null, timeout: seconds = null, query: map = null, bearer: str = null) -> Promise<map>
815
Make an HTTP request, using the thread pool in the background so that other Stof processes can continue running.
916
```rust
1017
const resp = await Http.fetch("https://restcountries.com/v3.1/region/europe");
1118
assert(resp.get('text').len() > 100);
1219
```
1320

21+
# Http.parse(response: map, context: obj = self) -> obj
22+
Parse an HTTP response into the context object (also the return value), using the response "Content-Type" header as a Stof format (binary import). Default content type if not found in response headers is "stof". Will throw an error if the format isn't accepted by this graph, or if the body doesn't exist.
23+
```rust
24+
const resp = await Http.fetch("https://restcountries.com/v3.1/region/europe");
25+
const body = new {};
26+
try Http.parse(resp, body);
27+
catch { /* didn't work out.. */ }
28+
```
29+
30+
# Http.server_error(response: map) -> bool
31+
Was the request a server error? Meaning, is the response 'status' between [500, 599]?
32+
```rust
33+
const resp = await Http.fetch("https://restcountries.com/v3.1/region/europe");
34+
assert_not(Http.server_error(resp));
35+
```
36+
37+
# Http.success(response: map) -> bool
38+
Was the request successful? Meaning, is the response 'status' between [200, 299]?
39+
```rust
40+
const resp = await Http.fetch("https://restcountries.com/v3.1/region/europe");
41+
assert(Http.success(resp));
42+
```
43+

project.stof

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,16 @@ Cargo cargo: {
4040
features: {
4141
default: ['basic']
4242
basic: ['stof_std', 'system', 'pkg']
43-
full: ['stof_std', 'system', 'pkg', 'image', 'docx', 'pdf', 'http', 'age_encrypt']
43+
full: ['stof_std', 'system', 'pkg', 'image', 'docx', 'pdf', 'http', 'tokio', 'age_encrypt']
4444

4545
stof_std: []
4646
system: []
4747
pkg: ['dep:zip', 'dep:regex', 'dep:walkdir']
4848
image: ['dep:image']
4949
docx: ['dep:docx-rs']
5050
pdf: ['dep:lopdf', 'image']
51-
http: ['dep:reqwest', 'dep:tokio']
51+
http: ['dep:reqwest', 'tokio']
52+
tokio: ['dep:tokio']
5253
age_encrypt: ['dep:age']
5354

5455
/// wasm/js version of the runtime

src/model/formats/stof/tests/libs/http.stof

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,31 @@
1515
//
1616

1717

18-
//#[test]
18+
#[test]
1919
fn get_request() {
20-
const resp = await Http.fetch('https://restcountries.com/v3.1/region/europe');
21-
assert(resp.get('text').len() > 100);
20+
if (lib('Http')) {
21+
const resp: map = await Http.fetch('https://restcountries.com/v3.1/region/europe');
22+
assert(Http.success(resp));
23+
assert_not(Http.client_error(resp));
24+
assert_not(Http.server_error(resp));
25+
26+
const body = Http.parse(resp, new {});
27+
for (const country in body.field) {
28+
//pln(country.name.common);
29+
}
30+
drop(body);
31+
}
2232
}
2333

24-
//#[test]
34+
#[test]
2535
fn get_requests() {
26-
const handles = [];
27-
for (let i in 10) {
28-
handles.push_back(Http.fetch('https://restcountries.com/v3.1/region/europe'));
29-
}
30-
for (const response in await handles) {
31-
pln(response.get('bytes'));
36+
if (lib('Http')) {
37+
const handles = [];
38+
for (let i in 10) {
39+
handles.push_back(Http.fetch('https://restcountries.com/v3.1/region/europe'));
40+
}
41+
for (const response in await handles) {
42+
pln(response.get('bytes'));
43+
}
3244
}
3345
}

0 commit comments

Comments
 (0)