1
00:00:00,540 --> 00:00:02,231
- [Instructor] Hi and
welcome back to the course.

2
00:00:02,231 --> 00:00:03,605
In this video we're going to be creating

3
00:00:03,605 --> 00:00:06,247
our first flask_restful app.

4
00:00:06,247 --> 00:00:08,354
It's going to be a really
simple app just to get you

5
00:00:08,354 --> 00:00:10,548
acquainted with sort of what it looks like

6
00:00:10,548 --> 00:00:11,381
and then in the next
video we're really gonna

7
00:00:11,381 --> 00:00:15,850
get into creating the
project for this section.

8
00:00:15,850 --> 00:00:18,062
The first thing to do
is to in the folder that

9
00:00:18,062 --> 00:00:19,953
you've got your venv,

10
00:00:19,953 --> 00:00:21,408
as you can see I'm going
to list the structure

11
00:00:21,408 --> 00:00:22,669
of this folder and there is

12
00:00:22,669 --> 00:00:25,030
a folder in here called venv.

13
00:00:25,030 --> 00:00:26,879
So, not inside this folder,

14
00:00:26,879 --> 00:00:30,719
but beside the folder I'm
going to create another

15
00:00:30,719 --> 00:00:32,082
folder called code.

16
00:00:32,082 --> 00:00:34,967
So, as you can see if I do
ls now we've got two folders,

17
00:00:34,967 --> 00:00:39,134
venv and code and then
we're going to cd into code

18
00:00:40,002 --> 00:00:42,835
or change that actually into code.

19
00:00:43,990 --> 00:00:46,317
Now, we are in code I'm
going to launch Atom

20
00:00:46,317 --> 00:00:49,675
and you can do so by typing Atom dot,

21
00:00:49,675 --> 00:00:51,961
this I think only works in Mac and Linux,

22
00:00:51,961 --> 00:00:54,649
if you're on Windows
just load Atom as normal,

23
00:00:54,649 --> 00:00:57,721
it really doesn't take much longer.

24
00:00:57,721 --> 00:01:00,221
And then when Atom is running,

25
00:01:01,154 --> 00:01:04,237
which it is just starting up in here,

26
00:01:06,264 --> 00:01:07,803
here we've got Atom.

27
00:01:07,803 --> 00:01:09,298
Now that Atom's running we can go ahead

28
00:01:09,298 --> 00:01:11,249
and create a new file.

29
00:01:11,249 --> 00:01:15,323
I'm going to call that app.py as normal.

30
00:01:15,323 --> 00:01:18,601
Remember, we're going to
be running this file from

31
00:01:18,601 --> 00:01:22,332
our virtual environment
and therefore we have flask

32
00:01:22,332 --> 00:01:25,896
and flask_restful already available.

33
00:01:25,896 --> 00:01:30,665
So, the first thing to do
is to import those two.

34
00:01:30,665 --> 00:01:33,122
So, from flask I'm gonna import flask

35
00:01:33,122 --> 00:01:37,644
and from flask_restful, I'm
going to import resource

36
00:01:37,644 --> 00:01:42,179
and API and these two things
are going to be necessary.

37
00:01:42,179 --> 00:01:45,004
A resource represents something

38
00:01:45,004 --> 00:01:47,671
that our API sort of represents.

39
00:01:48,571 --> 00:01:50,898
That was too many
represents in one phrase.

40
00:01:50,898 --> 00:01:54,759
For example, if our API
is concerned with students

41
00:01:54,759 --> 00:01:57,710
and it can return students
and create students,

42
00:01:57,710 --> 00:01:59,707
a student would be a resource.

43
00:01:59,707 --> 00:02:02,983
If our API is concerned with items,

44
00:02:02,983 --> 00:02:04,734
then an item may be a resource.

45
00:02:04,734 --> 00:02:07,016
If our API is concerned with pianos,

46
00:02:07,016 --> 00:02:09,210
a piano might be resource and so on.

47
00:02:09,210 --> 00:02:11,277
So, our resource is just
a thing that our API

48
00:02:11,277 --> 00:02:15,640
can return and create
and things like that.

49
00:02:15,640 --> 00:02:19,839
Resource are usually mapped
into database tables as well,

50
00:02:19,839 --> 00:02:23,119
so that's something to keep in mind.

51
00:02:23,119 --> 00:02:25,027
Okay, and then we're gonna create our app

52
00:02:25,027 --> 00:02:29,675
just by typing app = flask(__name__),

53
00:02:29,675 --> 00:02:32,208
we know the drill with this one.

