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

2
00:00:02,520 --> 00:00:03,759
In this video, we're going to continue

3
00:00:03,759 --> 00:00:07,760
with our project, or in fact,
actually start our project

4
00:00:07,760 --> 00:00:10,832
and make sure that we
match the API endpoints

5
00:00:10,832 --> 00:00:13,621
that we decided in the last video.

6
00:00:13,621 --> 00:00:16,519
So, we had a couple of endpoints,

7
00:00:16,519 --> 00:00:21,153
some related to item and
some related to items,

8
00:00:21,153 --> 00:00:22,329
and in this video we're going to be

9
00:00:22,329 --> 00:00:24,579
creating the item resource.

10
00:00:26,901 --> 00:00:29,759
I mentioned earlier that
these resources normally

11
00:00:29,759 --> 00:00:32,200
are stored in a database.

12
00:00:32,200 --> 00:00:35,362
For this section, we're not
going to use a database.

13
00:00:35,362 --> 00:00:37,452
We're going to introduce
that in the next section.

14
00:00:37,452 --> 00:00:39,397
In this section, we're going to introduce

15
00:00:39,397 --> 00:00:42,588
flask restful and authentication,

16
00:00:42,588 --> 00:00:44,300
so in the next section
we'll look at databases.

17
00:00:44,300 --> 00:00:47,256
For now, we're going to
use an in-memory database,

18
00:00:47,256 --> 00:00:49,826
which is just a Python list,

19
00:00:49,826 --> 00:00:53,993
and I'm going to just create
a sort of list called items.

20
00:00:54,930 --> 00:00:59,069
And this is going to contain
a dictionary for each item,

21
00:00:59,069 --> 00:01:02,020
just as we did in the last section.

22
00:01:02,020 --> 00:01:04,853
Then I'm going to rename
my resource to be item,

23
00:01:04,853 --> 00:01:06,925
just so it makes a bit more sense,

24
00:01:06,925 --> 00:01:10,063
and also remember to rename
it down here as well,

25
00:01:10,063 --> 00:01:13,154
and also to rename it there.

26
00:01:13,154 --> 00:01:14,498
Now I'm going to delete this comment

27
00:01:14,498 --> 00:01:16,658
because it no longer makes sense,

28
00:01:16,658 --> 00:01:18,288
however, we're still gonna access items

29
00:01:18,288 --> 00:01:22,455
the very same way, only it's
going to be /item/itemname.

30
00:01:24,077 --> 00:01:27,160
Now, we're going to need to get items

31
00:01:28,894 --> 00:01:32,077
and also to be able to create items.

32
00:01:32,077 --> 00:01:35,639
We're going to start with
those two capabilities

33
00:01:35,639 --> 00:01:38,209
and we're going to add the
ability to modify items

34
00:01:38,209 --> 00:01:41,448
and delete items just a bit later.

35
00:01:41,448 --> 00:01:44,907
So the get method has some code there,

36
00:01:44,907 --> 00:01:46,824
and we're gonna change that in a moment,

37
00:01:46,824 --> 00:01:48,617
and the post method has to have

38
00:01:48,617 --> 00:01:52,082
exactly the same method signature,

39
00:01:52,082 --> 00:01:55,082
that is, the same set of parameters.

40
00:01:56,539 --> 00:02:00,706
That is because when we
access /item/itemname,

41
00:02:01,864 --> 00:02:05,560
that name is going to
come to be name here,

42
00:02:05,560 --> 00:02:07,561
so even though this request

43
00:02:07,561 --> 00:02:09,905
is going to have a JSON payload,

44
00:02:09,905 --> 00:02:12,201
some data sent in through JSON,

45
00:02:12,201 --> 00:02:16,368
and we could send the name
on that JSON file as well,

46
00:02:17,601 --> 00:02:19,396
we are not going to do that.

47
00:02:19,396 --> 00:02:21,001
We are going to instead receive it

48
00:02:21,001 --> 00:02:24,084
through the URL so all of
these things can be accessed

49
00:02:24,084 --> 00:02:28,167
at the same endpoint just
changing the http verb.

50
00:02:29,091 --> 00:02:32,213
That makes things just a lot easier.

51
00:02:32,213 --> 00:02:35,775
Okay, in order to create
items, it's very simple.

52
00:02:35,775 --> 00:02:39,183
We're going to start without
even looking at the JSON.

