1
00:00:00,150 --> 00:00:02,820
Now in the very beginning of today's lessons,

2
00:00:02,880 --> 00:00:07,880
I mentioned that we can do other requests in addition to get and post like put

3
00:00:08,580 --> 00:00:09,413
and delete.

4
00:00:09,900 --> 00:00:14,900
And we can do that using a similar format as we have done for get and post.

5
00:00:15,750 --> 00:00:20,250
Just by simply changing the method we end up with a different behavior.

6
00:00:20,850 --> 00:00:25,850
So put will change an existing piece of data and delete will delete the piece

7
00:00:26,760 --> 00:00:29,160
of data. Let's try it out for size.

8
00:00:29,790 --> 00:00:34,440
If we go into the documentation and we scroll down to all the things that we can

9
00:00:34,440 --> 00:00:36,120
do to pixels, you can see

10
00:00:36,120 --> 00:00:39,840
we can also update a pixel, which is put, and delete a pixel.

11
00:00:40,200 --> 00:00:44,850
So let's click on update first and let's say that, um,

12
00:00:44,940 --> 00:00:49,620
I decided that yesterday I didn't actually cycle 15 kilometers. I lied.

13
00:00:49,650 --> 00:00:54,330
I only cycled 4. Well, how would we do that? Well,

14
00:00:54,340 --> 00:00:59,340
we would use the documentation here and also the put method to achieve this.

15
00:01:00,660 --> 00:01:05,250
So I want you to give it a go and see if you can figure it out. Now,

16
00:01:05,310 --> 00:01:09,810
I've already told you that you need to use the put method and you know how to

17
00:01:09,840 --> 00:01:13,860
format the endpoints and provide the request body and the header.

18
00:01:14,160 --> 00:01:17,370
So pause the video and give this challenge a go.

19
00:01:19,730 --> 00:01:20,390
Right?

20
00:01:20,390 --> 00:01:20,720
All right.

21
00:01:20,720 --> 00:01:25,720
So the first thing we're going to do is to copy this new format of the end

22
00:01:25,910 --> 00:01:29,630
point and I'm going to comment out the previous part

23
00:01:29,630 --> 00:01:34,610
where we posted our pixel and I'm going to create this new endpoint.

24
00:01:34,640 --> 00:01:36,800
So this is the update_endpoint,

25
00:01:38,660 --> 00:01:42,470
and it's going to be a combination of everything up to here,

26
00:01:42,680 --> 00:01:44,450
which is our pixela

27
00:01:44,630 --> 00:01:45,463
...

28
00:01:47,480 --> 00:01:50,840
endpoint. And then it's your own username

29
00:01:53,570 --> 00:01:56,360
and then it's graphs and then the graph ID.

30
00:01:58,760 --> 00:02:00,800
And finally, it's the year,

31
00:02:00,800 --> 00:02:04,850
month and date for the piece of data that you actually want to update.

32
00:02:05,870 --> 00:02:09,530
So let's say that I'm going to update yesterday's piece of data

33
00:02:09,560 --> 00:02:11,870
which I've manually created up here.

34
00:02:11,960 --> 00:02:15,320
And I formatted using strftime right here.

35
00:02:15,830 --> 00:02:20,830
So I'm going to replace this with a set of curly brackets and paste that in

36
00:02:22,220 --> 00:02:25,460
there. Now notice,

37
00:02:25,460 --> 00:02:28,850
because I've got these two sets of double quotes,

38
00:02:28,910 --> 00:02:30,380
it's getting a little bit confused.

39
00:02:30,680 --> 00:02:34,790
So I'm going to change one of those sets to a single quote instead.

40
00:02:35,480 --> 00:02:38,960
And now I get rid of all my errors and warnings.

41
00:02:40,220 --> 00:02:42,560
So this is our update endpoint.

42
00:02:43,130 --> 00:02:46,580
And the only thing that we're going to change is the quantity.

43
00:02:47,810 --> 00:02:51,980
Let's say I create a new pixel data dictionary.

44
00:02:52,430 --> 00:02:57,430
And now my quantity instead of being 15 kilometers was, actually,  let's say 4.5.

45
00:03:03,160 --> 00:03:04,990
So this is my new pixel data,

46
00:03:05,410 --> 00:03:08,470
and now I'm ready to make my put request.

47
00:03:08,860 --> 00:03:12,040
So we're using requests.put,

