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

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's review the main concepts behind implementing our API. We created the Pyramid web app, we went into the __init__
0:09 and we configured the routing to define two new routes one called api_all_cars, that maps to this function, this is going to be just /api/autos
0:21 and the idea here is you do a get request against this and we want to return a json representation of some number of cars.
0:29 So, it all begins with going to the database and actually getting the cars out in this case you can see we're limiting it to 25,
0:37 we don't let that be controlled by the API, but obviously, in a real one, we would;
0:42 we go to the database, we get 25 cars not 20 thousand cars or whatever in there and we're going to return those and serialize them as json.
0:49 And the reason they're going to be sterilized as json is we set the renderer to be json, we're also matching the get verb to this function
0:58 because later on, we're going to want to be able to let you submit cars to /api/autos via post, we want to make sure that this one only processes
1:07 the reading of the data, not the writing. Alright, so once we got that, we can return it to be formatted as json
1:12 and the reason this works, we have the json renderer and the individual elements in that list are already dictionaries
1:19 which are easily serializable to json. Well that's for all the cars, what if we want an individual car?
1:26 Well, we're going to come over here and we're going to define another API method, this time called api car for the route, again json a can get
1:35 so it's only going to respond to get it's only going to return json, but this time, it's a little more complex,
1:40 we need to take some input from the user as part of the url so we're going to use the match dict, which are
1:47 where all the little cutouts that we put into the routes, where the keys are the cutout names and the values get dropped in there,
1:54 so we can go to this request.match dict and we can pull out the string that represents that id.
2:00 Then we go to a repository, and we say give us the car by looking it up by id if this was say an integer id, we'd have to do some kind of conversion
2:07 from strings to int, because the cars are stored by string ids, they could just pass it right along, so that looks nicely.
2:15 We got the car back, but we might not have gotten the car back, right it depends on whether there's a car actually matching that id
2:21 so we do a test to say if there's no car, we want to return an hp friendly response set up an error, return our response object to say 404,
2:29 there is nothing here with his id, but instead of just saying 404 not found we're going to actually say the problem is
2:36 there's no car with this id that you passed us and we're going to pass that in as a dictionary to json body which again gets converted to json.
2:44 This response object basically skips the renderer set on the overall function,
2:47 but if all is well, we're going to return just the car object again that's a dictionary,
2:51 so this is going to work just fine and serialize the individual car as a response and if you don't do anything, the response code is of course 200.


Talk Python's Mastodon Michael Kennedy's Mastodon