 Good morning and welcome to the second day of conference talks And I'm really happy that you got up this early in the morning or that you're maybe in a time zone Where it's not that early in the morning But anyway, we have a great speaker with a fast topic to get us running and to get us all pumped up So I will not waste much of your time and just say let's welcome Dave Bodenic Hello. Good morning. Hey, good morning. Well, are you located this morning? In Israel. Oh, so what's the time in Israel this morning? 9 a.m. Oh, this is much nicer Okay, so you work for NVIDIA, right? And I saw the topic you're doing something very fast today. So I will let you have the stage Okay, thank you very much Okay, so what we're doing today. We're developing an application a web application That that serves the funniest Tech tweets that can be found and we want the entire works. We want Schema validation. We want API documentation We wanted to run fast and we want to develop fast. So that's why we chose fast API Framework created by Sebastian Ramirez not too long ago. I think a year and a half. So without further ado Let's get started first, I'm initializing I'm initializing the app instance Okay, and as we can see now we initialize the first API Everything is live coding here. The only thing that is prepared in advance is the tweets database and Some files with with comments That will guide it through so right now I'm going to write something that will display this those comments during the session. I'm going to use a feature called event template For and then I'm going to use the stock assistant. So let's see how it goes. I'm annotating the app and I'm saying On event and the event that I'm providing it is the startup event okay, and Right now I'm going to open a comments file What about that? Okay, and I'm using I'm using calcet to display it The format the JSON a bit fancy here Okay, and So now we're going to run the app First API is a web framework and it uses an ASGI web server called uvicon. So right now We're going to run it We also used that that's reload flag which reloads the app while we're making changes Which is great for development, but make sure it doesn't go into productions Okay So without further ado, let's implement our first HTTP handler We'll start with The HTTP method get and we're going to display all the tech tweets That we have okay, and now I'm opening My database which is a json file I'm not saying that this should be a production database, but Not saying it's not but it's not the problem that we're trying to solve at the moment Okay So the app that we loaded and Okay, so we have a first web application and How can we consume it? I like to consume it with a command line application called CRL it's everywhere and It has all the features that you want that you want and you're in your ID store. So that's great Okay, and we can see that we're getting a response back, which is great This is exactly what we're hoping for I like to combine it with an additional command line application called jq And it's pretty fast json output. So it's much more pleasant from the eye Okay, didn't see the comment properly because I had I had a tackle here. We can see it better right now and Another great thing about first API is that it takes all the handlers All the HTTPS that you are writing and auto creates a documentation out of it. So I'll show you how it looks like Okay, it doesn't let me show it to you That's a shame What to do Okay, never mind. I'll show it in I'll show it in the chat later, but it gives the presentation trust my word for it Moving on we're going to do a crud here and Basically, I'm going to take the IO that is here and I'm going to move it And to another model and we did it at the first attempt with firefox. So right now I'm just Going to call it We fixed the import You can see the app that we loaded and we can see that it's still working And what I did here with the IO. It's a similar pattern fast API as an amazing project generation template And in that project generation template You can see everything development deployment front and back end tests everything So the way I like to explore new technologies is either take this template that is ready as possible And and use it and then do minor changes To to put my business knowledge here or I can Start from scratch and only put in the stuff that I want. This is the the pattern that we are going to use now Okay, so this was about crud Moving on to our next example Okay, um, you can use a fast API as both synchronous and asynchronous You can also use them together meaning that here we obstructed the IO, but this if this IO is non-blocking IO we can Make the http under async And our application can work and still work and we can also do a mix and match Moving on Moving on is we're going to talk about about query prompts And fast API is an interesting approach here. It uses a dependency injection pattern meaning that those Parameters are not available during development time, but at one time they magically appear So let's start with query prompts as our tech tweets Database gets larger this call gets slower and we want to limit And what's being returned here if we have a ui will we might also want to page it so there are many use case for it so I'm saying that It has a query prompt name size And its type is an integer And right now I'm going to slice the array that is being returned Yep is being reloaded and let's see that it's working I'm adding a query prompt name size and you can see we have less stuff coming back And moving on of your algorithms, so Let's implement an event under that is very similar to the previous one But with a your algorithm, let's say that every tech tweet that we have is uniquely identified By its array index, so we'll call it a tech tweet ID Way to define it is very similar to f strings just without the f and I'm injecting the tech tweet ID As a dependency right now I'm saying that our crowd return get at like the ID index, let's see if we implemented it we've implemented it So the app gets reloaded and Instead of giving it a query program giving it a URL program and You can see we have a unique tech ID a tech tweet that is being returned For certain moving on Okay, and when we're talking about REST RESTful applications usually for get HTTP method to get data and for post HTTP There are those are the requests that are important and we want to create New data. We usually do a post request. So let's see how it goes and for a post request you provide the data as As an HTTP body and not as a URL program or a query form So let's do a post request for Tech tweets or the create tweets you want to add something Okay, and what What we dependency inject here Is a by daunting class which Gives us the ability to define strictly the input schema So Sounds very complicated, but it's actually pretty simple. So let's have a look. We're saying it's getting a tweet And the tweet is of type tweet And let's create this class tweet And it's supposed to inherit from by daunting base model Okay, we're importing it and we're only saying that it has One key called content and the content is a string And so what we're doing at the moment Okay, we are saying, okay, let's use the crud to save this tweet Save this with content And what we're going to do that is custom for and For post request that creating data we're going to return 201 status code. Uh, we can also say that instead of returning the status code here We can say that The user status code here is 21. It's the same. So right now we are going to the crud and we're going to Implement the save method So what we're going to do now we're going to We're going to append it to our database and we're going to save it. So It will be persistent between reloads Open the file for writing and instead of loading We'll do a dump just get the object and then it gets fun Okay, so the app that we loaded right now Um, okay So This is for crl. We're saying that This is a json that we're sending Data as json and we're sending it to take tweets and the data that we send it's content of a new tweet Okay And we can see that it got to 101 created. So let's see if it's saved I think it's number four Yeah Okay, and we implemented a post request with a body So let's say that you don't know you don't want the ski you don't know the schema in advance Or you want to do some exploration? And so instead of defining the type you can You can say that It gets a dict And then you can send whatever you want to json and it's not strict and it's not validated And but And allow you to do some exploration And let's try again. I'll try again to to share with you how it looks like on On on the on the schema. It didn't let me to do it earlier See if it would behave right now Yeah, okay, everything is here So we can see all All the HTTP methods that we implemented it has auto generated documentation You can see here the tech tweet ID is a url param We can see it is an integer and we can see that it is required. We can try it out Provided with the number we get the response here the status code and we can also get The crl command and for the post if we use a strict schema, we can also see the scheme Okay, so let's get back Okay, moving on to our next example Okay Crl is really great. It's combined Combining with j combining it with jq is even greater But I like to use a python commanding application called htpi Which gives in my opinion a much nicer command line interface So as you can see We don't need to use jq and it returns a colored output and it returns the headers in different colors And I prefer to use applications like command line application like crl and htpi instead of using Instead of using like external applications like postman Because everything is in my id view. I don't need I don't need to go out. I'm inside the development scope And This is this is my preference. I hope you like it as well Okay Right now we're going to talk about testing. Okay, let's say that okay. We have an application We already see that in I don't know 15 or 20 minutes. We're already even implemented three Http handlers if you develop this application for months you can have hundreds of thousands Of http handlers and they might be simple They might be complicated, but every time you make a change before we deliver it to the customers You want to make sure that everything is working? So you're going to look another application and you're trying each route. Hopefully and So I know incorporate everything in the test suite This will be much more maintainable. Okay so The recommended way to To test fast api applications is by test So It provides a test client The input is fixed it the test line gets the test line gets the application as input And You write a function to start with this underscore and it will be picked up by by pie test And then I can do and then I can say response is client get by the way, the sentence here is Request Sympact so if you know request, you know how to use And the test clients Okay, and let's assert something The status code is 200 Okay And I run it through by charm. It can be run through command line as well that I prefer to do by charm And we can see here that that the test cool So HTTP HTTP has a lot of status codes rest api has a lot of status codes and sometimes, you know You have to use a bunch of them and you go to other projects and you get back And you have new team members that are not necessarily familiar with web application So do yourself and everyone a favor and just be explicit instead of saying 200 or Something else like we had 201 earlier say Use a built-in enum called HTTP status and say it's okay It's much more with it If you have a large test here, you can understand What is supposed to happen in the in the entire flow? So be explicit about it It will be easier to maintain an application like this So using a test line like this is nice and everything but It has some limitation It doesn't allow you to override dependencies and It doesn't let you to it doesn't give you it doesn't perform the startup and shutdown event So much more Let's call it's robust way of using the the test line is with a fixture With a bypass fixture Okay, we'll call it client and what it does with a complex manager We yield the client And now we pass it as input here Okay, let's see that everything still passes. Yeah, and this way let's say And we add authentication and we don't want authentication for test, but you know, it won't pass so we can do stuff like Override the let's say get current user and set it as me And this way we don't need a authentication for application. This is uses that only That that can that can only be executed in test if you're using It has a feature and with a complex manager So let's see that everything still passes Okay, cool Another feature that that I like to use I use it in pychom. It's called toggle auto test, but it's also There are several tools in With pytest that allows you to to do this. I think python X this or or tersemon or something like this or even both And so basically what it means that if you have something that that fails Let's fail something on purpose What we tested we tested the purpose, okay, so let's say instead I have a bug here and I Raising an HTTP exception Once again, I'm explicit the status code is It's I don't know in terms of so You see the test got auto rerun I didn't have to do anything and it failed and I'm getting an explicit failure message Saying it expected to get an okay 200, but it got an internal server error 500 so right now I'm fixing the bug And again, I'm not doing anything and I hope Okay, and the test got rerun and It turned successfully. So that's a great pattern. I'll recommend you all use it and one last thing that I like to talk about is that When when you develop a fast API application and gets bigger then your schema also gets much more complicated and the input for the open ai Specification three, which is the UI that I showed you earlier of the API documentation And might break. So what I like to do I like to add a test The app that the documentation is generated. So right now I'm only delivering an application that I know This documentation is working as well Oh, right The URL is wrong So that's it I hope you enjoyed this talk and I hope you'll develop a web application using a fast API app And thank you very much Anyone has any questions? Okay, yes, we do have some questions before we get started with this Uh a short information the talk following you Alas is not happening So we have some more time for q&a We will keep you updated on the the website On the if we find a new time slot for the next talk That being said we now have time for q&a and I did not have time because everything was a bit chaotic to prepare the the banners. So I'll just have to read these and Let's see if somebody asked like julian asked. What's the crud library? Can you say something about that? Okay, uh, it's not a library. It's like a package that uh, I wrote that it's like a python package inside This project that only only does only obstructs the iobit. So nothing too fancy here I have a model here named tweet that Opens the json finds and we implemented some methods In order to interact with it like in a real world application. You'll probably have a database driver here And maybe a database connection. Probably the database collection will come from somewhere else It's only an api to interact with your data Okay, I've got a question from dragos And uh The question is it would be interesting to see how you packaged that all and deploy it in a production environment Maybe you can give some tips on doing that Yeah, sure. Um, Sebastian Ramirez, which the creator of first api also He releases like a base a base docker image of Of first api with unicorn ng unicorn with a basic sane configuration. So what you do Let me try to do this live so and you're saying from You're saying from the anglo Uh slash I am not a member of like something g unicorn Uh Unicorn movie core fapi something like this and you provide it with the virgin and then what you do you install your dependencies And then you put in your code And it has uh, it has the anti point that it has Um Runs it. So this is this is what I use like very simple. I'm using this base image. It's great I'm installing the dependencies and putting the code everything is has some reasonable defaults And and this is what I use either with dr. Compose or kubernetes It really doesn't Okay, um, you showed that that's a question from Diego You showed that you showed url handlers as functions Can you group them in a class as methods instead? I haven't tried But it makes sense to do so But it it doesn't I know I know I know why you asked the question because of Like flask has this Connection I think or something like this like it really it really makes sense. Um I find it I think it's possible. I haven't tried it. I It doesn't feel so comfortable. It doesn't feel so comfortable to do it with first api first api feels more like functions wise instead of Instead of object-oriented wise But I think it's going to possible There's a question from aina. I see that fast api doesn't support socket io Server like the python socket io is there a plan or is is there anything about this being integrated in the future? Do you know anything about this? I haven't tried it And I think it is supposed to be supported Like I think I saw something at the documentation, but I haven't tried it Okay, and uh, there's a final question Um, normally you would use uvcon for example to to run the um fast api application Is there a way to stop uvcon from within the fast api? So that for example, you could run fast api as a background service Um, so that from the logic here you decide. Oh, I need to shut this off now Uh, usually I like to control it From the outside like usually I like to control execution for from the outside and not from the inside It makes much more sense. I don't like that the application is aware of its Of its state or the application changes its state. It says its state should be changed from the outside So I'm not familiar with the way but Instead, but I think it's possible is that instead of doing like I did here You can do you can have a main function and you can import uvcon And if you can run it Uh, which is like equivalent to what we did To work with it with the command line. I think you can also stop it But I really like to control stuff stuff like this from the outside and not from the inside Okay, uh While you answer this question in the chat david mentioned That there is web sockets already built into fast api and it's also documented on the website So if somebody wants to do sockets then the web sockets or fast api might help So this went quite well after our little problems at the beginning So, uh, at this point, I'll just thank you for taking the time and that you Managed to get everything running and then we could restart it again So let's have another round of applause for you And uh, then we'll see that we'll take a short break until the next talk So let's see, uh, if vince is there, uh, I'll just Say applause and Let's Let's see. Otherwise, I'll just give you a short applause Thank you. Bye