WEBVTT

0
00:00.150 --> 00:02.820
Now in the very beginning of today's lessons,

1
00:02.880 --> 00:07.880
I mentioned that we can do other requests in addition to get and post like put

2
00:08.580 --> 00:09.413
and delete.

3
00:09.900 --> 00:14.900
And we can do that using a similar format as we have done for get and post.

4
00:15.750 --> 00:20.250
Just by simply changing the method we end up with a different behavior.

5
00:20.850 --> 00:25.850
So put will change an existing piece of data and delete will delete the piece

6
00:26.760 --> 00:29.160
of data. Let's try it out for size.

7
00:29.790 --> 00:34.440
If we go into the documentation and we scroll down to all the things that we can

8
00:34.440 --> 00:36.120
do to pixels, you can see

9
00:36.120 --> 00:39.840
we can also update a pixel, which is put, and delete a pixel.

10
00:40.200 --> 00:44.850
So let's click on update first and let's say that, um,

11
00:44.940 --> 00:49.620
I decided that yesterday I didn't actually cycle 15 kilometers. I lied.

12
00:49.650 --> 00:54.330
I only cycled 4. Well, how would we do that? Well,

13
00:54.340 --> 00:59.340
we would use the documentation here and also the put method to achieve this.

14
01:00.660 --> 01:05.250
So I want you to give it a go and see if you can figure it out. Now,

15
01:05.310 --> 01:09.810
I've already told you that you need to use the put method and you know how to

16
01:09.840 --> 01:13.860
format the endpoints and provide the request body and the header.

17
01:14.160 --> 01:17.370
So pause the video and give this challenge a go.

18
01:19.730 --> 01:20.390
<v 1>Right?</v>

19
01:20.390 --> 01:20.720
<v 0>All right.</v>

20
01:20.720 --> 01:25.720
So the first thing we're going to do is to copy this new format of the end

21
01:25.910 --> 01:29.630
point and I'm going to comment out the previous part

22
01:29.630 --> 01:34.610
where we posted our pixel and I'm going to create this new endpoint.

23
01:34.640 --> 01:36.800
So this is the update_endpoint,

24
01:38.660 --> 01:42.470
and it's going to be a combination of everything up to here,

25
01:42.680 --> 01:44.450
which is our pixela

26
01:44.630 --> 01:45.463
<v 1>...</v>

27
01:47.480 --> 01:50.840
<v 0>endpoint. And then it's your own username</v>

28
01:53.570 --> 01:56.360
and then it's graphs and then the graph ID.

29
01:58.760 --> 02:00.800
And finally, it's the year,

30
02:00.800 --> 02:04.850
month and date for the piece of data that you actually want to update.

31
02:05.870 --> 02:09.530
So let's say that I'm going to update yesterday's piece of data

32
02:09.560 --> 02:11.870
which I've manually created up here.

33
02:11.960 --> 02:15.320
And I formatted using strftime right here.

34
02:15.830 --> 02:20.830
So I'm going to replace this with a set of curly brackets and paste that in

35
02:22.220 --> 02:25.460
there. Now notice,

36
02:25.460 --> 02:28.850
because I've got these two sets of double quotes,

37
02:28.910 --> 02:30.380
it's getting a little bit confused.

38
02:30.680 --> 02:34.790
So I'm going to change one of those sets to a single quote instead.

39
02:35.480 --> 02:38.960
And now I get rid of all my errors and warnings.

40
02:40.220 --> 02:42.560
So this is our update endpoint.

41
02:43.130 --> 02:46.580
And the only thing that we're going to change is the quantity.

42
02:47.810 --> 02:51.980
Let's say I create a new pixel data dictionary.

43
02:52.430 --> 02:57.430
And now my quantity instead of being 15 kilometers was, actually,  let's say 4.5.

44
03:03.160 --> 03:04.990
So this is my new pixel data,

45
03:05.410 --> 03:08.470
and now I'm ready to make my put request.

46
03:08.860 --> 03:12.040
So we're using requests.put,

