Skip to content

Commit

Permalink
Add query params pane
Browse files Browse the repository at this point in the history
This pane will show the query params in the URL, and let the user easily
modify the values of the query params as a table rather than having to
touch the URL.

Closes #35
  • Loading branch information
danirod committed Jul 26, 2024
1 parent 3a64ac3 commit 1c1eb6d
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 24 deletions.
7 changes: 4 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ srtemplate = { version = "0.3.0", features = [] }
thiserror = "1.0.60"
tokio = { version = "1.37.0", features = ["sync"] }
toml = "0.8.12"
url = "2.5.2"
21 changes: 21 additions & 0 deletions data/ui/endpoint_pane.blp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,27 @@ template $CarteroEndpointPane: Adw.BreakpointBin {
Notebook {
show-border: false;

NotebookPage {
tab: Label {
label: _("Parameters");
};

child: ScrolledWindow {
hexpand: true;
vexpand: true;

Adw.Clamp {
styles [
"background"
]

maximum-size: 720;

$CarteroKeyValuePane parameter_pane {}
}
};
}

NotebookPage {
tab: Label {
label: _("Headers");
Expand Down
12 changes: 8 additions & 4 deletions po/cartero.pot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: cartero\n"
"Report-Msgid-Bugs-To: https://github.com/danirod/cartero/issues\n"
"POT-Creation-Date: 2024-07-26 09:25+0200\n"
"POT-Creation-Date: 2024-07-26 21:32+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
Expand Down Expand Up @@ -90,15 +90,19 @@ msgstr ""
msgid "Execute this HTTP request"
msgstr ""

#: data/ui/endpoint_pane.blp:103 data/ui/response_panel.blp:47
#: data/ui/endpoint_pane.blp:103
msgid "Parameters"
msgstr ""

#: data/ui/endpoint_pane.blp:124 data/ui/response_panel.blp:47
msgid "Headers"
msgstr ""

#: data/ui/endpoint_pane.blp:124
#: data/ui/endpoint_pane.blp:145
msgid "Variables"
msgstr ""

#: data/ui/endpoint_pane.blp:145 data/ui/response_panel.blp:68
#: data/ui/endpoint_pane.blp:166 data/ui/response_panel.blp:68
msgid "Body"
msgstr ""

Expand Down
12 changes: 8 additions & 4 deletions po/en.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: cartero\n"
"Report-Msgid-Bugs-To: https://github.com/danirod/cartero/issues\n"
"POT-Creation-Date: 2024-07-26 09:25+0200\n"
"POT-Creation-Date: 2024-07-26 21:32+0200\n"
"PO-Revision-Date: 2024-06-22 13:31+0200\n"
"Last-Translator: Automatically generated\n"
"Language-Team: none\n"
Expand Down Expand Up @@ -91,15 +91,19 @@ msgstr ""
msgid "Execute this HTTP request"
msgstr ""

#: data/ui/endpoint_pane.blp:103 data/ui/response_panel.blp:47
#: data/ui/endpoint_pane.blp:103
msgid "Parameters"
msgstr ""

#: data/ui/endpoint_pane.blp:124 data/ui/response_panel.blp:47
msgid "Headers"
msgstr ""

#: data/ui/endpoint_pane.blp:124
#: data/ui/endpoint_pane.blp:145
msgid "Variables"
msgstr ""

#: data/ui/endpoint_pane.blp:145 data/ui/response_panel.blp:68
#: data/ui/endpoint_pane.blp:166 data/ui/response_panel.blp:68
msgid "Body"
msgstr ""

Expand Down
12 changes: 8 additions & 4 deletions po/eo.po
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ msgid ""
msgstr ""
"Project-Id-Version: cartero\n"
"Report-Msgid-Bugs-To: https://github.com/danirod/cartero/issues\n"
"POT-Creation-Date: 2024-07-26 09:25+0200\n"
"POT-Creation-Date: 2024-07-26 21:32+0200\n"
"PO-Revision-Date: 2024-07-25 22:08+0200\n"
"Last-Translator: <>\n"
"Language-Team: Spanish\n"
Expand Down Expand Up @@ -89,15 +89,19 @@ msgstr "Petu"
msgid "Execute this HTTP request"
msgstr "Sendu ĉi tiun http-peton"

#: data/ui/endpoint_pane.blp:103 data/ui/response_panel.blp:47
#: data/ui/endpoint_pane.blp:103
msgid "Parameters"
msgstr ""

#: data/ui/endpoint_pane.blp:124 data/ui/response_panel.blp:47
msgid "Headers"
msgstr "Kapoj"

#: data/ui/endpoint_pane.blp:124
#: data/ui/endpoint_pane.blp:145
msgid "Variables"
msgstr "Variabloj"

#: data/ui/endpoint_pane.blp:145 data/ui/response_panel.blp:68
#: data/ui/endpoint_pane.blp:166 data/ui/response_panel.blp:68
msgid "Body"
msgstr "Korpo"

Expand Down
14 changes: 9 additions & 5 deletions po/es.po
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ msgid ""
msgstr ""
"Project-Id-Version: cartero\n"
"Report-Msgid-Bugs-To: https://github.com/danirod/cartero/issues\n"
"POT-Creation-Date: 2024-07-26 09:25+0200\n"
"PO-Revision-Date: 2024-07-26 01:40+0200\n"
"POT-Creation-Date: 2024-07-26 21:32+0200\n"
"PO-Revision-Date: 2024-07-26 21:32+0200\n"
"Last-Translator: <>\n"
"Language-Team: Spanish\n"
"Language: es\n"
Expand Down Expand Up @@ -89,15 +89,19 @@ msgstr "Enviar"
msgid "Execute this HTTP request"
msgstr "Ejecutar esta petición HTTP"

#: data/ui/endpoint_pane.blp:103 data/ui/response_panel.blp:47
#: data/ui/endpoint_pane.blp:103
msgid "Parameters"
msgstr "Parámetros"

#: data/ui/endpoint_pane.blp:124 data/ui/response_panel.blp:47
msgid "Headers"
msgstr "Cabeceras"

#: data/ui/endpoint_pane.blp:124
#: data/ui/endpoint_pane.blp:145
msgid "Variables"
msgstr "Variables"

#: data/ui/endpoint_pane.blp:145 data/ui/response_panel.blp:68
#: data/ui/endpoint_pane.blp:166 data/ui/response_panel.blp:68
msgid "Body"
msgstr "Cuerpo"

Expand Down
74 changes: 74 additions & 0 deletions src/widgets/endpoint_pane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::{entities::EndpointData, error::CarteroError};

mod imp {
use std::cell::RefCell;
use std::sync::{Arc, Mutex};
use std::time::Instant;

use adw::subclass::breakpoint_bin::BreakpointBinImpl;
Expand All @@ -30,6 +31,7 @@ mod imp {
use gtk::subclass::prelude::*;
use gtk::{prelude::*, CompositeTemplate};
use isahc::RequestExt;
use url::Url;

use crate::app::CarteroApplication;
use crate::client::{BoundRequest, RequestError};
Expand All @@ -45,6 +47,9 @@ mod imp {
#[template_child(id = "send")]
pub send_button: TemplateChild<gtk::Button>,

#[template_child]
pub parameter_pane: TemplateChild<KeyValuePane>,

#[template_child]
pub header_pane: TemplateChild<KeyValuePane>,

Expand All @@ -68,6 +73,8 @@ mod imp {

#[property(get, set, nullable)]
pub item_pane: RefCell<Option<ItemPane>>,

variable_changing: Arc<Mutex<bool>>,
}

#[glib::object_subclass]
Expand Down Expand Up @@ -95,6 +102,33 @@ mod imp {
self.init_settings();
self.variable_pane.assert_always_placeholder();
self.header_pane.assert_always_placeholder();
self.parameter_pane.assert_always_placeholder();

let url_arc = self.variable_changing.clone();
self.request_url
.connect_changed(glib::clone!(@weak self as window => move |_| {
// It is important to allow the redundant pattern matching because
// is_ok() does not capture the mutex and will cause sync issues.
#[allow(clippy::redundant_pattern_matching)]
if let Ok(_) = url_arc.try_lock() {
if let Err(err) = window.update_query_params() {
println!("{err}");
}
}
}));

let parameter_arc = self.variable_changing.clone();
self.parameter_pane
.connect_changed(glib::clone!(@weak self as window => move |_| {
// It is important to allow the redundant pattern matching because
// is_ok() does not capture the mutex and will cause sync issues.
#[allow(clippy::redundant_pattern_matching)]
if let Ok(_) = parameter_arc.try_lock() {
if let Err(err) = window.update_url_from_query_params() {
println!("{err}");
}
}
}));
}
}

Expand All @@ -104,6 +138,46 @@ mod imp {

#[gtk::template_callbacks]
impl EndpointPane {
fn update_url_from_query_params(&self) -> Result<(), url::ParseError> {
let table = self.parameter_pane.get_entries();

let parsed_url = self.request_url.text().to_string();
let mut url = Url::parse(&parsed_url)?;
{
let mut pairs = url.query_pairs_mut();
pairs.clear();
for item in table {
if item.active() {
let key = item.header_name();
let value = item.header_value();
pairs.append_pair(&key, &value);
}
}
}
self.request_url.set_text(url.as_str());
Ok(())
}

fn update_query_params(&self) -> Result<(), url::ParseError> {
let parsed_url = self.request_url.text().to_string();
let url = Url::parse(&parsed_url)?;
let pairs = url.query_pairs();

let entries: Vec<KeyValueItem> = pairs
.map(|(key, value)| {
let key = String::from(key);
let value = String::from(value);
let entry = KeyValue::from((key, value));
let value = KeyValueItem::from(entry);
value.set_active(true);
value.set_secret(false);
value
})
.collect();
self.parameter_pane.set_entries(&entries);
Ok(())
}

fn mark_dirty(&self) {
if let Some(item_pane) = self.obj().item_pane() {
item_pane.set_dirty(true);
Expand Down
5 changes: 1 addition & 4 deletions src/widgets/key_value_pane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,7 @@ impl KeyValuePane {
pub fn get_entries(&self) -> Vec<KeyValueItem> {
let model = &self.model();
let iter = model.iter::<KeyValueItem>();
iter.filter(|value| value.is_ok())
.flatten()
.filter(|value| !value.header_name().is_empty() && !value.header_value().is_empty())
.collect()
iter.filter(|value| value.is_ok()).flatten().collect()
}

pub fn set_entries(&self, entries: &[KeyValueItem]) {
Expand Down

0 comments on commit 1c1eb6d

Please sign in to comment.