Python for Entrepreneurs Transcripts
Chapter: Building your email list
Lecture: Storing your API keys in your web app
0:01 Our MailChimp account is created, we've created a list and we've created an API key.
0:05 Let's start moving this into our web application.
0:09 So here you can see I've created blue_yellow_app_11,
0:12 so this is in a separate section in a GitHub repo
0:15 so you can see the snapshots over time,
0:17 so make sure that you go to the correct section if you want to check out this code.
0:20 One of the things is how do we get this API key into our app,
0:25 we probably don't want to just jam it in code and bury it somewhere,
0:29 that's not a good idea, we could put it into our configuration.ini files here
0:34 so here is our development.ini, we'll also have a production.ini,
0:38 maybe these are actually different between production,
0:39 and development, I don't know, we want to put them somewhere,
0:42 another option that is pretty common is
0:44 to put them into environment variables in the server
0:47 so they never get checked into source control, that's pretty solid,
0:50 what we're going to do is I am just going to leave some empty spots here
0:53 when you get this code, you can copy it,
0:56 you can fork it over and you can put your own API keys here and fiddle with it if you like.
1:00 Or just the MailChimp part won't work in the demo if you don't put your own API keys here.
1:05 Once I fill this out, which I'll do off-camera,
1:08 the question is how do we get this data in from our configuration files.
1:12 so here is our "main", startup for our dunder init,
1:15 and I've added an init_mailing_list section,
1:18 so first of all we're going to read this data in, so we can come over here to config,
1:22 I don't know if we've talked about this yet, but the config has a get_settings,
1:27 which returns a dictionary of all the things that are in that area
1:31 we were just looking at.
1:33 If we want the MailChimp API key, it's pretty straightforward,
1:37 we just say settings.get or however we do dictionary access, right,
1:42 do you want to crash or get null if the key is not there;
1:45 so we'll do this, and let's just say that's not misspelled,
1:49 so we'll grab this value and then we'll also do for the list_id, OK,
1:52 so it's loaded into our running Python app, now where do we store it?
1:56 Well, much like we have an album service, let's create an email service.
2:01 Or a mailing list service.
2:06 OK, and down here let's just create a class called MailingListService,
2:10 and let's just give it a couple of pieces of information,
2:14 first of all, maybe you could hold that, right,
2:20 and maybe we could treat these as static
2:22 so we'll have a static initialize method here that we can initialize in the beginning
2:25 and then those will be set for the rest of the app, so we'll call this global_init,
2:31 so here we can come down and say "I want to set MailingListService.mailchimp_api
2:37 is going to be this", and what else do we want?
2:40 We want to set the list to be the list, like so.
2:46 And that is going to store the data here,
2:48 and then later, we're going to have things like "def add_subscriber... email". Like that,
2:55 something like that, and let's just put a pass.
2:58 So in this area we are actually going to work with the MailChimp API itself
3:01 and use this information that got set during startup to initialize the API
3:07 and then we'll do the work that we've got to do to actually submit the user to the list,
3:12 so last thing to do is import and call this from dunder init, so let PyCharm import it,
3:18 thank you PyCharm, and then we'll call global_init and what do we want to give it,
3:21 the mainchimp_api and mailchimp_list_id. And after that,
3:26 we should be able to go and call MailChimp mailing list .add_subscriber,
3:33 and just know that that information is already right there for that class to use
3:38 to get them on the list, but of course, we're not doing this here,
3:41 we're going to do this somewhere else.
3:44 Let's just go and run the code to make sure that we've gotten something out,
3:48 and I will print, let's just print out the length of the list id, OK.
3:54 Up and running, OK looks like we imported something
3:59 if that crash was zero,
4:01 maybe that would be a problem but it looks like it's working.
4:04 Now we're ready to structure our controllers and a little bit of other code
4:07 to make sure that we can submit these people to the MailChimp API
4:11 and of course we'll be using the MailChimp API
4:13 when we get down to the final step.