1
00:00:00,305 --> 00:00:01,808
- [Instructor] Hi and
welcome back to the course.

2
00:00:01,808 --> 00:00:04,342
In the last video, we did request parsing,

3
00:00:04,342 --> 00:00:07,269
and we made sure that we
could only pass certain fields

4
00:00:07,269 --> 00:00:10,334
through our JSON payload to our endpoints.

5
00:00:10,334 --> 00:00:12,249
I also suggested that you try to do

6
00:00:12,249 --> 00:00:14,409
the same thing for the POST method,

7
00:00:14,409 --> 00:00:17,265
so let's do that just now.

8
00:00:17,265 --> 00:00:21,432
I'm just going to copy, and
then I'm going to paste,

9
00:00:22,293 --> 00:00:26,032
then I'm going to remove
the data from here.

10
00:00:26,032 --> 00:00:29,449
Now, what was wrong with what I just did?

11
00:00:32,578 --> 00:00:35,628
Hopefully, you acknowledged
that it is wrong,

12
00:00:35,628 --> 00:00:39,562
normally at least,
often, to copy and paste.

13
00:00:39,562 --> 00:00:42,537
I essentially copied and
pasted and didn't change

14
00:00:42,537 --> 00:00:46,456
anything at all, which
means that this thing here

15
00:00:46,456 --> 00:00:48,622
is the same as this thing here,

16
00:00:48,622 --> 00:00:51,106
and there is probably a way of making sure

17
00:00:51,106 --> 00:00:52,772
that they are not duplicated.

18
00:00:52,772 --> 00:00:54,883
So why do we not want to duplicate them?

19
00:00:54,883 --> 00:00:58,259
For example, say we want
to change the help message,

20
00:00:58,259 --> 00:01:03,057
we now have to change it in
line 26, and also in line 47,

21
00:01:03,057 --> 00:01:04,870
and if we forget to change one of them,

22
00:01:04,870 --> 00:01:06,062
they're going to be out of sync,

23
00:01:06,062 --> 00:01:06,941
and then that's going to result

24
00:01:06,941 --> 00:01:09,597
in poor user experience and so on.

25
00:01:09,597 --> 00:01:12,728
Another example, we want
to add a new argument

26
00:01:12,728 --> 00:01:16,365
to the parser, so now we have
to add a new argument up here,

27
00:01:16,365 --> 00:01:18,279
and then another one down here.

28
00:01:18,279 --> 00:01:21,170
Overall, just not very nice.

29
00:01:21,170 --> 00:01:25,961
So what I'm gonna do, is
I'm going to remove this,

30
00:01:25,961 --> 00:01:29,952
and I'm going to cut
this, and then we're gonna

31
00:01:29,952 --> 00:01:31,535
put it in the item.

32
00:01:33,115 --> 00:01:37,166
So now the parser is going
to belong to the item class

33
00:01:37,166 --> 00:01:41,799
as opposed to belonging
in one of the methods,

34
00:01:41,799 --> 00:01:45,293
but notice there is no self in front,

35
00:01:45,293 --> 00:01:48,330
so the parser belongs to the class itself,

36
00:01:48,330 --> 00:01:51,631
and not to one specific item resource,

37
00:01:51,631 --> 00:01:54,106
so the way to call and use this parser

38
00:01:54,106 --> 00:01:57,833
is by saying Item.parser.parse_args.

39
00:01:57,833 --> 00:01:59,642
Remember the I has to be upper case

40
00:01:59,642 --> 00:02:02,258
just as it is in the class,

41
00:02:02,258 --> 00:02:05,069
and similarly down here in the PUT method,

42
00:02:05,069 --> 00:02:08,069
it should be Item.parser.parse_args.

43
00:02:09,077 --> 00:02:10,819
and that just makes things a lot nicer,

44
00:02:10,819 --> 00:02:12,451
and also a lot easier to reuse

45
00:02:12,451 --> 00:02:13,933
because now if we want to use

46
00:02:13,933 --> 00:02:16,558
this in another method,
we just have to do this,

47
00:02:16,558 --> 00:02:18,475
and it's really simple.

48
00:02:19,608 --> 00:02:23,116
As an aside, I'll also
mention why this data

49
00:02:23,116 --> 00:02:27,283
is up here, or rather why this
data used to be down here.

50
00:02:30,271 --> 00:02:31,845
This data used to be down here,

51
00:02:31,845 --> 00:02:36,012
and it should be down here
because of the following.

52
00:02:37,243 --> 00:02:41,298
The first thing we do
is we filter out things,

53
00:02:41,298 --> 00:02:45,544
so we're saying if the name
is already in our database,

54
00:02:45,544 --> 00:02:47,528
we don't want to do anything.

55
00:02:47,528 --> 00:02:50,312
This is an error first approach.

56
00:02:50,312 --> 00:02:52,480
The first thing we do
is we look for errors,

57
00:02:52,480 --> 00:02:56,397
and if any errors happen,
we stop and we break,

58
00:02:57,313 --> 00:03:00,249
and once all of the
errors have been cleared,

59
00:03:00,249 --> 00:03:02,152
we start doing what we want to do.

60
00:03:02,152 --> 00:03:04,520
So the first thing we do
is we deal with errors,

61
00:03:04,520 --> 00:03:06,247
and then we do what we want to do,

62
00:03:06,247 --> 00:03:09,364
and that way, we know
that there are no errors

63
00:03:09,364 --> 00:03:12,168
that could happen in
this piece of code here

64
00:03:12,168 --> 00:03:15,516
because we've already
dealt with them before,

65
00:03:15,516 --> 00:03:19,341
and because here we are
stopping if the name

66
00:03:19,341 --> 00:03:22,547
is already in the database,
it doesn't make sense

67
00:03:22,547 --> 00:03:25,540
to load the data before it.

68
00:03:25,540 --> 00:03:29,379
Here we'd be loading data,
and then we would be stopping

69
00:03:29,379 --> 00:03:33,546
if the item already exists, and
we would never use the data,

70
00:03:34,405 --> 00:03:37,888
so that seems a bit wasteful, which is why

71
00:03:37,888 --> 00:03:40,159
the data was originally down here.

72
00:03:40,159 --> 00:03:41,411
The first thing we do is we make sure

73
00:03:41,411 --> 00:03:45,088
that we are okay to proceed,
and then we actually proceed,

74
00:03:45,088 --> 00:03:48,427
and do the stuff that we want to do.

75
00:03:48,427 --> 00:03:49,799
Now okay, that's it for this video,

76
00:03:49,799 --> 00:03:51,232
so hopefully you learned something,

77
00:03:51,232 --> 00:03:53,090
and I'll see you in the very next one.

