Presentación
Vídeo
Transcripción
Extracto de la transcripción automática del vídeo realizada por YouTube.
good afternoon everyone welcome back after lunch as the first of the afternoon sessions we're going to be listening to Miguel giving us a talk about doing restful web services with flask thank you thank you for coming so before I start in case you don't
know me it's a little bit of background information of myself about two years ago I wrote my blog engine in flask from scratch and then in these two years I wrote a few articles on it about flask you may have heard about the the mega tutorial or maybe
the the four also articles on api's I wrote I have a bunch of extensions also for flask these are the fact that I am most proud of so feel free to check them out and last but not least I wrote the O'Reilly book on flask which it's going to be in
bookstores prison so another preliminary slide all the stuff that I'm going to show today exists in a github project it's all working code that runs bookmark that address and after after the talk if you're interested you're welcome to play
with the API that I'm going to present here there are very detailed instructions in the readme file so even if you are a beginner you should be able to run this now for those of you that have no idea what flask is this is the only beginners slide I have
this is basically the seven-line hello world from the flask official documentation the first two lines create an application the bottom two lines run it the middle stuff is where they important things go on and as you can see there's there's a decorated
function up the route tells flask that the function that follows is the handler for the root URL that you can see represented as a slash so flask knows that when a client requests the root URL it needs to call this function and then it collects the return
value of the function and sends that to the client as the response and if you understand this I think you understand most of what flask is because it doesn't have much more than this okay if you run this as a script on you know on your server then you
connect with a web browser and you will see hello world in your new browser window that's it so okay so let's start with the good stuff press relate the eyes so I'm going to show the few basic API methods and how I implement them note that I say
how I implement them it's not the only way flask it's all about freedom and choice so anything you don't like about how I do things you should improve it and let me know so I learned to sew okay for this example and I have a system that keeps track
of students and classes and students registered to classes so that that's the example that I'm going to follow through in all the examples so I have a route that gets the list of students this is a get request my URL is slash student slash I want it
to end in slash this is optional but I want to look like a folder because it returns a collection so how I implement this let's look at the the one line in this function from the right first of all I talk to my database and and for this I'm using flasks
equal alchemy again it's my preference you can use whatever you want but using flats equal Oconee I get all my students and then I for each student I get I ask the student to give me its URL so that ends up being a list of URLs I put that in a dictionary
with key name URLs and then that dictionary gets JSON defied this is a flask helper function that converts a dictionary to JSON format as a string and then that's the response that's what the client gets a list of URLs so the client knows only a few
top-level URLs he knows how to get the list of students how to get the list of classes and so on but it doesn't know much more than that but now the client asked for the list of students and now it's got a few new URLs that I didn't know about
before so a single student can be obtained with with this route and for this one since I I need to have a unique URL per student I have a dynamic component in my route which you can see with the angle brackets so I say that my URL for students is slash students
slash and then the numeric ID this is my primary key in my database and that is my choice I chose to do it this way and I say that this is going to be a handler for a gate like like the previous one and the implementation is very simple I just talk to my database
to get the student from the ID which flask nicely send us an argument to my function and if the ID turns out to be invalid flasks equal alchemy will send a photo for back to the client I don't need to worry about that and this is a thing that I'm going
to repeat a lot in this session my routes are very clean I try not to do you know silly error checks that that really make the code kind of you know dirty so I need lower level code handle my errors I don't do it here so if s gets a sign that means that
the student exists and it's known and then in the next line I I tell the student which is a you know one of my data base models to convert itself to JSON and that returns a Python dictionary which then gets rich on file and now the client has the student
so in some cases collections are no are not global collections of things there are specific collections that apply to single resources so an example of that would be the list of classes a student is taking so for that you build a URL that has the uniqueness
so I need to include my student ID but I also need to make sure it looks like a collection because that's what it is so I append a final component that tells me that this is a collection registrations and that ended with slash so implementation is also
very simple pretty similar to the big collection of students that we did before the difference is that I get my collection from a relationship in my database so I have my student and then I say s dot registrations and that that's wire to be the relationship
in my database and then from that I get the URLs into an 18-2 list and that list gets into the dictionary of URLs and that gets to identify and off it goes to the client so this is a post request the restful principles say that when you need to create a request
the client needs to send the request as opposed to the collection URL so that goes to students the generic URL and the way I do this is I create an empty student and then I call another method that I have in there from JSON this is the reverse of the to JSON
that I showed before so this guy takes the JSON from flask philosophy gives me that in request JSON that that comes in the form of a Python dictionary and and then the student imports itself from that dictionary then I write it to the database those are the
[ ... ]
Nota: se han omitido las otras 3.291 palabras de la transcripción completa para cumplir con las normas de «uso razonable» de YouTube.