Python-powered Chat Apps with Twilio and SendGrid Transcripts
Chapter: Course conclusion and review
Lecture: Review: Creating a Flask API

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Working with twilio studio was great, but what we wanted to do is actually get that information and get those triggers over
0:08 to our code to run on our back end so we could put into our Admin system. We could send them their emails and all of those sorts of things.
0:15 In order to do that, we created a cake order, api endpoint'/api/orders/cake'. And all we have to do is to get the posted Json body with the request
0:26 So we say request.get_json(). That's a Python dictionary, and we use pydantic to validate and convert that.
0:33 So we said Order model, which is a pydantic model, passed in the keyword arguments out of the dictionary.
0:38 So that's the Star Star(**). We got our validated model, and then we used our service,
0:43 our user service, in order to record it and save it to the database and even create the user if needed.
0:49 And then finally, we create another pydantic model that had the response message and we
0:53 just returned response.dict. That was the happy path if something went wrong, we did this in a try except block, so we caught an exception and we said,
1:01 Here's the error. We got 500 we returned a response with an error code. In this case, we're just catching general errors and saying 500.
1:09 But of course, we might want to catch validation errors out of pydantic and convert those to 400 or 402 errors to tell the user you submitted something
1:18 correct. You should be a little more nuance, but just for the basic idea. Here's what we got going on.
1:23 Try to do this stuff, catch it as an exception and then return a flask response with an error code instead of just a
1:31 Hey, just everything's OK. 200. Here's your error. That's it. Incredibly simple, right?

Talk Python's Mastodon Michael Kennedy's Mastodon