Anvil: Web apps with nothing but Python Transcripts
Chapter: Consuming the API with Fitnessd Local app
Lecture: Authenticating from the app

Login or purchase this course to watch this video and the rest of the course contents.
0:00 You have the user interaction side of our little app built
0:03 but we also need to have the server side
0:06 with server communication built.
0:07 So it's going to be in several steps.
0:08 Actually, let's go up here and add
0:11 the bit to get the API key separate.
0:15 I'll pass in the data
0:17 and I'm going to create this here as well.
0:20 Let's just do it like this right here.
0:23 It's going to be a dict
0:24 and it's going to return an Optional of str
0:28 if you're familiar with this,
0:30 the typing thing that says it could be a string
0:31 or it could be nothing in case it failed.
0:34 So what we're going to do down here is
0:35 we're going to get email,
0:36 it's going to be data.get('email')
0:39 and same thing for password.
0:41 Now, it gets interesting.
0:42 Now we get to the server side bit
0:44 and maybe we'll refactor this into something nicer,
0:47 have a server side, piece, and a user interaction piece,
0:50 I would like that, but let's just keep rolling here for now.
0:53 So, we're going to use requests.
0:54 So we have to import requests up at the top.
0:57 and PyCharm luckily will do that for us
0:59 And then we need to do a post for a URL.
1:03 Save the json equals, something like that.
1:06 Those don't exist yet
1:08 we'll figure them out in a second.
1:09 Let's save the response to that
1:10 and we want to make sure the response succeeded
1:13 so there's a couple things we could do.
1:15 We could either just check the status code
1:17 or we could say resp.raise_for_status
1:20 if it's not 200 or 201,
1:22 this would cause an exception.
1:24 I think though, actually, what I want to do is say
1:26 something like this:
1:27 if not resp.status_code == 200
1:32 return None.
1:33 There's our optional bit.
1:35 Otherwise, we want to return resp.json
1:39 This will parse the body.
1:41 .get and what do we have?
1:43 API key I believe is what we're sending over.
1:47 All right, well that should pretty much do it
1:49 except we have to come up with the body.
1:51 So, body is going to be super easy.
1:55 Email. So that fixes body.
1:59 If I were able to put an equal sign there,
2:02 and colon there,
2:03 and then the other thing is URL is going to be
2:06 a base URL, remember that
2:08 plus authorize, I believe is what we called it.
2:11 Okay, so let's just check this part
2:15 and we also have to pass the API key in here
2:18 and just print API key.
2:20 Let's run this again.
2:21 See it before getting to the server
2:23 or actually getting this authentication here.
2:25 So Type this again. Moment of truth.
2:29 Well we actually probably should
2:31 do the authentication right away
2:32 but whatever.
2:33 This will still let us test it.
2:35 See it took a moment
2:37 and save measurement
2:39 is not written correctly.
2:40 Over here it expects
2:43 not an API key.
2:45 You know what that means?
2:46 That means awesome stuff
2:47 because it went past that line where it printed it out.
2:50 Look! We got our API key.
2:52 We logged in! Yes! It worked!
2:55 I think there's a couple things we need to do.
2:57 Let's put this at the top here
3:00 and let's say this is auth_data = get_auth_data().
3:05 And we'll pass that over like this
3:07 and then this one, we need to tell it to do less.
3:10 So let's go here and let's ask like that,
3:27 There we go. So we can factor getting the email
3:30 and the password in one step.
3:32 Getting the other information here,
3:34 that let's us do the authentication
3:36 and we're going to say if not api_key
3:39 and bail out early.
3:41 So let's do this one more time,
3:42 just to make sure this is working.
3:47 All right let me put something bad in here
3:50 invalid login, exit, all right.
3:51 Let me put in something correct this time
3:55 Perfect! We logged in, we're ready.
3:58 Now we can go enter our heart rate which is 71
4:02 whatever, something like that.
4:04 It worked. Looks like our interaction
4:06 and our little login even printed out later
4:09 In the flow we printed this out.
4:11 So I feel like getting the login and getting the user,
4:14 the API key is actually what we need.
4:16 This is totally working.