Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Soup for request #349

Merged
merged 2 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
- name: Install Dependencies
run: |
apt update
apt install -y meson libadwaita-1-dev libfwupd-dev libgranite-7-dev libgtk-4-dev libgtop2-dev libgudev-1.0-dev libudisks2-dev libswitchboard-3-dev libappstream-dev libpackagekit-glib2-dev libpolkit-gobject-1-dev valac
apt install -y meson libadwaita-1-dev libfwupd-dev libgranite-7-dev libgtk-4-dev libgtop2-dev libgudev-1.0-dev libudisks2-dev libswitchboard-3-dev libappstream-dev libpackagekit-glib2-dev libpolkit-gobject-1-dev libsoup-3.0-dev valac
- name: Build
env:
DESTDIR: out
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ You'll need the following dependencies:
* libappstream-dev
* libpackagekit-glib2-dev
* libpolkit-gobject-1-dev
* libsoup-3.0-dev
* meson
* valac
* switcheroo-control (at runtime)
Expand Down
75 changes: 36 additions & 39 deletions src/Views/OperatingSystemView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -793,53 +793,50 @@ public class About.OperatingSystemView : Gtk.Box {
}

private void get_goal_progress (Gtk.LevelBar levelbar, Gtk.Label target_label) {
new Thread<void*> ("get_goal_progress", () => {
var token = About.GITHUB_TOKEN;
var query = "{\"query\": \"query { organization(login: \\\"elementary\\\") { sponsorsListing { activeGoal { percentComplete, targetValue } } } }\"}";
var curl_command = """
curl -X POST https://api.github.com/graphql \
-H "Authorization: Bearer %s" \
-H "Content-Type: application/json" \
-d '%s'
""".printf (token, query);
var query = "{\"query\": \"query { organization(login: \\\"elementary\\\") { sponsorsListing { activeGoal { percentComplete, targetValue } } } }\"}";

try {
string standard_output;
string standard_error;
int exit_status;

Process.spawn_command_line_sync (
curl_command,
out standard_output,
out standard_error,
out exit_status
);
var message = new Soup.Message ("POST", "https://api.github.com/graphql");
message.request_headers.append ("Authorization", "Bearer %s".printf (About.GITHUB_TOKEN));
message.request_headers.append ("Content-Type", "application/json");
message.request_headers.append ("User-Agent", "About.OperatingSystemView/1.0");
message.set_request_body_from_bytes (null, new Bytes (query.data));

if (exit_status == 0 && standard_output != null) {
var parser = new Json.Parser ();
parser.load_from_data (standard_output);
var session = new Soup.Session ();
session.send_and_read_async.begin (message, GLib.Priority.DEFAULT, null , (obj, res) => {
try {
var bytes = session.send_and_read_async.end (res);

var root = parser.get_root ();
if (root.get_node_type () == Json.NodeType.OBJECT) {
var sponsors_listing = root.get_object ()
.get_object_member ("data")
.get_object_member ("organization")
.get_object_member ("sponsorsListing")
.get_object_member ("activeGoal");
var output = (string) bytes.get_data ();
if (output == null) {
return;
}

int64 percent_complete = sponsors_listing.get_int_member ("percentComplete");
double target_value = sponsors_listing.get_double_member ("targetValue");
var parser = new Json.Parser ();
parser.load_from_data (output);

levelbar.value = percent_complete / 100.0;
target_label.label = _("%s%% towards $%s per month goal")
.printf (percent_complete.to_string (), target_value.to_string ());
}
var root = parser.get_root ();
if (root.get_node_type () != OBJECT) {
return;
}

var sponsors_listing = root.get_object ()
.get_object_member ("data")
.get_object_member ("organization")
.get_object_member ("sponsorsListing")
.get_object_member ("activeGoal");

int64 percent_complete = sponsors_listing.get_int_member ("percentComplete");
double target_value = sponsors_listing.get_double_member ("targetValue");

levelbar.value = percent_complete / 100.0;
target_label.label = _("%s%% towards $%s per month goal").printf (
percent_complete.to_string (),
target_value.to_string ()
);

} catch (Error e) {
debug ("Error: %s\n".printf (e.message));
critical (e.message);
}

return null;
});
}
}
Expand Down
1 change: 1 addition & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ shared_module(
dependency('gtk4'),
dependency('libadwaita-1'),
dependency('libgtop-2.0'),
dependency('libsoup-3.0'),
dependency('packagekit-glib2'),
dependency('gudev-1.0'),
dependency('udisks2'),
Expand Down
Loading