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

2
00:00:02,619 --> 00:00:05,259
In this video we're going
to be creating new stores,

3
00:00:05,259 --> 00:00:06,426
using our API.

4
00:00:07,438 --> 00:00:10,596
However, we're not
going to be able to test

5
00:00:10,596 --> 00:00:12,705
the creation of stores,

6
00:00:12,705 --> 00:00:15,198
using our JavaScript application,

7
00:00:15,198 --> 00:00:17,539
because, that gets
slightly more complicated,

8
00:00:17,539 --> 00:00:19,740
and this is not a JavaScript course.

9
00:00:19,740 --> 00:00:20,584
However,

10
00:00:20,584 --> 00:00:23,280
there is another way of testing the API

11
00:00:23,280 --> 00:00:25,335
and we are gonna do that

12
00:00:25,335 --> 00:00:27,060
the right way really.

13
00:00:27,060 --> 00:00:29,489
We normally wouldn't test the API

14
00:00:29,489 --> 00:00:31,037
using JavaScript,

15
00:00:31,037 --> 00:00:34,572
we would test it using a
specific API testing tool,

16
00:00:34,572 --> 00:00:36,993
but we will still look at using the API

17
00:00:36,993 --> 00:00:38,552
from within JavaScript

18
00:00:38,552 --> 00:00:40,860
and then we will look at
testing the API afterwards.

19
00:00:40,860 --> 00:00:42,383
Anyway, for this video,

20
00:00:42,383 --> 00:00:45,732
we're going to create a new store.

21
00:00:45,732 --> 00:00:46,928
And the first thing we have to do,

22
00:00:46,928 --> 00:00:50,845
is to be able to access the
data that is coming back

23
00:00:50,845 --> 00:00:53,594
to us from the request.

24
00:00:53,594 --> 00:00:55,074
So the browser,

25
00:00:55,074 --> 00:00:56,596
or the API testing tool,

26
00:00:56,596 --> 00:00:58,721
is gonna send us some data,

27
00:00:58,721 --> 00:01:00,496
the name of the store,

28
00:01:00,496 --> 00:01:03,748
and we have to be able
to access that data.

29
00:01:03,748 --> 00:01:05,307
So the first thing we have to do

30
00:01:05,307 --> 00:01:07,103
is go up to our first line of code,

31
00:01:07,103 --> 00:01:09,375
and we're going to import something else.

32
00:01:09,375 --> 00:01:10,850
We're gonna import flask,

33
00:01:10,850 --> 00:01:11,967
JSONify,

34
00:01:11,967 --> 00:01:14,156
and also request.

35
00:01:14,156 --> 00:01:16,630
Request without an s at the end.

36
00:01:16,630 --> 00:01:19,612
There's also a thing called requests.

37
00:01:19,612 --> 00:01:21,018
And that's a different package.

38
00:01:21,018 --> 00:01:24,204
So from flask we want to import request.

39
00:01:24,204 --> 00:01:26,270
And then in the create store,

40
00:01:26,270 --> 00:01:28,057
what we wanna do is say

41
00:01:28,057 --> 00:01:31,890
request data is equal
to request dot get JSON.

42
00:01:35,882 --> 00:01:39,573
This request is the request that was made

43
00:01:39,573 --> 00:01:41,374
to this endpoint.

44
00:01:41,374 --> 00:01:45,103
So when the browser sends us the request

45
00:01:45,103 --> 00:01:47,600
to create a new store,

46
00:01:47,600 --> 00:01:50,367
this request is that one.

47
00:01:50,367 --> 00:01:52,419
And the browser will also send us some

48
00:01:52,419 --> 00:01:53,862
JSON data.

49
00:01:53,862 --> 00:01:56,463
Which is the name of the store.

50
00:01:56,463 --> 00:02:01,182
So this is going to allow
us to get that data back.

51
00:02:01,182 --> 00:02:03,015
Then we're gonna create a new store,

52
00:02:03,015 --> 00:02:04,677
which is gonna be a dictionary

53
00:02:04,677 --> 00:02:05,839
with a name,

54
00:02:05,839 --> 00:02:09,253
where the name is request data name,

