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