54
00:02:32,208 --> 00:02:36,697
And here's the new thing,
flask is going to be our app

55
00:02:36,697 --> 00:02:39,813
and our app is gonna have all these routes

56
00:02:39,813 --> 00:02:41,600
as we saw in the last section

57
00:02:41,600 --> 00:02:42,667
and then we can create new routes

58
00:02:42,667 --> 00:02:45,611
and assign methods to
them and things like that.

59
00:02:45,611 --> 00:02:46,512
And we're gonna create something else

60
00:02:46,512 --> 00:02:49,246
which is gonna be the API.

61
00:02:49,246 --> 00:02:53,808
And the API as you can see is
imported from flask_restful

62
00:02:53,808 --> 00:02:58,196
and that's just going to
allow us to very easily

63
00:02:58,196 --> 00:03:00,299
add these resources to it.

64
00:03:00,299 --> 00:03:01,852
And we're gonna be able to say okay,

65
00:03:01,852 --> 00:03:05,345
for this resource you can get and post,

66
00:03:05,345 --> 00:03:08,597
for this other resource
you can get and delete,

67
00:03:08,597 --> 00:03:11,629
for this other resource you
can get, put, post, and delete

68
00:03:11,629 --> 00:03:12,872
and so on.

69
00:03:12,872 --> 00:03:15,600
So, it's going to be
making things a lot easier,

70
00:03:15,600 --> 00:03:18,577
but obviously there's no
way to explain this without

71
00:03:18,577 --> 00:03:20,513
like a proper sort of example,

72
00:03:20,513 --> 00:03:24,478
so I'm gonna give you a quick example now.

73
00:03:24,478 --> 00:03:26,329
The API works with resources

74
00:03:26,329 --> 00:03:29,412
and every resource has to be a class.

75
00:03:30,377 --> 00:03:34,544
So, for example, a class
student then inherits from

76
00:03:36,957 --> 00:03:39,109
the class resource.

77
00:03:39,109 --> 00:03:40,817
If you've not looked at
an inheritance before

78
00:03:40,817 --> 00:03:43,006
I'd suggest you go back
to the first section

79
00:03:43,006 --> 00:03:45,618
of this course and quickly go
through the object oriented

80
00:03:45,618 --> 00:03:48,272
programming refresher
that's in the first section,

81
00:03:48,272 --> 00:03:50,602
sorry the second section, apologies.

82
00:03:50,602 --> 00:03:53,203
Because that will cover
sort of what inheritance is.

83
00:03:53,203 --> 00:03:56,407
Essentially all we're doing
is we're also retrieving

84
00:03:56,407 --> 00:03:59,527
some stuff from the resource class.

85
00:03:59,527 --> 00:04:02,520
So, our student class
now becomes essentially

86
00:04:02,520 --> 00:04:05,052
a copy of the resource class,

87
00:04:05,052 --> 00:04:07,552
but we can also change things.

88
00:04:08,808 --> 00:04:10,458
So, it is not gonna be an exact copy

89
00:04:10,458 --> 00:04:14,326
it's gonna be a copy with
a couple of things changed.

90
00:04:14,326 --> 00:04:15,802
The first thing that it's gonna change

91
00:04:15,802 --> 00:04:17,302
is the get method.

92
00:04:20,396 --> 00:04:24,146
So, this get method is
going to take in self,

93
00:04:25,174 --> 00:04:28,723
which is just normal,
whenever you create a method

94
00:04:28,723 --> 00:04:30,753
and an object and also a name.

95
00:04:30,753 --> 00:04:34,379
And that's gonna be a name of the student.

96
00:04:34,379 --> 00:04:38,546
Okay, and then we're going
to do something like return

97
00:04:39,858 --> 00:04:42,775
a dictionary where student is name.

98
00:04:43,914 --> 00:04:46,270
So, this is gonna be a really basic API,

99
00:04:46,270 --> 00:04:48,907
it's not really gonna do
anything except return

100
00:04:48,907 --> 00:04:51,179
whatever we give it.

101
00:04:51,179 --> 00:04:53,665
Okay, so now we have our resource

102
00:04:53,665 --> 00:04:56,226
and what we've sent is
that this resource can only

103
00:04:56,226 --> 00:04:58,726
be accessed with a get method.

104
00:05:00,533 --> 00:05:02,734
If we wanted, for example, to allow post,

105
00:05:02,734 --> 00:05:06,582
we would create a post
method here and so on.

106
00:05:06,582 --> 00:05:08,450
But we're gonna look at
that just in the next

