Python-powered chat apps with Twilio and SendGrid Transcripts
Chapter: Creating the Flask Python web app
Lecture: Concept: Creating an HTTP JSON API
Login or purchase this course to watch this video and the rest of the course contents.
0:00 We saw how to call an API from Twilio studio. But how do we create one? Well, we're going to use Python and flask,
0:08 and it's really, really straight forward, but we're going to define a Python function called cake_order(). You call it whatever you want.
0:15 This name actually is only needs to be making sense. You it's not used publicly. We're going to add a route. And we used to blueprints this properly,
0:24 separate our code and not just jam it into one giant file. So blueprint.route give it the URL until it accepts posts.
0:31 Remember? That's what we were doing over in studio. We said we're going to post JSON body, so make sure we set it to be post.
0:37 Then we need to get the data submitted. My twilio studio. So we say flask,
0:41 request get JSON. That gives us a Python dictionary equivalent of what we had over
0:45 there Is that JSON document, we used pydantic to convert and validate that model that data
0:51 So we created an order model and it had a cake and a customer and a price and so on. So we just take the data and this double *
0:59 thing means take a dictionary and converted to keyword arguments, passed to the constructor or initializer here that generates the model data,
1:07 which is all validated and verified. We had this user service which recorded the order and created the user if necessary.
1:13 So we passed over things like the cake customer and price. And then finally we create a response because, remember, Twilio Studio wants to say,
1:21 thanks that worked, your order was accepted and your orderid is 700 or whatever it is. So then we're going to pass that back,
1:28 and because we want to have a dictionary and not a pydantic models return, we have to say response.dict And that's all we do.
1:36 This is all of it, except it's not because maybe something goes wrong.
1:40 Maybe they submitted the wrong data and that model validation that order model **data Maybe it says, Whoa,
1:46 the customer part is missing. That's not going to work. That would look like our service crashed. But the problem really is what they submitted right.
1:53 In order to communicate that back, we put this into a try except section, and then we send back some kind of error here.
2:00 We're just saying error code 500. But we could also catch a validation error out of pydantic and say, That's a 400 or 422,
2:07 you know, invalid data was passed in all sorts of things like that. We could have different error exceptions and send those back.
2:13 But the main takeaways come up with their body, come up with the error code. That's the status code. And then we just say,
2:19 flask.response. Give it the JSON has text, set the status code, set the mimetype application, JSON, and send it right back to them.
2:28 That's it. That's the entire order, API implementation. And this is how studio and our Python flask application work
2:36 together to hand of the ordering process.