1
00:00:00,120 --> 00:00:03,000
So we're now finally ready to move on to the last step,

2
00:00:03,330 --> 00:00:05,939
which is posting a value to our graph.

3
00:00:06,360 --> 00:00:10,920
And this is going to create a new pixel on our habit graph.

4
00:00:11,820 --> 00:00:15,570
So I want you to pull up the relevant piece of documentation

5
00:00:15,660 --> 00:00:18,630
which is going to be posting a pixel.

6
00:00:19,320 --> 00:00:24,320
And I want you to take a look at the description and the header and the body,

7
00:00:25,470 --> 00:00:28,320
and see if you can complete the step as a challenge.

8
00:00:28,710 --> 00:00:33,330
Notice that, firstly, the endpoint is again different and everything that's

9
00:00:33,330 --> 00:00:34,163
inside

10
00:00:34,170 --> 00:00:38,520
these angle brackets need to be replaced with your actual values.

11
00:00:38,580 --> 00:00:42,660
So your actual username and your actual graph ID. In addition,

12
00:00:42,720 --> 00:00:47,720
this is again going to be a post request and it requires the authentication in

13
00:00:48,150 --> 00:00:50,670
the request header. Finally,

14
00:00:50,670 --> 00:00:55,530
you have to provide the date in the format of four characters for the year,

15
00:00:55,530 --> 00:00:58,710
two characters for the month and two characters for the day.

16
00:00:59,010 --> 00:01:01,380
So today would be 2020

17
00:01:01,410 --> 00:01:04,739
0724 in this format.

18
00:01:05,280 --> 00:01:09,750
And then you have to provide the quantity that you want to add to today's pixel.

19
00:01:10,050 --> 00:01:14,250
So if you cycled 10.5 kilometers, then it would be 10.5.

20
00:01:14,640 --> 00:01:19,080
If you originally set up your graph to track integers, for example,

21
00:01:19,080 --> 00:01:22,590
the number of pages you read in a book, then that would be the number.

22
00:01:23,310 --> 00:01:27,780
So I want you to pause the video and see if you can complete this step as a

23
00:01:27,780 --> 00:01:29,850
challenge. Have a go now.

24
00:01:33,260 --> 00:01:37,430
All right. So the first thing we're going to do is to update our end point.

25
00:01:38,720 --> 00:01:42,590
I'm gonna comment out this previous step because we've already created our graph

26
00:01:43,040 --> 00:01:45,890
and we're now going to move on to our pixel creation.

27
00:01:46,010 --> 00:01:48,890
So that's what I'm going to call my endpoint.

28
00:01:51,260 --> 00:01:55,550
Now, this pixel creation endpoint is obviously not what we need.

29
00:01:55,610 --> 00:01:59,990
We need to format this URL so that we add in the first part

30
00:02:00,110 --> 00:02:04,520
which is the pixela endpoint which goes up to /users.

31
00:02:05,330 --> 00:02:07,160
So that's all of this part.

32
00:02:07,280 --> 00:02:11,180
So we can use an f-string and add the pixel endpoint.

33
00:02:11,570 --> 00:02:16,370
And then we're going to replace the /username with our own

34
00:02:16,400 --> 00:02:19,880
username, and then it's /graphs

35
00:02:20,030 --> 00:02:23,930
and we have to replace this graph ID as well. And in my case,

36
00:02:23,930 --> 00:02:26,570
the graph ID is simply just graph1.

37
00:02:26,990 --> 00:02:30,830
So it might make sense to create a constant for that as well,

38
00:02:31,160 --> 00:02:31,993
right?

39
00:02:41,630 --> 00:02:45,950
So that we can use it at the very end of this endpoint.

40
00:02:47,090 --> 00:02:49,550
So now that we've created our end point,

41
00:02:49,940 --> 00:02:53,420
the next step is to create the request body.

42
00:02:53,780 --> 00:02:56,570
So passing over the date and the quantity.

43
00:02:57,620 --> 00:02:59,740
So I'm going to create the pixel_data,

44
00:03:00,790 --> 00:03:04,000
and this is going to first have the date,