53
00:02:39,183 --> 00:02:44,047
We're going to start by saying
item is equal to a name,

54
00:02:44,047 --> 00:02:46,662
which is the name which is a parameter,

55
00:02:46,662 --> 00:02:50,829
and a price, which I'm going
to hardcode to 12, say.

56
00:02:53,086 --> 00:02:57,053
Now, how do we add this
item to our items list?

57
00:02:57,053 --> 00:02:59,803
I'm sure you know how to do that.

58
00:03:03,099 --> 00:03:04,195
Hopefully you got it right.

59
00:03:04,195 --> 00:03:08,013
The way to do that is to do items.append

60
00:03:08,013 --> 00:03:09,924
and then the item there,
and what that'll do,

61
00:03:09,924 --> 00:03:12,452
is that'll put this dictionary
as one of the elements

62
00:03:12,452 --> 00:03:15,084
in this list at the very end of the list.

63
00:03:15,084 --> 00:03:17,171
And when we do that, again,
it'll put another element

64
00:03:17,171 --> 00:03:19,004
at the end, and so on.

65
00:03:20,077 --> 00:03:23,494
Finally, we also want to tell the client,

66
00:03:24,784 --> 00:03:27,341
in this case, Postman, but
it could be a mobile app

67
00:03:27,341 --> 00:03:31,976
or web app, that we have
processed this addition,

68
00:03:31,976 --> 00:03:36,774
that we have created this item
and added it to our database,

69
00:03:36,774 --> 00:03:38,605
in this case, our list.

70
00:03:38,605 --> 00:03:41,294
In order to do that, we're
going to just return item

71
00:03:41,294 --> 00:03:45,461
so that the application
knows that this has happened.

72
00:03:47,580 --> 00:03:50,848
Okay, now, I would like to test this out,

73
00:03:50,848 --> 00:03:53,663
see if it works, but unfortunately,
it's not really possible

74
00:03:53,663 --> 00:03:57,183
without implementing the get method,

75
00:03:57,183 --> 00:03:59,567
so let's do that as well.

76
00:03:59,567 --> 00:04:03,852
Get is going to look in this
list and retrieve an item

77
00:04:03,852 --> 00:04:07,521
that matches the name
that has been requested.

78
00:04:07,521 --> 00:04:09,114
I think that with that explanation,

79
00:04:09,114 --> 00:04:11,432
you should be able to do that already.

80
00:04:11,432 --> 00:04:14,846
All it has to do is iterate over this list

81
00:04:14,846 --> 00:04:17,513
and return the appropriate item.

82
00:04:21,195 --> 00:04:22,319
I'm sure you got that.

83
00:04:22,319 --> 00:04:25,924
What to do is say for item in items:

84
00:04:25,924 --> 00:04:29,424
if item name equals the name, return item.

85
00:04:31,589 --> 00:04:34,056
Remember, item is going
to be a dictionary,

86
00:04:34,056 --> 00:04:35,891
so that's just gonna get returned.

87
00:04:35,891 --> 00:04:38,129
And I forgot to mention,
we'd no longer need

88
00:04:38,129 --> 00:04:41,637
to do JSONify when
talking with flask restful

89
00:04:41,637 --> 00:04:43,718
because flask restful does it for us,

90
00:04:43,718 --> 00:04:46,220
so we can just return dictionaries.

91
00:04:46,220 --> 00:04:49,036
That probably evident by now,

92
00:04:49,036 --> 00:04:52,469
but nevertheless, thought
I should mention that.

93
00:04:52,469 --> 00:04:56,472
Okay, so now we've got
two working endpoints,

94
00:04:56,472 --> 00:04:58,471
and what we have to do is just try it out

95
00:04:58,471 --> 00:05:00,047
to make sure it works, really.

96
00:05:00,047 --> 00:05:02,846
So let's open up a terminal
that we've got here

97
00:05:02,846 --> 00:05:07,013
and I am going to go and
cancel that and run it again.

98
00:05:09,457 --> 00:05:13,944
Then, let's go over to,
not this one, but Postman,

99
00:05:13,944 --> 00:05:16,106
and we're going to use the endpoints

100
00:05:16,106 --> 00:05:18,318
that we've already created.

101
00:05:18,318 --> 00:05:21,988
So the first thing to do
is to create a new item,

102
00:05:21,988 --> 00:05:23,830
and this item has a body,

103
00:05:23,830 --> 00:05:26,349
but we're not using that in
our code, and that's fine.

