Skip to content

Commit 7ea55d9

Browse files
authored
library: Simplify HTTP Request demo (#797)
1 parent b2ccfb7 commit 7ea55d9

1 file changed

Lines changed: 9 additions & 21 deletions

File tree

  • src/Library/demos/HTTP Request

src/Library/demos/HTTP Request/main.js

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ import GLib from "gi://GLib";
22
import Gio from "gi://Gio";
33
import Soup from "gi://Soup";
44

5-
Gio._promisify(Soup.Session.prototype, "send_async", "send_finish");
6-
Gio._promisify(Gio.OutputStream.prototype, "splice_async", "splice_finish");
5+
Gio._promisify(
6+
Soup.Session.prototype,
7+
"send_and_read_async",
8+
"send_and_read_finish",
9+
);
710

811
const http_session = new Soup.Session();
912
const article_text_view = workbench.builder.get_object("article_text_view");
@@ -22,7 +25,7 @@ async function fetchWikipediaTodaysFeaturedArticle() {
2225
)}`;
2326
const message = Soup.Message.new("GET", url);
2427

25-
const input_stream = await http_session.send_async(
28+
const bytes = await http_session.send_and_read_async(
2629
message,
2730
GLib.PRIORITY_DEFAULT,
2831
null,
@@ -33,25 +36,10 @@ async function fetchWikipediaTodaysFeaturedArticle() {
3336
return;
3437
}
3538

36-
const data = await readAsString(input_stream);
37-
const json = JSON.parse(data);
39+
const text_decoder = new TextDecoder("utf-8");
40+
const decoded_text = text_decoder.decode(bytes.toArray());
41+
const json = JSON.parse(decoded_text);
3842

3943
article_text_view.buffer.set_text(json.tfa.extract, -1);
4044
article_title.label = json.tfa.titles.normalized;
4145
}
42-
43-
async function readAsString(input_stream) {
44-
const output_stream = Gio.MemoryOutputStream.new_resizable();
45-
46-
await output_stream.splice_async(
47-
input_stream,
48-
Gio.OutputStreamSpliceFlags.CLOSE_TARGET |
49-
Gio.OutputStreamSpliceFlags.CLOSE_SOURCE,
50-
GLib.PRIORITY_DEFAULT,
51-
null,
52-
);
53-
54-
const bytes = output_stream.steal_as_bytes();
55-
const text_decoder = new TextDecoder("utf-8");
56-
return text_decoder.decode(bytes.toArray().buffer);
57-
}

0 commit comments

Comments
 (0)