107
00:05:08,450 --> 00:05:09,972
couple of videos.

108
00:05:09,972 --> 00:05:12,450
For this video, let's stick to just get

109
00:05:12,450 --> 00:05:16,758
and not actually do anything
just return a dictionary.

110
00:05:16,758 --> 00:05:18,820
Then what we're going to
do is we're going to say

111
00:05:18,820 --> 00:05:22,987
API.add_resource(student) and
this is going to essentially

112
00:05:24,718 --> 00:05:26,301
tell our API, okay.

113
00:05:27,771 --> 00:05:30,287
This resource that we've
created, the student,

114
00:05:30,287 --> 00:05:33,537
now is gonna be accessible via our API,

115
00:05:34,950 --> 00:05:39,501
but as you can see we've never
put any endpoints in here,

116
00:05:39,501 --> 00:05:43,668
any routes, so how are we
gonna access this student?

117
00:05:44,689 --> 00:05:48,261
Do we have to do something
like app.route here?

118
00:05:48,261 --> 00:05:49,094
Student.

119
00:05:50,697 --> 00:05:53,139
Is this the way to do it?

120
00:05:53,139 --> 00:05:56,593
And the answer is fortunately, no.

121
00:05:56,593 --> 00:05:59,257
We have to do something like this.

122
00:05:59,257 --> 00:06:02,436
At the end of the
add_resource, put in a comma

123
00:06:02,436 --> 00:06:06,603
and say/student/string:name.

124
00:06:09,787 --> 00:06:13,370
So, we're going to
access students like so.

125
00:06:20,063 --> 00:06:20,896
Okay, so it's going to be something

126
00:06:20,896 --> 00:06:25,063
like http://127.0.0.1:5000,
just the normal stuff.

127
00:06:26,871 --> 00:06:31,082
And then /student, which
is what we got here,

128
00:06:31,082 --> 00:06:32,936
slash and the name.

129
00:06:32,936 --> 00:06:34,913
And we've looked at this
in the past as well.

130
00:06:34,913 --> 00:06:38,145
This name variable is
going to go straight into

131
00:06:38,145 --> 00:06:39,728
the parameter here.

132
00:06:40,804 --> 00:06:42,827
So, you can imagine that we are doing

133
00:06:42,827 --> 00:06:46,994
@app.route('/student/<string:name>').

134
00:06:48,783 --> 00:06:52,335
These two things here
are essentially the same,

135
00:06:52,335 --> 00:06:55,921
but we no longer have to
do the decorator ourselves,

136
00:06:55,921 --> 00:06:59,790
which kind of save us a
bit of hassle as well.

137
00:06:59,790 --> 00:07:03,457
Okay, ad finally app.run and port=5000,

138
00:07:04,667 --> 00:07:07,163
this is not necessary, that's the default,

139
00:07:07,163 --> 00:07:08,635
port=5000's the default,

140
00:07:08,635 --> 00:07:10,199
but nevertheless I like putting it in

141
00:07:10,199 --> 00:07:12,902
just to be a little bit more explicit.

142
00:07:12,902 --> 00:07:16,266
So, here we have our flask_restful app.

143
00:07:16,266 --> 00:07:18,118
It doesn't really do much,

144
00:07:18,118 --> 00:07:21,914
but we are going to essentially
follow this structure

145
00:07:21,914 --> 00:07:25,114
over the next few videos, in
fact the rest of this section

146
00:07:25,114 --> 00:07:28,443
and we're gonna be creating
a more professional rest API

147
00:07:28,443 --> 00:07:30,592
and we're gonna include
things like error handling

148
00:07:30,592 --> 00:07:32,575
and authentication and things like that.

149
00:07:32,575 --> 00:07:35,486
On a very small level
it's gonna be a small app,

150
00:07:35,486 --> 00:07:39,153
but over the next few sections
we're going to be expanding

151
00:07:39,153 --> 00:07:40,206
in each of those areas

152
00:07:40,206 --> 00:07:43,447
and we're gonna create like
some really cool projects.

153
00:07:43,447 --> 00:07:47,372
So, what I'm gonna do now is
I'm going to launch Postman

154
00:07:47,372 --> 00:07:51,539
that I've got here and what
I'm gonna do is just go to

155
00:07:53,345 --> 00:07:57,903
the new tab here and I'm
going to do a sample request.

156
00:07:57,903 --> 00:08:01,396
I'm not gonna put in any
collection or anything like that.

157
00:08:01,396 --> 00:08:04,604
Of course, first of all, make
sure that your app is running.

