RESTful and HTTP APIs in Pyramid Transcripts
Chapter: Your first service
Lecture: Reorganizing the project for APIs

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Let's do a little bit of code reorganization, so that it's a little easier to understand where we should be putting various pieces,
0:10 and we'll also do a little bit of design work to get started, we don't want that basic red page, we want something about APIs.
0:16 So notice I've copied what we just created, so here is sort of a snapshot of this service starting here,
0:23 so this is when it was just created and now we're going to keep working on this one which I'll probably snapshot later as well.
0:28 So here we have it loaded up, now first thing I want to reorganize is our views actually let's go ahead and do the HTML first;
0:35 so remember if we run this what we get- down here we have this red pyramid starter thing and I'd rather have a page that says welcome to our API
0:45 here's the operations that you can call, issue a get request here, things like that
0:50 so instead of you watch me write that, let me just drop in a little bit of HTML and css okay notice that I've made the color of the page black,
0:58 I've made the background white, it should not be red anymore, but will it? So recall over in our start up we're setting this max cash thing here
1:09 to say cash for an hour, so if I go and I pull this up again notice the words here have changed, and it has these endpoints
1:17 but the look and feel of it- the look and feel is still the same, like it's not white, so we've got to do a hard refresh
1:25 which is a little unfortunate, but that's the way it is, okay? Let me make one more change, so over here, maybe just for this moment
1:35 while we're doing our dev I'm going to set that to one second for the cash age so it doesn't last that long;
1:41 There's interesting tricks we can do around cash busting and basically generating a different url any time that that changes,
1:47 but that doesn't really apply to services very much, so let's just fix it for this homepage and then we'll be good.
1:53 Let me also drop in a different layout here, I think we can get a slightly better layout if we do this
1:58 we come over here, we refresh it, oops it looks like the first auto service notice there is this static url here, we want to just, we could fix it
2:07 but the stuff kind of makes me crazy anyway, we'll just do like that, all right, so we have our service back and running,
2:13 we have auto service, a basic restful auto service, well it's not super restful yet, in fact it doesn't do anything
2:18 but we do have our little operations here, get autos, individual car, we have our github project, which actually goes back just to my account.
2:27 Alright, so our goal now that we have some kind of documentation, is going to be to have / api /autos do a thing, right,
2:35 we want to have this little APi section, so the next organization thing that we want to do here,
2:40 is to go over and actually reorganize this a little bit, like I said, do not put all of your views, this is our little homepage view,
2:48 let's rename that to index, so we don't want this to be completely full of every possible request,
2:55 and I am not sure that this has any value so let's take this off, just doub le check, it's asking can it sh ut it down or rerun it,
3:02 that's exactly what we want, alright, so yeah, it's not using this so we can clean this up a little bit,
3:07 now notice right here, this request in PyCharm gets this warning like hey this is not used, and if you have an unused parameter,
3:13 that could be a bug, but in this case, we just have to have it here for the system to work, and we are not going to use it,
3:18 so anytime you have that you can out it underscore, and that means I know I am not using this variable,
3:23 I am naming this thing to ignore it, so that warning goes away. Alright, so this is all well and good, maybe if we call that home I can call this home.
3:30 If the route name is home. But, let's reorganize this a little bit, let's make a views folder, and let's make an API folder.
3:42 So we can keep our regular views and our views that are associated with API calls separate.
3:48 So I am going to put this in here, and we will move it there, and let's move it there and then I will rename it,
3:53 and I don't want vies.view so let's rename this to home page, or something like that, okay, now, we may be able to run his, we may not. Let's find out.
4:06 So if we run it, there is a section here that does this scan, and it is just looking for a thing with a view config, called home.
4:14 The uncertainty is will it actually look down inside that view, let's find out. Let's rerun it, my first thought is no but let's see, yes, no.
4:23 We're missing our template, oh it looks like maybe it did find it, but we need to go set our template, okay, so here what we can do is
4:31 we can actually give it the package name, the package name is svc1_first_auto_service: like this, and it says
4:41 look at the top of the package in the template folder. Okay, back to good, so now it's found it, that is all great,
4:48 let's wrap this around a little, so we've organized our stuff into the views here, and I believe that fact that PyCharm added as subpackage
4:55 instead of the regular view, a regular directory is why it was found. The next thing we want to do is we want to have an auto service here,
5:02 so let's just call this auto API, how's that; so over here, we're going to have very similar code, so let's go ahead and just copy this for a moment,
5:14 we'll tweak it, so we're going to need the Pyramid view, we need the view config, basically the route name is not what we're looking for,
5:23 and the renderer is definitely not what we're looking for. We want that to just be straight up json.
5:28 And for this, let' say we're going to have the name of it just autos. We can go one step further and we can say the request method=get
5:40 alright, so that means even if somebody does a post, to autos, whatever that url is, we'll specify it in a moment, somebody does a post to it,
5:49 it's going to come up 404, but if they do a get to it, it will run t his function. Now, home is probably not the best,
5:55 let's just call this all_autos or something like that, okay, so we're getting close to having our service here, but,
6:01 we're not really calling, there is no way to get to this, like what does autos even mean,
6:08 and it may be an error to try to run it, it didn't look in here, let's add a __init__ to make that a subpackage,
6:19 try it again, now there is the crash we were hoping for, so, it says, no route named autos found in view registration.
6:28 We came over here, and we tried to say we're going to use this thing called autos to map to this.
6:37 and Pyramid said yeah, but there is now a thing called autos, so this is broken, there is no way to put the pieces together, kaboom, crash.
6:43 Alright, so what we're going to do next, is we're actually going to map that url over here.


Talk Python's Mastodon Michael Kennedy's Mastodon