1
00:00:00,557 --> 00:00:02,035
- [Instructor] Hi, and welcome back.

2
00:00:02,035 --> 00:00:03,311
In this video, we're going to perform

3
00:00:03,311 --> 00:00:05,732
our last changes to the API,

4
00:00:05,732 --> 00:00:09,899
and that's going to involve
retrieving the item list.

5
00:00:10,934 --> 00:00:12,790
That means we're only
going to have a GET method,

6
00:00:12,790 --> 00:00:15,961
and that GET method
has to return every row

7
00:00:15,961 --> 00:00:19,087
from our database as a dictionary.

8
00:00:19,087 --> 00:00:22,603
Remember that it has to be in JSON format.

9
00:00:22,603 --> 00:00:24,231
So what are we going to do?

10
00:00:24,231 --> 00:00:26,370
Well, I'm going to copy and paste,

11
00:00:26,370 --> 00:00:30,134
and remember, I hate copy and pasting

12
00:00:30,134 --> 00:00:32,690
so you can already see
that the next section

13
00:00:32,690 --> 00:00:35,485
is going to bring a lot of improvement

14
00:00:35,485 --> 00:00:37,327
because the next section
is going to prevent

15
00:00:37,327 --> 00:00:40,647
all this copy pasting, which is awful,

16
00:00:40,647 --> 00:00:42,671
but nevertheless, so you understand really

17
00:00:42,671 --> 00:00:44,143
what's going on behind the scenes,

18
00:00:44,143 --> 00:00:48,310
it is useful to have this
context every stop of the way,

19
00:00:50,112 --> 00:00:51,762
and in the next section,
we're going to change that,

20
00:00:51,762 --> 00:00:55,495
and make sure that
everything is neat and clean,

21
00:00:55,495 --> 00:00:57,450
which is really important.

22
00:00:57,450 --> 00:01:01,450
Now the query is going
to just select everything

23
00:01:03,829 --> 00:01:08,289
from the item's table
because we want to return

24
00:01:08,289 --> 00:01:11,372
everything that's on our table, okay?

25
00:01:12,586 --> 00:01:15,084
And the result is going to be the result

26
00:01:15,084 --> 00:01:16,204
of executing this query.

27
00:01:16,204 --> 00:01:17,470
It doesn't have any parameters,

28
00:01:17,470 --> 00:01:21,637
so we're just gonna say result
= cursor.execute(query),

29
00:01:23,203 --> 00:01:25,747
and remember we can
iterate over the result

30
00:01:25,747 --> 00:01:29,403
and that gives us, well
it essentially allows us

31
00:01:29,403 --> 00:01:32,335
to go over each of the rows in the table,

32
00:01:32,335 --> 00:01:35,002
so we can do four row in result,

33
00:01:36,047 --> 00:01:40,082
and here we can do something
with each row, okay?

34
00:01:40,082 --> 00:01:42,600
So before the for loop,
I'm going to create

35
00:01:42,600 --> 00:01:46,627
a variable called items that
is going to be an empty list,

36
00:01:46,627 --> 00:01:49,956
and here I'm gonna do items.append,

37
00:01:49,956 --> 00:01:52,391
and I'm going to append a dictionary

38
00:01:52,391 --> 00:01:55,841
representing the row, the specific item.

39
00:01:55,841 --> 00:01:58,410
You can do that as well, and
I'm sure you can just complete

40
00:01:58,410 --> 00:02:00,726
this entire endpoint
at this point, really.

41
00:02:00,726 --> 00:02:03,453
If you want to just pause
the video and do it yourself,

42
00:02:03,453 --> 00:02:07,617
then by all means do go ahead and do that,

43
00:02:07,617 --> 00:02:09,007
but if not, then let's continue.

44
00:02:09,007 --> 00:02:11,888
I'm gonna append a dictionary
representing the item,

45
00:02:11,888 --> 00:02:15,594
so that has a name which is row(0),