55
00:02:09,253 --> 00:02:11,926
because that's gonna be the JSON.

56
00:02:11,926 --> 00:02:14,322
When we do get JSON,

57
00:02:14,322 --> 00:02:17,422
and this method already
converts the JSON string

58
00:02:17,422 --> 00:02:19,429
into a python dictionary,

59
00:02:19,429 --> 00:02:22,448
so we can access it like that.

60
00:02:22,448 --> 00:02:24,869
And also it's going to have a set of items

61
00:02:24,869 --> 00:02:28,019
which is going to be an empty list.

62
00:02:28,019 --> 00:02:30,283
We're then going to do
stores that dot append,

63
00:02:30,283 --> 00:02:32,169
and append our new store.

64
00:02:32,169 --> 00:02:34,073
And finally we're going to return

65
00:02:34,073 --> 00:02:35,742
the new store that we've created.

66
00:02:35,742 --> 00:02:39,692
Just so the browser can understand that we

67
00:02:39,692 --> 00:02:42,252
have indeed created this store.

68
00:02:42,252 --> 00:02:45,335
So how would we return the new store.

69
00:02:50,004 --> 00:02:51,376
I'm sure you got it.

70
00:02:51,376 --> 00:02:53,163
Return, JSONify,

71
00:02:53,163 --> 00:02:54,964
of new store.

72
00:02:54,964 --> 00:02:56,019
It's important to include JSONify,

73
00:02:56,019 --> 00:02:57,029
because if not,

74
00:02:57,029 --> 00:02:59,063
we will try to return a dictionary

75
00:02:59,063 --> 00:03:03,230
and that will fail because
we have to return a string.

76
00:03:04,586 --> 00:03:08,063
Okay, so this is how we
would create a new store.

77
00:03:08,063 --> 00:03:09,312
And as I said,

78
00:03:09,312 --> 00:03:10,845
over the next few videos
we're going to be testing

79
00:03:10,845 --> 00:03:11,915
this out with JavaScript,

80
00:03:11,915 --> 00:03:13,632
but we will not be able to test the

81
00:03:13,632 --> 00:03:15,368
creation of stores with JavaScript,

82
00:03:15,368 --> 00:03:16,545
that gets too complicated,

83
00:03:16,545 --> 00:03:18,801
so we're going to test
it with a different tool,

84
00:03:18,801 --> 00:03:19,899
and that tool is what we use

85
00:03:19,899 --> 00:03:22,159
professionally very very often.

86
00:03:22,159 --> 00:03:25,691
So it's good for you
to learn that as well.

87
00:03:25,691 --> 00:03:28,453
To retrieve a specific store,

88
00:03:28,453 --> 00:03:32,180
well, all we wanna do
is go over the stores,

89
00:03:32,180 --> 00:03:34,879
finding the one that matches this name,

90
00:03:34,879 --> 00:03:36,806
and return that one.

91
00:03:36,806 --> 00:03:39,089
So I will write a wee comment here,

92
00:03:39,089 --> 00:03:40,756
iterate over stores,

93
00:03:42,559 --> 00:03:44,367
if the store name matches,

94
00:03:44,367 --> 00:03:46,256
return that one.

95
00:03:46,256 --> 00:03:49,673
If none matches, return an error message.

96
00:03:51,809 --> 00:03:53,010
And I think you can pause
the video at this point

97
00:03:53,010 --> 00:03:54,792
and give this a go.

98
00:03:54,792 --> 00:03:56,659
Because you know how to iterate,

99
00:03:56,659 --> 00:03:58,904
you know how to check
using an if statement,

100
00:03:58,904 --> 00:04:01,195
and you know how to return a store.

101
00:04:01,195 --> 00:04:03,737
So go ahead and do that,

102
00:04:03,737 --> 00:04:07,904
I would definitely recommend
that you try that now.

103
00:04:10,490 --> 00:04:11,961
Hopefully you gave it a go,

104
00:04:11,961 --> 00:04:14,768
and what I would do is
to iterate over stores,

105
00:04:14,768 --> 00:04:18,468
for store in stores, stores
remember is our variable,

106
00:04:18,468 --> 00:04:20,423
and then I would say,

