Now in the very beginning of today's lessons,
I mentioned that we can do other requests in addition to get and post like put
and delete.
And we can do that using a similar format as we have done for get and post.
Just by simply changing the method we end up with a different behavior.
So put will change an existing piece of data and delete will delete the piece
of data. Let's try it out for size.
If we go into the documentation and we scroll down to all the things that we can
do to pixels, you can see
we can also update a pixel, which is put, and delete a pixel.
So let's click on update first and let's say that, um,
I decided that yesterday I didn't actually cycle 15 kilometers. I lied.
I only cycled 4. Well, how would we do that? Well,
we would use the documentation here and also the put method to achieve this.
So I want you to give it a go and see if you can figure it out. Now,
I've already told you that you need to use the put method and you know how to
format the endpoints and provide the request body and the header.
So pause the video and give this challenge a go.
Right?
All right.
So the first thing we're going to do is to copy this new format of the end
point and I'm going to comment out the previous part
where we posted our pixel and I'm going to create this new endpoint.
So this is the update_endpoint,
and it's going to be a combination of everything up to here,
which is our pixela
...
endpoint. And then it's your own username
and then it's graphs and then the graph ID.
And finally, it's the year,
month and date for the piece of data that you actually want to update.
So let's say that I'm going to update yesterday's piece of data
which I've manually created up here.
And I formatted using strftime right here.
So I'm going to replace this with a set of curly brackets and paste that in
there. Now notice,
because I've got these two sets of double quotes,
it's getting a little bit confused.
So I'm going to change one of those sets to a single quote instead.
And now I get rid of all my errors and warnings.
So this is our update endpoint.
And the only thing that we're going to change is the quantity.
Let's say I create a new pixel data dictionary.
And now my quantity instead of being 15 kilometers was, actually, let's say 4.5.
So this is my new pixel data,
and now I'm ready to make my put request.
So we're using requests.put,
and then the URL is going to be the update endpoint,
the JSON is going to be our new pixel data,
and finally, the headers is going to be our headers for authentication.
Right.
So now let's go ahead and run this code.
And once we get our message success, we can go back to our chart,
refresh it,
and you'll see that yesterday's pixel no longer is as dense.
And in fact,
it's now lost a bit of color and it's been updated with the new value.
So finally,
we have our delete method and we're going to use that to delete a pixel.
Again, I want you to do this by yourself.
See if you can delete one of the pixels in your graph using the endpoint and the
documentation.
Go!
All right. So the endpoint is identical to the update endpoint.
So I'm simply just going to copy it and create a new one called delete.
And I'm also gonna comment out this request so that I use this one
instead. Now the delete endpoint doesn't actually take any data,
but it does need the header.
So we can use our request module and call the delete method and pass in the end
point which is the delete endpoint, the headers
which is our authentication headers,
and we can already enact our request.
Right
Now when we hit run, if it gives us a success message,
then we know that we've updated our data here
and we now no longer have two pixels.
We only have one cause we deleted the one for yesterday.
That's all there is to it. If now you want to be able to track your habit,
all you have to do is to comment out all of the rest of the responses and
reactivate the one where we post our pixel data and changing the quantity into a
custom input. So in my case,
it'll be how many kilometers did you cycle today?
And then of course changing our datetime back to now.
And now when I run my code,
it will go ahead and ask me how many kilometers I run.
I could type in the number, and then when I hit enter there,
data is going to be posted to the servers. Now, of course,
because I already have a piece of data, this is not going to let me,
so I'm going to have to wait for tomorrow to post my updated cycling data.
I hope you enjoyed building this project together.
And I hope you'll have a go at trying to modify this to your own habit tracking
and see if you can do some creative stuff with it. And as always,
if you create something fun, be sure to share it with the rest of us.