RESTful and HTTP APIs in Pyramid Transcripts
Chapter: Your first service
Lecture: Implementing the API

Login or purchase this course to watch this video and the rest of the course contents.
0:01 We've seen that we tried to run the website and it crashes because we were referencing a route name called autos and there is none.
0:08 So what we need to do, is we need to go to our __init__ for the primary package, the main website
0:14 and we need to go here and we need to change our routing. Now, this is not that big of a deal, and that is pretty simple
0:20 but as this grows, we probably want to reorganize this, so let's come over here and make a function,
0:27 a method and we're going to call this register routes. Okay, so now we've got register routes over here
0:41 and to pull that menu up I highlighted it and hit control t, so control t pulls that up. So we got our static piece, which I have cranked way down,
0:49 you wouldn't do this in production, but for this particular example we did, so the next one we're going to do is I want to come down here
0:56 and have our API, I think I just called it autos, let's have a look because of that name, let's call this API just to be a super clear,
1:04 so remember those names have to match, so what we're going to do is we're going to map api /autos to this.
1:10 Now, one thing that's kind of unfortunate here is autos like this won't map whereas this will, so you can set up a second route to the same one
1:21 or you can just make sure that you don't put this slash on the end you can decide how strict you want to be about it.
1:26 So we're going to map this over there, and let's go in and add one more thing while we're here
1:32 maybe call it auto api, and we're going to go to / api /autos and we'll have a car id. How do I know I can call it car id like right here?
1:44 Well because you make this up, like you put a name here this little cut out, it gets dropped into a dictionary with a value
1:49 when somebody goes to that url, and you pull that out so you just have to be consistent here I call it car id,
1:55 in my view function I'll call a car id, it works great. Okay, we don't have something to map to this yet but I put it in place, so we're ready;
2:02 now, let's go over here and we should have this mapped and let's just put a little bit of test data here so test was autos okay
2:12 so if I run this again, hooray, it doesn't crash, that's already pretty excellent, right; let's pull this up again, if we do get api /autos
2:20 now this looks kind of funky, check this out let me close all these extra stuff here, we'll come back to this in a moment.
2:27 Firefox just recently added like super nice support for json display so we can save or copy this, and this is like an interpreted version
2:36 if we hit raw data this is what was actually returned, we could pretty print it or copy it and we could also go and see the headers
2:43 so application.json is the type, servers waitress, it won't be in production but it is for this development bit here.
2:51 You saw that we've got the function to be called, but does that look like car auto data to you- no, it's not car auto data,
2:59 let's go and do another thing real quick, let's add one more function here one more view function that it is the individual piece
3:08 so the other part of our API, we'll just do another test, I'll put is auto singular, and down here, this is going to be auto API,
3:17 and now be very careful here, this name and this name are the same the way Python works is that gets stuffed into the module name space,
3:26 when this runs down here, it's going to eject that function so basically only the other auto api url works now
3:37 so make sure this is something like single auto. Now we're going to need to get the request for this one,
3:43 so I am going to unhide that piece, let's go ahead and run this so if we go back, refresh it so we can still get to this one,
3:54 was autos and this one, well we give it 123, normally this doesn't work, because we're going to go to the database
4:00 there might not be a 123, but right now, you can see those two pieces are wired up so our little endpoints are wired up.
4:06 What do we need now, well we need autos, we need data, we need cars, there's no data to return, so the next thing we're going to do
4:13 is we're going to add some temporary fake data until we get to the point where we implement enter actual database integration with sqlalchemy.

Talk Python's Mastodon Michael Kennedy's Mastodon