46
00:02:15,594 --> 00:02:19,761
and a price which is
row(1), and that's it.

47
00:02:20,994 --> 00:02:22,883
Then at the end, we're gonna not commit

48
00:02:22,883 --> 00:02:23,989
because we've not saved anything.

49
00:02:23,989 --> 00:02:28,182
We're gonna just close,
and we're gonna return

50
00:02:28,182 --> 00:02:31,599
 where items is our list.

51
00:02:34,977 --> 00:02:36,608
That's it.

52
00:02:36,608 --> 00:02:40,242
Let's go into Postman,
and we're gonna go over

53
00:02:40,242 --> 00:02:41,308
very fine the entire thing.

54
00:02:41,308 --> 00:02:44,124
Also, do remember to have your app running

55
00:02:44,124 --> 00:02:47,874
which I wasn't, so I'm
going to run it again,

56
00:02:50,629 --> 00:02:51,962
and there we go.

57
00:02:54,184 --> 00:02:56,877
Okay, so the first thing
to do is register a user.

58
00:02:56,877 --> 00:02:57,710
Let's do that.

59
00:02:57,710 --> 00:03:02,292
Remember to check the body to
know what you're registering,

60
00:03:02,292 --> 00:03:04,450
and the user already
exists because of course

61
00:03:04,450 --> 00:03:06,913
we've not deleted the
data, so that's fine.

62
00:03:06,913 --> 00:03:10,529
So let's authenticate with
the user name and password,

63
00:03:10,529 --> 00:03:12,426
and that gives us the access token.

64
00:03:12,426 --> 00:03:16,138
I'm going to copy that,
and now we're gonna go over

65
00:03:16,138 --> 00:03:17,820
each of these endpoints.

66
00:03:17,820 --> 00:03:21,491
We've implemented them all,
so they all should work, okay?

67
00:03:21,491 --> 00:03:24,441
The first thing I'm going
to try is to put a new item,

68
00:03:24,441 --> 00:03:27,658
so I'm gonna create a piano, for example,

69
00:03:27,658 --> 00:03:31,324
and that gives me the price of $17.99,

70
00:03:31,324 --> 00:03:34,170
so I can see that a piano
costing $17.99 comes back.

71
00:03:34,170 --> 00:03:35,641
That's a pretty cheap piano.

72
00:03:35,641 --> 00:03:37,607
Wouldn't mind some of that,

73
00:03:37,607 --> 00:03:39,219
and now I'm going to change the price

74
00:03:39,219 --> 00:03:42,464
to make sure that it does work,

75
00:03:42,464 --> 00:03:45,877
and as you can see, the piano
costing $20.99 comes back.

76
00:03:45,877 --> 00:03:48,260
Okay, this doesn't tell us

77
00:03:48,260 --> 00:03:49,880
that the thing is in the database

78
00:03:49,880 --> 00:03:52,201
because as we know, the PUT endpoint

79
00:03:52,201 --> 00:03:56,439
doesn't read the database
after writing to it,

80
00:03:56,439 --> 00:03:59,574
it just returns the thing
that it thinks we wrote.

81
00:03:59,574 --> 00:04:03,574
So to verify, we're going
to call the get items,

82
00:04:04,463 --> 00:04:07,397
and as you can see, we
get here a list of items

83
00:04:07,397 --> 00:04:09,730
with a piano costing $20.99.

84
00:04:11,383 --> 00:04:13,357
Let's make sure that that does work

85
00:04:13,357 --> 00:04:15,857
getting a single item as well.

86
00:04:17,115 --> 00:04:19,448
Now I could press send, and this gives me

87
00:04:19,448 --> 00:04:20,973
that the signature has expired

88
00:04:20,973 --> 00:04:24,114
because these authentication
tokens expire after awhile,

89
00:04:24,114 --> 00:04:25,713
so we have to refresh that.

90
00:04:25,713 --> 00:04:28,264
Make sure to have an authorization header

91
00:04:28,264 --> 00:04:31,113
that has the value JWT space,

