Python-powered chat apps with Twilio and SendGrid Transcripts
Chapter: Beautiful confirmation emails
Lecture: Loading the secrets
0:00 So, you put your secrets into your secrets.json file,
0:04 which we shall not open while screen recording.
0:07 And you shall not put into your github repository either.
0:10 Now we need to get them out.
0:12 Let's go back to our app here.
0:14 Remember what I talked about this configure method?
0:16 I said, Oh, let's go.
0:17 And just add these couple of things here because it's going to build up over time
0:20 Well, here's one more function that we need to add.
0:23 And I'm going to add this right at the beginning because maybe some of these things
0:26 need the connection string or something like that.
0:28 So to configure secrets this and write little function here that will let us open up
0:38 the file, Speak of the file.
0:40 Which one is it? Oh,
0:41 it files easy. It's just gonna be secrets.json
0:44 look how awesome pycharm is.
0:47 It's like, yeah, yeah, secrets.json, we got that file.
0:49 We've got you. That's true.
0:52 But remember, when you run these apps,
0:54 especially in production, the working directory might not be the project directory.
0:59 It should be. I would think it should be,
1:01 but it might not be depending on how it's run. So let's be very explicit and
1:06 do a little bit more work up here.
1:07 So I'm going to use a new path object.
1:14 We can come out here and say we would like to create a path based on
1:17 something fixed that doesn't vary on how the app is being run,
1:21 what the working directory is and so on.
1:23 And for any python file, we can say Dunder file doesn't come up in auto
1:27 complete, but is the full path to this.
1:30 So right now it's once you expand the user bit,
1:33 you would see what the path is out there,
1:36 right? So it's that path right there.
1:39 That's a file. If we want the directory containing that,
1:41 we can say parent, And then if we want to get to the secrets,
1:46 which is the file right there,
1:47 we need to concatenate those. And so you just use / like the divide operator
1:51 and it overrides what divide means to mean build up the string that is the
1:56 file path. And, of course,
1:57 on windows. That would be backslash.
1:58 That's just how it looks in code.
2:01 Now what we want to do is verify that this exists so we can say,
2:05 If not file.exists well,
2:09 that's going to be a problem.
2:10 So it's just raise exception. All right,
2:15 this little, somewhat helpful message cannot start the secrets.json file is missing.
2:20 Do you need to copy the template over?
2:22 If you do go get on that now.
2:24 The next thing to do is just to load this up.
2:27 Using Json will create a pathway with open file in read mode as file input stream
2:36 And then we're going to say,
2:37 Json, which needs to be imported.
2:42 Just do a load, then take the stream.
2:45 The data is that so that's going to be our secrets.
2:48 Let's call it secrets. Now what are we going to do with it?
2:50 Well, we have these secrets loaded up as a dictionary,
2:53 but I want to be a little more structured,
2:55 something that's a little easier to program against.
2:57 So we're going to have a new category of code that we're going to use later
3:01 as well. We've got views,
3:04 templates, models, data models,
3:06 API stuff. Let's have some kind of helper things that just call infrastructure.
3:13 And in here, let's go ahead and add a file called app Secrets,
3:18 an app.secrets is going to have what I had started to put here like
3:22 that. So we'll cut those out and put them here, because this is sharing stuff
3:30 like, say, from send grid from twilio from the database.
3:32 I'll call this the sendgrid_key_name in the send_grid_api_key ofcourse.
3:39 You wouldn't have more down here as we go.
3:44 So let's go and just set these values over in our app.
3:49 The app_secrets.sendgrid.api name is going to be secrets. Now I'm a fan
3:55 of doing the kind of friendly get and give me the default value if you can't
3:59 get it and we'll just keep going with dictionaries in general.
4:01 But for this one, we want to make sure those entries are in there so
4:04 we can be more explicit. More crashy about it,
4:08 right? So if they give us the sendgrid thing and if it's not in there
4:10 it's going to crash. And then what do we need from that?
4:14 We're going to need that's build little variable here and we go to our send grid
4:21 and what was it? We're going to look in our template file and it is
4:26 a secret key here. We also have the key name,
4:34 which will be the other one.
4:39 Perfect. Now, this should be totally working.
4:41 I'm gonna run it. Just make sure that it does. Starts up.
4:47 No crashes. So, yeah,
4:49 it looks like everything is going just fine.
4:52 Now, we haven't tested this,
4:53 but I'll find out whether it works,
4:55 Not real quickly. Of course,
4:57 in your set, a breakpoint printed out.
4:58 Whatever. But I don't want to share those keys.
5:00 Right. I think this is ready.
5:02 And all we gotta do is go back to our email service that we were working
5:06 on and over here and say,
5:10 app_secrets, api_key. Perfect.
5:16 We should be able to create our client.
5:19 And if it'll go, we can do one more test,
5:22 I suppose. Let's run postman once again,
5:26 just like we did before against this order_api.
5:29 And make sure that we can create the sendgrid client correctly and that it accepts our
5:33 API key. Probably not validated on the server,
5:35 But it's a step. Hey,
5:38 look, it works. It works just fine.
5:39 We should see we'll email. So and so about this.
5:42 And of course, that means that line ran,
5:44 and I think we should be all good.
5:46 Yeah. So we've got our secrets loaded by storing them in this file that is
5:50 not shared and git hub. And you know,
5:53 when you deploy, you're gonna have to make a copy of it and pasting the
5:55 values. But, hey, that's just how those sorts of things go.
5:59 It's definitely better than showing up and should get.