107
00:04:20,423 --> 00:04:23,340
if the store name is equal to name,

108
00:04:24,853 --> 00:04:28,027
which is the one that
we're receiving here,

109
00:04:28,027 --> 00:04:29,451
then return the store,

110
00:04:29,451 --> 00:04:31,451
return JSONify of store.

111
00:04:32,804 --> 00:04:35,278
Remember, store is a dictionary,

112
00:04:35,278 --> 00:04:38,638
so we can just return that store directly,

113
00:04:38,638 --> 00:04:40,451
and if no store matches,

114
00:04:40,451 --> 00:04:42,133
we want to return some
sort of error message,

115
00:04:42,133 --> 00:04:44,883
such as message, store not found.

116
00:04:47,257 --> 00:04:50,061
That's just to tell
whoever's calling the API,

117
00:04:50,061 --> 00:04:54,147
that there was indeed some sort of error.

118
00:04:54,147 --> 00:04:57,158
Okay, now you can do the same thing

119
00:04:57,158 --> 00:05:01,314
for the get store slash name slash item.

120
00:05:01,314 --> 00:05:04,239
I'm sure you can go ahead
and implement this now

121
00:05:04,239 --> 00:05:08,406
yourself, now that you know
how the other one works.

122
00:05:12,425 --> 00:05:13,480
Hopefully you managed,

123
00:05:13,480 --> 00:05:15,666
the way I would do it is
actually very similar,

124
00:05:15,666 --> 00:05:20,182
for store in stores, if
the stores name matches,

125
00:05:20,182 --> 00:05:24,725
then we're going to return
the items in that store,

126
00:05:24,725 --> 00:05:28,044
so we're going to return
a JSONify of items.

127
00:05:28,044 --> 00:05:30,445
Where that's the stores items.

128
00:05:30,445 --> 00:05:33,187
And also if we don't find anything,

129
00:05:33,187 --> 00:05:37,354
we're going to return the
appropriate error message.

130
00:05:40,154 --> 00:05:40,987
Like so.

131
00:05:43,110 --> 00:05:44,762
Finally, the most challenging in point,

132
00:05:44,762 --> 00:05:46,859
is the create item.

133
00:05:46,859 --> 00:05:48,929
I'm sure that you can do this as well.

134
00:05:48,929 --> 00:05:50,043
If you give it a bit of thought

135
00:05:50,043 --> 00:05:53,447
and you look at how we've
created a store above,

136
00:05:53,447 --> 00:05:55,035
in a create store method,

137
00:05:55,035 --> 00:05:56,234
you can do a very similar thing

138
00:05:56,234 --> 00:05:57,734
to create an item.

139
00:05:58,658 --> 00:06:00,181
Once again I would
recommend pausing the video

140
00:06:00,181 --> 00:06:01,322
and giving this a go.

141
00:06:01,322 --> 00:06:02,667
But then I always recommend that,

142
00:06:02,667 --> 00:06:05,718
so you may be ignoring
this recommendation by now,

143
00:06:05,718 --> 00:06:06,935
hopefully not.

144
00:06:06,935 --> 00:06:08,865
So pause the video and give it a go

145
00:06:08,865 --> 00:06:11,032
and then we'll do it here.

146
00:06:14,639 --> 00:06:16,233
Hopefully you were successful,

147
00:06:16,233 --> 00:06:17,536
and what I would do is,

148
00:06:17,536 --> 00:06:18,369
once again,

149
00:06:18,369 --> 00:06:20,001
for store in stores,

150
00:06:20,001 --> 00:06:23,653
if the store name matches our name,

151
00:06:23,653 --> 00:06:26,279
then we're going to create a new item,

152
00:06:26,279 --> 00:06:28,752
that item is going to be one with a name

153
00:06:28,752 --> 00:06:32,335
which is going to be
the request data name,

154
00:06:35,493 --> 00:06:38,592
and with a price which is
going to be the request.

155
00:06:38,592 --> 00:06:39,670
Oh, sorry,

156
00:06:39,670 --> 00:06:42,404
price like that, and request data.

157
00:06:42,404 --> 00:06:43,276
Price.

