We've been using the requests module for a while now,
and we've been using it to make HTTP requests across the internet.
We've already seen the get request,
and this is a way for us to get pieces of data from somebody else,
like an API provider.
Now there's also four other common types of requests that we should probably know
about; post put and delete.
We can use the request module to complete a get request by simply writing
requests.get
and then we put our parameters inside the parentheses. For the other three
requests,
the code looks pretty much the same. It's .post, .put, and .delete.
Whereas a get request is made where we ask an external system for a
particular piece of data and they give that to us in the response.
a post request is where we give the external system some piece of data
and we're not so interested in the response we're getting back other than
whether if it was successful or not. For example,
if you wanted to save a piece of data in Google sheets,
then you might use the Google sheets API
to you post your data into your Google sheet.
Or maybe you want to use the Twitter API to post a tweet
then your program is going to send your tweet through a post request to the
Twitter API. So that's how get
and post works. Put is where you simply update a piece of data in the
external service. So if you had a spreadsheet in Google sheets
and maybe you want to update some of the values in the spreadsheet,
then you would use a put request.
And finally delete is simply where you want to delete a piece of data in the
external service
like a tweet that you posted or a Facebook post. Today
we're going to be building our habit tracker using the Pixela API.
And in order to use this API,
we're going to post our habit tracking data, for example,
how many pages have a book we've read, how many kilometers we've cycled,
and we're going to be posting that data to Pixela
to be tracked in our graph.
If you head over to their website and you scroll down,
you can see that it tells you how to get started.
And it's a series of six steps.
So we're going to be completing these in order
and we're going to be pairing this short form
get started guide with their documentation.
So we're going to have that side by side in order to see all of the
documentation on each of the steps. Step one
involves creating a user account. And it says you have to hit up this particular
end point using the HTTP post request.
So this is the first time we're making a post request.
Now I've set up a new project in PyCharm habit tracker
and in my main.py, as always, that I'm going to start off by importing the
requests module. Now,
because this is a brand new project we're going to have to install that package
to make this red underline go away.
And once it's been installed successfully, we can tap into that method requests
.post. What are we going to
put into this parentheses, what are the parameters, what are the inputs?
Well, the first thing is the URL endpoint, right?
So this is the Pixela endpoint,
and that is going to be all of this.
Let's copy that over and paste it in as a string.
Now, if we head over to the API document,
you can see that there's an index of the API on the right,
and also at the beginning of the docs.
So what we want to do is first create a user,
That's step one.
Now, this API is actually really well-documented,
especially given the fact that it's actually not a big development team behind
this. If we take a look, this is the end point,
and this is what we've got in our code. Next,
we're going to make our post request
and we're going to add some parameters into the request body.
So you can see that some of these are required and others are optional.
So we're going to add a value for each of the required parameters;
token, username, agreedTermsOfService, and that we're not a minor.
Cool.
So let's create our user_params. And the first key is our token.
Now let's see what this token needs to be.
It has to be a string that's used to authenticate your user
and we're going to use it later on as well when we access our graph
and when we add to it. So make sure you save this securely.
This is basically like an API key that you're going to generate yourself.
This token can contain any characters,
so you can make up a key like this,
but the length of the token has to be between 8 characters and 128
characters. So I think I've definitely got more than eight characters there,
so I can move on to the next key, which is my username.
I'm going to try and see if I can get away with just my first name. If it fails,
we can always try it again. Next,
we've got agreeTermsOfService and this is going to be either yes or no.
We're probably going to have to say yes
if we actually want to use this service.
And also we're going to have to say yes that we're not a minor.
Let's make sure that we don't make any typos by simply pasting this in.
So it's a yes for agreed to terms of service and a yes
to the fact that I'm not a minor, sadly. Not a minor anymore.
Once we've got our user parameters and our Pixela endpoint,
then we're ready to make our post request. Again,
we're going to start off with the URL which is going to be our Pixela endpoint,
and then we're going to add a new keyword argument.
And this is called json.
Notice how all the data that we're posting over to Pixela is pretty much in
the format of a JSON. String and string.
This is basically a piece of JSON data that we're going to send over.
And that's all there is to it. The endpoint
and also the JSON data that we want to send over.
So let's go ahead and save this inside a response variable just as we did
before. And then once this is completed,
let's go ahead and print what the response is.
Now you can either tap into the response as a JSON,
but given that in this case we're not really looking to do anything with the
response. We just wanna check if it's actually successful or not.
We can actually tap into a property called text.
So it'll give you back the response as a piece of text.
Let's go ahead and run this and see if I can get away with setting my username
as angela. Wow.
So there's actually no user called angela at the moment
and I was able to nab that username.
So the reason why we're interested in looking at the response is
if there were some issues, for example, if I tried to run this again
now that I've taken this username, I think it's probably going to fail
and it tells us this user already exists. Now, alternatively, if we said
no to one of these, agreed to terms of service or not minor,
it's probably also going to give us a message telling us why this post request
failed. But now that I've completed this step,
I can actually comment it out because I've now created my user.
We've now set up ourselves with a new account on Pixela with a username and also
a secret token that we're gonna use in the future to access this account.
It's effectively our user name and password. So in the next lesson
we're going to create our graph and we're going to learn how to use a more
secure method of authentication.
So for all of that and more, I'll see you on the next lesson.