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