In this video we're going to create our first API view the API view that we
create is going to be a hello world API view that demonstrates how you would use
an API view in practice
Start by loading up the atom editor and opening the views
dot py file that was created in our profiles API app in our project
remove all the contents of this file and we're going to start by importing some
things at the top of the file so let's type from rest framework dot views import
API view
and from rest framework dot response import response with a capital
R
what this does is it imports the API view class from the rest framework dot
views module the rest framework is the Django rest framework that we installed
previously in our requirements dot txt file
this line imports the response object
which is used to return responses from the API view so it's a standard response
object that when you call the API view or when Django rest framework calls our
API view it's expecting it to return this standard response object I'll show
you how this works in a moment
Now that we've added these two imports let's
create our API view class so type class hello API view and then in the base
class let's type API view and let's add the doc string test API view so this
creates a new class based on the API view class that Django rest framework
provides and it allows us to define the application logic for our endpoint that
we're going to assign to this view so the way it works is you define a URL
which is our endpoint and then you assign it to this
view and the Django rest framework handles it by calling the appropriate
function in the view for the HTTP request that you make so we're going to
be accepting HTTP GET request to our API so let's type def get self request format
equals none and then in the doc string we'll type returns a list of API view
features which we're going to be doing in a moment
I'll just explain what this
get function is so the way API views are broken up is it expects a function for
the different HTTP requests that can be made to the view so the HTTP GET request
is typically used to retrieve a list of objects or a specific object so whenever
you make a HTTP GET request to the URL that it will be assigned to this API
view it will call the get function and it will execute the logic that we
write in the get function
the parameters here are self which is required for all
class functions a request object this is passed in by the Django rest framework
and contains details of the request being made to the API and the format
which is used to add a format suffix to the end of the endpoint URL we're not
going to be using the format but it's best practice to keep it in here so if
you do ever enable formats on your API it will be supported by the functions
that we create
Okay so what we're going to do in our get function here is we're
going to define a list which describes all of the features of an API view so let's
type an underscore API view equals and create new list here and the first item
in the list we're going to type a string and write uses HTTP methods as functions
and in brackets we'll put get post patch put delete so these are all
the functions that you can add to your API view to support the different HTTP
requests and we're adding the get function here
okay so the next item that
we're going to add is is similar to a traditional django view so the API view
is very similar to a traditional django view but specifically intended to be
used for apis
Okay next we're going to type gives you the most control over
your application logic
and then we'll write is mapped manually to URLs and
that is the list that we're going to create for our API view so this is just
a simple list to demonstrate how an API view works in practice feel free to
change this list and enter whatever you want in it it doesn't matter what the
contents is we just basically want to demonstrate returning an object in our
API view
Okay every function that you add to an API view that is a
HTTP function so one of these get post patch put delete must return a response
object so Django rest framework is expecting it to return a response which
it will then output as part of when the API is called so let's type return response
and the response needs to contain a dictionary or a list which it will then
output when the API is called so it converts the response object to json
and in order to convert it to json it needs to be either a list or a dictionary
we're going to return a dictionary so open these squiggly bracket lines here and
we're going to add a key called message and we'll assign a value of just a string
that says hello
and then we're going to add a second key
called an API view and we're going to pass in our an API view list that we created
here so an API view
and that's how you define an API view with a get function
so make sure you save the file and then we're ready to move on to the next video