104
00:05:26,349 --> 00:05:29,600
Even though it has a body,
we don't have to use it.

105
00:05:29,600 --> 00:05:31,400
So we're going to leave the
body and the content type

106
00:05:31,400 --> 00:05:33,758
as it is, and with the knowledge

107
00:05:33,758 --> 00:05:35,931
that it's not getting used,

108
00:05:35,931 --> 00:05:39,787
and then we are going to create a chair.

109
00:05:39,787 --> 00:05:41,037
So, let's send.

110
00:05:42,445 --> 00:05:46,047
Notice that what we get
back is a dictionary

111
00:05:46,047 --> 00:05:48,096
that contains the price and the name.

112
00:05:48,096 --> 00:05:49,872
The price is hardcoded to 12

113
00:05:49,872 --> 00:05:53,832
and it's coming back as an
integer as opposed to a float,

114
00:05:53,832 --> 00:05:55,187
and that's fine in this case.

115
00:05:55,187 --> 00:05:57,263
It doesn't really matter.

116
00:05:57,263 --> 00:05:59,976
And the name is a chair there.

117
00:05:59,976 --> 00:06:04,105
Now, this should also have
added the item, the chair,

118
00:06:04,105 --> 00:06:05,938
to our items database.

119
00:06:06,922 --> 00:06:09,839
So, let's go and retrieve the chair

120
00:06:11,830 --> 00:06:15,149
and see if that does come out.

121
00:06:15,149 --> 00:06:17,538
And as you can see, now
we are getting the chair,

122
00:06:17,538 --> 00:06:21,112
and it's coming out with
the price 12, as well.

123
00:06:21,112 --> 00:06:22,869
So what does this tell us?

124
00:06:22,869 --> 00:06:27,111
It tells us that what we've
implemented here is correct.

125
00:06:27,111 --> 00:06:31,360
So therefore, we are able
now to move forwards.

126
00:06:31,360 --> 00:06:33,922
There's a couple of things
that I would like to improve

127
00:06:33,922 --> 00:06:37,749
just as a matter of making
things slightly better,

128
00:06:37,749 --> 00:06:40,985
which is what happens if
we don't find the item

129
00:06:40,985 --> 00:06:42,735
with a specific name.

130
00:06:44,422 --> 00:06:46,716
Well, here's what would happen right now

131
00:06:46,716 --> 00:06:49,216
if we do something like piano.

132
00:06:52,363 --> 00:06:53,726
Null.

133
00:06:53,726 --> 00:06:56,915
Now, for those of you that
remember what I mentioned earlier

134
00:06:56,915 --> 00:07:01,109
about JSON always having
to be a dictionary,

135
00:07:01,109 --> 00:07:03,688
this is not valid JSON.

136
00:07:03,688 --> 00:07:07,136
Postman is accepting it
because Postman is really good,

137
00:07:07,136 --> 00:07:09,702
but there are many other libraries

138
00:07:09,702 --> 00:07:13,291
like Angular JS, which
may not accept this.

139
00:07:13,291 --> 00:07:15,174
So, instead of returning just null,

140
00:07:15,174 --> 00:07:16,490
which is what this method returns,

141
00:07:16,490 --> 00:07:21,269
as we know, all Python methods
return none by default,

142
00:07:21,269 --> 00:07:23,275
what we have to do is
change that return none

143
00:07:23,275 --> 00:07:26,108
to be something like item is none.

144
00:07:28,880 --> 00:07:32,452
Now, this is essentially
the same thing, but now,

145
00:07:32,452 --> 00:07:35,285
and we have to restart, of course,

146
00:07:36,752 --> 00:07:39,880
but now when we do this, we get

147
00:07:39,880 --> 00:07:43,941
a valid JSON representation of this thing.

148
00:07:43,941 --> 00:07:48,135
Another thing, the status code is 200 OK,

149
00:07:48,135 --> 00:07:50,022
but evidently things are not okay,

150
00:07:50,022 --> 00:07:52,963
because we were not able to find the item.

151
00:07:52,963 --> 00:07:55,308
So what to return in this case,

152
00:07:55,308 --> 00:07:56,697
I'm sure you know it if you remember it

153
00:07:56,697 --> 00:07:59,197
from one of the presentations.

154
00:08:01,437 --> 00:08:02,667
That's right.

155
00:08:02,667 --> 00:08:05,667
The error code for not found is 404.

