Python-powered chat apps with Twilio and SendGrid Transcripts
Chapter: Course conclusion and review
Lecture: Review: Creating a Flask API
0:00 Working with twilio studio was great,
0:02 but what we wanted to do is actually get that information and get those triggers over
0:07 to our code to run on our back end so we could put into our Admin
0:11 system. We could send them their emails and all of those sorts of things.
0:14 In order to do that, we created a cake order,
0:17 api endpoint'/api/orders/cake'.
0:21 And all we have to do is to get the posted Json body with the request
0:25 So we say request.get_json().
0:27 That's a python dictionary, and we use pydantic to validate and convert that.
0:32 So we said Order model, which is a pydantic model,
0:35 passed in the keyword arguments out of the dictionary.
0:37 So that's the Star Star(**). We got our validated model,
0:41 and then we used our service,
0:42 our user service, in order to record it and save it to the database and
0:46 even create the user if needed.
0:48 And then finally, we create another pydantic model that had the response message and we
0:52 just returned response.dict. That was the happy path if something went wrong,
0:57 we did this in a try except block,
0:58 so we caught an exception and we said,
1:00 Here's the error. We got 500 we returned a response with an error code.
1:05 In this case, we're just catching general errors and saying 500.
1:08 But of course, we might want to catch validation errors out of pydantic and convert
1:13 those to 400 or 402 errors to tell the user you submitted something
1:17 correct. You should be a little more nuance,
1:19 but just for the basic idea.
1:21 Here's what we got going on.
1:22 Try to do this stuff, catch it as an exception and then return a flask
1:27 response with an error code instead of just a
1:30 Hey, just everything's OK. 200. Here's your error.
1:32 That's it. Incredibly simple, right?