158
00:06:43,276 --> 00:06:45,222
Naturally, you may have already
realised the request data

159
00:06:45,222 --> 00:06:47,445
doesn't exist in this method,

160
00:06:47,445 --> 00:06:49,064
so we have to create it.

161
00:06:49,064 --> 00:06:50,549
Request data,

162
00:06:50,549 --> 00:06:52,799
equal request dot get JSON.

163
00:06:54,589 --> 00:06:55,983
Like so.

164
00:06:55,983 --> 00:06:58,378
So this is our new item there,

165
00:06:58,378 --> 00:07:00,647
and what we wanna do is append it,

166
00:07:00,647 --> 00:07:03,730
so store, items, dot append new item.

167
00:07:05,179 --> 00:07:07,172
And then at the end,

168
00:07:07,172 --> 00:07:08,005
new item,

169
00:07:08,878 --> 00:07:11,249
at the end we can return a JSONify,

170
00:07:11,249 --> 00:07:12,936
of the store,

171
00:07:12,936 --> 00:07:14,998
or if you prefer, you can return JSONify

172
00:07:14,998 --> 00:07:17,595
if new item that may be also an option.

173
00:07:17,595 --> 00:07:19,560
It's totally up to you
what you want to return

174
00:07:19,560 --> 00:07:21,991
or up to your API rather.

175
00:07:21,991 --> 00:07:22,887
Also at the end,

176
00:07:22,887 --> 00:07:25,637
we wanna return an error message,

177
00:07:28,997 --> 00:07:31,214
if we didn't find the store.

178
00:07:31,214 --> 00:07:32,823
Something to keep in mind

179
00:07:32,823 --> 00:07:36,372
is that if we return an item here,

180
00:07:36,372 --> 00:07:38,097
that terminates the method

181
00:07:38,097 --> 00:07:40,175
and we don't return this message.

182
00:07:40,175 --> 00:07:41,859
We will only return this message

183
00:07:41,859 --> 00:07:43,891
if we go through every store,

184
00:07:43,891 --> 00:07:46,308
and we never return anything.

185
00:07:47,456 --> 00:07:49,835
So if we start on our first store,

186
00:07:49,835 --> 00:07:52,158
and the name does not match,

187
00:07:52,158 --> 00:07:54,190
we will just skip all of this code,

188
00:07:54,190 --> 00:07:56,356
and we will move onto the next store.

189
00:07:56,356 --> 00:07:57,716
And if the name doesn't match,

190
00:07:57,716 --> 00:07:58,892
we will skip all of that code

191
00:07:58,892 --> 00:07:59,958
and we will go on to the next one,

192
00:07:59,958 --> 00:08:02,061
and eventually if none of them match,

193
00:08:02,061 --> 00:08:05,973
we will never have ran this code here,

194
00:08:05,973 --> 00:08:08,258
so we will just exit the loop,

195
00:08:08,258 --> 00:08:11,572
and return this error message at the end.

196
00:08:11,572 --> 00:08:12,405
Okay.

197
00:08:15,949 --> 00:08:16,782
Perfect.

198
00:08:16,782 --> 00:08:18,739
So these are our end points,

199
00:08:18,739 --> 00:08:20,901
now in the next video,

200
00:08:20,901 --> 00:08:23,718
I've got an HTML file for you to download,

201
00:08:23,718 --> 00:08:25,949
that has some boiler plate code,

202
00:08:25,949 --> 00:08:27,356
and then we're gonna use that

203
00:08:27,356 --> 00:08:29,175
to kinda test this out a bit and

204
00:08:29,175 --> 00:08:31,092
look at how you can run

205
00:08:32,027 --> 00:08:34,148
some JavaScript code in this API,

206
00:08:34,148 --> 00:08:36,122
and after that we're going to,

207
00:08:36,122 --> 00:08:39,167
properly test this API
using a professional tool.

208
00:08:39,167 --> 00:08:40,234
Which is free as well.

209
00:08:40,234 --> 00:08:42,724
So don't worry about paying anything.

210
00:08:42,724 --> 00:08:43,947
So without further ado,

211
00:08:43,947 --> 00:08:48,114
I'll see you in the next
video, with some JavaScript.

