1 00:00:00,300 --> 00:00:02,690 So this video has a new workspace. 2 00:00:02,690 --> 00:00:05,660 So again, you should close yours and open the new one. 3 00:00:05,660 --> 00:00:09,870 And then you'll be able to see the get and post methods. 4 00:00:09,870 --> 00:00:15,820 So like here's the post for courses and the stuff for views. 5 00:00:15,820 --> 00:00:17,666 I want to talk about what I did in reviews real quick. 6 00:00:20,386 --> 00:00:21,865 Some of you may have noticed this, 7 00:00:21,865 --> 00:00:24,740 I kinda left it in to see how your debugging skills were. 8 00:00:24,740 --> 00:00:28,030 But if you leave your review fields as just course, 9 00:00:28,030 --> 00:00:33,330 which is fine, then it becomes really hard to turn it into the URL for the course. 10 00:00:33,330 --> 00:00:36,860 So I actually added a new one called for_course, 11 00:00:36,860 --> 00:00:39,520 and we just set that to be where the course goes. 12 00:00:39,520 --> 00:00:43,880 So we never actually reveal like the course's ID which doesn't really matter 13 00:00:43,880 --> 00:00:45,870 one way or the other, but that's the way that I've been doing it. 14 00:00:47,010 --> 00:00:53,030 And then inside post, create sends back the record that was created so 15 00:00:53,030 --> 00:00:57,140 you can just grab the review and add the course to it, send it right through. 16 00:00:58,430 --> 00:01:01,796 So again, this is in the workspace so you can look through all of it. 17 00:01:01,796 --> 00:01:05,477 So we've talked get, we've talked about post, and so for 18 00:01:05,477 --> 00:01:08,430 this one I want to focus on put and delete. 19 00:01:08,430 --> 00:01:10,840 And again I'm going to do them on courses and 20 00:01:10,840 --> 00:01:13,590 you can kind of replicate them onto reviews. 21 00:01:13,590 --> 00:01:16,070 Put requests are for updating records. 22 00:01:16,070 --> 00:01:19,070 So we're going to be working on here in the course resource on the courses. 23 00:01:20,280 --> 00:01:24,700 But unlike patch requests, they have to contain all of the data for the record, 24 00:01:24,700 --> 00:01:26,490 not just the updated pieces. 25 00:01:26,490 --> 00:01:29,790 So with a patch, you can send in, I need to change the title, and 26 00:01:29,790 --> 00:01:31,070 send just the title. 27 00:01:31,070 --> 00:01:33,080 With put you have to send the entire record. 28 00:01:34,250 --> 00:01:37,140 Now I'm not going to implement patch in this course, but 29 00:01:37,140 --> 00:01:39,790 feel free to experiment with it if you want to. 30 00:01:39,790 --> 00:01:41,650 So let's talk about put. 31 00:01:41,650 --> 00:01:45,572 So first of all i'm gonna marshal this with the course fields. 32 00:01:45,572 --> 00:01:47,290 Cuz put's always going to be a single record. 33 00:01:48,620 --> 00:01:53,309 And so then we need to parse the args like usual, so 34 00:01:53,309 --> 00:01:56,556 self.reqparse.parse_args. 35 00:01:56,556 --> 00:01:58,510 Make sure the arguments are good. 36 00:01:58,510 --> 00:02:00,940 And then because of how peewee works, 37 00:02:00,940 --> 00:02:04,640 we have to kind of write our query a little funny. 38 00:02:04,640 --> 00:02:09,743 So we'll do query = models.Course.update and we're going to update it 39 00:02:09,743 --> 00:02:14,850 with the args .where(models.Course.id==id) that was passed in. 40 00:02:18,832 --> 00:02:22,400 That line's a little long, but whatever I'm just gonna leave it. 41 00:02:22,400 --> 00:02:30,740 And query.execute to actually run execute to actually run the query. 42 00:02:30,740 --> 00:02:32,712 And then what do we want to return? 43 00:02:32,712 --> 00:02:36,830 Well what we wanna do is we want to return, oops no need to type return twice. 44 00:02:37,840 --> 00:02:43,090 Add reviews and then I wanna go fetch the new model again. 45 00:02:43,090 --> 00:02:49,570 So models.course.get(models.course.id==id)). 46 00:02:49,570 --> 00:02:52,090 So add reviews to that. 47 00:02:52,090 --> 00:02:52,590 Right? 48 00:02:53,990 --> 00:02:55,855 This lines not long, 73, I read the lines wrong. 49 00:02:55,855 --> 00:02:59,620 Okay, so, but here's the thing though. 50 00:02:59,620 --> 00:03:02,230 I am going to do the some of the other ones too. 51 00:03:02,230 --> 00:03:07,060 This is a put and it's gonna come back automatically with a 200, so that's fine, 52 00:03:07,060 --> 00:03:08,190 I'm gonna go ahead and specify that. 53 00:03:08,190 --> 00:03:09,430 But I need to add a header to this. 54 00:03:10,680 --> 00:03:15,190 So let's put a parenthesis right there, so that we can break this to the next line. 55 00:03:15,190 --> 00:03:18,760 And we're gonna add a dictionary here at the end. 56 00:03:18,760 --> 00:03:21,170 So this 200, specifies the status code. 57 00:03:21,170 --> 00:03:23,960 This dictionary has any additional headers that you want to include. 58 00:03:23,960 --> 00:03:26,220 So let's include the location header. 59 00:03:26,220 --> 00:03:28,510 And then inside that location header, we're gonna use url_for. 60 00:03:28,510 --> 00:03:33,646 And we're gonna say resources.courses.course. 61 00:03:33,646 --> 00:03:35,370 And the id is going to be equal to the id. 62 00:03:36,620 --> 00:03:38,370 So close that dictionary. 63 00:03:38,370 --> 00:03:42,060 Close that tuple that reusing just to hold everything together. 64 00:03:42,060 --> 00:03:43,310 I'm gonna save that. 65 00:03:43,310 --> 00:03:46,380 Now, a lot of put is just like post. 66 00:03:46,380 --> 00:03:48,415 Right? Right, we look up here at post. 67 00:03:48,415 --> 00:03:52,055 Post and put aren't super different, right. 68 00:03:52,055 --> 00:03:54,315 Take the args, parse them, and use them. 69 00:03:54,315 --> 00:03:56,995 Unlike post, where I could do models.Course.create, 70 00:03:56,995 --> 00:03:58,955 p requires me to do this query thing. 71 00:03:58,955 --> 00:04:01,535 But we already went over that, so that's cool. 72 00:04:01,535 --> 00:04:05,345 And I'm gonna actually reuse this pattern for delete as well. 73 00:04:05,345 --> 00:04:07,645 So I don't mind having something a little different right here. 74 00:04:08,890 --> 00:04:13,540 You may not have seen the returning of a tuple before, but we can talk about that. 75 00:04:13,540 --> 00:04:15,790 This one's the response body. 76 00:04:16,930 --> 00:04:19,200 So this'll be a template, or for right now, 77 00:04:19,200 --> 00:04:21,460 it's gonna be a thing that gets marshaled. 78 00:04:21,460 --> 00:04:23,106 So it'll be JSON data. 79 00:04:23,106 --> 00:04:25,720 This is the status code like we talked about, right? 80 00:04:25,720 --> 00:04:29,590 So this one's 200, because there's not a status code that relates to, 81 00:04:30,620 --> 00:04:34,150 I updated everything, put is actually a little bit weird in this though. 82 00:04:34,150 --> 00:04:38,240 According to the http spec, put should have an empty body. 83 00:04:38,240 --> 00:04:39,560 So nothing in the body. 84 00:04:39,560 --> 00:04:42,687 And a status code of 204, which means there's no body, but 85 00:04:42,687 --> 00:04:44,600 that's not very useful. 86 00:04:44,600 --> 00:04:48,120 So instead I'm sending back the updated record and a status code of 200, 87 00:04:48,120 --> 00:04:50,760 to indicate that the body is not empty. 88 00:04:50,760 --> 00:04:54,510 And finally in the dictionary, like I said, is the different headers. 89 00:04:54,510 --> 00:04:57,270 So in this view I'm going to send back the location header to 90 00:04:57,270 --> 00:04:58,720 where the updated record is. 91 00:04:58,720 --> 00:05:01,840 Which is what you would do with the empty body in the 204. 92 00:05:01,840 --> 00:05:03,710 It's on the location so they know where to go. 93 00:05:04,780 --> 00:05:09,290 I'm kind of just mixing these two things up. 94 00:05:09,290 --> 00:05:10,070 Yeah? Maybe? 95 00:05:11,090 --> 00:05:12,670 Okay, cool whatever. 96 00:05:12,670 --> 00:05:19,670 So let's actually take this stuff and let's paste it here into delete. 97 00:05:19,670 --> 00:05:22,770 Cuz I'm here, I might as well use the delete method, right? 98 00:05:22,770 --> 00:05:27,100 So we're gonna change this to delete, and we don't have any arguments for delete, 99 00:05:27,100 --> 00:05:28,700 we just delete stuff. 100 00:05:28,700 --> 00:05:30,116 So where the Course.id==id. 101 00:05:30,116 --> 00:05:31,210 And then we're gonna execute that. 102 00:05:32,310 --> 00:05:34,410 And then, to be good, 103 00:05:34,410 --> 00:05:39,600 we're gonna send back an empty body and a 204 because that's what it should be. 104 00:05:41,330 --> 00:05:46,150 And I'm gonna send back a location, but the location's gonna go to go to courses, 105 00:05:46,150 --> 00:05:50,450 not to course, because this record's been deleted. 106 00:05:50,450 --> 00:05:53,920 So we just send them on to somewhere new. 107 00:05:53,920 --> 00:05:54,790 Right? 108 00:05:54,790 --> 00:05:59,430 We send them on to where the other stuff is. 109 00:05:59,430 --> 00:06:03,620 And, of course, we can get rid of the parentheses there if we want to or 110 00:06:03,620 --> 00:06:04,270 you can leave them. 111 00:06:04,270 --> 00:06:06,240 It's up to you. All right, so let's try this out. 112 00:06:06,240 --> 00:06:08,530 First of all though, let's create a new post. 113 00:06:10,000 --> 00:06:11,380 All right. So let's look at our body. 114 00:06:12,380 --> 00:06:17,528 Let's call this object oriented python. 115 00:06:17,528 --> 00:06:22,500 And object-oriented-python. 116 00:06:22,500 --> 00:06:23,480 All right. 117 00:06:23,480 --> 00:06:25,780 So, there's our body. 118 00:06:25,780 --> 00:06:30,380 And if we send this post in to, let's make sure that we're not, yeah, 119 00:06:30,380 --> 00:06:31,240 there we go, okay. 120 00:06:31,240 --> 00:06:32,780 We send those into courses. 121 00:06:33,850 --> 00:06:36,710 Then we should get a 200 Okay, and we should get this back. 122 00:06:36,710 --> 00:06:39,910 You know, while I'm here let's go change something real quick. 123 00:06:41,980 --> 00:06:46,792 So we can send that back, let's send back 124 00:06:46,792 --> 00:06:51,739 a 201 and I can send back location URL for 125 00:06:51,739 --> 00:06:58,202 resources.courses.course', id=course.id and 126 00:06:58,202 --> 00:07:03,151 for this we should have the parenthesis and 127 00:07:03,151 --> 00:07:06,742 we'll break it here for that. 128 00:07:11,222 --> 00:07:12,100 Yeah, cool. 129 00:07:12,100 --> 00:07:13,260 So that gets us to 80. 130 00:07:13,260 --> 00:07:14,146 All right. 131 00:07:14,146 --> 00:07:20,610 Sorry, sidetracked, okay so [LAUGH] we created one and we get back a 200. 132 00:07:20,610 --> 00:07:25,840 Now, though, we'll get back a 201, which is the created status code. 133 00:07:25,840 --> 00:07:30,360 So okay, cool, so now I don't want to do a put, and I wanna change some of this. 134 00:07:31,560 --> 00:07:34,320 So I'm gonna actually change it, so I'm gonna take out that hyphen. 135 00:07:35,470 --> 00:07:36,870 And i'm going to send this through. 136 00:07:38,220 --> 00:07:39,340 And I got a 405. 137 00:07:39,340 --> 00:07:40,430 And that's right. 138 00:07:40,430 --> 00:07:41,580 That's not allowed. 139 00:07:43,090 --> 00:07:45,400 Because we're going to courses like that. 140 00:07:45,400 --> 00:07:51,040 So instead we should go to courses/2. 141 00:07:51,040 --> 00:07:52,520 All right, let's do a get real quick. 142 00:07:54,020 --> 00:07:55,090 Yep. So there is that. 143 00:07:55,090 --> 00:08:01,000 All right so now let's do a PUT, look at our body, look at our URL too. 144 00:08:01,000 --> 00:08:05,950 Okay, so send that, and now we get back the new one, and we get this. 145 00:08:05,950 --> 00:08:09,000 And if we look at headers we have a location header. 146 00:08:10,230 --> 00:08:13,220 So this is the location we would go to to get that new resource. 147 00:08:14,280 --> 00:08:18,150 Cool, so I go to the update and I got the header, right? 148 00:08:18,150 --> 00:08:22,440 I really want to stress this, if you're not going to send back body content, 149 00:08:22,440 --> 00:08:26,664 please, please, please, please, please, please, please, please, 150 00:08:26,664 --> 00:08:29,340 please send back the location header. 151 00:08:29,340 --> 00:08:34,500 Because that way users of your API know where to go to get that new data. 152 00:08:34,500 --> 00:08:35,180 Right? 153 00:08:35,180 --> 00:08:38,010 And it may not even be the same location that they put to. 154 00:08:38,010 --> 00:08:42,500 You might let them do /put/update or something, right? 155 00:08:42,500 --> 00:08:44,900 Like courses/to/update, and that's what they put to. 156 00:08:44,900 --> 00:08:50,530 But then they have to go back to courses/to to get the record. 157 00:08:50,530 --> 00:08:53,730 Just tell them where to go, that's all I'm asking for. 158 00:08:53,730 --> 00:08:56,110 Okay, so now this demo right here, 159 00:08:56,110 --> 00:08:58,290 this wouldn't be complete without trying the delete. 160 00:08:59,770 --> 00:09:00,840 This data's gonna be ignored. 161 00:09:00,840 --> 00:09:03,390 I'm gonna to send it just because it's there, but this data's gonna be ignored. 162 00:09:03,390 --> 00:09:05,220 So let's hit send on the delete. 163 00:09:06,340 --> 00:09:07,830 We get back a 204 No Content. 164 00:09:07,830 --> 00:09:10,250 Which is correct, there should be no content. 165 00:09:10,250 --> 00:09:14,720 And if we look at headers, our location header tells us to go back to courses. 166 00:09:14,720 --> 00:09:16,300 Right. No response. 167 00:09:16,300 --> 00:09:17,750 Got the location, which is where I want to go to. 168 00:09:18,840 --> 00:09:21,750 Cool. So if I was to go to this location, 169 00:09:21,750 --> 00:09:22,770 if I was to follow this. 170 00:09:23,940 --> 00:09:26,620 As I should, right? 171 00:09:28,340 --> 00:09:29,730 And I do a get and I Send. 172 00:09:30,930 --> 00:09:32,600 And I look at the body. 173 00:09:32,600 --> 00:09:36,410 I only have the Python collections that I created before I do not have object or 174 00:09:36,410 --> 00:09:38,270 a Python that I created. 175 00:09:38,270 --> 00:09:39,210 So that's what I want to do. 176 00:09:39,210 --> 00:09:41,880 Okay, so I'm going to build the basics of the review end points between 177 00:09:41,880 --> 00:09:43,200 these videos. 178 00:09:43,200 --> 00:09:46,540 I got a lot to show you with them once we got authentication set up. 179 00:09:46,540 --> 00:09:48,950 You should try to build the put and 180 00:09:48,950 --> 00:09:52,050 delete methods on the reviews by yourself, see if you can do those. 181 00:09:53,440 --> 00:09:55,120 Our basic API is complete. 182 00:09:55,120 --> 00:09:59,750 We have all of the crud operations, and we're ACDP methods and headers like pros. 183 00:09:59,750 --> 00:10:02,500 Now it's time to batten down the hatches on our API, so 184 00:10:02,500 --> 00:10:04,500 it can withstand the storms of the open internet.