1
00:00:00,537 --> 00:00:02,253
- [Man] Hi and welcome back to the course.

2
00:00:02,253 --> 00:00:05,086
In this video, we're going
to be creating the item list

3
00:00:05,086 --> 00:00:08,464
and also creating the
items properly this time

4
00:00:08,464 --> 00:00:11,679
using the data in the JSON payload.

5
00:00:11,679 --> 00:00:14,625
First of all, there's unfortunate chances

6
00:00:14,625 --> 00:00:17,814
that we may run into errors
throughout the course,

7
00:00:17,814 --> 00:00:19,727
and there's nothing more infuriating

8
00:00:19,727 --> 00:00:24,606
than very terrible error
messages that don't help you out.

9
00:00:24,606 --> 00:00:29,042
Fortunately, Flask has a way
of showing good error messages,

10
00:00:29,042 --> 00:00:30,573
and the only thing we have to do

11
00:00:30,573 --> 00:00:35,025
is in app.run before or after
port, it doesn't matter.

12
00:00:35,025 --> 00:00:37,858
Just type comma debug equals true.

13
00:00:39,107 --> 00:00:41,328
Now when you get on error message,

14
00:00:41,328 --> 00:00:43,560
when something really goes
wrong in your application,

15
00:00:43,560 --> 00:00:45,844
now, you'll get a nice html page

16
00:00:45,844 --> 00:00:48,132
that you can sort of visit,

17
00:00:48,132 --> 00:00:50,416
and it will tell you what's wrong.

18
00:00:50,416 --> 00:00:53,231
Also, if you ask a
question in the course Q&A,

19
00:00:53,231 --> 00:00:56,857
having that html page really helps me out

20
00:00:56,857 --> 00:00:58,448
when it comes to helping you

21
00:00:58,448 --> 00:01:01,531
debug and troubleshoot your problems.

22
00:01:02,384 --> 00:01:06,551
Okay, so the item list, that
was a challenge for this video.

23
00:01:08,001 --> 00:01:09,872
I suggested that you try this on your own.

24
00:01:09,872 --> 00:01:12,073
Hopefully, you have managed to do that,

25
00:01:12,073 --> 00:01:14,030
and it's really simple.

26
00:01:14,030 --> 00:01:17,697
Class item list is gonna
be on new resource,

27
00:01:18,565 --> 00:01:21,481
and it's going to have a get
because that's what we defined

28
00:01:21,481 --> 00:01:23,648
in the postman collection.

29
00:01:27,293 --> 00:01:30,460
We didn't pass in any parameters
in the postman collection,

30
00:01:30,460 --> 00:01:33,778
so there's not gonna be any
parameters to the method

31
00:01:33,778 --> 00:01:38,186
and all this is going to
return is a list of items,

32
00:01:38,186 --> 00:01:42,365
Is items three dictionary
of items is items.

33
00:01:42,365 --> 00:01:45,478
Items is a list which
contains all our items.

34
00:01:45,478 --> 00:01:47,957
So that's really everything.

35
00:01:47,957 --> 00:01:51,540
Don't forget to add
the resource item list,

36
00:01:53,337 --> 00:01:57,170
and make sure to give
it the correct endpoint,

37
00:01:59,237 --> 00:02:02,487
which is going to be /items.

38
00:02:03,792 --> 00:02:04,625
That's it.

39
00:02:04,625 --> 00:02:08,530
So, hopefully you managed
to do that in the challenge.

40
00:02:08,530 --> 00:02:11,530
Now we're going to go into improving

41
00:02:12,425 --> 00:02:15,307
these two methods slightly.

42
00:02:15,307 --> 00:02:18,729
The first improvement that is
going to happen in this video

43
00:02:18,729 --> 00:02:22,812
is going to be to actually
create items properly.

44
00:02:24,471 --> 00:02:29,172
Now, the first thing we
need is the JSON payload.