45
00:03:04,300 --> 00:03:07,360
but it has to be formatted in the way they specified.

46
00:03:07,510 --> 00:03:11,350
So four characters for the year, which is 2020,

47
00:03:11,380 --> 00:03:13,060
and then two characters for the month,

48
00:03:13,330 --> 00:03:15,850
07, and then two characters for the day.

49
00:03:16,510 --> 00:03:21,510
But notice here that they specify the data type for the date has to be a string.

50
00:03:22,390 --> 00:03:24,640
So we can't simply leave it as a number.

51
00:03:24,790 --> 00:03:27,610
So we have to add some quotation marks around it.

52
00:03:28,480 --> 00:03:31,360
And then finally we can add the actual data

53
00:03:31,480 --> 00:03:35,140
which is going to go under the key quantity

54
00:03:35,680 --> 00:03:37,840
and this is also going to be a string

55
00:03:38,140 --> 00:03:41,830
and this is the quantity that we're going to register in the pixel

56
00:03:42,070 --> 00:03:46,360
and that's going to determine the intensity of the actual pixel.

57
00:03:47,320 --> 00:03:51,370
So in my case, I did actually go for a quick ride today. Um,

58
00:03:51,880 --> 00:03:55,480
and my stats were not fantastic, but, um,

59
00:03:55,780 --> 00:03:56,890
I'm still gonna log it.

60
00:03:57,040 --> 00:04:00,790
So that was 9.74 kilometers that I cycled.

61
00:04:01,660 --> 00:04:06,640
So that's what my quantity is going to be, 9.74. And again,

62
00:04:06,670 --> 00:04:10,690
this is a string because that's what the documentation tells us.

63
00:04:12,160 --> 00:04:14,320
And remember that when you set up your graph,

64
00:04:14,410 --> 00:04:17,079
if you set up the data type to receive floats,

65
00:04:17,380 --> 00:04:21,339
then you can actually give it a floating-point number. If you set it up as int,

66
00:04:21,350 --> 00:04:23,380
then you have to give it a whole number.

67
00:04:24,340 --> 00:04:26,650
Now that we've got our data and our endpoint,

68
00:04:26,680 --> 00:04:31,330
then we're finally ready to create our requests. So you request.posts.

69
00:04:31,690 --> 00:04:32,590
And again,

70
00:04:32,620 --> 00:04:36,910
our URL is going to be this new pixel creation endpoint.

71
00:04:38,230 --> 00:04:42,100
Our JSON that we're going to pass over is our pixel_data,

72
00:04:42,820 --> 00:04:43,990
and finally,

73
00:04:44,020 --> 00:04:47,920
we're going to reuse the headers that we had from previously,

74
00:04:48,340 --> 00:04:52,750
because it still contains the same token that we need to authenticate ourselves.

75
00:04:53,440 --> 00:04:56,350
So let's see what is the response that we get back.

76
00:04:56,470 --> 00:05:01,470
So I'm going to print out the response.text and hopefully if everything went

77
00:05:03,790 --> 00:05:07,450
well, then we should get the success message.

78
00:05:08,110 --> 00:05:10,570
But now is the most exciting part.

79
00:05:10,780 --> 00:05:14,710
You can go back to our cycling graph and then hit refresh,

80
00:05:15,040 --> 00:05:20,040
and we should see all brand new pixel show up in our graph right there.

81
00:05:22,540 --> 00:05:26,980
And it tells us today we cycled 9.74 kilometers.

82
00:05:28,300 --> 00:05:30,520
One thing you might be wondering is, well,

83
00:05:30,580 --> 00:05:33,370
how can we get this date to be automated?

84
00:05:33,400 --> 00:05:36,550
Because I don't want to have to check the date and type it in.

85
00:05:36,790 --> 00:05:40,510
We can use the date time module, right? So in the next lesson,

86
00:05:40,540 --> 00:05:45,370
I'm going to show you how you can use a method called strftime in order to

87
00:05:45,370 --> 00:05:50,370
format the date time that you get from Python into any format that you require.

88
00:05:51,820 --> 00:05:54,640
For all of that and more, I'll see you on the next lesson.