92
00:04:31,113 --> 00:04:33,946
and then the thing that we copied,

93
00:04:34,872 --> 00:04:38,030
and there we have a piano costing $20.99.

94
00:04:38,030 --> 00:04:40,296
Then let's go over to the POST endpoint,

95
00:04:40,296 --> 00:04:43,920
and see if we can create another piano.

96
00:04:43,920 --> 00:04:47,334
This time it's going to cost $15.99,

97
00:04:47,334 --> 00:04:49,746
and now we get an item with
that name already exists

98
00:04:49,746 --> 00:04:51,397
which is what we would expect,

99
00:04:51,397 --> 00:04:55,332
so let's create something
else such as a desk,

100
00:04:55,332 --> 00:04:58,390
and there we have a desk costing $15.99.

101
00:04:58,390 --> 00:05:00,143
Let's call the item's endpoint again

102
00:05:00,143 --> 00:05:03,256
to see if we get both
items, and indeed we do.

103
00:05:03,256 --> 00:05:06,724
We get the list of items
with two dictionaries in it,

104
00:05:06,724 --> 00:05:09,389
and let's try the last
one, which is to delete,

105
00:05:09,389 --> 00:05:12,139
and we're gonna delete the piano,

106
00:05:13,575 --> 00:05:15,895
and we get item deleted, but once again,

107
00:05:15,895 --> 00:05:18,701
we cannot believe this
because the delete endpoint

108
00:05:18,701 --> 00:05:21,151
always returns that, so what we have to do

109
00:05:21,151 --> 00:05:23,084
is get the items again to make sure

110
00:05:23,084 --> 00:05:27,118
that they are deleted, and indeed it is.

111
00:05:27,118 --> 00:05:29,220
So that's everything for this video.

112
00:05:29,220 --> 00:05:31,352
We have implemented all of our endpoints

113
00:05:31,352 --> 00:05:34,956
making sure to write and
read from the database.

114
00:05:34,956 --> 00:05:37,629
We've learned a bit of SQL on the way

115
00:05:37,629 --> 00:05:40,476
which really wasn't the
objective of this course,

116
00:05:40,476 --> 00:05:42,914
and because the stuff
that we're gonna learn

117
00:05:42,914 --> 00:05:46,740
in the next section is gonna
make that slightly obsolete

118
00:05:46,740 --> 00:05:50,149
to some extent, but
nevertheless, we've learned that

119
00:05:50,149 --> 00:05:54,448
creating a simple CRUD
API, which let me tell you,

120
00:05:54,448 --> 00:05:59,233
it's the majority of APIs
out there, is not difficult.

121
00:05:59,233 --> 00:06:01,782
All it requires is for you
to have some understanding

122
00:06:01,782 --> 00:06:05,646
of requests and saving to
a database, and committing,

123
00:06:05,646 --> 00:06:08,690
and returning things back,
and then all you have to do

124
00:06:08,690 --> 00:06:11,857
is create, read, update, delete, CRUD,

125
00:06:13,244 --> 00:06:14,810
and now we've managed to do that.

126
00:06:14,810 --> 00:06:17,658
We've also included the very popular

127
00:06:17,658 --> 00:06:19,925
sign-up and login mechanism, and this one

128
00:06:19,925 --> 00:06:21,680
is fairly secure as well.

129
00:06:21,680 --> 00:06:25,729
We're going to look into
security more later on as well.

130
00:06:25,729 --> 00:06:27,673
So hopefully you've found this useful.

131
00:06:27,673 --> 00:06:29,522
This is gonna be the end of this section,

132
00:06:29,522 --> 00:06:33,162
and in the next one we're going
to look at Flask-SQLAlchemy

133
00:06:33,162 --> 00:06:36,792
which is going to make
all of this a lot easier.

134
00:06:36,792 --> 00:06:38,911
So without further ado,
I'm excited to guide you

135
00:06:38,911 --> 00:06:41,990
through that, and I'll
see you in the next video.

