WEBVTT

0
00:00.120 --> 00:03.000
So we're now finally ready to move on to the last step,

1
00:03.330 --> 00:05.940
which is posting a value to our graph.

2
00:06.360 --> 00:10.920
And this is going to create a new pixel on our habit graph.

3
00:11.820 --> 00:15.570
So I want you to pull up the relevant piece of documentation

4
00:15.660 --> 00:18.630
which is going to be posting a pixel.

5
00:19.320 --> 00:24.320
And I want you to take a look at the description and the header and the body,

6
00:25.470 --> 00:28.320
and see if you can complete the step as a challenge.

7
00:28.710 --> 00:33.330
Notice that, firstly, the endpoint is again different and everything that's

8
00:33.330 --> 00:34.163
inside

9
00:34.170 --> 00:38.520
these angle brackets need to be replaced with your actual values.

10
00:38.580 --> 00:42.660
So your actual username and your actual graph ID. In addition,

11
00:42.720 --> 00:47.720
this is again going to be a post request and it requires the authentication in

12
00:48.150 --> 00:50.670
the request header. Finally,

13
00:50.670 --> 00:55.530
you have to provide the date in the format of four characters for the year,

14
00:55.530 --> 00:58.710
two characters for the month and two characters for the day.

15
00:59.010 --> 01:01.380
So today would be 2020

16
01:01.410 --> 01:04.740
0724 in this format.

17
01:05.280 --> 01:09.750
And then you have to provide the quantity that you want to add to today's pixel.

18
01:10.050 --> 01:14.250
So if you cycled 10.5 kilometers, then it would be 10.5.

19
01:14.640 --> 01:19.080
If you originally set up your graph to track integers, for example,

20
01:19.080 --> 01:22.590
the number of pages you read in a book, then that would be the number.

21
01:23.310 --> 01:27.780
So I want you to pause the video and see if you can complete this step as a

22
01:27.780 --> 01:29.850
challenge. Have a go now.

23
01:33.260 --> 01:37.430
All right. So the first thing we're going to do is to update our end point.

24
01:38.720 --> 01:42.590
I'm gonna comment out this previous step because we've already created our graph

25
01:43.040 --> 01:45.890
and we're now going to move on to our pixel creation.

26
01:46.010 --> 01:48.890
So that's what I'm going to call my endpoint.

27
01:51.260 --> 01:55.550
Now, this pixel creation endpoint is obviously not what we need.

28
01:55.610 --> 01:59.990
We need to format this URL so that we add in the first part

29
02:00.110 --> 02:04.520
which is the pixela endpoint which goes up to /users.

30
02:05.330 --> 02:07.160
So that's all of this part.

31
02:07.280 --> 02:11.180
So we can use an f-string and add the pixel endpoint.

32
02:11.570 --> 02:16.370
And then we're going to replace the /username with our own

33
02:16.400 --> 02:19.880
username, and then it's /graphs

34
02:20.030 --> 02:23.930
and we have to replace this graph ID as well. And in my case,

35
02:23.930 --> 02:26.570
the graph ID is simply just graph1.

36
02:26.990 --> 02:30.830
So it might make sense to create a constant for that as well,

37
02:31.160 --> 02:31.993
<v 1>right?</v>

38
02:41.630 --> 02:45.950
<v 0>So that we can use it at the very end of this endpoint.</v>

39
02:47.090 --> 02:49.550
So now that we've created our end point,

40
02:49.940 --> 02:53.420
the next step is to create the request body.

41
02:53.780 --> 02:56.570
So passing over the date and the quantity.

42
02:57.620 --> 02:59.740
So I'm going to create the pixel_data,

43
03:00.790 --> 03:04.000
and this is going to first have the date,

44
03:04.300 --> 03:07.360
but it has to be formatted in the way they specified.

45
03:07.510 --> 03:11.350
So four characters for the year, which is 2020,

46
03:11.380 --> 03:13.060
and then two characters for the month,

47
03:13.330 --> 03:15.850
07, and then two characters for the day.

48
03:16.510 --> 03:21.510
But notice here that they specify the data type for the date has to be a string.

49
03:22.390 --> 03:24.640
So we can't simply leave it as a number.

50
03:24.790 --> 03:27.610
So we have to add some quotation marks around it.

51
03:28.480 --> 03:31.360
And then finally we can add the actual data

52
03:31.480 --> 03:35.140
which is going to go under the key quantity

53
03:35.680 --> 03:37.840
and this is also going to be a string

54
03:38.140 --> 03:41.830
and this is the quantity that we're going to register in the pixel

55
03:42.070 --> 03:46.360
and that's going to determine the intensity of the actual pixel.

56
03:47.320 --> 03:51.370
So in my case, I did actually go for a quick ride today. Um,

57
03:51.880 --> 03:55.480
and my stats were not fantastic, but, um,

58
03:55.780 --> 03:56.890
I'm still gonna log it.

59
03:57.040 --> 04:00.790
So that was 9.74 kilometers that I cycled.

60
04:01.660 --> 04:06.640
So that's what my quantity is going to be, 9.74. And again,

61
04:06.670 --> 04:10.690
this is a string because that's what the documentation tells us.

62
04:12.160 --> 04:14.320
And remember that when you set up your graph,

63
04:14.410 --> 04:17.080
if you set up the data type to receive floats,

64
04:17.380 --> 04:21.340
then you can actually give it a floating-point number. If you set it up as int,

65
04:21.350 --> 04:23.380
then you have to give it a whole number.

66
04:24.340 --> 04:26.650
Now that we've got our data and our endpoint,

67
04:26.680 --> 04:31.330
then we're finally ready to create our requests. So you request.posts.

68
04:31.690 --> 04:32.590
And again,

69
04:32.620 --> 04:36.910
our URL is going to be this new pixel creation endpoint.

70
04:38.230 --> 04:42.100
Our JSON that we're going to pass over is our pixel_data,

71
04:42.820 --> 04:43.990
and finally,

72
04:44.020 --> 04:47.920
we're going to reuse the headers that we had from previously,

73
04:48.340 --> 04:52.750
because it still contains the same token that we need to authenticate ourselves.

74
04:53.440 --> 04:56.350
So let's see what is the response that we get back.

75
04:56.470 --> 05:01.470
So I'm going to print out the response.text and hopefully if everything went

76
05:03.790 --> 05:07.450
well, then we should get the success message.

77
05:08.110 --> 05:10.570
But now is the most exciting part.

78
05:10.780 --> 05:14.710
You can go back to our cycling graph and then hit refresh,

79
05:15.040 --> 05:20.040
and we should see all brand new pixel show up in our graph right there.

80
05:22.540 --> 05:26.980
And it tells us today we cycled 9.74 kilometers.

81
05:28.300 --> 05:30.520
One thing you might be wondering is, well,

82
05:30.580 --> 05:33.370
how can we get this date to be automated?

83
05:33.400 --> 05:36.550
Because I don't want to have to check the date and type it in.

84
05:36.790 --> 05:40.510
We can use the date time module, right? So in the next lesson,

85
05:40.540 --> 05:45.370
I'm going to show you how you can use a method called strftime in order to

86
05:45.370 --> 05:50.370
format the date time that you get from Python into any format that you require.

87
05:51.820 --> 05:54.640
For all of that and more, I'll see you on the next lesson.