A command-line client

hier_client is a command-line tool that controls my Anvil app Hier.

Right now the features are fairly bare-boned. Users can read/write today’s notes, and admins can get some basic stats (I validate admin status server-side). But, I’m pretty happy with the skeleton: generating and storing credentials, how easy adding endpoints is, etc. Things should be extensible from here.

Feel free to fork and adapt to your own apps. And while I can’t share Hier, I’m happy to answer questions about server side logic.

As always, feedback and suggestions are appreciated!

1 Like

This is a great example of creating a command-line tool to drive an Anvil app.

I notice you’re using requests to communicate with your app’s HTTP API. Have you considered using the Uplink instead?

You can create clients for your app using the Uplink in Client Mode:

09

Uplink scripts connected with this key will have the same privileges as client-side code, so it’s safe to give them to your users.

You can prevent anvil.server.connect from printing any output when it starts up by using quiet=True:

anvil.server.connect("[YOUR UPLINK KEY]", quiet=True)

Oh, interesting! I’d seen the Uplink in the docs, but relegated it to the use-case of “needing to hand off CPU-intensive to some non-Anvil machine”, and hadn’t dug in much further than that.

The main question I have is about user authentication. If a user registered with Google/Facebook, will the anvil.users.login_with_email("my_user@example.com", "MY_PASSWORD") functionality still work? Oauth stuff is still a little beyond me, to be honest.

Edit: It doesn’t seem like it’ll work for Oauth users. I tried using a Google account, and I get the error:

Connecting to wss://anvil.works/uplink
Anvil websocket open
Authenticated OK
Error during session initialisation
Traceback...
You haven't confirmed your email address. Please check your email and click the confirmation link, or reset your password.

As far as I can tell, there’s never an option for someone who registered with a Google account to confirm their email. I read through the docs, and it doesn’t appear that there’s an option for this (but maybe I’m missing something?)

The Google and Facebook OAuth flows are designed to involve user interaction with a Google/Facebook web form - this is a security measure to stop third-party developers handling Google/Facebook account passwords. So unfortunately, it’s not possible to login using Google or Facebook in a commanline tool.

Yep, I assumed that wasn’t going to be possible, which is why I ended up with my UUID generation scheme instead. This way, everyone can use the client regardless of how they signed up for Hier. To be honest, I would have been a little sad if all of that work was already wrapped into anvil.server.connect!

1 Like

@shaun, today I realized I wasn’t timezone aware in the client. It looked like the best way to handle this was through the datetime magic you guys do with anvil-uplink. So I converted.

It’s beautiful. This commit has so many red lines!

Great job, you guys.

1 Like