Hello everyone.
And welcome back.
It's finally time to write our first web API using django.
We're going to see how to set up two endpoints for our API.
The first one will allow users to retrieve information about this specific product instance and the
second one will allow users to retrieve at least we have all the available products in our database.
As we've already mentioned in the first part of this lesson we're going to build our EPA views only
using their functions and classes provided by pure Django and our API will make use of the Chaser file
format.
Let's get started.
We are invisible to your code and as you can see the view is that by five of our products application
is yet open in these words to your codes workbench.
As we've said in this second part of the lesson we want to develop to API and points for our online
store project both and points are when to use the Jace on file format and the first one is going to
allow our users to retrieve information about a specific product instance while the second one is going
to provide at least with all the products that are available in our database.
So the first thing that we need to do is to define two views which are basically going to use the Jace
on file format to provide the same kind of information that their product detail view and product list
view are now providing using django templates so I can just comment on this code because we are not
going to need it anymore but you might just want to check it later on and we need to import a very important
class instead which is the g sound response class from Django not HDTV.
So import response we can ever look at its code by right clicking and then selecting peak definition.
And of course as the name suggests Jason response is in HD too deep in response class that consumes
data to be serialized.
To Jason we're going to talk in great detail about the sterilization and the sterilization process later
on during the course.
For now let's just focus on how to make all of this work okay.
We can use a function based views to make the code as explicit as possible and we can start by defining
that product at least function view so death defying product list which is going to accept a request
parameter.
As for all the function based views and because we want to provide at least to with all the products
in our database we need to get the product the query set so products equals product not objects at all.
If you want to you can also just get a slice of all the products available like so and with now facing
our first challenge how can we set our products squarely set in order to use it.
With Jason response in a meaningful way.
Well Python gives us the dictionary data type and so we can use that data to define a dictionary which
is going to add products key into the product key.
I'm going to assign a list containing the values of our products so products that values and we can
also select some specific attributes for our instances such as for example primary key and name of each
product.
We can now define a response object using Jason response and data dictionary and we are now ready to
return our response object.
Now that product the least function is ready we can finally set up our first and bind and we do it the
same way we would normally map a function to you around.
It's the same thing.
So just go going to import the product list function so I can just comment this out.
And so from viewers I'm going to import the product least or rather I can just delete one even two because
we're not going to need that one anymore.
So this is going to be products and I'm going to assign it to the product least view.
Typically when defining an API endpoint we wouldn't normally use the API prefix let's call it in front
of the rest of the end point it's best practice and it allows us to distinguish all the different parts
in our project.
So I can just go and modify the world and start by file loved it online store project and I'm just going
to add API so that starting from API forward slash isn't going to include all the other endpoints that
we're going to define.
So let's try this out.
We're going to execute that code out of development server is running.
We have no issues.
So let's move to Chrome.
And of course at the moment it gives us page not found because we are in the home page.
Let's call it we need to go to slash API Slash.
And of course you see we now get at least we've already available and points we can't therefore go to
slash products and tease outages so response.
It's a Jason object which as you can see contains an array with some details of our products.
Of course we've decided to only show the primary key in the name for each product and therefore that's
everything we getting.
Let's go ahead and change this.
We've been out of use that by five.
You can just delete or just maybe comment out this code.
Let's rerun our development server no issues perfect let's make another get request in fact and we get
all the details of all the products in our database.
In this case two products you see.
We get to first of all of course the AIDS epidemic of the product that we get manufactured at I.D. meaning
we get to the primary key of the manufacturer of this product.
We get Name Description price shipping cost.
And we even get the name of images that are assigned to our products.
But it's easy to understand that this is not the best way to actually deliver information to let's say
you a client.
First of all talking about images we say we only get the name of each photo.
We don't get its actual URL while for example using django as template we can just use dot UML to get
the proper URL for each image and say feed.
We could argue for the manufacturer that we only get its I.D. no further information we might want for
example to show the name of the manufacturer which is way more valuable to a customer and then it's
a deal.
So as we can see that Jesus response class is really useful but it's not enough for real world scenarios.
We would need to heavily tweak our code in order to actually show the information that we need.
And that's why we normally use something like django rest framework.
However let's get the best out of it anyway.
So let's go back to these ones to your code and let's define the second view that you actually need
in order to build our second end point.
So let's make this one to function based.
So product detail which is going to accept the request the parameter of course but also the primary
key parameter which we need to actually determine which product we need to show the details for.
So considering that we need the details of a specific product based on their primary keyless best we
also need a way to find out if that product actually exists.
We could use that get object or photo for shortcut which is really useful.
However it is better if we define the try except this by yourself because this way we can also specify
adjacent response to send back.
If the product doesn't exist.
So right product equals product dot objects not get.
We became equals P.K. we can now define the dictionary we have all the details of these specific product
that the user is looking for.
So data equals a dictionary product and here let's define another dictionary.
So name it's going to be product don't name.
We're going to have manufacture manufactured product dot manufacture dot name and we can do the same
for all the other attributes.
So description is going to be product dot description then we've got photo and this time we can get
you read of a photo so we can do product.
Dot dot dot dot you are real which is provided by the field itself by the image field itself.
Now we got price it's going to be product price and then we got I can just maybe to let these so we
got shipping costs into the product not shipping cost and quantity product not quantity is now dead.
We've got data.
We can also define our response so as Jason response of data so this is all good of course in case this
specific object exists.
So we need to tell the client if the product doesn't exist.
So accept the product with capital p dot does not exist.
In this case response is going to be different.
So going to be response based on Jason's response.
But we have a different message of course.
So we can define error like so code so the error code for 0 4 not found in the message product not found.
And if we want to we can also pass a status so status for a fourth.
We got no universal guidelines telling us whenever our response should look like talking with developers
from different companies maybe different industries.
And of course looking on Google you'll find that there are different schools of thought and the way
we've defined our error message here is actually one of the most common ones.
Our response is basically divided in two parts.
We've got status four or four which is important to tell the client.
Meaning the application that the resource we're looking for doesn't exist.
And then we got this Jason which provides further information to us humans.
We go to verbose message product not found and then decode 44.
So anyway whatever happens we need to actually return the response right.
So return response so let's save and let's define our second endpoint.
So I'm going to import product detail.
And so here we can define our second end point it's going to be products then we're going to need a
primary key.
So of course we want to actually call the product detail view and the name is going to be product details.
So let's restart our development server and let's give our new and to point a try.
So I can just minimize the term you know let's go back to Chrome instead and let's say we want the details
of our city by.
Okay.
So I'm going to go to slash API slash products slash 1 and then put order slash and you see how we get
all the details of our product very well.
We see that we get the name of the manufacturer First the manufacturer and we also get the other end
of the image that is associated with these bike.
Are you a client.
Good.
Now for example go ahead and grab this URL using it to show the photo of these specific bike to our
users and speaking about images or media files in general in the context of a rest api.
I want to point out that nowadays is pretty uncommon for a company let's say to develop an API for its
services managing images on its own.
It's much more common to actually use a third party service like fighter for example or a W S which
would provide all day needed content to your customers based on date location.
For example we have a CDMA content delivery network and these services are also getting really cheap
nowadays so if you're serious we have a project that we intend to build.
I definitely suggest you take a look at those services to provide media files to your users.
So before finishing the lesson let us go ahead and let's try to get the details of a product that we
know doesn't exist like for example if product with primary key three which we haven't defined which
we haven't created yet.
So it is we get our error code for a fourth message product not found very well.
So that was it for this video.
We've seen that it's actually pretty easy to use the Jason response class to create simple API is using
django.
However we also see oh this is not the most flexible solution one can find and it's also important to
point out that we've written only two views.
We've created to and points to get information from our database.
Just imagine how messy fees could get if we for example would have to provide our users the ability
to maybe create new instances and in general perform different kind of actions on our platform.
Luckily for us the Jenkins framework more package makes all of these really easy and we'll get to the
details of how to use it in the next session but before that we've got our competency assessment.
So see you in the next lesson.