WEBVTT
Kind: captions
Language: en

00:00:00.030 --> 00:00:06.299
In this video we're going to create our
first API view the API view that we

00:00:06.299 --> 00:00:11.610
create is going to be a hello world API
view that demonstrates how you would use

00:00:11.610 --> 00:00:15.180
an API view in practice

00:00:15.180 --> 00:00:18.960
Start by loading
up the atom editor and opening the views

00:00:18.960 --> 00:00:25.910
dot py file that was created
in our profiles API app in our project

00:00:25.910 --> 00:00:31.320
remove all the contents of this file and
we're going to start by importing some

00:00:31.320 --> 00:00:39.270
things at the top of the file so let's
type from rest framework dot views import

00:00:39.270 --> 00:00:42.400
API view

00:00:42.400 --> 00:00:51.719
and from rest framework dot
response import response with a capital

00:00:51.719 --> 00:00:53.800
R

00:00:53.800 --> 00:01:00.059
what this does is it imports the API
view class from the rest framework dot

00:01:00.059 --> 00:01:06.750
views module the rest framework is the
Django rest framework that we installed

00:01:06.750 --> 00:01:11.260
previously in our requirements dot txt file

00:01:11.260 --> 00:01:13.740
this line imports the response object

00:01:13.740 --> 00:01:19.500
which is used to return responses from
the API view so it's a standard response

00:01:19.500 --> 00:01:24.930
object that when you call the API view
or when Django rest framework calls our

00:01:24.930 --> 00:01:29.579
API view it's expecting it to return
this standard response object I'll show

00:01:29.579 --> 00:01:32.680
you how this works in a moment

00:01:32.680 --> 00:01:34.740
Now that
we've added these two imports let's

00:01:34.740 --> 00:01:43.890
create our API view class so type class
hello API view and then in the base

00:01:43.890 --> 00:01:54.299
class let's type API view and let's add
the doc string test API view so this

00:01:54.299 --> 00:01:58.590
creates a new class based on the API
view class that Django rest framework

00:01:58.590 --> 00:02:04.710
provides and it allows us to define the
application logic for our endpoint that

00:02:04.710 --> 00:02:10.259
we're going to assign to this view so
the way it works is you define a URL

00:02:10.259 --> 00:02:13.680
which is our endpoint and then you
assign it to this

00:02:13.680 --> 00:02:18.150
view and the Django rest framework
handles it by calling the appropriate

00:02:18.150 --> 00:02:23.969
function in the view for the HTTP
request that you make so we're going to

00:02:23.969 --> 00:02:36.629
be accepting HTTP GET request to our API
so let's type def get self request format

00:02:36.629 --> 00:02:44.760
equals none and then in the doc string
we'll type returns a list of API view

00:02:44.760 --> 00:02:49.460
features which we're going to be doing
in a moment

00:02:49.460 --> 00:02:51.180
I'll just explain what this

00:02:51.180 --> 00:02:56.970
get function is so the way API views are
broken up is it expects a function for

00:02:56.970 --> 00:03:04.230
the different HTTP requests that can be
made to the view so the HTTP GET request

00:03:04.230 --> 00:03:10.620
is typically used to retrieve a list of
objects or a specific object so whenever

00:03:10.620 --> 00:03:16.829
you make a HTTP GET request to the URL
that it will be assigned to this API

00:03:16.829 --> 00:03:22.829
view it will call the get function
and it will execute the logic that we

00:03:22.829 --> 00:03:25.940
write in the get function

00:03:25.940 --> 00:03:28.889
the parameters
here are self which is required for all

00:03:28.889 --> 00:03:34.349
class functions a request object this is
passed in by the Django rest framework

00:03:34.349 --> 00:03:40.500
and contains details of the request
being made to the API and the format

00:03:40.500 --> 00:03:46.590
which is used to add a format suffix to
the end of the endpoint URL we're not

00:03:46.590 --> 00:03:50.519
going to be using the format but it's
best practice to keep it in here so if

00:03:50.519 --> 00:03:55.379
you do ever enable formats on your API
it will be supported by the functions

00:03:55.379 --> 00:03:57.760
that we create

00:03:57.760 --> 00:04:01.590
Okay so what we're going to
do in our get function here is we're

00:04:01.590 --> 00:04:08.669
going to define a list which describes all
of the features of an API view so let's

00:04:08.669 --> 00:04:16.169
type an underscore API view equals and
create new list here and the first item

00:04:16.169 --> 00:04:22.889
in the list we're going to type a string
and write uses HTTP methods as functions

00:04:22.889 --> 00:04:31.320
and in brackets we'll put get post
patch put delete so these are all

00:04:31.320 --> 00:04:36.230
the functions that you can add to your
API view to support the different HTTP

00:04:36.230 --> 00:04:41.480
requests and we're adding the get
function here

00:04:41.480 --> 00:04:43.770
okay so the next item that

00:04:43.770 --> 00:04:56.220
we're going to add  is is similar to a
traditional django view so the API view

00:04:56.220 --> 00:05:01.470
is very similar to a traditional django
view but specifically intended to be

00:05:01.470 --> 00:05:05.820
used for apis

00:05:05.820 --> 00:05:11.580
Okay next we're going to
type gives you the most control over

00:05:11.580 --> 00:05:15.480
your application logic

00:05:15.480 --> 00:05:21.920
and then we'll
write is mapped manually to URLs and

00:05:21.920 --> 00:05:27.450
that is the list that we're going to
create for our API view so this is just

00:05:27.450 --> 00:05:33.420
a simple list to demonstrate how an API
view works in practice feel free to

00:05:33.420 --> 00:05:36.600
change this list and enter whatever you
want in it it doesn't matter what the

00:05:36.600 --> 00:05:41.580
contents is we just basically want to
demonstrate returning an object in our

00:05:41.580 --> 00:05:44.440
API view

00:05:44.440 --> 00:05:49.440
Okay every function that you
add to an API view that is a

00:05:49.440 --> 00:05:55.440
HTTP function so one of these get post
patch put delete must return a response

00:05:55.440 --> 00:06:00.510
object so Django rest framework is
expecting it to return a response which

00:06:00.510 --> 00:06:07.500
it will then output as part of when the API
is called so let's type return response

00:06:07.500 --> 00:06:13.980
and the response needs to contain a
dictionary or a list which it will then

00:06:13.980 --> 00:06:21.420
output when the API is called so
it converts the response object to json

00:06:21.420 --> 00:06:26.960
and in order to convert it to json it needs
to be either a list or a dictionary

00:06:26.960 --> 00:06:32.130
we're going to return a dictionary so open
these squiggly bracket lines here and

00:06:32.130 --> 00:06:39.510
we're going to add a key called message and
we'll assign a value of just a string

00:06:39.510 --> 00:06:42.100
that says hello

00:06:42.100 --> 00:06:44.220
and then we're going to add a second key

00:06:44.220 --> 00:06:52.229
called an API view and we're going to pass
in our an API view list that we created

00:06:52.229 --> 00:06:55.580
here so an API view

00:06:55.580 --> 00:07:00.600
and that's how you
define an API view with a get function

00:07:00.600 --> 00:07:07.910
so make sure you save the file and then
we're ready to move on to the next video

