1
00:00:05,570 --> 00:00:06,560
Welcome back.

2
00:00:06,560 --> 00:00:11,960
To finish off this section of the API we're going to show you how to connect the actual things like

3
00:00:12,020 --> 00:00:15,250
get posts and delete methods to a database.

4
00:00:15,260 --> 00:00:20,300
So far we've been dealing with things just held temporarily in memory such as a list.

5
00:00:20,300 --> 00:00:24,800
So what we're going to do is we're going to convert our previous guest posts and delete methods inside

6
00:00:24,860 --> 00:00:29,810
of our rest a resource class and we're going to use it to actually change his methods to grab objects

7
00:00:30,050 --> 00:00:31,550
from a database.

8
00:00:31,550 --> 00:00:36,140
So all we really need to do is create a model and then replace the methods with what we've already learned

9
00:00:36,200 --> 00:00:39,860
about basic applications of sequel alchemy based models.

10
00:00:39,860 --> 00:00:42,770
Essentially we're really not going to learn anything new in this lecture.

11
00:00:42,770 --> 00:00:48,750
We're just going to see how to connect what we already know about basic crud with models to our rest

12
00:00:48,800 --> 00:00:51,030
API functionalities that we just learned.

13
00:00:51,050 --> 00:00:53,870
So we're going to work at the same files as we did in the previous lecture.

14
00:00:53,900 --> 00:00:56,580
We're just going to create a model and then edit the methods.

15
00:00:56,720 --> 00:00:58,040
Let's get started.

16
00:00:58,060 --> 00:00:58,400
OK.

17
00:00:58,400 --> 00:01:01,310
Here we are at our simple API that py file.

18
00:01:01,400 --> 00:01:06,260
We're going to keep the same files for secure check as well as user pie.

19
00:01:06,260 --> 00:01:10,700
Keep in mind if you want to expand on what we're showing in this lecture you could convert this user

20
00:01:10,700 --> 00:01:16,460
to be a model itself and then just call those models from the secure check file and set of these tables.

21
00:01:16,490 --> 00:01:22,310
But really what we want to focus on are these three methods right here get post and delete.

22
00:01:22,450 --> 00:01:27,560
And essentially all we need to do is connect these to the methods that we already learn when manipulating

23
00:01:27,560 --> 00:01:29,980
models and databases with flask.

24
00:01:29,990 --> 00:01:34,640
So in order to do that let's go ahead and create a flask model.

25
00:01:35,000 --> 00:01:42,600
So we're going to say up at the top from flask sequel alchemy import sequel alchemy.

26
00:01:42,620 --> 00:01:43,880
So nothing new here.

27
00:01:44,000 --> 00:01:50,950
And we're also going to use flask Miree so say from flask migrate import migrate.

28
00:01:51,170 --> 00:01:54,390
So we've seen all that before we set up our app.

29
00:01:54,440 --> 00:01:58,160
We set up our configurations and we have a couple of more configurations to set up.

30
00:01:58,280 --> 00:02:04,380
And these are the ones related to our model so say base directory is equal to OS path.

31
00:02:05,430 --> 00:02:06,770
Absolute path.

32
00:02:07,200 --> 00:02:13,370
And then inside of that we'll say OS path directory name and then we'll pass and file.

33
00:02:13,650 --> 00:02:19,070
So we do this every time that we create models and then we'll set up the configurations for the model.

34
00:02:19,380 --> 00:02:26,290
So one will be sequel alchemy underscore database underscore.

35
00:02:26,330 --> 00:02:27,070
You are.

36
00:02:27,210 --> 00:02:31,250
I will set that equal to sequel.

37
00:02:31,360 --> 00:02:36,830
Colon three forward slashes plus OS not path.

38
00:02:36,890 --> 00:02:40,960
Join and join that base directory.

39
00:02:41,060 --> 00:02:42,320
Data sequel.

40
00:02:42,470 --> 00:02:47,240
Essentially saying go ahead and create that database you or I in the same location that we're currently

41
00:02:47,240 --> 00:02:48,160
in.

42
00:02:48,180 --> 00:02:52,200
They'll say app config and then will say sequel.

