1 00:00:00,640 --> 00:00:04,400 All right, so now it's time to actually make the resources. 2 00:00:04,400 --> 00:00:08,060 And instead of creating them as a single file or 3 00:00:08,060 --> 00:00:10,210 whatever, I'm gonna create them as a directory. 4 00:00:11,670 --> 00:00:16,590 And gotta click on that and I'm going to call those resources 5 00:00:17,790 --> 00:00:22,770 and then inside of here, I need to make dunder and init.py. 6 00:00:23,920 --> 00:00:26,470 And we don't need edit that file, all right and 7 00:00:26,470 --> 00:00:29,470 so now instead of here I'm going to make a file for each of the resources. 8 00:00:29,470 --> 00:00:33,140 It's a little easier to just break these things up in multiple files because 9 00:00:33,140 --> 00:00:37,920 they're a little bit bulky and you don't have to do this but I highly suggest 10 00:00:37,920 --> 00:00:42,090 doing this just because it makes things a little easier to deal with. 11 00:00:43,210 --> 00:00:44,960 So okay, we're going to call this courses.py, 12 00:00:44,960 --> 00:00:48,590 this is gonna be for our courses resource. 13 00:00:49,820 --> 00:00:53,280 All right, so, from flask import jsonify, 14 00:00:54,450 --> 00:00:57,075 which we'll use to turn these into json responses. 15 00:00:57,075 --> 00:01:03,560 And from flask.ext.restful import Resource. 16 00:01:03,560 --> 00:01:06,250 I'm actually going to break those up just a little bit. 17 00:01:06,250 --> 00:01:07,370 And then import models. 18 00:01:07,370 --> 00:01:09,810 All right, cool. 19 00:01:09,810 --> 00:01:12,710 So those are all of our basic imports out of the way. 20 00:01:12,710 --> 00:01:16,090 Class CourseList is a resource. 21 00:01:16,090 --> 00:01:18,360 And this is the resource for a list of courses. 22 00:01:19,630 --> 00:01:22,800 And we're just going to define the get method on this. 23 00:01:22,800 --> 00:01:27,090 And so get handles whenever somebody sends in a get request to the resource. 24 00:01:28,470 --> 00:01:34,430 And we're going to return just jsonify, courses and 25 00:01:34,430 --> 00:01:39,200 then make up a fake course here, we'll say the title is Python Basics. 26 00:01:41,140 --> 00:01:43,980 And I know that we have URL in our model. 27 00:01:43,980 --> 00:01:46,742 We're not actually dealing with the model right now, so we can just leave that off. 28 00:01:46,742 --> 00:01:50,340 We're just doing this to make sure that we get a response that we like and expect. 29 00:01:52,110 --> 00:01:54,230 All right, so now we'll make another one here for 30 00:01:54,230 --> 00:01:59,780 the course resource, and this will be very similar. 31 00:01:59,780 --> 00:02:03,710 But we're gonna create three methods on here instead of just one. 32 00:02:03,710 --> 00:02:08,290 We'll just do self and ID because individual courses will always get an ID 33 00:02:08,290 --> 00:02:17,000 or should, and we'll return jsonify, title is Python Basics. 34 00:02:17,000 --> 00:02:18,760 So basically, I'm just making this stuff match. 35 00:02:19,810 --> 00:02:25,540 And since, this is what I'm gonna to use for the other two methods. 36 00:02:25,540 --> 00:02:30,590 I'm just gonna copy them, and this will be put and this will be delete. 37 00:02:32,290 --> 00:02:33,508 All right. 38 00:02:33,508 --> 00:02:37,036 Really I should have a post up here for course list as well but 39 00:02:37,036 --> 00:02:41,715 we'll build that one later whenever we get to working with actual resources. 40 00:02:41,715 --> 00:02:44,885 So jsonify, if you haven't seen it before, 41 00:02:44,885 --> 00:02:50,540 turns whatever this thing is whatever's in here into a json response. 42 00:02:50,540 --> 00:02:52,540 So it turns it into a json string. 43 00:02:52,540 --> 00:02:57,000 And it sets the content type to applications /json. 44 00:02:57,000 --> 00:03:01,250 It does all the things that you need to do to make a json response. 45 00:03:02,250 --> 00:03:06,060 And I know that I imported models here and I'm not using it. 46 00:03:06,060 --> 00:03:08,100 I will get to that, I promise. 47 00:03:08,100 --> 00:03:10,070 We'll get to the database lookups a little bit later. 48 00:03:10,070 --> 00:03:15,730 All right, so now, let's actually, let's actually take all of this, 49 00:03:15,730 --> 00:03:21,090 copy it and make a new file in that folder and we're gonna call this reviews.py. 50 00:03:21,090 --> 00:03:25,110 So this is going to be our reviews resource 51 00:03:26,810 --> 00:03:28,990 and so we got to change a few things here, 52 00:03:28,990 --> 00:03:34,020 we want to change this to ReviewList and I wanna change this to Review. 53 00:03:35,570 --> 00:03:42,640 And then here, I wanna send back reviews instead of courses, I want to send back course. 54 00:03:44,140 --> 00:03:50,030 Which is gonna be a 1, and I wanna send back rating which is gonna be a 5. 55 00:03:50,030 --> 00:03:53,490 All right, so copy that. 56 00:03:55,670 --> 00:03:59,090 And we're gonna paste and we don't need to copy that actually. 57 00:03:59,090 --> 00:04:00,930 We're just gonna change this. 58 00:04:00,930 --> 00:04:05,946 So this will just be course, 59 00:04:05,946 --> 00:04:10,480 is 1 and rating is 5. 60 00:04:10,480 --> 00:04:12,279 All right, and this will copy. 61 00:04:15,490 --> 00:04:18,350 And paste. 62 00:04:18,350 --> 00:04:19,360 All right, cool. 63 00:04:19,360 --> 00:04:24,610 So, a lot of copying and pasting just to get things up and running. 64 00:04:24,610 --> 00:04:28,340 But there we go, so we have everything up and working. 65 00:04:28,340 --> 00:04:32,240 Now, there's not really anything different about the reviews thing 66 00:04:32,240 --> 00:04:37,230 other than the courses thing except for names, courses list, reviews list, 67 00:04:37,230 --> 00:04:38,760 things like that. 68 00:04:38,760 --> 00:04:43,910 And these resources aren't really useful, yet, because they don't do anything yet. 69 00:04:43,910 --> 00:04:48,190 But this gives me a really solid base to build my API. 70 00:04:48,190 --> 00:04:50,880 And some more complete resources a little bit later on. 71 00:04:52,730 --> 00:04:55,870 We have resources but now we need to tie them into our flask app. 72 00:04:55,870 --> 00:04:58,460 So it'll know all about the routes that we defined. 73 00:04:58,460 --> 00:05:00,580 This can be a little tricky to do cleanly. 74 00:05:00,580 --> 00:05:04,010 They gave me a lot of trouble actually trying to get ready for this, but 75 00:05:04,010 --> 00:05:07,920 trouble just means that you have something else to learn, so let me show you a great 76 00:05:07,920 --> 00:05:11,300 flask feature known as Blueprints that solves exactly this problem.