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