43
00:02:52,220 --> 00:03:02,660
Alchemy again underscore track modifications and we'll set that equal to false.

44
00:03:02,730 --> 00:03:05,400
OK so we have our configuration set up.

45
00:03:05,520 --> 00:03:12,810
Next we're going to do is we'll say D-B is equal to sequel alchemy in our app and then we want to be

46
00:03:12,810 --> 00:03:15,040
able to call the migrations from the command line.

47
00:03:15,150 --> 00:03:18,700
So say migrate Hassen the app to connect it to the database.

48
00:03:19,140 --> 00:03:19,770
Okay perfect.

49
00:03:19,770 --> 00:03:26,880
So this takes care of our database configurations and this takes care of our API as well as authentication.

50
00:03:26,880 --> 00:03:31,680
Now what we need to do is instead of storing things in this puppy's list to create a section here for

51
00:03:31,680 --> 00:03:34,050
the model and we're doing this all in one file.

52
00:03:34,050 --> 00:03:39,840
You could just create an other file called models that Pizer them in the past and import it from there.

53
00:03:39,950 --> 00:03:40,990
We'll keep things simple.

54
00:03:41,010 --> 00:03:53,710
We'll just say class puppie say D-B model and then here will say name is equal to D-B column and.

55
00:03:53,910 --> 00:03:59,120
But Beezer is going to have a name that's a string type column let's say 80 characters is the limit

56
00:03:59,510 --> 00:04:01,610
and we'll just have this be their primary key.

57
00:04:01,880 --> 00:04:06,350
Obviously you could expand this to whatever you wanted it could have IDs breeds and so on is it done

58
00:04:06,350 --> 00:04:07,390
before.

59
00:04:07,420 --> 00:04:19,300
So we have the name will say in its self common name and we'll just say when you create a puppy give

60
00:04:19,300 --> 00:04:20,290
that puppy a name.

61
00:04:20,650 --> 00:04:24,160
And in this case it's a little unrealistic because every puppy has to have a unique name.

62
00:04:24,160 --> 00:04:32,100
So but what really need to worry about the IDs here we'll just say DFI Jaison because we want to do

63
00:04:32,160 --> 00:04:37,680
is when we have an instance of a puppy we can actually return back a puppy instance with flasks.

64
00:04:37,710 --> 00:04:43,920
API are not going understand that we return back something that basically looks like adjacent objects

65
00:04:43,920 --> 00:04:52,640
so when we say def Jaison return we're going to say return back name with self-taught name.

66
00:04:52,920 --> 00:04:57,450
So when we actually return calls we're not going to return a puppy object we're gonna get that puppy

67
00:04:57,450 --> 00:05:03,420
object and then called J son off of it in order to return a on a representation of the object.

68
00:05:03,480 --> 00:05:06,440
And if we have more stuff here you could say then breed.

69
00:05:06,780 --> 00:05:07,940
Self-taught breed.

70
00:05:08,400 --> 00:05:09,290
And so on.

71
00:05:09,300 --> 00:05:14,460
There's actually lots of examples like this in the flask restful documentation and at the very end we'll

72
00:05:14,460 --> 00:05:15,970
kind of show you some of those as well.

73
00:05:16,860 --> 00:05:17,390
OK.

74
00:05:17,680 --> 00:05:21,610
So we have this puppy model we initiated and then we have the Jason.

75
00:05:21,670 --> 00:05:27,280
So now when you do just go through the method calls and fix them so they are actually linking to this

76
00:05:27,280 --> 00:05:28,540
model.

77
00:05:28,540 --> 00:05:32,830
So if we want to grab something from a model database then we already know what we need to do.

78
00:05:33,160 --> 00:05:37,350
We need to basically not have this list for that anymore.

79
00:05:37,420 --> 00:05:46,610
We'll say pup is equal to puppy and we run a query that also filter by name is equal to name and then

80
00:05:46,610 --> 00:05:52,370
grab the first one and then if we actually get a puppy out we'll say if there's a puppy return.

