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

Refactor #12

Merged
merged 5 commits into from
Dec 8, 2021
Merged

Refactor #12

merged 5 commits into from
Dec 8, 2021

Conversation

berendsliedrecht
Copy link
Member

MERGE #9 FIRST :D

  • Abstracted the agent more so we can move to other agents in the future (unlikely, but nice to have)
  • Removed unused code

Copy link
Contributor

@morrieinmaas morrieinmaas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @blu3beri, left a few comments, but really nice work man! (Comments mostly me being confused tbf)

cli.yaml Outdated
long: invite
help: create a proper mediation invitation
- auto-accept:
help: Wether the invitation should be auto-accepted
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whether 😅

.expect(&format!("Could not join on {}", id).to_string())
}
fn create_invitation(agent: &HttpAgent) -> Url {
reqwest::Url::parse(&agent.url)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOt sure as I'm a total noob here but

reqwest::Url::parse(&agent.url)
            .expect(&format!("Could not join on {}", agent.url).to_string())
            .join("connections/")
            .expect(&format!("Could not join on connections/").to_string())

seems to be the same base route 3 times? Maybe this can be refactored into a base connections route and used in all 3 methods just that get_connection_by_id and create_invitation append sth else as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah definitely agree! The whole endpoint generation needs a refactor as this is an extremely boated way to create urls :)

use async_trait::async_trait;

#[async_trait]
pub trait Agent {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

async fn check_endpoint(&self) -> typing::Result<bool> {
match reqwest::get(Endpoint::connections(&self)).await {
Ok(res) => {
if res.status() == 404 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it'd make sense to check for status != 200 instead of == 400?

Copy link
Member Author

@berendsliedrecht berendsliedrecht Dec 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about:

if res.status() >= 200 && res.status() < 300

:D

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm but then you won't catch the 400 ones, will you?!

b.insert("group", "admin");
a.insert("metadata", b);

body = Some(a);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting! This is how you create

{"metadata": {"group": "admin"}}

?! Just wondering whether tehre is another way of doing this wo having to create two hast maps

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just been reading into json in Rust a little bit. and serde seems to be recommended by people to handle json like stuff - and it's in the dependencies. Maybe it makes sense to use that here? Not sure, again, just a noob atm...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah Serde-json can be used. Serde is a lib that just does SERialization and DEserialization. serde-json is def the way to go. The reqwest json just expected this type so I am not sure wether it'll work.

Will try!

match http::post(Endpoint::create_invitation(&self), query, body).await {
Ok(res) => match res.json().await {
Ok(parsed) => parsed,
//Err(_) => error::throw(error::Error::ServerResponseParseError),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be removed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oui!

let mut body = None;

if config.toolbox {
query.push(("multi_use", false.to_string()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I udnerstand this 100%. does false.to_string() mean you're always setting this to false or default value?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we want to create a toolbox invite we don't want multi use. This way we don't pollute the invitations.

false.to_string() just gives back "false". However this is less error-prone as typos cannot occur :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah ok get it - nice!

@@ -3,8 +3,15 @@ use serde::{Deserialize, Serialize};

pub type Result<T> = std::result::Result<T, Error>;

pub struct InviteConfiguration<'a> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does the ' do in <'a>?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is about lifetimes or variables. I truly do not understand it one bit and this was recommend to me by fellow ferris-lovers. :)

https://doc.rust-lang.org/rust-by-example/scope/lifetime.html

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah hanks for the pointer (pun intended)

src/utils/qr.rs Outdated

if let Err(_) = qr2term::print_qr(&url.as_ref()) {
pub fn print_qr_code(text: &String) {
if let Err(_) = qr2term::print_qr(text) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is some weird syntax😅🤓 does this actually print the qr code? Is this some Rust magic short hand thing? Looks like this only throws and error to me 🤦‍♂️

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an odd way to ignore the out come and only handle the error case. This should probably be done with an expect when the proper panic handler is implemented!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah ok. interesting! Adn how does the QR get printed then?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

print_qr prints a qrcode in the terminal by painting the background of a cell black or white. Quite fancy :)

Signed-off-by: Berend Sliedrecht <[email protected]>
cli.yaml Outdated
short: a
long: auto-accept
- multi-use:
help: Wether the invitation should be multi-use
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all the wether should be whether ;P whether you like it or not

@berendsliedrecht berendsliedrecht merged commit 7e2ef48 into main Dec 8, 2021
@berendsliedrecht berendsliedrecht deleted the refactor branch December 9, 2021 14:43
gitbook-com bot pushed a commit that referenced this pull request Apr 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants