1
00:00:05,560 --> 00:00:07,520
Welcome back everyone in this lecture.

2
00:00:07,540 --> 00:00:13,000
We're going to expand on what we just showed and build out a basic crud API application basically being

3
00:00:13,000 --> 00:00:15,550
able to create read or daily items.

4
00:00:15,700 --> 00:00:20,920
And in this case we're just going to keep items in a list inside our Python script later on we're actually

5
00:00:20,920 --> 00:00:26,380
going to build out a full database and show you how to connect the flask Web site database to a rest

6
00:00:26,380 --> 00:00:27,430
application.

7
00:00:27,430 --> 00:00:32,190
But let's learn a couple of more functions or really methods inside of those resource calls.

8
00:00:32,380 --> 00:00:34,090
That's up over to the editor.

9
00:00:34,090 --> 00:00:34,300
All right.

10
00:00:34,300 --> 00:00:36,550
Here we are back at this the PI file.

11
00:00:36,550 --> 00:00:40,420
I went ahead and cleared everything just so we can get a little more practice setting everything up

12
00:00:40,420 --> 00:00:41,000
again.

13
00:00:41,260 --> 00:00:48,720
Basically for a start again from the top we'll say from flask import flask and then we'll say from flask

14
00:00:48,780 --> 00:00:56,940
restful import and we'll import resource as well as API.

15
00:00:57,000 --> 00:00:59,430
And let me collapse Left-Hand time.

16
00:00:59,570 --> 00:01:01,370
We're just dealing with one file.

17
00:01:01,680 --> 00:01:05,470
We create our application as always passing in the name variable.

18
00:01:05,470 --> 00:01:07,010
It's built in.

19
00:01:07,180 --> 00:01:14,060
And then we wrap that into the API call and then set that equal to the variable API.

20
00:01:14,440 --> 00:01:16,670
Notice the capitalization there.

21
00:01:16,780 --> 00:01:22,360
So we're going to do is we're basically going to have a list of puppies and each puppy is going to have

22
00:01:22,450 --> 00:01:27,020
a name sort to say puppies is equal to an empty list.

23
00:01:27,210 --> 00:01:34,090
And we're going to use the rest API in order to get a puppy name or Dilley a puppy name or post new

24
00:01:34,090 --> 00:01:35,200
puppy names as well.

25
00:01:36,730 --> 00:01:44,050
So we're going to create a class here called Puppy names and that's going to inherit from resource and

26
00:01:44,050 --> 00:01:48,850
then the main methods we're going to say here is def get.

27
00:01:48,950 --> 00:01:53,370
And then also have the post.

28
00:01:53,900 --> 00:01:58,600
And right now we'll just say pass for all of these so we don't get any errors here.

29
00:01:58,630 --> 00:02:00,410
There is a colon.

30
00:02:00,410 --> 00:02:06,130
So we have get and post and then we'll also do delete.

31
00:02:06,200 --> 00:02:12,730
So Def delete pass and then start filling these out.

32
00:02:12,730 --> 00:02:17,950
So we want to be able to do here is using the rest API we'll do our calls from postman.

33
00:02:18,160 --> 00:02:22,290
I want to be able to get a specific puppie given the name of a puppy.

34
00:02:22,360 --> 00:02:28,000
So what I'm imagining here is that puppies is going to be a list and inside a single puppy will have

35
00:02:28,000 --> 00:02:29,260
the following format.

36
00:02:29,410 --> 00:02:35,610
It's going to be a dictionary with the key name and the name of the puppy for example.

37
00:02:35,640 --> 00:02:40,360
Rufus So we're going to have each puppy have this format.

38
00:02:40,360 --> 00:02:44,070
So our list of puppies is then going to look something like this.

39
00:02:44,080 --> 00:02:46,160
Get a copy and paste from the notes here for a second.

40
00:02:46,330 --> 00:02:49,720
But basically we have a dictionary here named Rufus.

41
00:02:49,720 --> 00:02:52,630
Then the next one named Franki and so on.

42
00:02:52,630 --> 00:02:54,270
So that's what this list looks like.

43
00:02:54,310 --> 00:02:56,620
The list of dictionaries.

44
00:02:56,620 --> 00:02:57,550
So we have puppies.

45
00:02:57,610 --> 00:03:01,930
So if we actually want to get a puppy you need to cycle through the puppies list.

46
00:03:02,110 --> 00:03:06,860
So before we do get let's actually show how we would post a new puppy.

47
00:03:06,920 --> 00:03:11,660
So all these methods should have self and something that's important to know is when you're building

48
00:03:11,660 --> 00:03:18,610
out your API the get methods post methods methods they all need to take in the same parameters as arguments.

49
00:03:18,620 --> 00:03:24,110
So in this case if we want to get a puppy by its name then we need to make sure that the rest these

50
00:03:24,200 --> 00:03:26,910
also have the exact same parameters.

51
00:03:26,930 --> 00:03:32,240
So let's start with post since that will allow us to post a new puppy to this list the puppies that

52
00:03:32,240 --> 00:03:35,290
will show you how to get a puppy and they will show you how to delete a puppy.

53
00:03:35,600 --> 00:03:38,770
And then we will actually create a new class to just get all the puppies.

54
00:03:38,820 --> 00:03:41,180
But start of actually posting a new puppy.

55
00:03:41,180 --> 00:03:45,040
So how do we actually add something to that list.

56
00:03:45,050 --> 00:03:53,840
Well first to say pup is equal to name and then name what's going to happen here is our resource is

57
00:03:53,840 --> 00:03:59,490
going to take in a name through an API call and we'll clarify that in a second through the euro.

58
00:03:59,540 --> 00:04:04,670
But basically we're going to pass in a Yoro for a name and then we're going to create a dictionary that

59
00:04:04,670 --> 00:04:08,070
looks like this name and the name that was passed then.

60
00:04:08,210 --> 00:04:18,320
Then we'll take that pup and we'll say puppies penned that particular pup that is going to return pup.

61
00:04:18,740 --> 00:04:24,080
So you can think of this as we're essentially treating puppies here as an in memory database which is

62
00:04:24,080 --> 00:04:25,310
really just a list.

63
00:04:25,730 --> 00:04:30,950
So when we post the new puppy based off name we create an instance of a dictionary called name with

64
00:04:30,950 --> 00:04:34,280
that particular puppy's names who would pass and something like post Rufus.

65
00:04:34,280 --> 00:04:38,750
Then we get the dictionary we append that dictionary to that list of puppies and then we return that

66
00:04:38,750 --> 00:04:44,000
pup basically to tell us Hey this was the actual dictionary passed then and then we can later on get

67
00:04:44,000 --> 00:04:44,410
it.

68
00:04:44,690 --> 00:04:47,200
So imagine that we just posted a new puppy.

69
00:04:47,240 --> 00:04:51,460
Let's go ahead and get a puppy.

70
00:04:51,460 --> 00:04:56,200
So hard to make that happen when you say for every pup in that list of puppies.

71
00:04:57,230 --> 00:05:05,830
Well say if pup pups name because remember pup here is a dictionary in the sort of format.

72
00:05:06,180 --> 00:05:10,860
So if we want to get a particular name of the puppy we'll go through every dictionary in puppies and

73
00:05:10,860 --> 00:05:14,740
say if that pup name is equal to the name provided.

74
00:05:14,820 --> 00:05:17,040
So for example maybe we're searching for Rufus.

75
00:05:17,070 --> 00:05:22,750
So I'll say it's up to good dictionaries value for the key name happens to be equal to Rufus them or

76
00:05:22,780 --> 00:05:24,440
turn the pup.

77
00:05:24,730 --> 00:05:29,410
However what if we actually try to request a puppy that isn't actually in the puppies list yet.

78
00:05:29,740 --> 00:05:34,060
Well in that case when you go through an entire for a loop this will never be true meaning you'll never

79
00:05:34,060 --> 00:05:35,070
return anything.

80
00:05:35,080 --> 00:05:40,750
So after the for loop has been run I'm going to add in one more return statement and this return statement

81
00:05:40,810 --> 00:05:45,640
only executes if this for loop never has a hit with the same puppy name.

82
00:05:45,820 --> 00:05:54,700
So they will say return name and we'll say none essentially indicating that hey it was empty.

83
00:05:54,870 --> 00:05:58,260
Now we're going to add a little more to this later on but just keep that in mind.

84
00:05:58,300 --> 00:06:02,440
So all we're doing here is we want to post the new puppy we create this dictionary.

85
00:06:02,560 --> 00:06:06,290
A pen that pup puppies and then returned pup.

86
00:06:06,450 --> 00:06:07,720
If you want to get a puppy.

87
00:06:07,760 --> 00:06:10,500
We go through every pup in that list of puppies.

88
00:06:10,560 --> 00:06:12,060
So then we have these dictionaries.

89
00:06:12,060 --> 00:06:13,410
Each pup has a dictionary.

90
00:06:13,620 --> 00:06:15,750
We check that tickler dictionaries name value.

91
00:06:15,750 --> 00:06:20,930
So that would be something that would be like Rufus or Frankie or whatever happened to call dog.

92
00:06:21,180 --> 00:06:26,040
You check that same name that was requested and if so you return that pup which you're going to look

93
00:06:26,040 --> 00:06:28,810
like adjacent object essentially a python dictionary.

94
00:06:28,980 --> 00:06:31,740
If you go through the tire for a loop never find the match then return.

95
00:06:31,740 --> 00:06:32,350
Name.

96
00:06:33,420 --> 00:06:35,280
Finally let's show you how to delete a puppy.

97
00:06:36,410 --> 00:06:38,700
So for this it's going to work really similar.

98
00:06:39,410 --> 00:06:48,350
It will do the following we'll say for index position and pup in enumerates puppies so remember enumerate.

99
00:06:48,360 --> 00:06:53,850
Basically pass along a counter sordidness say Little did a tuple and packing here this will be a counter

100
00:06:53,850 --> 00:06:55,620
like 0 1 2 3 4.

101
00:06:55,650 --> 00:06:57,330
Pup is going to be the actual dictionary.

102
00:06:57,630 --> 00:07:08,220
So say for index position come up any numeric puppies if pup name is equal to name.

103
00:07:08,410 --> 00:07:15,030
So we have an actual match then we're going to do is say deleted pup

104
00:07:18,180 --> 00:07:24,900
is equal to puppies that pop at the index essentially since we're dealing for less we're just going

105
00:07:24,900 --> 00:07:29,520
to pop at that particular index or there happened to be a match and then if that happens we're just

106
00:07:29,510 --> 00:07:32,930
going to return note

107
00:07:35,960 --> 00:07:39,910
delete success.

108
00:07:39,970 --> 00:07:43,930
OK so all we're doing here for Dilley is someone is going to pass on the name of the puppy.

109
00:07:43,930 --> 00:07:47,540
We're going to go through the entire list of puppies and see if we find the matching name.

110
00:07:47,530 --> 00:07:52,700
They're going to pop that puppy and then we're going to send the note that hey the delete all successful

111
00:07:52,840 --> 00:07:55,990
if you want you could have also return more information like the actual deleted pup.

112
00:07:55,990 --> 00:08:00,490
In fact you don't really need to save this you could just do this command if you want it to but maybe

113
00:08:00,490 --> 00:08:06,100
later on you want to have a little print message here like print deleted pup just so you can keep track

114
00:08:06,100 --> 00:08:06,540
of things.

115
00:08:06,550 --> 00:08:08,260
It's really up to you.

116
00:08:08,290 --> 00:08:09,330
So we're going to save that.

117
00:08:09,730 --> 00:08:13,390
And those are the three basic methods that we can play around with right now since we're just dealing

118
00:08:13,390 --> 00:08:14,660
with a python list.

119
00:08:14,920 --> 00:08:22,200
So again we can post the new puppy we can't get a single puppy and we can Dilly a puppy.

120
00:08:22,200 --> 00:08:28,130
Now what I want to do is create one more resource in case you want to get a list of all the puppies.

121
00:08:28,200 --> 00:08:32,590
So say class all names resource.

122
00:08:32,920 --> 00:08:41,410
And this is going to have its own get call and we're going to say return and we can't just return puppies

123
00:08:41,440 --> 00:08:42,520
because that's a list.

124
00:08:42,520 --> 00:08:46,800
So what we want to do is return it in a Jaison type form.

125
00:08:46,840 --> 00:08:52,000
So we'll say puppies Colan puppies so knows this is going to be a dictionary of puppies and it is going

126
00:08:52,000 --> 00:08:53,770
to be a list of more dictionaries.

127
00:08:53,770 --> 00:08:56,380
So it's going to be kind of a nested J song structure.

128
00:08:56,390 --> 00:08:56,860
All right.

129
00:08:57,100 --> 00:09:01,810
And then what we need to do is we need to register these resources essentially add resources to the

130
00:09:01,810 --> 00:09:02,680
API.

131
00:09:03,040 --> 00:09:09,460
So we're going to say API dot add resource and then we'll start with the puppy names class that we created.

132
00:09:09,640 --> 00:09:12,070
And this is where we get to choose your l.

133
00:09:12,080 --> 00:09:13,800
You can actually connect to.

134
00:09:13,810 --> 00:09:16,560
So if I just head forward slash don't connect directly home.

135
00:09:16,660 --> 00:09:20,840
But again we probably want to have something that works if these actual methods.

136
00:09:21,010 --> 00:09:26,520
So notice all these methods take in a name as a parameter so you can think of this very akin to when

137
00:09:26,530 --> 00:09:31,590
we said app route we're essentially saying API add resource.

138
00:09:31,690 --> 00:09:37,460
And so then we're going to put in the your L or or someone should connect to this particular API call.

139
00:09:37,690 --> 00:09:43,560
So I'll say puppie and then we'll make sure it's a string.

140
00:09:43,610 --> 00:09:47,920
We've shown this before in the course and then we'll passen name.

141
00:09:47,930 --> 00:09:54,460
So essentially what this means is someone is going to be able to programmatically reach the home your

142
00:09:54,490 --> 00:10:00,170
elves for Slash puppie forward slash the name of a puppy and that connects to puppy names which then

143
00:10:00,170 --> 00:10:04,920
you can call either delete method or a post method or get method and we'll be playing around with that

144
00:10:04,920 --> 00:10:06,120
and just a little bit.

145
00:10:06,230 --> 00:10:12,620
Then I also want to add and this all names resource so I'll say API add resource.

146
00:10:12,840 --> 00:10:19,440
All names and this one will have it go to a different you or I will have this one go to puppies.

147
00:10:19,510 --> 00:10:25,120
Then once that's all done we'll say if name is equal to mean go ahead and run our app and we'll say

148
00:10:25,120 --> 00:10:28,860
that debug is equal to true.

149
00:10:28,870 --> 00:10:29,380
All right.

150
00:10:29,530 --> 00:10:30,790
So that's what we have so far.

151
00:10:30,790 --> 00:10:36,100
Let me zoom out just so we can get an idea of everything we just made same as before we set up our flask

152
00:10:36,100 --> 00:10:36,730
application.

153
00:10:36,730 --> 00:10:38,130
Connect that to an API.

154
00:10:38,260 --> 00:10:41,430
Right now we're just storing everything in a Python list keeping things simple.

155
00:10:41,530 --> 00:10:46,420
And we created our class puppy names and because it's a list we're just going to able to do a get a

156
00:10:46,420 --> 00:10:52,420
post and delete for the posts we create this little new dictionary based off what was provided at this

157
00:10:52,570 --> 00:10:53,070
url.

158
00:10:53,160 --> 00:10:57,970
We added it as a resource then we append that particular dictionary to the list and then we return it

159
00:10:58,230 --> 00:11:02,830
just so it's a confirmation that yes we did post this then if we want to grab a particular puppie based

160
00:11:02,830 --> 00:11:07,390
on name we go through every dictionary in the puppies list and if we get a match we would turn that

161
00:11:07,390 --> 00:11:08,200
puppy.

162
00:11:08,200 --> 00:11:12,210
Or else if we went through a for loop and then didn't get a match we say name was done.

163
00:11:12,610 --> 00:11:17,020
Then if we wanted to see a puppy really similar process to getting a puppy except we're also keeping

164
00:11:17,020 --> 00:11:20,810
track of the index so that we can pop that puppy out of that list.

165
00:11:21,040 --> 00:11:22,670
Then we have class for all names.

166
00:11:22,750 --> 00:11:28,060
And so same thing as a resource except the what it's going to return is just the entire list of puppies.

167
00:11:28,060 --> 00:11:30,990
OK then we added those resources and then we're good to go.

168
00:11:31,240 --> 00:11:33,470
So let's actually run this will say Python.

169
00:11:33,520 --> 00:11:36,550
I'm calling the simple API that pi will go ahead and run that.

170
00:11:36,610 --> 00:11:38,170
And then let's bring up postman.

171
00:11:38,320 --> 00:11:46,150
So now I have postman first thing I'm going to do is I'm going to try to get a puppy that does not exist.

172
00:11:46,240 --> 00:11:53,680
So say our home your L forward slash puppy forward slash and then you can type in any name here because

173
00:11:53,680 --> 00:11:57,160
technically there are no puppies that exist in the list right now.

174
00:11:57,160 --> 00:12:00,400
So let's go ahead and hit send and see what we get back.

175
00:12:00,400 --> 00:12:01,480
So you notice we get back.

176
00:12:01,480 --> 00:12:02,810
Name no.

177
00:12:03,040 --> 00:12:07,720
So essentially what happened is we returned back an empty dictionary and now we're receiving that as

178
00:12:07,720 --> 00:12:08,290
Jason.

179
00:12:08,290 --> 00:12:11,620
So the python nun gets returned as a note of Jaison.

180
00:12:11,800 --> 00:12:16,330
So that makes sense we just asked for a puppy that's not there let's try posting some puppies.

181
00:12:16,540 --> 00:12:22,360
So say post and now I make such a keep in the you are all the same but saying Hey post Sammy to that

182
00:12:22,360 --> 00:12:28,720
list the puppies so we'll send it and then it sends the request over and now I see name Sammy.

183
00:12:28,860 --> 00:12:30,000
So that looks like it worked.

184
00:12:30,000 --> 00:12:32,580
Let's go ahead and add Frankie.

185
00:12:32,590 --> 00:12:35,320
So these are my two dogs are going to add them to that list of puppies.

186
00:12:35,370 --> 00:12:36,550
So we're going to send that as well.

187
00:12:36,600 --> 00:12:38,190
Now I see named Frankie.

188
00:12:38,430 --> 00:12:39,560
And let's add one more.

189
00:12:39,560 --> 00:12:42,870
Let's go ahead and Cindy has a puppy name.

190
00:12:42,870 --> 00:12:43,680
We'll send that.

191
00:12:43,680 --> 00:12:45,410
And now I see named Cindy.

192
00:12:45,420 --> 00:12:45,920
OK.

193
00:12:46,080 --> 00:12:47,600
So now we have three puppies.

194
00:12:47,610 --> 00:12:49,310
Let's go ahead and try to get one of them.

195
00:12:49,590 --> 00:12:52,920
Let's make sure that this is all working on the say puppy Max.

196
00:12:53,040 --> 00:12:55,550
That puppy isn't something we posted.

197
00:12:55,710 --> 00:12:57,090
So if I send I get back.

198
00:12:57,090 --> 00:12:57,930
My name is nals.

199
00:12:57,930 --> 00:12:59,070
That makes sense.

200
00:12:59,070 --> 00:13:02,570
Now let's try one that we did actually put in which was Frankie.

201
00:13:02,690 --> 00:13:06,200
And if I send that now we actually get back name Frankie.

202
00:13:06,210 --> 00:13:07,850
So it looks like that's working.

203
00:13:07,920 --> 00:13:11,300
Finally let's try getting all the puppies.

204
00:13:11,340 --> 00:13:14,260
So you say gets in a of puppy for Slash name.

205
00:13:14,400 --> 00:13:17,080
We're going to say get puppies.

206
00:13:17,090 --> 00:13:21,860
So remember that was the all names class that was connected to four slush puppies.

207
00:13:21,900 --> 00:13:27,260
So going to send this and noticed when I get back I get back puppies and then a list of these Jason

208
00:13:27,260 --> 00:13:33,420
objects Sammy Frankie and Cindy let's tried deleting Sindy using our delete method.

209
00:13:33,800 --> 00:13:40,680
So say delete forward slash puppie forward slash Cindy.

210
00:13:41,330 --> 00:13:46,200
We're going to send this and notice it says delete success.

211
00:13:46,210 --> 00:13:49,900
So now let's try to get all the puppies again and see what we get back.

212
00:13:49,960 --> 00:13:53,740
So I'll say get puppies send this.

213
00:13:53,740 --> 00:13:55,510
And now I only see Sammie and.

214
00:13:55,510 --> 00:13:58,650
Since we removed Sindhi from that list of puppies.

215
00:13:58,690 --> 00:14:03,700
So it looks like everything's working and hopefully you can tell that the actual method calls are pretty

216
00:14:03,700 --> 00:14:04,510
straightforward.

217
00:14:04,510 --> 00:14:09,700
You just put in the method call make sure it matches and flask restfully is taking care of everything

218
00:14:09,700 --> 00:14:12,820
else on the back and for us something I want to know is.

219
00:14:12,850 --> 00:14:18,280
Let's imagine if we tried to grab a puppy that again isn't in our actual database so we're going to

220
00:14:18,280 --> 00:14:19,890
say a totally new name of puppy.

221
00:14:19,900 --> 00:14:23,350
Let's go ahead and say Davy isn't there a puppy.

222
00:14:23,410 --> 00:14:27,290
We're going to try to get Davy and noticed we get back named Noel.

223
00:14:27,460 --> 00:14:30,370
But take a look over here where it says status.

224
00:14:30,370 --> 00:14:35,670
So status 200 OK is the standard response for a successful request.

225
00:14:35,680 --> 00:14:40,510
What we can do is instead of just passing in or passing back Jason.

226
00:14:40,630 --> 00:14:46,150
When you actually don't have a puppy we can also use tuples to pass out a status.

227
00:14:46,150 --> 00:14:49,490
So let's go back to our code.

228
00:14:49,620 --> 00:14:55,770
So when I come back to our code and we said here return name none are going to come back to this and

229
00:14:55,770 --> 00:14:59,490
I'm going to say comma also for 0 4.

230
00:14:59,640 --> 00:15:01,120
Let me save that.

231
00:15:01,440 --> 00:15:07,210
And notice this is now updated again and then I'm going to again request puppy Davey.

232
00:15:07,290 --> 00:15:09,950
Remember Davy is not actually in the database.

233
00:15:10,110 --> 00:15:13,940
And when I send my request to get Davey I get back again.

234
00:15:13,950 --> 00:15:15,070
Name No.

235
00:15:15,210 --> 00:15:21,210
But now look this is changed to say four or four not found so that how you can actually send back HTP

236
00:15:21,240 --> 00:15:26,130
statuses as well because in this case you probably want to send that a 4 or four so the program knows

237
00:15:26,160 --> 00:15:28,790
hey you're asking for something that doesn't exist.

238
00:15:28,800 --> 00:15:34,920
Keep in mind because we're storing puppies as an in-memory list every time you restart your python script

239
00:15:35,220 --> 00:15:37,410
is essentially going to wipe clean that puppy's list.

240
00:15:37,410 --> 00:15:42,180
So keep that in mind if you keep restarting the lists because if you had a typo or something you may

241
00:15:42,270 --> 00:15:46,770
accidently request Papy that you thought was there but then got wiped clean when you restarted the python

242
00:15:46,770 --> 00:15:47,900
script.

243
00:15:47,940 --> 00:15:48,690
All right.

244
00:15:48,690 --> 00:15:52,480
That's the very basics of getting things posting things and deleting things.

245
00:15:52,560 --> 00:15:58,410
You should note however that pretty much anyone was allowed to get post and delete items from that list

246
00:15:58,920 --> 00:15:59,750
in the next lecture.

247
00:15:59,760 --> 00:16:04,410
We're going to discuss authentication which means we're going to need a special key in order to actually

248
00:16:04,440 --> 00:16:09,530
access this rest API which is always good practice in a more realistic situation.

249
00:16:09,570 --> 00:16:11,690
So we're going to cover that in the very next lecture.

250
00:16:11,700 --> 00:16:12,340
We'll see if.
