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

Export request to CURL format #86

Merged
merged 15 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from 13 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: 2 additions & 0 deletions data/cartero.gresource.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
<file alias="main_window.ui" compressed="true" preprocess="xml-stripblanks">ui/main_window.ui</file>
<file alias="method_dropdown.ui" compressed="true" preprocess="xml-stripblanks">ui/method_dropdown.ui</file>
<file alias="payload_tab.ui" compressed="true" preprocess="xml-stripblanks">ui/payload_tab.ui</file>
<file alias="export_tab.ui" compressed="true" preprocess="xml-stripblanks">ui/export_tab.ui</file>
<file alias="code_export_pane.ui" compressed="true" preprocess="xml-stripblanks">ui/code_export_pane.ui</file>
<file alias="raw_payload_pane.ui" compressed="true" preprocess="xml-stripblanks">ui/raw_payload_pane.ui</file>
<file alias="response_headers.ui" compressed="true" preprocess="xml-stripblanks">ui/response_headers.ui</file>
<file alias="response_panel.ui" compressed="true" preprocess="xml-stripblanks">ui/response_panel.ui</file>
Expand Down
2 changes: 2 additions & 0 deletions data/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ blueprint_files = [
'ui/key_value_row.blp',
danirod marked this conversation as resolved.
Show resolved Hide resolved
'ui/method_dropdown.blp',
'ui/payload_tab.blp',
'ui/export_tab.blp',
'ui/raw_payload_pane.blp',
'ui/code_export_pane.blp',
'ui/response_headers.blp',
'ui/response_panel.blp',
'ui/save_dialog.blp',
Expand Down
66 changes: 66 additions & 0 deletions data/ui/code_export_pane.blp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2024 the Cartero authors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0-or-later
using Gtk 4.0;
using GtkSource 5;

template $CarteroCodeExportPane: $CarteroBaseExportPane {
ScrolledWindow {
hexpand: true;
vexpand: true;

Gtk.Overlay {
vexpand: true;
hexpand: true;

GtkSource.View view {
left-margin: 10;
right-margin: 10;
top-margin: 10;
bottom-margin: 10;
smart-backspace: true;
monospace: true;
buffer: buffer;
editable: false;
}

[overlay]
Gtk.Button copy_button {
valign: end;
halign: end;
margin-end: 10;
margin-bottom: 10;
clicked => $on_copy_button_clicked() swapped;

Gtk.Box {
orientation: horizontal;
spacing: 6;

Image {
icon-name: "edit-copy-symbolic";
}

Label {
label: "Copy";
danirod marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}
}
}

GtkSource.Buffer buffer {}
8 changes: 8 additions & 0 deletions data/ui/endpoint_pane.blp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,14 @@ template $CarteroEndpointPane: Adw.BreakpointBin {

child: $CarteroPayloadTab payload_pane {};
}

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

child: $CarteroExportTab export_pane {};
}
}
};

Expand Down
72 changes: 72 additions & 0 deletions data/ui/export_tab.blp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2024 the Cartero authors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0-or-later
using Gtk 4.0;
using Adw 1;

template $CarteroExportTab: Gtk.Box {
styles [
"background"
]

orientation: vertical;
spacing: 15;

Adw.Clamp {
margin-top: 10;
vexpand: false;

Adw.PreferencesGroup {
Adw.ComboRow combo {
title: _("Export to");
model: entries;
notify::selected => $on_selection_changed() swapped;
}
}
}

Gtk.Box {
orientation: vertical;
vexpand: true;
valign: fill;

Gtk.Separator sep {}

Gtk.Stack stack {
Gtk.StackPage {
name: "none";

child: Adw.Bin {};
}

Gtk.StackPage {
name: "code";

child: $CarteroCodeExportPane code {};
}

visible-child-name: "none";
}
}
}

Gtk.StringList entries {
strings [
_("(none)"),
"cURL",
]
}
7 changes: 7 additions & 0 deletions src/entities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,13 @@ pub enum RequestPayload {
},
}

#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub enum RequestExportType {
#[default]
None,
Curl(EndpointData),
}

#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub struct EndpointData {
pub url: String,
Expand Down
73 changes: 71 additions & 2 deletions src/widgets/endpoint_pane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ mod imp {

use crate::app::CarteroApplication;
use crate::client::{BoundRequest, RequestError};
use crate::entities::{EndpointData, KeyValue};
use crate::entities::{EndpointData, KeyValue, RequestExportType};
use crate::error::CarteroError;
use crate::objects::KeyValueItem;
use crate::widgets::{ItemPane, KeyValuePane, MethodDropdown, PayloadTab, ResponsePanel};
use crate::widgets::{
ExportTab, ExportType, ItemPane, KeyValuePane, MethodDropdown, PayloadTab, ResponsePanel,
};

#[derive(CompositeTemplate, Properties, Default)]
#[template(resource = "/es/danirod/Cartero/endpoint_pane.ui")]
Expand All @@ -65,6 +67,9 @@ mod imp {
#[template_child]
pub payload_pane: TemplateChild<PayloadTab>,

#[template_child]
pub export_pane: TemplateChild<ExportTab>,

#[template_child]
pub response: TemplateChild<ResponsePanel>,

Expand Down Expand Up @@ -129,6 +134,18 @@ mod imp {
}
}
}));

