WEBVTT 00:00.090 --> 00:00.750 Hello everyone. 00:00.840 --> 00:02.130 And welcome back. 00:02.130 --> 00:06.120 It's finally time to write our first web API using django. 00:06.120 --> 00:10.200 We're going to see how to set up two endpoints for our API. 00:10.200 --> 00:16.650 The first one will allow users to retrieve information about this specific product instance and the 00:16.650 --> 00:22.260 second one will allow users to retrieve at least we have all the available products in our database. 00:22.290 --> 00:27.570 As we've already mentioned in the first part of this lesson we're going to build our EPA views only 00:27.570 --> 00:35.280 using their functions and classes provided by pure Django and our API will make use of the Chaser file 00:35.370 --> 00:35.970 format. 00:36.210 --> 00:37.510 Let's get started. 00:37.560 --> 00:43.170 We are invisible to your code and as you can see the view is that by five of our products application 00:43.260 --> 00:46.110 is yet open in these words to your codes workbench. 00:46.110 --> 00:52.650 As we've said in this second part of the lesson we want to develop to API and points for our online 00:52.650 --> 00:59.040 store project both and points are when to use the Jace on file format and the first one is going to 00:59.070 --> 01:05.970 allow our users to retrieve information about a specific product instance while the second one is going 01:05.970 --> 01:10.380 to provide at least with all the products that are available in our database. 01:10.380 --> 01:16.280 So the first thing that we need to do is to define two views which are basically going to use the Jace 01:16.280 --> 01:23.070 on file format to provide the same kind of information that their product detail view and product list 01:23.070 --> 01:32.040 view are now providing using django templates so I can just comment on this code because we are not 01:32.040 --> 01:38.400 going to need it anymore but you might just want to check it later on and we need to import a very important 01:38.400 --> 01:44.730 class instead which is the g sound response class from Django not HDTV. 01:44.850 --> 01:53.520 So import response we can ever look at its code by right clicking and then selecting peak definition. 01:53.670 --> 01:59.520 And of course as the name suggests Jason response is in HD too deep in response class that consumes 01:59.520 --> 02:00.980 data to be serialized. 02:00.990 --> 02:07.230 To Jason we're going to talk in great detail about the sterilization and the sterilization process later 02:07.230 --> 02:08.700 on during the course. 02:08.700 --> 02:12.540 For now let's just focus on how to make all of this work okay. 02:12.870 --> 02:19.050 We can use a function based views to make the code as explicit as possible and we can start by defining 02:19.050 --> 02:27.690 that product at least function view so death defying product list which is going to accept a request 02:27.690 --> 02:28.420 parameter. 02:28.470 --> 02:34.770 As for all the function based views and because we want to provide at least to with all the products 02:34.860 --> 02:46.540 in our database we need to get the product the query set so products equals product not objects at all. 02:46.740 --> 02:54.090 If you want to you can also just get a slice of all the products available like so and with now facing 02:54.090 --> 02:59.430 our first challenge how can we set our products squarely set in order to use it. 02:59.430 --> 03:01.950 With Jason response in a meaningful way. 03:02.490 --> 03:10.500 Well Python gives us the dictionary data type and so we can use that data to define a dictionary which 03:10.500 --> 03:16.340 is going to add products key into the product key. 03:16.340 --> 03:26.060 I'm going to assign a list containing the values of our products so products that values and we can 03:26.060 --> 03:34.730 also select some specific attributes for our instances such as for example primary key and name of each 03:34.730 --> 03:35.570 product. 03:35.630 --> 03:46.090 We can now define a response object using Jason response and data dictionary and we are now ready to 03:46.180 --> 03:49.690 return our response object. 03:49.840 --> 03:56.170 Now that product the least function is ready we can finally set up our first and bind and we do it the 03:56.170 --> 04:00.580 same way we would normally map a function to you around. 04:00.580 --> 04:01.650 It's the same thing. 04:01.660 --> 04:08.770 So just go going to import the product list function so I can just comment this out. 04:08.770 --> 04:16.340 And so from viewers I'm going to import the product least or rather I can just delete one even two because 04:16.490 --> 04:18.450 we're not going to need that one anymore. 04:18.500 --> 04:26.960 So this is going to be products and I'm going to assign it to the product least view. 04:27.140 --> 04:34.970 Typically when defining an API endpoint we wouldn't normally use the API prefix let's call it in front 04:34.970 --> 04:41.060 of the rest of the end point it's best practice and it allows us to distinguish all the different parts 04:41.150 --> 04:42.140 in our project. 04:42.230 --> 04:50.050 So I can just go and modify the world and start by file loved it online store project and I'm just going 04:50.050 --> 04:57.940 to add API so that starting from API forward slash isn't going to include all the other endpoints that 04:57.940 --> 04:59.310 we're going to define. 04:59.320 --> 05:00.740 So let's try this out. 05:00.850 --> 05:06.820 We're going to execute that code out of development server is running. 05:06.820 --> 05:08.040 We have no issues. 05:08.320 --> 05:09.770 So let's move to Chrome. 05:10.750 --> 05:15.530 And of course at the moment it gives us page not found because we are in the home page. 05:15.550 --> 05:20.000 Let's call it we need to go to slash API Slash. 05:20.710 --> 05:25.810 And of course you see we now get at least we've already available and points we can't therefore go to 05:25.820 --> 05:30.190 slash products and tease outages so response. 05:30.290 --> 05:37.130 It's a Jason object which as you can see contains an array with some details of our products. 05:37.180 --> 05:42.160 Of course we've decided to only show the primary key in the name for each product and therefore that's 05:42.310 --> 05:43.690 everything we getting. 05:43.820 --> 05:47.030 Let's go ahead and change this. 05:47.070 --> 05:48.570 We've been out of use that by five. 05:49.010 --> 05:54.680 You can just delete or just maybe comment out this code. 05:54.690 --> 06:05.490 Let's rerun our development server no issues perfect let's make another get request in fact and we get 06:05.610 --> 06:08.520 all the details of all the products in our database. 06:08.520 --> 06:10.710 In this case two products you see. 06:11.010 --> 06:16.890 We get to first of all of course the AIDS epidemic of the product that we get manufactured at I.D. meaning 06:16.950 --> 06:20.210 we get to the primary key of the manufacturer of this product. 06:20.250 --> 06:24.370 We get Name Description price shipping cost. 06:24.630 --> 06:29.540 And we even get the name of images that are assigned to our products. 06:29.580 --> 06:35.550 But it's easy to understand that this is not the best way to actually deliver information to let's say 06:35.550 --> 06:37.020 you a client. 06:37.020 --> 06:42.650 First of all talking about images we say we only get the name of each photo. 06:42.780 --> 06:50.580 We don't get its actual URL while for example using django as template we can just use dot UML to get 06:50.580 --> 06:54.160 the proper URL for each image and say feed. 06:54.180 --> 07:00.360 We could argue for the manufacturer that we only get its I.D. no further information we might want for 07:00.360 --> 07:05.850 example to show the name of the manufacturer which is way more valuable to a customer and then it's 07:05.850 --> 07:06.250 a deal. 07:06.870 --> 07:13.910 So as we can see that Jesus response class is really useful but it's not enough for real world scenarios. 07:14.040 --> 07:18.670 We would need to heavily tweak our code in order to actually show the information that we need. 07:18.810 --> 07:22.940 And that's why we normally use something like django rest framework. 07:22.960 --> 07:25.790 However let's get the best out of it anyway. 07:25.830 --> 07:31.850 So let's go back to these ones to your code and let's define the second view that you actually need 07:31.860 --> 07:34.310 in order to build our second end point. 07:34.350 --> 07:36.800 So let's make this one to function based. 07:36.820 --> 07:43.260 So product detail which is going to accept the request the parameter of course but also the primary 07:43.260 --> 07:49.710 key parameter which we need to actually determine which product we need to show the details for. 07:49.710 --> 07:55.380 So considering that we need the details of a specific product based on their primary keyless best we 07:55.380 --> 07:59.340 also need a way to find out if that product actually exists. 07:59.370 --> 08:04.600 We could use that get object or photo for shortcut which is really useful. 08:04.620 --> 08:11.520 However it is better if we define the try except this by yourself because this way we can also specify 08:11.520 --> 08:14.270 adjacent response to send back. 08:14.400 --> 08:16.440 If the product doesn't exist. 08:16.440 --> 08:27.070 So right product equals product dot objects not get. 08:27.370 --> 08:35.470 We became equals P.K. we can now define the dictionary we have all the details of these specific product 08:35.470 --> 08:37.210 that the user is looking for. 08:37.240 --> 08:45.760 So data equals a dictionary product and here let's define another dictionary. 08:45.850 --> 08:52.790 So name it's going to be product don't name. 08:52.860 --> 09:00.370 We're going to have manufacture manufactured product dot manufacture dot name and we can do the same 09:00.370 --> 09:01.690 for all the other attributes. 09:01.690 --> 09:13.060 So description is going to be product dot description then we've got photo and this time we can get 09:13.180 --> 09:16.500 you read of a photo so we can do product. 09:16.570 --> 09:23.450 Dot dot dot dot you are real which is provided by the field itself by the image field itself. 09:23.470 --> 09:35.890 Now we got price it's going to be product price and then we got I can just maybe to let these so we 09:35.890 --> 09:49.030 got shipping costs into the product not shipping cost and quantity product not quantity is now dead. 09:49.030 --> 09:50.530 We've got data. 09:50.800 --> 10:03.500 We can also define our response so as Jason response of data so this is all good of course in case this 10:03.500 --> 10:06.350 specific object exists. 10:06.380 --> 10:09.310 So we need to tell the client if the product doesn't exist. 10:09.350 --> 10:17.720 So accept the product with capital p dot does not exist. 10:17.720 --> 10:20.480 In this case response is going to be different. 10:20.480 --> 10:24.580 So going to be response based on Jason's response. 10:24.860 --> 10:28.610 But we have a different message of course. 10:28.610 --> 10:44.140 So we can define error like so code so the error code for 0 4 not found in the message product not found. 10:44.560 --> 10:51.490 And if we want to we can also pass a status so status for a fourth. 10:52.100 --> 10:59.180 We got no universal guidelines telling us whenever our response should look like talking with developers 10:59.180 --> 11:02.560 from different companies maybe different industries. 11:02.690 --> 11:08.960 And of course looking on Google you'll find that there are different schools of thought and the way 11:08.960 --> 11:13.550 we've defined our error message here is actually one of the most common ones. 11:13.550 --> 11:15.770 Our response is basically divided in two parts. 11:15.770 --> 11:20.090 We've got status four or four which is important to tell the client. 11:20.090 --> 11:24.800 Meaning the application that the resource we're looking for doesn't exist. 11:24.830 --> 11:30.910 And then we got this Jason which provides further information to us humans. 11:31.040 --> 11:35.950 We go to verbose message product not found and then decode 44. 11:35.990 --> 11:41.130 So anyway whatever happens we need to actually return the response right. 11:41.150 --> 11:49.660 So return response so let's save and let's define our second endpoint. 11:49.730 --> 11:54.690 So I'm going to import product detail. 11:54.720 --> 12:00.860 And so here we can define our second end point it's going to be products then we're going to need a 12:00.860 --> 12:02.110 primary key. 12:02.340 --> 12:12.040 So of course we want to actually call the product detail view and the name is going to be product details. 12:12.290 --> 12:17.540 So let's restart our development server and let's give our new and to point a try. 12:17.810 --> 12:25.460 So I can just minimize the term you know let's go back to Chrome instead and let's say we want the details 12:25.520 --> 12:26.810 of our city by. 12:26.910 --> 12:27.260 Okay. 12:27.300 --> 12:35.960 So I'm going to go to slash API slash products slash 1 and then put order slash and you see how we get 12:36.080 --> 12:39.370 all the details of our product very well. 12:39.530 --> 12:45.410 We see that we get the name of the manufacturer First the manufacturer and we also get the other end 12:45.920 --> 12:49.280 of the image that is associated with these bike. 12:49.290 --> 12:50.170 Are you a client. 12:50.180 --> 12:50.600 Good. 12:50.600 --> 12:58.110 Now for example go ahead and grab this URL using it to show the photo of these specific bike to our 12:58.110 --> 13:03.990 users and speaking about images or media files in general in the context of a rest api. 13:04.020 --> 13:09.630 I want to point out that nowadays is pretty uncommon for a company let's say to develop an API for its 13:09.640 --> 13:12.570 services managing images on its own. 13:12.600 --> 13:18.810 It's much more common to actually use a third party service like fighter for example or a W S which 13:18.810 --> 13:24.060 would provide all day needed content to your customers based on date location. 13:24.060 --> 13:30.120 For example we have a CDMA content delivery network and these services are also getting really cheap 13:30.150 --> 13:34.230 nowadays so if you're serious we have a project that we intend to build. 13:34.230 --> 13:39.910 I definitely suggest you take a look at those services to provide media files to your users. 13:39.990 --> 13:45.240 So before finishing the lesson let us go ahead and let's try to get the details of a product that we 13:45.240 --> 13:51.840 know doesn't exist like for example if product with primary key three which we haven't defined which 13:51.840 --> 13:52.800 we haven't created yet. 13:52.830 --> 13:59.430 So it is we get our error code for a fourth message product not found very well. 13:59.670 --> 14:01.270 So that was it for this video. 14:01.290 --> 14:07.770 We've seen that it's actually pretty easy to use the Jason response class to create simple API is using 14:07.770 --> 14:08.220 django. 14:08.400 --> 14:15.870 However we also see oh this is not the most flexible solution one can find and it's also important to 14:15.870 --> 14:18.860 point out that we've written only two views. 14:18.870 --> 14:23.160 We've created to and points to get information from our database. 14:23.190 --> 14:29.370 Just imagine how messy fees could get if we for example would have to provide our users the ability 14:29.370 --> 14:35.860 to maybe create new instances and in general perform different kind of actions on our platform. 14:36.120 --> 14:41.490 Luckily for us the Jenkins framework more package makes all of these really easy and we'll get to the 14:41.490 --> 14:47.700 details of how to use it in the next session but before that we've got our competency assessment. 14:47.730 --> 14:49.530 So see you in the next lesson.