48
00:03:12,340 --> 00:03:15,970
and then the URL is going to be the update endpoint,

49
00:03:16,630 --> 00:03:20,170
the JSON is going to be our new pixel data,

50
00:03:20,710 --> 00:03:25,330
and finally, the headers is going to be our headers for authentication.

51
00:03:26,550 --> 00:03:27,383
Right.

52
00:03:33,240 --> 00:03:35,880
So now let's go ahead and run this code.

53
00:03:36,480 --> 00:03:41,460
And once we get our message success, we can go back to our chart,

54
00:03:41,700 --> 00:03:42,533
refresh it,

55
00:03:42,990 --> 00:03:47,880
and you'll see that yesterday's pixel no longer is as dense.

56
00:03:47,970 --> 00:03:48,720
And in fact,

57
00:03:48,720 --> 00:03:53,610
it's now lost a bit of color and it's been updated with the new value.

58
00:03:55,290 --> 00:03:56,130
So finally,

59
00:03:56,130 --> 00:04:01,130
we have our delete method and we're going to use that to delete a pixel.

60
00:04:04,620 --> 00:04:06,690
Again, I want you to do this by yourself.

61
00:04:06,750 --> 00:04:11,750
See if you can delete one of the pixels in your graph using the endpoint and the

62
00:04:11,880 --> 00:04:12,750
documentation.

63
00:04:14,190 --> 00:04:15,023
Go!

64
00:04:16,410 --> 00:04:20,700
All right. So the endpoint is identical to the update endpoint.

65
00:04:20,730 --> 00:04:25,730
So I'm simply just going to copy it and create a new one called delete.

66
00:04:27,960 --> 00:04:32,610
And I'm also gonna comment out this request so that I use this one

67
00:04:32,610 --> 00:04:36,990
instead. Now the delete endpoint doesn't actually take any data,

68
00:04:37,170 --> 00:04:38,580
but it does need the header.

69
00:04:39,120 --> 00:04:44,120
So we can use our request module and call the delete method and pass in the end

70
00:04:45,630 --> 00:04:49,230
point which is the delete endpoint, the headers

71
00:04:49,260 --> 00:04:51,720
which is our authentication headers,

72
00:04:52,260 --> 00:04:55,830
and we can already enact our request.

73
00:05:01,920 --> 00:05:02,580
Right

74
00:05:02,580 --> 00:05:06,720
Now when we hit run, if it gives us a success message,

75
00:05:07,050 --> 00:05:10,740
then we know that we've updated our data here

76
00:05:11,280 --> 00:05:13,470
and we now no longer have two pixels.

77
00:05:13,500 --> 00:05:16,980
We only have one cause we deleted the one for yesterday.

78
00:05:17,970 --> 00:05:22,860
That's all there is to it. If now you want to be able to track your habit,

79
00:05:23,190 --> 00:05:28,190
all you have to do is to comment out all of the rest of the responses and

80
00:05:29,580 --> 00:05:34,580
reactivate the one where we post our pixel data and changing the quantity into a

81
00:05:37,140 --> 00:05:40,400
custom input. So in my case,

82
00:05:40,410 --> 00:05:45,410
it'll be how many kilometers did you cycle today?

83
00:05:47,940 --> 00:05:52,320
And then of course changing our datetime back to now.

84
00:05:54,090 --> 00:05:56,850
And now when I run my code,

85
00:05:57,680 --> 00:06:01,390
it will go ahead and ask me how many kilometers I run.

86
00:06:01,720 --> 00:06:05,350
I could type in the number, and then when I hit enter there,

87
00:06:05,440 --> 00:06:09,460
data is going to be posted to the servers. Now, of course,

88
00:06:09,460 --> 00:06:13,180
because I already have a piece of data, this is not going to let me,

89
00:06:13,570 --> 00:06:17,740
so I'm going to have to wait for tomorrow to post my updated cycling data.

90
00:06:18,550 --> 00:06:21,340
I hope you enjoyed building this project together.

91
00:06:21,730 --> 00:06:26,730
And I hope you'll have a go at trying to modify this to your own habit tracking

92
00:06:27,310 --> 00:06:31,240
and see if you can do some creative stuff with it. And as always,

93
00:06:31,360 --> 00:06:34,540
if you create something fun, be sure to share it with the rest of us.