81
00:05:52,370 --> 00:05:58,820
So if pup return pup Jaison and that's where it's really nice to have that Jason method built into the

82
00:05:58,820 --> 00:05:59,830
model class.

83
00:05:59,870 --> 00:06:04,280
So I would recommend that you're building up models and you think rest API is going to be a big part

84
00:06:04,280 --> 00:06:05,210
of your web site.

85
00:06:05,210 --> 00:06:09,140
Go ahead and add in that Jason method call built into the model.

86
00:06:09,360 --> 00:06:09,930
OK.

87
00:06:10,040 --> 00:06:11,420
So else.

88
00:06:11,510 --> 00:06:12,720
So there's no puppy there.

89
00:06:13,620 --> 00:06:14,970
We're just going to say return.

90
00:06:15,150 --> 00:06:16,930
Name none 404.

91
00:06:16,940 --> 00:06:17,160
OK.

92
00:06:17,190 --> 00:06:18,170
So that's our simple guess.

93
00:06:18,190 --> 00:06:22,980
Again we just filter by whatever unique identifier primary key you have and then return that based on

94
00:06:22,980 --> 00:06:25,290
version for a posting.

95
00:06:25,390 --> 00:06:29,430
It's going to be kind of similar to what we've done before we'll say pup is equal to.

96
00:06:29,590 --> 00:06:33,030
And then here we're going to do is create a new instance of a puppy.

97
00:06:33,470 --> 00:06:35,370
Her name is equal to name that's passed in.

98
00:06:35,380 --> 00:06:43,050
When someone's posting a request and they will say D-B session go ahead and add that up and then grab

99
00:06:43,050 --> 00:06:50,860
D-B session and then go ahead and commit the changes and then we're just going to return d.j some version

100
00:06:50,860 --> 00:06:55,850
of the pup as essentially a confirmation that hey we added it and then we're putting them in the j's

101
00:06:55,850 --> 00:07:01,220
on OK then for deleting a puppy or we're going to do here.

102
00:07:03,050 --> 00:07:11,290
Say pup is equal to essentially the same command so we therefore queery filter by then we're going to

103
00:07:11,290 --> 00:07:18,640
filter by name as equal to name grab the first one that shows up grab or database session and then we'll

104
00:07:18,640 --> 00:07:21,650
just call delete that pup and then commit that delete.

105
00:07:21,670 --> 00:07:28,480
So say D-B session commit that delete and then we're just going to actually and overturn the same thing

106
00:07:28,480 --> 00:07:29,360
it did before.

107
00:07:29,530 --> 00:07:32,410
We'll just go ahead and return hey little success.

108
00:07:32,430 --> 00:07:35,930
OK so last one for all names though.

109
00:07:35,950 --> 00:07:37,450
We can do that is if you want.

110
00:07:37,450 --> 00:07:40,140
You can authenticate this or not authenticate this.

111
00:07:40,150 --> 00:07:44,800
Let's go ahead and just cancel out that authentications we won't worry about it for guess what we're

112
00:07:44,800 --> 00:07:54,170
going to do here is we're going to say puppies is equal to puppy and then we can say queery all in order

113
00:07:54,170 --> 00:07:55,930
to grab every single puppy.

114
00:07:55,940 --> 00:07:59,320
So keep in mind this is going to be a list of puppy objects.

115
00:07:59,330 --> 00:08:04,730
And as I mentioned we can actually return puppy objects when we return the Jason version of them.

116
00:08:04,730 --> 00:08:13,990
So what we need to do here is I can use list comprehension to say pup Jason for pup in puppies because

117
00:08:13,990 --> 00:08:16,020
this list of puppies is just a bunch of puppy objects.

118
00:08:16,030 --> 00:08:21,110
So going to return them all is based on in this giant list and then return that for all the names.

119
00:08:21,400 --> 00:08:21,930
OK.

120
00:08:22,210 --> 00:08:23,770
So we could keep everything else the same.

121
00:08:23,770 --> 00:08:26,120
And now we've connected this two models.

122
00:08:26,140 --> 00:08:29,980
So really this should just serve as a reference for you when you want to add in rest.

