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

2
00:00:02,491 --> 00:00:07,212
In this video we would like
to implement the put endpoint.

3
00:00:07,212 --> 00:00:09,513
But unfortunately there is something that

4
00:00:09,513 --> 00:00:12,624
we have to do before we can do that.

5
00:00:12,624 --> 00:00:15,296
Notice how the put
endpoint also has a line

6
00:00:15,296 --> 00:00:19,709
items dot append because
this used to append the new

7
00:00:19,709 --> 00:00:22,716
item to the items list, or update an item

8
00:00:22,716 --> 00:00:25,403
if it was already there.

9
00:00:25,403 --> 00:00:28,998
There is an items dot append
here, but we don't have

10
00:00:28,998 --> 00:00:33,499
a way to add an item to
the database right now.

11
00:00:33,499 --> 00:00:35,582
We do have a post method.

12
00:00:38,532 --> 00:00:41,049
The post method does create an item and

13
00:00:41,049 --> 00:00:43,021
put it in the database.

14
00:00:43,021 --> 00:00:47,128
However, there is no
way for the put method

15
00:00:47,128 --> 00:00:50,933
to call the post method
unless it also wants

16
00:00:50,933 --> 00:00:55,146
to do things like return
the item on the status code

17
00:00:55,146 --> 00:00:58,113
and check that the
items are already there.

18
00:00:58,113 --> 00:01:00,215
What we're going to do, is
we're going to once again

19
00:01:00,215 --> 00:01:03,632
extract this here into a separate method.

20
00:01:05,133 --> 00:01:08,853
Extract the insertion of
items onto a separate method.

21
00:01:08,853 --> 00:01:13,037
I'm going to cut that out
and then create another

22
00:01:13,037 --> 00:01:16,370
class method which I'm gonna call insert

23
00:01:18,332 --> 00:01:23,287
and it's going to take in
an item actually not a name.

24
00:01:23,287 --> 00:01:25,923
The item that it takes
in is then used to get a

25
00:01:25,923 --> 00:01:28,379
name and a price and then that's inserted

26
00:01:28,379 --> 00:01:29,879
into the database.

27
00:01:31,395 --> 00:01:36,282
The way we call this is by
doing self dot insert item.

28
00:01:36,282 --> 00:01:38,284
The item is this piece of chasing here,

29
00:01:38,284 --> 00:01:40,117
this dictionary there.

30
00:01:41,832 --> 00:01:44,514
Hopefully that makes sense.

31
00:01:44,514 --> 00:01:48,014
Do remember that there might be a problem.

32
00:01:49,095 --> 00:01:54,002
The database may fail to
insert the item for example.

33
00:01:54,002 --> 00:01:57,051
If that does happen there
is a specific construct

34
00:01:57,051 --> 00:02:00,905
that python has that is
to deal with exceptions.

35
00:02:00,905 --> 00:02:04,322
An exception is what python runs whenever

36
00:02:05,173 --> 00:02:06,506
an error occurs.

37
00:02:07,655 --> 00:02:11,488
We can try self dot
insert, accept, exception.

38
00:02:15,440 --> 00:02:16,912
Actually we don't need that.

39
00:02:16,912 --> 00:02:21,079
Except only and then return
message an error occurred

40
00:02:23,986 --> 00:02:25,986
with two r's, inserting.

41
00:02:28,927 --> 00:02:32,747
Let me explain what's happening here.

42
00:02:32,747 --> 00:02:35,938
This really is as logical as it seems.

43
00:02:35,938 --> 00:02:38,707
The first thing we do is
we try to do something.

44
00:02:38,707 --> 00:02:42,849
We try to run the self dot insert method.

45
00:02:42,849 --> 00:02:47,477
The except block only runs
if there was an error.

46
00:02:47,477 --> 00:02:49,144
An exception raised.

47
00:02:50,702 --> 00:02:53,640
The cursor for example
may raise an exception

48
00:02:53,640 --> 00:02:57,958
if it cannot insert the
item into the database.

49
00:02:57,958 --> 00:03:00,542
The commit may raise an
exception if the connection

50
00:03:00,542 --> 00:03:03,041
gets closed prematurely.

51
00:03:03,041 --> 00:03:05,035
There's a few things that could go wrong

52
00:03:05,035 --> 00:03:06,956
and here all that we're
doing is we're saying,

53
00:03:06,956 --> 00:03:09,649
we're gonna try to insert
the item in and if we

54
00:03:09,649 --> 00:03:11,970
fail for any reason we're
just going to return

55
00:03:11,970 --> 00:03:15,508
a message saying an error occurred.

56
00:03:15,508 --> 00:03:17,585
It's not something we've
done before but it makes

57
00:03:17,585 --> 00:03:19,699
sense to be able to do that.

58
00:03:19,699 --> 00:03:24,346
You can also apply this to
any other part of your code.

59
00:03:24,346 --> 00:03:27,400
For example up here in the get.

60
00:03:27,400 --> 00:03:30,212
The find by name may
also fail prematurely.

61
00:03:30,212 --> 00:03:34,311
You may also want to round
up with a try accept block

62
00:03:34,311 --> 00:03:36,163
given an appropriate error message

63
00:03:36,163 --> 00:03:38,417
if it fails to find something.

64
00:03:38,417 --> 00:03:39,753
Sorry, not if it fails to find something,

65
00:03:39,753 --> 00:03:42,253
if it fails to run the search.

66
00:03:43,209 --> 00:03:45,044
Remember if it fails to find something,

67
00:03:45,044 --> 00:03:48,293
we're already saying not found.

68
00:03:48,293 --> 00:03:50,926
Also something to remember,
if an error occurs

69
00:03:50,926 --> 00:03:53,351
inserting the item, that's not the

70
00:03:53,351 --> 00:03:57,018
requester's fault and
it's certainly not OK.

71
00:03:58,524 --> 00:04:02,085
We don't want to return
200, 200 is the default.

72
00:04:02,085 --> 00:04:03,401
We do not want to do that.

73
00:04:03,401 --> 00:04:07,063
What we want to return is
500 internal server error.

74
00:04:07,063 --> 00:04:09,082
That's what that means.

75
00:04:09,082 --> 00:04:10,915
Internal server error.

76
00:04:13,081 --> 00:04:14,588
That just means something went wrong

77
00:04:14,588 --> 00:04:16,481
and we can't tell you exactly what.

78
00:04:16,481 --> 00:04:21,095
But something went wrong
and it's not your fault.

79
00:04:21,095 --> 00:04:23,416
Do remember that it's very important

80
00:04:23,416 --> 00:04:28,001
you are returning 400 when
something did go wrong

81
00:04:28,001 --> 00:04:31,204
with the request, and
500 when something didn't

82
00:04:31,204 --> 00:04:34,705
go wrong with the request
but the server messed up

83
00:04:34,705 --> 00:04:37,574
so that the user knows they
didn't do anything wrong

84
00:04:37,574 --> 00:04:40,907
it's just the server that had a problem.

85
00:04:42,904 --> 00:04:44,158
That's everything for this video.

86
00:04:44,158 --> 00:04:46,632
We've covered the try
accept block that's really

87
00:04:46,632 --> 00:04:49,644
useful in python and also we've extracted

88
00:04:49,644 --> 00:04:53,392
the insert method out and
we're gonna be able to

89
00:04:53,392 --> 00:04:56,642
write the put method in the next video.

90
00:04:58,044 --> 00:05:00,356
Thanks for joining me
and I'll see you there.