// update export pane data when user selects another option in the combo box.
self.export_pane
.connect_changed(glib::clone!(@weak self as window => move |_| {
if window.export_pane.imp().export_type() == ExportType::Curl {
if let Ok(data) = window.extract_endpoint() {
window.export_pane_load_endpoint_data(&data);
}
}
}));

self.configure_export_pane_bindings();
}
}

Expand Down Expand Up @@ -191,6 +208,8 @@ mod imp {
.connect_changed(glib::clone!(@weak self as pane => move |_| pane.mark_dirty()));
self.payload_pane
.connect_changed(glib::clone!(@weak self as pane => move |_| pane.mark_dirty()));
self.export_pane
.connect_changed(glib::clone!(@weak self as pane => move |_| pane.mark_dirty()));
self.header_pane
.connect_changed(glib::clone!(@weak self as pane => move |_| pane.mark_dirty()));
self.variable_pane
Expand Down Expand Up @@ -223,13 +242,62 @@ mod imp {
#[template_callback]
fn on_url_changed(&self) {
self.update_send_button_sensitivity();

if let Ok(data) = self.extract_endpoint() {
self.export_pane_load_endpoint_data(&data);
}
}

#[template_callback]
fn on_url_activated(&self) {
let _ = self.obj().activate_action("win.request", None);
}

/// Loads data for the export pane module by using an `EndpointData` structure.
fn export_pane_load_endpoint_data(&self, endpoint: &EndpointData) {
let req_export_type = self.export_pane.request_export_type();

if let RequestExportType::None = req_export_type {
return;
}

if let RequestExportType::Curl(_) = req_export_type {
self.export_pane
.set_request_export_type(&RequestExportType::Curl(endpoint.clone()));
}
}

/// Retrieves `EndpointData` and builds a new state for the export request module.
fn update_export_pane(&self) {
if let Ok(data) = self.extract_endpoint() {
self.export_pane_load_endpoint_data(&data);
}
}

/// Connect ourself to every widget in order to pass new data and rehydrate the
/// export pane module so it gets realtime, maybe we should consider doing some
/// kind of reactive bindings?
fn configure_export_pane_bindings(&self) {
self.request_method.connect_changed(
glib::clone!(@weak self as pane => move |_| pane.update_export_pane()),
);
self.request_url.connect_changed(
glib::clone!(@weak self as pane => move |_| pane.update_export_pane()),
);
self.payload_pane.connect_changed(
glib::clone!(@weak self as pane => move |_| pane.update_export_pane()),
);
self.export_pane.connect_changed(
glib::clone!(@weak self as pane => move |_| pane.update_export_pane()),
);
self.header_pane.connect_changed(
glib::clone!(@weak self as pane => move |_| pane.update_export_pane()),
);
self.variable_pane.connect_changed(
glib::clone!(@weak self as pane => move |_| pane.update_export_pane()),
);
}

/// Sets the value of every widget in the pane into whatever is set by the given endpoint.
pub fn assign_request(&self, endpoint: &EndpointData) {
self.request_url.buffer().set_text(endpoint.url.clone());
Expand All @@ -248,6 +316,7 @@ mod imp {
self.header_pane.set_entries(&headers);
self.variable_pane.set_entries(&variables);
self.payload_pane.set_payload(&endpoint.body);
self.export_pane_load_endpoint_data(endpoint);
}

/// Takes the current state of the pane and extracts it into an Endpoint value.
Expand Down
66 changes: 66 additions & 0 deletions src/widgets/export_tab/base.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2024 the Cartero authors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: GPL-3.0-or-later

use gtk::glib;
use gtk::prelude::*;
use gtk::subclass::prelude::*;

use crate::entities::RequestExportType;

mod imp {
use adw::subclass::bin::BinImpl;
use gtk::subclass::prelude::*;
use gtk::subclass::widget::WidgetImpl;

#[derive(Default)]
pub struct BaseExportPane;

#[glib::object_subclass]
impl ObjectSubclass for BaseExportPane {
const NAME: &'static str = "CarteroBaseExportPane";
const ABSTRACT: bool = true;

type Type = super::BaseExportPane;
type ParentType = adw::Bin;
}

impl ObjectImpl for BaseExportPane {}

impl WidgetImpl for BaseExportPane {}

impl BinImpl for BaseExportPane {}
}

glib::wrapper! {
pub struct BaseExportPane(ObjectSubclass<imp::BaseExportPane>)
@extends gtk::Widget, adw::Bin,
@implements gtk::Accessible, gtk::Buildable;
}

pub trait BaseExportPaneExt: IsA<BaseExportPane> {
fn request_export_type(&self) -> RequestExportType;

fn set_request_export_type(&self, req_export_type: &RequestExportType);
}

pub trait BaseExportPaneImpl: WidgetImpl + ObjectImpl + 'static {}

unsafe impl<T: BaseExportPaneImpl> IsSubclassable<T> for BaseExportPane {
fn class_init(class: &mut glib::Class<Self>) {
Self::parent_class_init::<T>(class.upcast_ref_mut());
}
}
Loading