47
03:12.340 --> 03:15.970
and then the URL is going to be the update endpoint,

48
03:16.630 --> 03:20.170
the JSON is going to be our new pixel data,

49
03:20.710 --> 03:25.330
and finally, the headers is going to be our headers for authentication.

50
03:26.550 --> 03:27.383
<v 1>Right.</v>

51
03:33.240 --> 03:35.880
<v 0>So now let's go ahead and run this code.</v>

52
03:36.480 --> 03:41.460
And once we get our message success, we can go back to our chart,

53
03:41.700 --> 03:42.533
refresh it,

54
03:42.990 --> 03:47.880
and you'll see that yesterday's pixel no longer is as dense.

55
03:47.970 --> 03:48.720
And in fact,

56
03:48.720 --> 03:53.610
it's now lost a bit of color and it's been updated with the new value.

57
03:55.290 --> 03:56.130
So finally,

58
03:56.130 --> 04:01.130
we have our delete method and we're going to use that to delete a pixel.

59
04:04.620 --> 04:06.690
Again, I want you to do this by yourself.

60
04:06.750 --> 04:11.750
See if you can delete one of the pixels in your graph using the endpoint and the

61
04:11.880 --> 04:12.750
documentation.

62
04:14.190 --> 04:15.023
<v 1>Go!</v>

63
04:16.410 --> 04:20.700
<v 0>All right. So the endpoint is identical to the update endpoint.</v>

64
04:20.730 --> 04:25.730
So I'm simply just going to copy it and create a new one called delete.

65
04:27.960 --> 04:32.610
And I'm also gonna comment out this request so that I use this one

66
04:32.610 --> 04:36.990
instead. Now the delete endpoint doesn't actually take any data,

67
04:37.170 --> 04:38.580
but it does need the header.

68
04:39.120 --> 04:44.120
So we can use our request module and call the delete method and pass in the end

69
04:45.630 --> 04:49.230
point which is the delete endpoint, the headers

70
04:49.260 --> 04:51.720
which is our authentication headers,

71
04:52.260 --> 04:55.830
and we can already enact our request.

72
05:01.920 --> 05:02.580
<v 1>Right</v>

73
05:02.580 --> 05:06.720
<v 0>Now when we hit run, if it gives us a success message,</v>

74
05:07.050 --> 05:10.740
then we know that we've updated our data here

75
05:11.280 --> 05:13.470
and we now no longer have two pixels.

76
05:13.500 --> 05:16.980
We only have one cause we deleted the one for yesterday.

77
05:17.970 --> 05:22.860
That's all there is to it. If now you want to be able to track your habit,

78
05:23.190 --> 05:28.190
all you have to do is to comment out all of the rest of the responses and

79
05:29.580 --> 05:34.580
reactivate the one where we post our pixel data and changing the quantity into a

80
05:37.140 --> 05:40.400
custom input. So in my case,

81
05:40.410 --> 05:45.410
it'll be how many kilometers did you cycle today?

82
05:47.940 --> 05:52.320
And then of course changing our datetime back to now.

83
05:54.090 --> 05:56.850
And now when I run my code,

84
05:57.680 --> 06:01.390
it will go ahead and ask me how many kilometers I run.

85
06:01.720 --> 06:05.350
I could type in the number, and then when I hit enter there,

86
06:05.440 --> 06:09.460
data is going to be posted to the servers. Now, of course,

87
06:09.460 --> 06:13.180
because I already have a piece of data, this is not going to let me,

88
06:13.570 --> 06:17.740
so I'm going to have to wait for tomorrow to post my updated cycling data.

89
06:18.550 --> 06:21.340
I hope you enjoyed building this project together.

90
06:21.730 --> 06:26.730
And I hope you'll have a go at trying to modify this to your own habit tracking

91
06:27.310 --> 06:31.240
and see if you can do some creative stuff with it. And as always,

92
06:31.360 --> 06:34.540
if you create something fun, be sure to share it with the rest of us.