RESTful and HTTP APIs in Pyramid Transcripts
Chapter: Web application building blocks
Lecture: Pyramid building block: Config
0:01 Our next building block is configuration.
0:04 Now, these web apps come with two prebuilt configurations
0:07 a production one and a development one,
0:10 but you can make as many of them as you like,
0:12 and just pass them to the start up for the web app.
0:14 So here's a typical configuration file,
0:17 we've got some stuff about running our web application
0:20 in this case when we created it we called it my_webapp,
0:24 not very creative, but that's what it was called.
0:26 So we can say the main entry point is to use basically the package my web app
0:31 and then we can have our specific things
0:34 that we care about in our web application,
0:37 so maybe we need to be able to specify a different db file
0:41 for when we're running in debug versus development
0:45 rather than when we're running in say production or test.
0:48 So here we can say db files can be db connection, right
0:50 in this case we're using sqlight but either way, we can put this key in here
0:53 we can also put like an API key.
0:56 Now a case where this might vary is, if you're using Stripe,
0:59 so Stripe has both test API keys and production API keys,
1:04 the test one accepts test credit cards and does not charge you
1:07 or charge whoever's card you put in there,
1:10 but the production one you obviously want it to take a real credit card, not test ones,
1:14 and you actually want it to charge when it says that the charge succeeded.
1:19 So you very much might want to have different API keys
1:21 say for your Stripe keys and so on.
1:24 You can also have different includes,
1:26 here we're using the Pyramid debug tool bar
1:28 and this might only appear in the development.any but not production.
1:31 Similarly reload templates maybe it's false and production to be faster,
1:35 but true here to be simpler like you make a change to our template file
1:41 and they just automatically appear, so you don't have to restart the web app.
1:44 Now, once you set this up and you run your app,
1:46 this will be passed over to our __init__ where we have our main entry point again,
1:51 and inside here we get our config right, this is already set up by Pyramid
1:55 and we can go to this config and we can say give me the settings
1:58 that I found, or that you found in this configuration file,
2:02 so I can go get me the settings, so there is a config.getsettings
2:06 and from there I can say let's take that db file and get it out,
2:10 so config settings is just a standard dictionary, and I'm using the safe way
2:15 rather than the type that will throw a key error if it's not there,
2:18 so the safe way to get db file, so I'll say .get db file and get none if it's not there.
2:23 Similarly API key get me the API key,
2:25 and now we can just take these and pass them off
2:28 to whatever part of our web app, whatever subsystem we might need
2:32 so the db file might be passed to the data access layer
2:35 and the API key may be to the credit card service and things like that.