So we're now finally ready to move on to the last step,
which is posting a value to our graph.
And this is going to create a new pixel on our habit graph.
So I want you to pull up the relevant piece of documentation
which is going to be posting a pixel.
And I want you to take a look at the description and the header and the body,
and see if you can complete the step as a challenge.
Notice that, firstly, the endpoint is again different and everything that's
inside
these angle brackets need to be replaced with your actual values.
So your actual username and your actual graph ID. In addition,
this is again going to be a post request and it requires the authentication in
the request header. Finally,
you have to provide the date in the format of four characters for the year,
two characters for the month and two characters for the day.
So today would be 2020
0724 in this format.
And then you have to provide the quantity that you want to add to today's pixel.
So if you cycled 10.5 kilometers, then it would be 10.5.
If you originally set up your graph to track integers, for example,
the number of pages you read in a book, then that would be the number.
So I want you to pause the video and see if you can complete this step as a
challenge. Have a go now.
All right. So the first thing we're going to do is to update our end point.
I'm gonna comment out this previous step because we've already created our graph
and we're now going to move on to our pixel creation.
So that's what I'm going to call my endpoint.
Now, this pixel creation endpoint is obviously not what we need.
We need to format this URL so that we add in the first part
which is the pixela endpoint which goes up to /users.
So that's all of this part.
So we can use an f-string and add the pixel endpoint.
And then we're going to replace the /username with our own
username, and then it's /graphs
and we have to replace this graph ID as well. And in my case,
the graph ID is simply just graph1.
So it might make sense to create a constant for that as well,
1: right?
0: So that we can use it at the very end of this endpoint.
So now that we've created our end point,
the next step is to create the request body.
So passing over the date and the quantity.
So I'm going to create the pixel_data,
and this is going to first have the date,
but it has to be formatted in the way they specified.
So four characters for the year, which is 2020,
and then two characters for the month,
07, and then two characters for the day.
But notice here that they specify the data type for the date has to be a string.
So we can't simply leave it as a number.
So we have to add some quotation marks around it.
And then finally we can add the actual data
which is going to go under the key quantity
and this is also going to be a string
and this is the quantity that we're going to register in the pixel
and that's going to determine the intensity of the actual pixel.
So in my case, I did actually go for a quick ride today. Um,
and my stats were not fantastic, but, um,
I'm still gonna log it.
So that was 9.74 kilometers that I cycled.
So that's what my quantity is going to be, 9.74. And again,
this is a string because that's what the documentation tells us.
And remember that when you set up your graph,
if you set up the data type to receive floats,
then you can actually give it a floating-point number. If you set it up as int,
then you have to give it a whole number.
Now that we've got our data and our endpoint,
then we're finally ready to create our requests. So you request.posts.
And again,
our URL is going to be this new pixel creation endpoint.
Our JSON that we're going to pass over is our pixel_data,
and finally,
we're going to reuse the headers that we had from previously,
because it still contains the same token that we need to authenticate ourselves.
So let's see what is the response that we get back.
So I'm going to print out the response.text and hopefully if everything went
well, then we should get the success message.
But now is the most exciting part.
You can go back to our cycling graph and then hit refresh,
and we should see all brand new pixel show up in our graph right there.
And it tells us today we cycled 9.74 kilometers.
One thing you might be wondering is, well,
how can we get this date to be automated?
Because I don't want to have to check the date and type it in.
We can use the date time module, right? So in the next lesson,
I'm going to show you how you can use a method called strftime in order to
format the date time that you get from Python into any format that you require.
For all of that and more, I'll see you on the next lesson.