Anvil: Web Apps with Nothing but Python Transcripts
Chapter: Consuming the API with Fitnessd Local app
Lecture: Remember the API key
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
With our app refactored nicely another thing I would like to do is stop asking for the email and password.
0:06
Only ask on first run and then just remember the account. So we've got this authenticate method here that uses those. But we're over in this part
0:16
where this get auth data down here. We're always asking this. So let's actually when we go through and get this API key here
0:25
let's actually save that somewhere. So let's go and create what we'll call author something like that. And I'm just going to paste in the code
0:34
and talk through it because it doesn't really matter. What we're going to do is we're going to have an email and an API key
0:40
and we're going to ask basically is this set, right? Is this set correctly? And we're going to be able to save those
0:47
which just puts them into a JSON file and load them, which just reads them out of a JSON file. Okay, so up here we'll say import auth.
0:58
And we'll say something like this if they're not authorized we're going to go and get this. We'll say we want to save that.
1:08
Actually, we don't want to save this yet, sorry. Let's go this authenticate. Let's actually change this to pass in email and password.
1:28
So we'll set the email and password that way. Else, I want to say email was auth.email. The API key equals auth.api_key.
1:42
All right, so that's not super clean, but that should work. All right, now here, I want to just pass the email that we had from before.
1:52
Okay, so this should work just like it did before. It should ask us, when we run it for the email and the password, go get the API key.
2:00
And then we also want to save it here. So we'll say auth.save, auth email and API. But say load_auth, and does that crash?
2:12
Yeah. If it doesn't exist, it's just going to bail. So this authentication bit, it's kind of clumsy but it'll make the user experience a lot nicer.
2:20
So let's run this again. The first time, it'll behave exactly the same. Ask for the username and the password.
2:29
And now, we go over here and maybe wait a second or certainly I can trigger that if I say sync. Now notice there's an account.json
2:39
with my API key and my email. It looks like it worked. So far as the user's going they probably are not seeing this, right.
2:48
So they probably don't care. They'll just type in whatever they type. Let's put 178. It prints out the API key, success is true.
2:58
That's all well and good. But here's the cool part, if we run it again, boom. Enter measurement. Now let's put 79 there and 184.
3:08
I don't think I've entered that number before. It worked. So nice! Do it again! 80, 185. It goes, awesome! If we go refresh again over here
3:25
you can see a whole bunch of those. 185, 184, all these things. My heart rate's going up because I'm getting excited! This is awesome.
3:32
So that's a super cool experience for our users. They can just come in here, login once and forever after, the app remembers them.