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

Conversation

AlphaTechnolog
Copy link
Contributor

@AlphaTechnolog AlphaTechnolog commented Oct 27, 2024

The idea is to implement #63 but in a way that it's open for also implementing #64 since those could be placed in the same panel but with more options in the combobox.

Note

Still this is work in progress and isn't fully done, just leaving this here as a draft to document a little bit the process 😅

image

The icons aren't supposed to look that way, it's just because of some weird environment im coding in XD

Status: Ready for review.

@AlphaTechnolog
Copy link
Contributor Author

Also recommendations on code quality and others are really really welcome, i've been trying my best to read all the code i've could to be able to do this because i'm basically rust + gtk noob lmao

@AlphaTechnolog
Copy link
Contributor Author

Right now this is only working with raw body format, but im not really sure how to export body for others formats such as Urlencoded and Multipart. How would you handle this @danirod ? For the raw body am just pasting it around format!("-d '{body}'") and skipping it for the missing body types mentioned before.

@AlphaTechnolog
Copy link
Contributor Author

AlphaTechnolog commented Oct 29, 2024

Another thing is that i added now a copy request contents button, the thing is that i'm not really sure now how could i make it display a little toast like the errors ones but when the user copies the thing... I saw the method error_toast() in window or something similar, but i can't seem to find a way to obtain the window instance from within the code.rs widget, is there any way to fetch it? or at least fetch the toaster widget which is wrapping the app?

After this and after discussing about the handling of others formats of body, i think this pr should be ready to review

@danirod
Copy link
Owner

danirod commented Oct 29, 2024

Sorry, I am slow to respond. I still need to read the code, but let me say that the screenshot looks incredible and I am in love with how modern the Copy button looks. I agree that the panel could be reused to provide code generation for other programming languages. It reminds me of what regex101 does in their Export dialog. One dialog, many languages.

Let me try to provide information here for what you're asking.

How would you handle this @danirod ? For the raw body am just pasting it around format!("-d '{body}'") and skipping it for the missing body types mentioned before.

That's a very good question that I didn't consider until now.

I've just made a test using Firefox to see how do they implement "Copy as cURL" when there is a payload, and it seems that all they do is to just convert to a raw body, similar to when it does a request, and use -d parameters.

After all, a urlencoded body like name: john and surname: smith can be converted to -d 'name=john&surname=smith'.

However, it seems that if one of the fields is binary (for instance, files), they don't copy the file to the curl command. I suppose that they don't want random bytes in the clipboard.

i can't seem to find a way to obtain the window instance from within the code.rs widget

There is a method in the Gtk.Application class called get_active_window(). Using CarteroApplication::get(), you should be able to retrieve the CarteroApplication instance and then it should let you use the get_active_window() method.

I am looking for examples and there is one in app.rs here, but a cast is required from GtkWindow to CarteroWindow.

cartero/src/app.rs

Lines 67 to 70 in 74c5600

let (window, is_new_window) = match self.obj().active_window() {
Some(window) => (window.downcast::<CarteroWindow>().unwrap(), false),
None => (CarteroWindow::new(&self.obj()), true),
};

To be fair, it's true that the toast_error assumes that all you have is errors. Don't be afraid to add a similar method that just accepts a string to toast it if you need it. The toast_error method is currently rather limited.

@AlphaTechnolog
Copy link
Contributor Author

Hey! 👋🏻

Sorry, I am slow to respond. I still need to read the code, but let me say that the screenshot looks incredible and I am in love with how modern the Copy button looks.

Don't worry! it's ok :) also thank you, yeah i felt like it would be a great addition since all one wants to do is click one button to get instantly the code :)

I agree that the panel could be reused to provide code generation for other programming languages. It reminds me of what regex101 does in their Export dialog. One dialog, many languages.

Yeah, it would be easier for the user to just do it one single view or panel, 100% agree

That's a very good question that I didn't consider until now.

I've just made a test using Firefox to see how do they implement "Copy as cURL" when there is a payload, and it seems that all they do is to just convert to a raw body, similar to when it does a request, and use -d parameters.

After all, a urlencoded body like name: john and surname: smith can be converted to -d 'name=john&surname=smith'.

However, it seems that if one of the fields is binary (for instance, files), they don't copy the file to the curl command. I suppose that they don't want random bytes in the clipboard.

Ohh i see, that's interesting, in that case i think all i could do is some kind of parsing for those urlencoded values and put there in that format if they come that way and just ignore the files if that's the actual body.

There is a method in the Gtk.Application class called get_active_window(). Using CarteroApplication::get(), you should be able to retrieve the CarteroApplication instance and then it should let you use the get_active_window() method.

I am looking for examples and there is one in app.rs here, but a cast is required from GtkWindow to CarteroWindow.

Ok that's certainly interesting, i could try doing it that way! thank you for the example aswell 👍🏻

@AlphaTechnolog AlphaTechnolog marked this pull request as ready for review October 30, 2024 19:39
@AlphaTechnolog
Copy link
Contributor Author

AlphaTechnolog commented Oct 30, 2024

I think with these last changes it's ready for code review.

data/meson.build Show resolved Hide resolved
data/ui/code_export_pane.blp Outdated Show resolved Hide resolved
src/widgets/export_tab/code.rs Outdated Show resolved Hide resolved
src/widgets/export_tab/service.rs Outdated Show resolved Hide resolved
@danirod danirod merged commit 1a75843 into danirod:trunk Nov 1, 2024
2 checks passed
@danirod danirod mentioned this pull request Nov 1, 2024
@danirod danirod added the Pending backport The pull request needs to be backported to older versions label Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Pending backport The pull request needs to be backported to older versions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants