1
00:00:00,691 --> 00:00:02,846
- [Narrator] Hi, and
welcome back to the course.

2
00:00:02,846 --> 00:00:05,898
Remember our simple Flask application?

3
00:00:05,898 --> 00:00:09,173
Now, we're going to extend
that to show the endpoints

4
00:00:09,173 --> 00:00:12,637
that we looked at in the
very last presentation.

5
00:00:12,637 --> 00:00:16,804
We're going to simulate an
online store using Flask.

6
00:00:18,288 --> 00:00:21,068
There were a couple of
things that we discussed.

7
00:00:21,068 --> 00:00:23,901
Net post was used to receive data,

8
00:00:26,547 --> 00:00:30,714
and the get was used to,
essentially, send data back only.

9
00:00:34,690 --> 00:00:37,248
Remember, from the browser's perspective,

10
00:00:37,248 --> 00:00:38,503
this is the opposite.

11
00:00:38,503 --> 00:00:42,003
The browser will use post to send us data.

12
00:00:43,036 --> 00:00:45,913
And it will use get to receive data.

13
00:00:45,913 --> 00:00:48,832
But here, we are not a
browser, we are a server.

14
00:00:48,832 --> 00:00:51,029
So, when we receive a post request,

15
00:00:51,029 --> 00:00:53,207
that means we are receiving some data,

16
00:00:53,207 --> 00:00:55,573
and therefore we have to deal with it.

17
00:00:55,573 --> 00:00:57,321
And when we receive a get request,

18
00:00:57,321 --> 00:01:00,975
that means that we have to send data back.

19
00:01:00,975 --> 00:01:04,998
For example, when we receive post store,

20
00:01:04,998 --> 00:01:07,724
and that means that we have
to probably create a store.

21
00:01:07,724 --> 00:01:10,114
We may receive get store, and that means

22
00:01:10,114 --> 00:01:13,339
we have to send back a list of stores.

23
00:01:13,339 --> 00:01:15,574
So, the endpoints that we're gonna create

24
00:01:15,574 --> 00:01:18,241
are post for store, and this is,

25
00:01:19,504 --> 00:01:23,171
the data that we're
gonna receive is a name.

26
00:01:24,164 --> 00:01:26,831
We're gonna create get of store,

27
00:01:28,037 --> 00:01:29,537
slash string name.

28
00:01:32,198 --> 00:01:35,784
Get for stores, we're gonna create post

29
00:01:35,784 --> 00:01:38,617
for a store string, oh, apologies,

30
00:01:40,049 --> 00:01:41,466
string name item.

31
00:01:42,894 --> 00:01:46,227
And also get for store string name item.

32
00:01:49,896 --> 00:01:51,696
So, what these endpoints are gonna do

33
00:01:51,696 --> 00:01:56,109
is, the first one is
gonna create a new store

34
00:01:56,109 --> 00:01:58,095
with a given name.

35
00:01:58,095 --> 00:02:02,262
The second one is going to
get a store for a given name,

36
00:02:03,484 --> 00:02:05,870
and it's gonna return some data about it.

37
00:02:05,870 --> 00:02:09,787
The third one is going to
return a list of all the stores.

38
00:02:09,787 --> 00:02:12,324
The fourth one is going to create an item

39
00:02:12,324 --> 00:02:15,300
inside a specific store,
with a given name.

40
00:02:15,300 --> 00:02:17,175
And the last one is
gonna get all the items

41
00:02:17,175 --> 00:02:19,168
in a specific store.

42
00:02:19,168 --> 00:02:23,335
This one will also have a
name and a price, for example.

43
00:02:26,453 --> 00:02:28,094
OK.

44
00:02:28,094 --> 00:02:30,795
So, I'll create an
example of a post request

45
00:02:30,795 --> 00:02:32,816
and a get request for you,

46
00:02:32,816 --> 00:02:36,763
and then we, I'll expect
you to create the rest.

47
00:02:36,763 --> 00:02:38,503
So, for the first port request.

48
00:02:38,503 --> 00:02:41,065
Well, we know how to create an endpoint.

49
00:02:41,065 --> 00:02:45,232
It is @app.root, and here,
before we put a forward slash,

50
00:02:46,353 --> 00:02:50,092
that was the root of our
application, the home page,

51
00:02:50,092 --> 00:02:53,263
now we wanna put it as slash store,

52
00:02:53,263 --> 00:02:56,962
because that is what this endpoint is.

53
00:02:56,962 --> 00:03:00,676
Therefore, our browser,
or our web application,

54
00:03:00,676 --> 00:03:04,642
or our m, whoever is
really calling our API

55
00:03:04,642 --> 00:03:07,225
is going to call this endpoint.

56
00:03:09,282 --> 00:03:11,699
However, it has to be a post.

57
00:03:12,625 --> 00:03:15,249
By default, when you use app.route,

58
00:03:15,249 --> 00:03:17,183
that is a get request.

59
00:03:17,183 --> 00:03:21,638
And browsers, by default,
only do get requests.

60
00:03:21,638 --> 00:03:25,805
So, we're gonna need to say
comma methods equal post.

61
00:03:28,433 --> 00:03:32,727
So, this defines our route
as being slash store,

62
00:03:32,727 --> 00:03:37,451
and as being accessible
only via a post request.

63
00:03:37,451 --> 00:03:41,572
If we want it to be accessible
via a get request, as well,

64
00:03:41,572 --> 00:03:44,450
then we could do post and get.

65
00:03:44,450 --> 00:03:46,429
So then, this endpoint could be called

66
00:03:46,429 --> 00:03:48,679
either via post or via get.

67
00:03:50,504 --> 00:03:54,208
We will look at how that
works later on in the course.

68
00:03:54,208 --> 00:03:56,415
Then, we have to associate
a method with it.

69
00:03:56,415 --> 00:04:00,582
And in this case, I'm gonna
call the method create store.

70
00:04:02,253 --> 00:04:03,931
And it's not going to do anything for now,

71
00:04:03,931 --> 00:04:07,098
but we will make it do stuff later on.

72
00:04:08,151 --> 00:04:12,001
To get the store, well, fairy simple.

73
00:04:12,001 --> 00:04:13,004
I'm sure you can do it yourself.

74
00:04:13,004 --> 00:04:15,071
So, if you think you
can, pause the video now

75
00:04:15,071 --> 00:04:16,321
and give it a go.

76
00:04:16,321 --> 00:04:20,842
And if not, just keep watching
and I will implement it here.

77
00:04:20,842 --> 00:04:24,925
To get a store, we'll have
app.route slash store,

78
00:04:26,503 --> 00:04:28,003
slash string name.

79
00:04:28,991 --> 00:04:32,799
This is a special Flask bit of syntax,

80
00:04:32,799 --> 00:04:36,488
which means that when we get store,

81
00:04:36,488 --> 00:04:37,982
when we create our method,

82
00:04:37,982 --> 00:04:41,199
our method can have a
parameter, which is name.

83
00:04:41,199 --> 00:04:45,086
This name here has to match this one here.

84
00:04:45,086 --> 00:04:47,642
And therefore, when we receive a request

85
00:04:47,642 --> 00:04:51,809
such as an
http://107.0.0.1/5000/store/some_name,

86
00:05:03,168 --> 00:05:05,529
some name is going to be this name here.

87
00:05:05,529 --> 00:05:09,217
So, the name variable
will contain some name.

88
00:05:09,217 --> 00:05:11,278
If we have a store that has that name,

89
00:05:11,278 --> 00:05:15,445
we're gonna be able to return
it, using that as a key.

90
00:05:16,361 --> 00:05:17,194
OK.

91
00:05:17,194 --> 00:05:19,777
So, this is also going to pass.

92
00:05:20,964 --> 00:05:22,719
And then, you know, this
is just going to apply

93
00:05:22,719 --> 00:05:25,041
for, really, the rest of them.

94
00:05:25,041 --> 00:05:26,987
And this one is only store, so it doesn't

95
00:05:26,987 --> 00:05:28,324
need the string name.

96
00:05:28,324 --> 00:05:32,550
And also, it's not gonna have a parameter.

97
00:05:32,550 --> 00:05:36,399
For the post, we're going
to copy this one here,

98
00:05:36,399 --> 00:05:40,446
and it's gonna have
string name slash item.

99
00:05:40,446 --> 00:05:43,286
Make sure to not forget
any of these little bit

100
00:05:43,286 --> 00:05:47,114
of syntax there, because
that can be quite confusing

101
00:05:47,114 --> 00:05:49,203
for Flask if you do that.

102
00:05:49,203 --> 00:05:53,488
And finally, we're going to
have something like this,

103
00:05:53,488 --> 00:05:54,321
down here.

104
00:06:02,248 --> 00:06:03,715
Remember to change the
method names, as well,

105
00:06:03,715 --> 00:06:06,298
because they have to be unique.

106
00:06:08,032 --> 00:06:10,665
So, I'm gonna call this
method get item store,

107
00:06:10,665 --> 00:06:12,289
because that's essentially
what this is going to do.

108
00:06:12,289 --> 00:06:15,005
This is going to retrieve all the items

109
00:06:15,005 --> 00:06:17,298
in a specific store.

110
00:06:17,298 --> 00:06:22,191
For this one, it's going
to be create item in store.

111
00:06:22,191 --> 00:06:24,489
For this one, it is gonna get stores,

112
00:06:24,489 --> 00:06:25,995
and this one is gonna be get store.

113
00:06:25,995 --> 00:06:28,001
This one is gonna be create store.

114
00:06:28,001 --> 00:06:29,871
So, these are our endpoints.

115
00:06:29,871 --> 00:06:31,571
So, we've got our five endpoints now,

116
00:06:31,571 --> 00:06:33,987
as we discussed in the last presentation.

117
00:06:33,987 --> 00:06:38,151
And all we have to do now
is really implement them.

118
00:06:38,151 --> 00:06:40,363
So, how do we implement them?

119
00:06:40,363 --> 00:06:44,530
Well, we, first of all, need
a way to store our stores.

120
00:06:46,632 --> 00:06:50,799
So, that can be, for example,
something like a list.

121
00:06:52,455 --> 00:06:55,769
And our stores can be
an empty list initially,

122
00:06:55,769 --> 00:06:58,813
and then it can have a dictionary in it,

123
00:06:58,813 --> 00:07:00,992
and the dictionary is gonna have a name,

124
00:07:00,992 --> 00:07:02,384
and the name's gonna be, for example,

125
00:07:02,384 --> 00:07:03,967
My Wonderful Store.

126
00:07:05,748 --> 00:07:09,047
And there's also gonna
have a list of items,

127
00:07:09,047 --> 00:07:12,196
and each item is going to have a name,

128
00:07:12,196 --> 00:07:15,779
such as My Item and a
price, such as 15.99.

129
00:07:20,024 --> 00:07:21,086
OK.

130
00:07:21,086 --> 00:07:23,496
So, hopefully this makes sense.

131
00:07:23,496 --> 00:07:27,054
I'm just gonna clean
this up a bit for you,

132
00:07:27,054 --> 00:07:29,649
so it's a bit easier to
understand or to know.

133
00:07:29,649 --> 00:07:31,195
OK.

134
00:07:31,195 --> 00:07:34,472
So, our store's list is
going to contain a list

135
00:07:34,472 --> 00:07:35,328
of dictionaries.

136
00:07:35,328 --> 00:07:39,626
So, here we've got one dictionary,
but we could have many.

137
00:07:39,626 --> 00:07:43,761
Each dictionary has a name,
such as our stores do,

138
00:07:43,761 --> 00:07:45,914
and the name, in this case,
is My Wonderful Store.

139
00:07:45,914 --> 00:07:48,178
And it also has a list of items.

140
00:07:48,178 --> 00:07:51,228
And in this case, items is a list.

141
00:07:51,228 --> 00:07:53,859
So, we can have many other
dictionaries in here,

142
00:07:53,859 --> 00:07:56,475
and each dictionary is
going to have a name

143
00:07:56,475 --> 00:07:57,475
and a price.

144
00:07:58,791 --> 00:08:01,040
We could do this with
object oriented programming,

145
00:08:01,040 --> 00:08:02,297
if we wanted, and we're gonna explore

146
00:08:02,297 --> 00:08:04,318
how to do that later on, of course.

147
00:08:04,318 --> 00:08:06,627
And also, normally, we would
be storing these things

148
00:08:06,627 --> 00:08:09,859
to a database, but in
order to simplify things

149
00:08:09,859 --> 00:08:13,439
and just teach you the basics of RS API,

150
00:08:13,439 --> 00:08:17,606
we're gonna stick to having
a dictionary here, in memory.

151
00:08:18,535 --> 00:08:20,161
So, hopefully that's all right.

152
00:08:20,161 --> 00:08:21,475
And in the next video, we're going to look

153
00:08:21,475 --> 00:08:23,974
into implementing these endpoints.

154
00:08:23,974 --> 00:08:25,782
So, I'll see you there.

