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

2
00:00:02,332 --> 00:00:03,901
In this video we're going to look at

3
00:00:03,901 --> 00:00:07,132
the last http method which is put.

4
00:00:07,132 --> 00:00:10,421
Put can be used as an item potent action

5
00:00:10,421 --> 00:00:14,281
which means that you can
call the same put request

6
00:00:14,281 --> 00:00:18,448
two, three, five, ten times
in a row and the output

7
00:00:19,488 --> 00:00:23,223
or what it causes should never change.

8
00:00:23,223 --> 00:00:26,323
What that means is that put can be used

9
00:00:26,323 --> 00:00:30,323
to both create items or
update an existing item.

10
00:00:34,131 --> 00:00:37,423
Let's create the put method here.

11
00:00:37,423 --> 00:00:39,541
Then what we're going
to do here is the usual.

12
00:00:39,541 --> 00:00:43,374
Get data from the request
and then essentially

13
00:00:47,104 --> 00:00:51,271
create the item or update the
item if it already exists.

14
00:00:52,461 --> 00:00:54,316
So what's the first thing
to do, find out if the

15
00:00:54,316 --> 00:00:56,148
item already exists.

16
00:00:56,148 --> 00:00:59,731
So, item is going to be next
off the filter function.

17
00:00:59,731 --> 00:01:03,185
What we're filtering
on is a specific name.

18
00:01:03,185 --> 00:01:07,102
Lamina x, x name equal
equal name four items

19
00:01:08,297 --> 00:01:10,595
and none as a default value.

20
00:01:10,595 --> 00:01:13,112
So remember here we're filtering and then

21
00:01:13,112 --> 00:01:16,289
we're getting the next
available value of that filter

22
00:01:16,289 --> 00:01:21,260
object, or we're getting
none if there are none left.

23
00:01:21,260 --> 00:01:25,059
If item is none, we want
to create a new item,

24
00:01:25,059 --> 00:01:27,226
so we'll say item is name.

25
00:01:28,124 --> 00:01:31,874
Name and price data
price, items append item.

26
00:01:37,974 --> 00:01:40,908
If item is not none it
means it already exists.

27
00:01:40,908 --> 00:01:45,161
What we're gonna do is
we're going to update it.

28
00:01:45,161 --> 00:01:48,625
It's really cool because
the item is going to be

29
00:01:48,625 --> 00:01:52,792
a dictionary and dictionaries
have an update method.

30
00:01:54,251 --> 00:01:57,717
We can just say item update with data.

31
00:01:57,717 --> 00:02:01,384
The values in item will
change together with

32
00:02:03,324 --> 00:02:05,378
the values in data.

33
00:02:05,378 --> 00:02:09,045
So if data has a price,
the items price will

34
00:02:10,020 --> 00:02:11,962
change to reflect that.

35
00:02:11,962 --> 00:02:14,320
This is slightly dangerous
because it does mean

36
00:02:14,320 --> 00:02:17,108
that the data may have a
name and that will change

37
00:02:17,108 --> 00:02:18,358
the items name.

38
00:02:19,921 --> 00:02:22,498
But we'll deal with that very soon, in the

39
00:02:22,498 --> 00:02:24,588
next video in fact.

40
00:02:24,588 --> 00:02:26,460
Then we return the item
to reflect that the

41
00:02:26,460 --> 00:02:28,293
changes have happened.

42
00:02:29,329 --> 00:02:32,172
Remember for verification
and for fun you can put

43
00:02:32,172 --> 00:02:35,089
the at jwt required on any of these

44
00:02:36,428 --> 00:02:39,243
and they will require a
jwt token and authorization

45
00:02:39,243 --> 00:02:41,076
header to be executed.

46
00:02:43,353 --> 00:02:45,608
I'm just not doing it for simplicity.

47
00:02:45,608 --> 00:02:47,818
You can do and you
probably will do in your

48
00:02:47,818 --> 00:02:49,235
production ABI's.

49
00:02:50,649 --> 00:02:52,731
Okay, so we've created the put endpoint.

50
00:02:52,731 --> 00:02:55,978
Let's go ahead into the
terminal, restart this

51
00:02:55,978 --> 00:02:58,425
and then go over to
postman and remember we're

52
00:02:58,425 --> 00:03:00,734
going to be verifying so I'm just going to

53
00:03:00,734 --> 00:03:03,067
close these other tabs here.

54
00:03:04,216 --> 00:03:06,361
Apologise for that.

55
00:03:06,361 --> 00:03:08,309
First of all let's get the list of items

56
00:03:08,309 --> 00:03:10,131
which should be empty
and then we're going to

57
00:03:10,131 --> 00:03:11,631
create a new item.

58
00:03:14,163 --> 00:03:15,913
Now that creates an item just fine

59
00:03:15,913 --> 00:03:19,631
and now we should be
able to modify the item.

60
00:03:19,631 --> 00:03:22,443
Now I'm going to put the items name here

61
00:03:22,443 --> 00:03:25,753
and then in the body we
can change the price.

62
00:03:25,753 --> 00:03:28,901
I'm going to put for example
5999, which is pretty

63
00:03:28,901 --> 00:03:30,401
cheap for a piano.

64
00:03:31,393 --> 00:03:34,643
Then what we get back is the new price.

65
00:03:35,819 --> 00:03:39,361
We can also do something
like a desk and give it

66
00:03:39,361 --> 00:03:43,528
a price of 110 and that
creates a brand new item there.

67
00:03:45,527 --> 00:03:47,905
Let's go into the item's
endpoint and call that

68
00:03:47,905 --> 00:03:50,245
and you can see that
there's two items there.

69
00:03:50,245 --> 00:03:52,098
I'm not lying to you, everything does work

70
00:03:52,098 --> 00:03:54,052
as you would expect.

71
00:03:54,052 --> 00:03:56,351
That's really important
for a put endpoint,

72
00:03:56,351 --> 00:04:01,346
an endpoint that uses the http
put verb to be item potent.

73
00:04:01,346 --> 00:04:05,430
You can call the same end
point, which I've got here.

74
00:04:05,430 --> 00:04:07,922
You can call the same end
point ten times and you

75
00:04:07,922 --> 00:04:11,040
don't end up with ten items.

76
00:04:11,040 --> 00:04:12,936
Just end up with the same item.

77
00:04:12,936 --> 00:04:14,654
No matter how often you call it or

78
00:04:14,654 --> 00:04:19,521
how many times you call it,
the output never changes.

79
00:04:19,521 --> 00:04:20,697
That's everything for this video.

80
00:04:20,697 --> 00:04:23,605
We've learned how to
implement a put request

81
00:04:23,605 --> 00:04:26,623
in our API and I'll see
you in the very next one.

