Skip to content

Commit

Permalink
library: add HTTP Request entry (#532)
Browse files Browse the repository at this point in the history
Co-authored-by: Sonny Piers <[email protected]>
  • Loading branch information
halfmexican and sonnyp authored Sep 17, 2023
1 parent 0fdfbae commit 5d1f68d
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/Library/demos/HTTP Request/main.blp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Gtk 4.0;
using Adw 1;

Adw.StatusPage {
title: _("HTTP Request");
description: _("Make a request to an API");

child: Box {
orientation: vertical;
spacing: 24;

Label article_title {
styles ["title-4"]
}

ScrolledWindow scrolled_window {
height-request: 200;
width-request: 400;
has-frame: true;

child: TextView article_text_view {
top-margin: 6;
bottom-margin: 6;
left-margin: 12;
right-margin: 12;
wrap-mode: word;
editable: false;
};
}

LinkButton {
label: _("Documentation");
uri: "https://libsoup.org/libsoup-3.0/index.html";
}
};
}
57 changes: 57 additions & 0 deletions src/Library/demos/HTTP Request/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import Soup from "gi://Soup";
import GLib from "gi://GLib";
import Gio from "gi://Gio";

Gio._promisify(Soup.Session.prototype, "send_async", "send_finish");
Gio._promisify(Gio.OutputStream.prototype, "splice_async", "splice_finish");

const http_session = new Soup.Session();
const article_text_view = workbench.builder.get_object("article_text_view");
const article_title = workbench.builder.get_object("article_title");

fetchWikipediaTodaysFeaturedArticle().catch(console.error);

async function fetchWikipediaTodaysFeaturedArticle() {
// https://gjs-docs.gnome.org/glib20~2.0/glib.datetime
const date = GLib.DateTime.new_now_local();

// https://api.wikimedia.org/wiki/Feed_API/Reference/Featured_content
const language = "en";
const url = `https://api.wikimedia.org/feed/v1/wikipedia/${language}/featured/${date.format(
"%Y/%m/%d",
)}`;
const message = Soup.Message.new("GET", url);

const input_stream = await http_session.send_async(
message,
GLib.PRIORITY_DEFAULT,
null,
);

if (message.status_code !== 200) {
console.error(`HTTP Status ${message.status_code}`);
return;
}

const data = await readAsString(input_stream);
const json = JSON.parse(data);

article_text_view.buffer.set_text(json.tfa.extract, -1);
article_title.label = json.tfa.titles.normalized;
}

async function readAsString(input_stream) {
const output_stream = Gio.MemoryOutputStream.new_resizable();

await output_stream.splice_async(
input_stream,
Gio.OutputStreamSpliceFlags.CLOSE_TARGET |
Gio.OutputStreamSpliceFlags.CLOSE_SOURCE,
GLib.PRIORITY_DEFAULT,
null,
);

const bytes = output_stream.steal_as_bytes();
const text_decoder = new TextDecoder("utf-8");
return text_decoder.decode(bytes.toArray().buffer);
}
6 changes: 6 additions & 0 deletions src/Library/demos/HTTP Request/main.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"category": "network",
"description": "Make a request to an API",
"panels": ["code", "preview"],
"autorun": true
}

0 comments on commit 5d1f68d

Please sign in to comment.