RESTful and HTTP APIs in Pyramid Transcripts
Chapter: Web application building blocks
Lecture: Pyramid building block: Routes

Login or purchase this course to watch this video and the rest of the course contents.
0:01 As you saw from looking at the views, the routes are critical
0:04 this is the first interaction the user has with their web app,
0:08 they enter a url it's the routes that decide
0:11 what functionality actually maps to that url.
0:14 So when you're working with Pyramid you'll see there is a main start up function
0:18 that is basically the entry point into your web application.
0:22 Now, in real apps, this is probably factored better,
0:25 but in this example we're just going to put the routing code right here,
0:28 what will happen is we will be given some settings
0:30 that come from our configuration file, and we'll create this thing called a config,
0:34 and then we can use this config to register routes,
0:38 so the first thing that we want to do is we want to register a static view
0:43 if you're just doing a purely dynamic API,
0:46 you can skip this step it doesn't matter,
0:49 but if you want to return anything that is
0:51 an image, css, javascript, anything like that
0:53 you're going to want to set up one of these static folders and register a static route,
0:58 and by default, none of the files can actually be served off of the server
1:03 that have to do with your web app, this is a good thing
1:06 you don't want them to grab like your Python source code
1:09 or your configuration files with your API keys
1:11 that would be really bad, but sometimes you do want to serve static files
1:14 and so you'll put them in this static folder you register here
1:17 and they have a cash age as well, so they don't keep getting redownloaded,
1:21 and this cash age is in seconds by the way.
1:24 Ok, so once you have added your static view, you can add these dynamic views
1:27 and in this very simple example we're gonna let you explore some albums
1:31 and if you hit the home page without going to /api/albums,
1:35 you're just going to see some basically static html that says
1:39 welcome to the album API, here are the functions you can call,
1:42 so I named this route docs, and it's just going to live at /
1:45 that just means it's just the basic,
1:48 this is the default thing when you type the server.
1:51 Next up, we're going to let the user get a list of albums,
1:55 so if they do a get request or I guess put,
1:58 they will also get this route as far as the route matches go
2:03 they can go to /api/albums and and in this case
2:06 we can return all the albums to them.
2:09 If they want an individual album, they maybe want to refer to it by name
2:12 like maybe that's the way we have structured it in our url
2:15 so we've got some sort of like slug that is associated with the album
2:20 so if it's like dark side of the moon
2:23 it might be all lowercase, dark-side-of-the-moon,
2:27 so maybe the route we're looking for is /api/albums/dark-side-of-the-moon,
2:32 so we're going to use this little name fragment here,
2:36 that will let us actually name a part of the url,
2:40 and then as you saw in the previous section
2:42 we can actually pull out the value for whatever goes in there
2:45 and use that in our query, so this routing matching template thing is really nice.
2:50 So you can have either one like the api/album that's just static if you will,
2:56 or you can have one that's passing data to the view methods
2:59 with /api/album/{} whatever you want to call it,
3:03 in this case I'm calling it name fragment,
3:05 and that's how it will be passed to the view;
3:07 once you get all this set up, then you tell the system to scan all the Python files
3:11 that it knows about in the project, it will scan them
3:15 and see if any of them have that view decorator, the view config,
3:19 and if they do, then it will wire up that function to the name routes here.
3:23 Once you have this all up and wired together,
3:26 you're ready to just start the app,
3:29 so you go to the config and say here's the app,
3:31 run it, and then you're off to processing requests.