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