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
0:02 another thing I would like to do
0:03 is stop asking for the email and password.
0:05 Only ask on first run and then just remember the account.
0:09 So we've got this authenticate method here that uses those.
0:12 But we're over in this part
0:15 where this get auth data down here.
0:18 We're always asking this.
0:19 So let's actually when we go through
0:21 and get this API key here
0:24 let's actually save that somewhere.
0:26 So let's go and create what we'll call author
0:31 something like that. And I'm just going to paste in the code
0:33 and talk through it because it doesn't really matter.
0:35 What we're going to do is we're going to have
0:37 an email and an API key
0:39 and we're going to ask basically is this set, right?
0:42 Is this set correctly?
0:44 And we're going to be able to save those
0:46 which just puts them into a JSON file
0:48 and load them, which just reads them out of a JSON file.
0:52 Okay, so up here we'll say import auth.
0:57 And we'll say something like this
0:58 if they're not authorized
1:02 we're going to go and get this.
1:05 We'll say we want to save that.
1:07 Actually, we don't want to save this yet, sorry.
1:09 Let's go this authenticate.
1:10 Let's actually change this to pass in email and password.
1:27 So we'll set the email and password that way.
1:29 Else, I want to say email was
1:37 The API key equals auth.api_key.
1:41 All right, so that's not super clean, but that should work.
1:43 All right, now here, I want to just pass the email
1:48 that we had from before.
1:51 Okay, so this should work just like it did before.
1:54 It should ask us, when we run it
1:56 for the email and the password, go get the API key.
1:59 And then we also want to save it here.
2:02 So we'll say, auth email and API.
2:08 But say load_auth, and does that crash?
2:11 Yeah. If it doesn't exist, it's just going to bail.
2:14 So this authentication bit, it's kind of clumsy
2:16 but it'll make the user experience a lot nicer.
2:19 So let's run this again.
2:21 The first time, it'll behave exactly the same.
2:24 Ask for the username and the password.
2:28 And now, we go over here and maybe wait a second
2:32 or certainly I can trigger that if I say sync.
2:35 Now notice there's an account.json
2:38 with my API key and my email.
2:42 It looks like it worked.
2:43 So far as the user's going
2:45 they probably are not seeing this, right.
2:47 So they probably don't care.
2:48 They'll just type in whatever they type.
2:50 Let's put 178.
2:54 It prints out the API key, success is true.
2:57 That's all well and good.
2:58 But here's the cool part, if we run it again, boom.
3:00 Enter measurement.
3:02 Now let's put 79 there and 184.
3:07 I don't think I've entered that number before.
3:11 It worked. So nice! Do it again!
3:15 80, 185. It goes, awesome!
3:20 If we go refresh again over here
3:24 you can see a whole bunch of those.
3:25 185, 184, all these things.
3:28 My heart rate's going up because I'm getting excited!
3:30 This is awesome.
3:31 So that's a super cool experience for our users.
3:34 They can just come in here, login once
3:36 and forever after, the app remembers them.