Anvil: Web Apps with Nothing but Python Transcripts
Chapter: Adding APIs and HTTP Endpoints
Lecture: Adding a measurement via the API
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
how It's time to finally start writing the code that we wanted, which is to add a measurement.
0:07
First we talked about Hello. Just to talk about how this ap I infrastructural works We need to do this user authorization stuff.
0:13
Let's get over here and make this happen. I'm gonna just paste a method here that work unused throughout all these other AP eyes
0:19
to just do to verify our a p I key and I'm gonna talk to it real quick. So what we do is make them pass in their A p I
0:27
key and also email for validation to make sure it is their A p I key through the post body as just a dictionary.
0:33
So that's what we're getting past here. And we're going to say if you don't give us any data,
0:37
just like before, you must give us data invalid request and then we're gonna get their email and the A p I key. They didn't give us that, you know,
0:46
there maybe four threes writing. So I'm not sure. Then we tried to log them in, and we say that log in is not good.
0:53
Well, we're going to return if it does log in just fine, the user and nothing. But if there's an error,
0:59
we're going to return no user in some error code. So new user, some air code. And then we can use that in our methods up here so I can come up
1:07
and say something like, User error equals log in request. And here we want we actually going to need this in a couple places.
1:15
So that's going to say data equals involved on server Dodd. Request Jason Body past the data there. Then what will say something like,
1:26
if there's an error, an air response type return A. Remember, this is HP status HP to respond so it could be processed directly.
1:37
And then down here we can just return Berks write something like this. So of course, we actually want to add the measurement,
1:44
get the measurements back. We can just check that this is working and we want a copy. Actually duplicate this cause there's some similarities here.
1:53
We're going to change this Teoh ad measurement. Let's copy it to make sure its exact had measurements,
2:00
and if we send it, it should just give us some kind of a P I key not found. If we go look at the body were Sydney.
2:06
We don't send password anymore. We want to send whatever the AP Nike is. How do we find out what it is? Well, we call this function writes.
2:14
We go it this once and we get our A p I key and then we can just use it over and over. Course, if we change it to seven there at the end.
2:23
Should run like before involved. Log in for three. Just like expected. But with the right A p I key.
2:30
Well, it works, so we're able to log in our user with a P I key. That's great. Now what we need to do is we need to
2:38
add the measurement over here and rename these things. All right, now we know which one working with,
2:49
and we can get back to it from our little collections over here. So what we need to add is a couple of things and we can decide what
2:56
these we're gonna be called, but we're gonna need to add something along the lines of weight. Remember, this was like 1 75.
3:05
Now it's unfortunate Jason does not like serializing actual dates. Kind of a shortcoming of it, but to pick a format and in parts it back apart.
3:14
So let's put something like this. Right. So this is what we're gonna pass over. Just make sure everything's happy. Yes,
3:21
it still works. Response status code 200 A. Okay, this is what we're gonna pass over. And we need to get these things back out on the server side here.
3:31
We're going to say, Well, the weight is not 75 but it's going to be equal to data, not get weight. We could put in a default value.
3:41
That's not none. It's probably an imager. But if they mess up and pass a string, we could probably parse it back into an energy as well.
3:49
Same thing for rate and recorded. It's gonna be more complicated. We have to do some string parsing, which is really rough. All right,
3:58
so we're gonna use date time, string pars time. I say we're getting year, month and day, and we don't want the daytime.
4:06
We just want the date part of it. Believe that's the way we set it over here. Yeah, it's just a date,
4:14
not a date time, so we have to get the date part of it. Now we have to import date time at the top or this won't work. Obviously, with that in place,
4:22
we should be able to get this data and let's just echo it back just that it works. So we have weight tweet between Just echo it back.
4:33
First to see that things were working over here and go like this. We probably need some kind of thing to check that these air imagers.
4:42
But, you know, that's okay for that response. The hair Let's go. We could actually put Remember, Jason doesn't like this. We could say I so format.
4:53
That's the way to get that back. Basically the same format. We sent it over here we go like that. It looks like our parson is working,
5:01
so we're passing our data across. Remember, if our a P I key let's do one more thing.
5:05
Let's put user user email so we can make sure that it is actually the right user that's being pulled back. Now,
5:13
this is not echoing this value I passed. This is going to the database getting the user and then returning their email.
5:19
So, yeah, it looks like everything is working great. Well, we have to do to save it. And it turns out that that's super easy.
5:26
So instead of doing all of this was going to go to APP tables thought measurement not add row and then created date is going to be date time.
5:34
Don date time got now we don't pass that in. Recorded date is gonna be recorded. Resting heart rate is ray and wait and pounds is a weight.
5:45
All right, let's format this so humans can read it. And let's just say status cars success True.
5:53
We technically don't even really need to return anything. Let's make people happy. Give my little nice message.
5:59
Yeah, that worked Before we go back here and run this, let's look at our data tables. The measurements for me here soar,
6:06
Judge, Here we go. My sort decreasing. Perfect. So we have one for September 20th. We should have one for September 21st. When I say go right here.
6:17
Perfect. Success is true. We refresh. There's our record. Oh, do we forget something? Yes, we did, But look, it got inserted. That's pretty cool,
6:28
but what do we forget. So if we go back over to our a p I last super thing as user go theater, right? So, user, we want to set that Get in the future.
6:39
Little change that a bit. Hit it one more time now Refresh. Perfect. Everything's working now we have the user associated with it.
6:54
This is the date There's are 1 76 and 70. We just entered. I love it. We have our a p I totally working.