158
00:08:04,604 --> 00:08:06,587
So, make sure you are in
the virtual environment

159
00:08:06,587 --> 00:08:10,687
and do Python app.py,
that should get it running

160
00:08:10,687 --> 00:08:12,750
and then go back to Postman

161
00:08:12,750 --> 00:08:16,917
and what we're gonna do
is http://127.0.0.1:5000

162
00:08:20,345 --> 00:08:23,542
and then student and then Rolf,

163
00:08:23,542 --> 00:08:25,709
that's the student's name.

164
00:08:26,900 --> 00:08:29,472
And as you can see we get back the student

165
00:08:29,472 --> 00:08:31,841
in the form of a dictionary
just as we said here

166
00:08:31,841 --> 00:08:33,341
in our get method.

167
00:08:35,255 --> 00:08:37,735
We're just returning student is the name

168
00:08:37,735 --> 00:08:40,981
and then Postman makes sure
that you're doing a get request

169
00:08:40,981 --> 00:08:42,002
and you're requesting this URL

170
00:08:42,002 --> 00:08:44,170
and you should get back this thing here.

171
00:08:44,170 --> 00:08:48,340
Of course, if you change
this for Chris, for example,

172
00:08:48,340 --> 00:08:51,039
that will then return Chris.

173
00:08:51,039 --> 00:08:54,874
If you change the method
to be such as a post,

174
00:08:54,874 --> 00:08:58,754
what you'll get is the usual
message that the method

175
00:08:58,754 --> 00:09:01,315
is not allowed for the requested URL

176
00:09:01,315 --> 00:09:03,778
because this doesn't accept post,

177
00:09:03,778 --> 00:09:05,445
it only accepts get.

178
00:09:07,191 --> 00:09:09,457
Now, okay, and similarly
if you access something

179
00:09:09,457 --> 00:09:14,046
incorrect and you'll just
get the usual 404 not found,

180
00:09:14,046 --> 00:09:17,087
that's the error code for not found.

181
00:09:17,087 --> 00:09:19,943
And so make sure that you
are accessing the right URL.

182
00:09:19,943 --> 00:09:22,413
And that's really
everything for this video.

183
00:09:22,413 --> 00:09:25,746
We've created a simple flask_restful app

184
00:09:27,471 --> 00:09:30,956
that nevertheless gives us a
lot of info regarding how these

185
00:09:30,956 --> 00:09:35,174
app are structured, these
APIs is a matter of having

186
00:09:35,174 --> 00:09:38,327
the API, defining our resource,

187
00:09:38,327 --> 00:09:42,049
which in this case we've only
got one, which is the student,

188
00:09:42,049 --> 00:09:46,512
defining the methods that this
resource is going to accept,

189
00:09:46,512 --> 00:09:48,456
in this case it's only get,

190
00:09:48,456 --> 00:09:53,425
but we can have others like
post, delete, put and so on.

191
00:09:53,425 --> 00:09:55,586
And then deciding what
that method is going to do

192
00:09:55,586 --> 00:09:57,523
when the endpoint is called

193
00:09:57,523 --> 00:09:59,799
and then finally added
the resource in here

194
00:09:59,799 --> 00:10:02,547
and determine how it's going
to be accessed as well,

195
00:10:02,547 --> 00:10:05,263
in this case /student/<string:name>'

196
00:10:05,263 --> 00:10:08,122
and the name parameter
always goes to the methods

197
00:10:08,122 --> 00:10:10,360
parameter as well.

198
00:10:10,360 --> 00:10:13,680
Finally, run the app and
try it out in Postman.

199
00:10:13,680 --> 00:10:15,449
I'd recommend you do this.

200
00:10:15,449 --> 00:10:17,163
Write this code, try it out in Postman,

201
00:10:17,163 --> 00:10:18,810
make sure everything works,

202
00:10:18,810 --> 00:10:21,334
and as we go along in this section,

203
00:10:21,334 --> 00:10:24,106
do code along with me as well.

204
00:10:24,106 --> 00:10:26,290
I've created the videos
in such a way that you can

205
00:10:26,290 --> 00:10:29,708
always code along and try
things out as we move along,

206
00:10:29,708 --> 00:10:31,021
just for your benefit.

207
00:10:31,021 --> 00:10:33,095
So, it would be silly to
waste that opportunity

208
00:10:33,095 --> 00:10:35,012
that you've got now.

209
00:10:35,012 --> 00:10:37,029
Without further ado, thanks for joining in

210
00:10:37,029 --> 00:10:38,856
and I'll see you in the very next video.