123
00:08:29,980 --> 00:08:35,380
API calls quickly using this resource method you can already have your existing projects that we've

124
00:08:35,380 --> 00:08:38,780
already done throughout the course and then just add these special methods.

125
00:08:38,800 --> 00:08:45,100
OK the final step though we have to do here is actually register our model using the migrations.

126
00:08:45,100 --> 00:08:51,470
So if you call flask D-B in it as you've done before and enter you may actually get an error the pinning

127
00:08:51,480 --> 00:08:52,790
on what you named your file.

128
00:08:52,960 --> 00:08:57,460
So if you named your file something other than look at that pie it may say something like Hey I couldn't

129
00:08:57,460 --> 00:08:59,000
locate that flask application.

130
00:08:59,170 --> 00:09:02,090
You forgot to provide that flask app environment variable.

131
00:09:02,110 --> 00:09:05,740
So we've seen this before we actually had a whole discussion on this and you can go back to the models

132
00:09:05,740 --> 00:09:06,960
lecture's to see it.

133
00:09:07,050 --> 00:09:14,830
All we need to do here is depending on your operating system you're either going to say export flask

134
00:09:14,950 --> 00:09:20,160
app equal to and then the name of your application so simple API that pie.

135
00:09:20,380 --> 00:09:21,300
So you can run that.

136
00:09:21,370 --> 00:09:24,490
However export that's for a Mac OS or Linux computers.

137
00:09:24,610 --> 00:09:31,720
If you're running a Windows like I am here going to say set flask at equal to and in this case you just

138
00:09:31,780 --> 00:09:33,610
choose it over the python.

139
00:09:34,000 --> 00:09:39,920
So now I've said it so that my environment variable tickly can also find Python with some OS calls.

140
00:09:40,000 --> 00:09:41,000
But we've done that.

141
00:09:41,050 --> 00:09:43,670
We definitely seen this before so check back the models lecture's.

142
00:09:43,680 --> 00:09:46,560
If you don't remember this error there's a whole discussion about it in the notes.

143
00:09:46,690 --> 00:09:53,140
But once you've done that then we can run flask VB in it enter and it looks like we forgot one thing

144
00:09:53,140 --> 00:09:54,790
which was to import os.

145
00:09:54,880 --> 00:09:58,460
So let's go ahead and add that in so say.

146
00:09:58,490 --> 00:09:59,720
Import os.

147
00:09:59,740 --> 00:10:00,510
Say that.

148
00:10:00,580 --> 00:10:04,640
That would make sense because we have those OS calls plus the BE IN IT.

149
00:10:04,730 --> 00:10:10,820
Ok that looks like it ran correctly will say Flast DBI migrates and then just have a little message

150
00:10:10,820 --> 00:10:11,300
here.

151
00:10:11,310 --> 00:10:18,140
The quotes will say first migration or first migrate they enter the migrations occur centrally adding

152
00:10:18,140 --> 00:10:25,550
in that name column for the puppy and then to make these actually happen say Flast DBI upgrade hit enter

153
00:10:25,980 --> 00:10:27,670
and now our database should exist.

154
00:10:27,710 --> 00:10:32,300
And if you open up this Left-Hand tab you should now see somewhere here like a folder for migrations

155
00:10:32,330 --> 00:10:36,860
and the data that sequel like nothing you definitely done this before many times throughout the course

156
00:10:37,260 --> 00:10:42,820
and Altes actually run our simple API OK it looks like everything's running.

157
00:10:42,960 --> 00:10:47,550
Let's go ahead and see if we can add puppies and puppies and read all the puppies and that will be the

158
00:10:47,550 --> 00:10:49,980
end of this so let's open up postman.

159
00:10:49,980 --> 00:10:51,960
So here I have postman open.

160
00:10:51,960 --> 00:10:55,510
First thing I'm going to do is I'm actually going to try to post the puppy.

161
00:10:55,740 --> 00:11:00,520
Right now nothing is authorized or nothing requires authorization as such thing.

162
00:11:00,630 --> 00:11:03,540
So let's go ahead and add in Sammy to the database.