156
00:08:06,616 --> 00:08:09,023
This is a common interview
question as well.

157
00:08:09,023 --> 00:08:13,016
What's the most popular http status code?

158
00:08:13,016 --> 00:08:16,852
And the most popular
is not 404, it is 200.

159
00:08:16,852 --> 00:08:19,840
So make sure to not fall into that trap.

160
00:08:19,840 --> 00:08:22,979
Let's restart the app, and press again,

161
00:08:22,979 --> 00:08:26,774
and now we get item is
null, 404 not found.

162
00:08:26,774 --> 00:08:28,031
Awesome.

163
00:08:28,031 --> 00:08:31,151
I think our get endpoint is now working.

164
00:08:31,151 --> 00:08:34,859
Something else to
remember is that creating

165
00:08:34,859 --> 00:08:38,144
also has its own http status code,

166
00:08:38,144 --> 00:08:41,561
and maybe you remember which one that is.

167
00:08:43,439 --> 00:08:44,342
Hopefully you did.

168
00:08:44,342 --> 00:08:46,686
It's very similar to 200 OK.

169
00:08:46,686 --> 00:08:50,950
200 OK is for when the server
just kind of returns some data

170
00:08:50,950 --> 00:08:52,095
and says everything's okay.

171
00:08:52,095 --> 00:08:54,247
I've given you what you wanted.

172
00:08:54,247 --> 00:08:55,830
201 is for created.

173
00:08:56,684 --> 00:09:01,282
So when we go ahead and
restart the app again,

174
00:09:01,282 --> 00:09:05,039
and then go back to Postman
and create the chair,

175
00:09:05,039 --> 00:09:07,847
now we gat back 201 created,

176
00:09:07,847 --> 00:09:09,619
which means the object has been created

177
00:09:09,619 --> 00:09:13,097
and it has been added to the database.

178
00:09:13,097 --> 00:09:16,080
Notice there is also a
very similar status code

179
00:09:16,080 --> 00:09:19,060
called 202, which is accepted,

180
00:09:19,060 --> 00:09:23,998
and the accepted code is when
you are delaying the creation.

181
00:09:23,998 --> 00:09:28,985
For example, if creating the
object takes a long time,

182
00:09:28,985 --> 00:09:31,835
you may say, "I'm gonna
create this object,

183
00:09:31,835 --> 00:09:35,089
"return 202, and the
object that's created then

184
00:09:35,089 --> 00:09:37,269
"after five or 10 minutes."

185
00:09:37,269 --> 00:09:39,974
The client doesn't have to
wait five or 10 minutes,

186
00:09:39,974 --> 00:09:43,797
but it knows that you have
accepted the creation of that.

187
00:09:43,797 --> 00:09:47,964
It may then fail, but that's
out with the client's control.

188
00:09:49,057 --> 00:09:51,630
So, hopefully all of that makes sense.

189
00:09:51,630 --> 00:09:53,970
Using the right status
codes is very important

190
00:09:53,970 --> 00:09:57,756
because it is a very quick way of clients,

191
00:09:57,756 --> 00:10:00,430
like web applications
or mobile applications,

192
00:10:00,430 --> 00:10:03,518
to check whether things went wrong or not,

193
00:10:03,518 --> 00:10:04,351
and in this case,

194
00:10:04,351 --> 00:10:07,090
if we get a 404, we don't
even need to check the payload

195
00:10:07,090 --> 00:10:08,638
because we know nothing's coming back

196
00:10:08,638 --> 00:10:10,960
that we're really interested in.

197
00:10:10,960 --> 00:10:13,329
If 201 comes back, then we
should check the payload

198
00:10:13,329 --> 00:10:16,413
to see what has been created.

199
00:10:16,413 --> 00:10:17,818
That's everything for this video.

200
00:10:17,818 --> 00:10:18,841
In the next one, we're going to be

201
00:10:18,841 --> 00:10:20,949
implementing the item list,

202
00:10:20,949 --> 00:10:23,364
which I'm sure you can do that already,

203
00:10:23,364 --> 00:10:26,022
just with the knowledge
that we've got now.

204
00:10:26,022 --> 00:10:26,855
I'll give you a hint.

205
00:10:26,855 --> 00:10:28,887
We're going to have to
create a new resource

206
00:10:28,887 --> 00:10:31,852
so you can give that a
go before the next video.

207
00:10:31,852 --> 00:10:33,190
I'll see you there.