45
00:02:29,172 --> 00:02:31,328
We looked at this in the last section.

46
00:02:31,328 --> 00:02:35,224
How to get the JSON
payload from a request.

47
00:02:35,224 --> 00:02:37,542
Hopefully, you remember how to do that,

48
00:02:37,542 --> 00:02:40,726
and I would encourage you
to try that on your own now

49
00:02:40,726 --> 00:02:42,726
and see how far you get.

50
00:02:45,959 --> 00:02:47,947
Hopefully, you were able to do that.

51
00:02:47,947 --> 00:02:50,197
The first thing we have
to do is to go up here

52
00:02:50,197 --> 00:02:52,802
and make sure to import request.

53
00:02:52,802 --> 00:02:56,482
That is going to be when
somebody such as postman

54
00:02:56,482 --> 00:02:59,466
makes a request to our API,

55
00:02:59,466 --> 00:03:02,216
that request is in this variable.

56
00:03:03,555 --> 00:03:06,308
So, that request is going
to have a JSON payload,

57
00:03:06,308 --> 00:03:08,781
a body attached to it.

58
00:03:08,781 --> 00:03:12,948
So, we're going to say data
equals request.get JSON.

59
00:03:15,782 --> 00:03:19,643
If the request does not
attach a JSON payload

60
00:03:19,643 --> 00:03:23,810
or the request does not have
the proper content-type header,

61
00:03:25,349 --> 00:03:29,547
this will give an error, so
that's something to remember.

62
00:03:29,547 --> 00:03:32,778
And what I mean with
that if we go to postman,

63
00:03:32,778 --> 00:03:37,091
notice how we have set
the content type properly.

64
00:03:37,091 --> 00:03:41,523
If you do not do this, that
line will give you an error,

65
00:03:41,523 --> 00:03:44,690
and similarly if the body is not JSON,

66
00:03:45,902 --> 00:03:47,342
then that will also give you an error.

67
00:03:47,342 --> 00:03:49,864
For example, if you put
some python code in there.

68
00:03:49,864 --> 00:03:50,800
I don't know why you would do that

69
00:03:50,800 --> 00:03:53,967
but nevertheless and just so you know.

70
00:03:55,078 --> 00:03:56,638
In case you are not sure

71
00:03:56,638 --> 00:03:59,697
if your clients are going
to give you JSON or not,

72
00:03:59,697 --> 00:04:03,887
you can prevent this
from giving you an error.

73
00:04:03,887 --> 00:04:05,972
There are two things you
that you can pass this method

74
00:04:05,972 --> 00:04:07,679
that are really useful.

75
00:04:07,679 --> 00:04:10,596
The first one is false equals true.

76
00:04:11,940 --> 00:04:14,383
False equals true means that

77
00:04:14,383 --> 00:04:18,369
you do not need the content-type header.

78
00:04:18,369 --> 00:04:21,843
It will just look in the
content and it will format it

79
00:04:21,843 --> 00:04:23,580
even if the content type header

80
00:04:23,580 --> 00:04:27,900
is not set to be application/JSON.

81
00:04:27,900 --> 00:04:30,057
This is nice, but it's also dangerous

82
00:04:30,057 --> 00:04:32,807
because it means that without it,

83
00:04:33,892 --> 00:04:35,929
you look at the header and
if it's not set correctly,

84
00:04:35,929 --> 00:04:39,292
you just do nothing and with it,

85
00:04:39,292 --> 00:04:40,528
you don't look at the header.

86
00:04:40,528 --> 00:04:42,010
Therefore, you're always going to be doing

87
00:04:42,010 --> 00:04:46,482
the processing of the text
even if it is incorrect.

88
00:04:46,482 --> 00:04:49,453
I tend to not use false equal true.

89
00:04:49,453 --> 00:04:51,648
The other one that is
also quite handy sometimes

90
00:04:51,648 --> 00:04:55,945
is silent equals true
and what this does is