163
00:11:03,540 --> 00:11:10,440
Keep in mind now that we're using a real database I can add in the puppies and then close off my Python

164
00:11:10,620 --> 00:11:14,620
script and then run my Python script again and the puppies will still be in that database.

165
00:11:14,640 --> 00:11:19,230
Unlike before where every time you restarted your python script that list was completely wiped out because

166
00:11:19,230 --> 00:11:20,050
it was in memory.

167
00:11:20,160 --> 00:11:24,020
Now we're actually dealing with something that's more secure and more permanent.

168
00:11:24,120 --> 00:11:27,210
So we're going to add in puppies so let's send the request.

169
00:11:27,380 --> 00:11:29,120
We got named Sammy so it looks good.

170
00:11:29,130 --> 00:11:35,180
It's going an add in Franki as well so I'll say Franki send it.

171
00:11:35,220 --> 00:11:37,560
We get back Franki So looks like that's working.

172
00:11:37,560 --> 00:11:39,130
Let's go ahead and get the puppies.

173
00:11:39,320 --> 00:11:41,750
So I'll say get all the puppies.

174
00:11:41,760 --> 00:11:42,540
Let's see if this works.

175
00:11:42,540 --> 00:11:48,220
Remember I commented out the authorization and it looks like that's working as well named Sammy named

176
00:11:48,230 --> 00:11:48,610
Frankie.

177
00:11:48,640 --> 00:11:51,670
That's working well let's try deleting a puppy.

178
00:11:51,760 --> 00:12:00,020
So say delete or say puppy and let's go ahead and delete Sammy will send that.

179
00:12:00,020 --> 00:12:03,010
Looks like the delete was successful so that method also worked.

180
00:12:03,020 --> 00:12:07,890
Let's get all the puppies again and see if it was actually removed from that database.

181
00:12:08,270 --> 00:12:09,260
So I'll send that.

182
00:12:09,290 --> 00:12:11,870
Looks like we only get back Franki So that's working well.

183
00:12:12,030 --> 00:12:17,510
OK so it looks like we were able to test out the commands for getting posting puppies grabbing all the

184
00:12:17,510 --> 00:12:19,200
puppies and deleting puppies.

185
00:12:19,400 --> 00:12:24,620
And if you want to update the puppies all you need to do is add in a method call called Put And then

186
00:12:24,620 --> 00:12:29,450
just use the update methods that we've already gone through multiple times throughout the course.

187
00:12:29,450 --> 00:12:34,190
Now something I want to mention is that the flask restful documentation actually has some really nice

188
00:12:34,190 --> 00:12:36,310
examples for you to check out.

189
00:12:36,410 --> 00:12:42,710
So if you come over to flask restful and expand this here you can check out the Quixtar but you can

190
00:12:42,710 --> 00:12:47,240
also scroll down and then check out the zoom in here.

191
00:12:47,300 --> 00:12:52,340
They have a bunch of examples that you can then extend flask restful intermediate users usage for Project

192
00:12:52,340 --> 00:12:53,010
structures.

193
00:12:53,120 --> 00:12:55,450
How you can use blueprints and so on.

194
00:12:55,490 --> 00:12:59,630
There's lots of really good resources here and I would really recommend you just check out the QuickStart

195
00:12:59,630 --> 00:13:05,030
guide and then go through out these different examples here because they're really useful as far as

196
00:13:05,120 --> 00:13:07,620
learning how to deal things like formatting.

197
00:13:07,640 --> 00:13:12,170
You know they have different full examples here that have an example here which is kind of a To Do list.

198
00:13:12,170 --> 00:13:17,030
You can then build off of and you can play around with this so this one page has lots of examples and

199
00:13:17,030 --> 00:13:19,300
the documentation is just pretty good in general.

200
00:13:19,340 --> 00:13:22,490
OK so that's it for flask restful applications.

201
00:13:22,490 --> 00:13:23,740
I hope you found that useful.

202
00:13:23,840 --> 00:13:25,340
And hope is a good jumping off point.

203
00:13:25,370 --> 00:13:30,630
In case you intend to use flask rest for any of your future projects we'll see at the next one.
