From the course: AI Pair Programming with GitHub Copilot X

Implementing a REST API with Copilot

From the course: AI Pair Programming with GitHub Copilot X

Implementing a REST API with Copilot

- [Instructor] So we have our models, we have our admin panel. Let's create an API for our expenses calculator app. Let's create a new file called serializers.py. Let's close the navigation tree and go ahead and save this. And let's see what copilot knows about serializers in Django REST framework. So from rest_framework, that's right copilot, import serializers. And the class is ExpenseSerializer. Nice, I'll Tab through. Now here is where it gets tricky because it makes a few assumptions but it's not 100% contextually aware. Now first of all, actually I think there's no reason why we shouldn't use a model serializer for that. So this is where I choose something different than what copilot offered. So let's do a model serializer. And I want to go ahead and give it a little more info about what I'm doing. And actually I do this thing sometimes where I just dump a bunch of code there and then remove it. So I'll say that the expense model is this. So pasting code for a minute in order to give copilot context is rare but sometimes it can go a long way and it's worth trying if you feel like that extra bit of context can help and you don't feel that copilot is getting it. You can also try copilot chat when you feel that copilot's completions are not as context aware. But this is a kind of a workaround that work for me. So I'll say, you know, model class and I'll let it do its thing. So now it's telling me the right thing. And right beneath there it gave me all the fields. And now expense needs to be imported, right? So right here from .models import Expense, which is great. Copilot also takes on some of the things that IDE tools use to do for us or sometimes do for us. So let's delete this since it's no longer necessary. Let's write a little descriptive docstring, serializer for expense model, exposes model fields. Yeah, that's okay. Good. Now let's head over to our views. So Command + P is a quick file jump and from rest_framework import, let's do viewsets, import ModelViewSets, that's right. And I'll go ahead and define a class ExpenseViewSet. Now importing goes a long way when working with a tool like this. So first of all, I don't need this shortcut anymore. And I'll say from .serializer, that's right. So seems just about right for now. This is where we head over to the docs just to be sure. So let's open a new tab go to, go rest framework, and API Guide, Viewsets, ModelViewSet, and serializer class. It was right, but I want to do and allow any, so let's see the permissions. Appoint permissions. And allow any. So actually a good idea would be to just do this at the settings level. So now all that's left really is URLs which would be saying something like add api url. And actually let's do a little routing. So from create a router for expenses. And from expenses import views. That should work. From rest_framework. Yep, that would be right. And now router. That's correct. That's also correct. This is where I should seeing ExpensesViewSet. That was my mistake actually. And then I have ExpenseViewSet, which is great. The route is registered. Lovely. And now let's add api url. Nice. And just include which comes from I think here, but copilot has confirmed, and settings.py. Now I want to do I a rest framework with allow any permission. Okay, so REST_FRAMEWORK, DEFAULT_PERMISSION_CLASSES. That is absolutely correct. Great. Now to the moment of truth. (laughs) Does my API work? Let's find out. Let's open our browser to /api. It tells me that api doesn't exist, which is, ah, it's telling me that TemplateDoesNotExist. Okay, so here's one of those things where you may have to go back and check out what is missing. So you have to configure the browsable API. Let's go ahead and see if it can actually do that for me as well. So configure rest_framework browsable api. There it is, api auth. So I'm going to go ahead and accept this solution. And now this has become redundant so I'll go ahead and delete this. Remove this to clean things up. Head over here. TemplateDoesNotExist. I know what I forgot. I need to configure REST framework in my installed apps. There it is. And now we should be doing much better. So there is my login screen, but I don't need it because there's no need for authentication in this app. And I see my expenses API, it works. Now let's try to add an expense via my API. So the name of this expense would be a orange juice. Why not? And the amount is, cost me 1.10 and it's food, why not? And there it is. It posted. If I go back to my expenses and refresh, there they are, one by one. Now if I go to the first one, how did I do? Good. If I go to the second one, how did I do? Good. Now friends as fun as it is, testing these expenses one by one and posting, there is something that I could do. It's not always so much fun to do, but copilot takes a lot of the effort out of which is writing out automated testing. I think it's time to try to do that in the next video.

Contents