91
00:04:55,945 --> 00:04:57,034
it doesn't give an error,

92
00:04:57,034 --> 00:05:00,177
it just basically returns none.

93
00:05:00,177 --> 00:05:01,095
That okay?

94
00:05:01,095 --> 00:05:05,262
So, this another thing that
you can do if you prefer.

95
00:05:08,173 --> 00:05:13,128
Now that we've got the data
there, we can get the price.

96
00:05:13,128 --> 00:05:15,106
So, I'm sure you know how to do that.

97
00:05:15,106 --> 00:05:17,322
Data is going to be a dictionary,

98
00:05:17,322 --> 00:05:20,004
and you know how to access
data from a dictionary.

99
00:05:20,004 --> 00:05:24,507
So, it should be fairly
straightforward data price

100
00:05:24,507 --> 00:05:27,233
in square brackets and as a string

101
00:05:27,233 --> 00:05:31,400
that's going to access the price
key of the data dictionary.

102
00:05:33,183 --> 00:05:37,105
And price is here, one of
the keys of this dictionary,

103
00:05:37,105 --> 00:05:40,136
and in this case, it's
going to be equal to 12.

104
00:05:40,136 --> 00:05:43,614
Apologies, this is the
return, the return content.

105
00:05:43,614 --> 00:05:45,848
Price is a key here in the dictionary,

106
00:05:45,848 --> 00:05:49,378
and the content is going to be 15.99,

107
00:05:49,378 --> 00:05:53,545
so when we create a chair,
it will have that price.

108
00:05:55,010 --> 00:05:56,403
Okay, so let's save that.

109
00:05:56,403 --> 00:05:59,130
Go back to the terminal,
press 'control + C'

110
00:05:59,130 --> 00:06:02,075
to quit and restart the app.

111
00:06:02,075 --> 00:06:04,103
Notice how now we get slightly
more different information

112
00:06:04,103 --> 00:06:07,677
because we've enabled debug equals true.

113
00:06:07,677 --> 00:06:11,812
And then let's go over to
postman and create our chair.

114
00:06:11,812 --> 00:06:16,758
Notice how now, the price is
no longer 12, it is 15.99,

115
00:06:16,758 --> 00:06:20,925
and also notice how now we
can go to our items endpoint

116
00:06:22,266 --> 00:06:26,775
and retrieve all of the
items in our database.

117
00:06:26,775 --> 00:06:30,218
Do remember everything
you restart the app,

118
00:06:30,218 --> 00:06:34,441
this list gets cleared because
it is an in-memory database.

119
00:06:34,441 --> 00:06:37,386
It only resides in memory,
and when you stop the app,

120
00:06:37,386 --> 00:06:38,942
that memory gets cleared,

121
00:06:38,942 --> 00:06:41,038
and you could lose the whole data there.

122
00:06:41,038 --> 00:06:42,513
That's why normally,
these things are stored

123
00:06:42,513 --> 00:06:45,680
in a database which is more permanent.

124
00:06:46,648 --> 00:06:50,448
Okay, so that's really it for this video.

125
00:06:50,448 --> 00:06:53,360
In the next video, we're going to improve

126
00:06:53,360 --> 00:06:57,792
on the iteration here by
using a filter function.

127
00:06:57,792 --> 00:07:00,041
We've looked at those
in the Python refresher.

128
00:07:00,041 --> 00:07:01,318
Now may be a good time

129
00:07:01,318 --> 00:07:04,142
to look at the Lambda Functions lecture

130
00:07:04,142 --> 00:07:08,022
in the Python refresher if
you haven't done so before,

131
00:07:08,022 --> 00:07:08,989
and because in the next video,

132
00:07:08,989 --> 00:07:11,974
we're going to use a lambda function.

133
00:07:11,974 --> 00:07:12,972
That's it for this video though,

134
00:07:12,972 --> 00:07:16,090
thanks for watching, and I'll
see you in the very next one.

