-
-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
library: add HTTP Request entry (#532)
Co-authored-by: Sonny Piers <[email protected]>
- Loading branch information
1 parent
0fdfbae
commit 5d1f68d
Showing
3 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |