Previously we completed step one
which is setting up a user account on Pixela.
Now we can move on to step two
which is to create a new graph on Pixela for our username.
So notice how the endpoint is now different because in addition to /
v1/users
we also have to add our username now and then for /graphs.
So let's go back to the documentation and scroll down to the relevant part of
the API, which is this part to create a graph, that's step two.
This is also going to be a HTTP post request,
but the end point has now changed.
So let's go ahead and create our new endpoint
which is the graph endpoint.
And this is going to be comprised of the Pixela endpoint
and then in addition, it's going to be / our username.
So let's take out our
username and our token and put it as a constant at the top.
Right
Now we can add that in here using an f-string
and then finally it's /graphs.
Now, remember if you're doing this along with me,
this username is going to have to be your own unique username and the token is
going to be your own secret token.
Once we've created our graph end point,
the next step is to make our post request. Again,
we're going to use our request module and call the post method to make this
request. This time what is the data that we want to send over?
Well,
we want to be able to add the ID of our new graph,
so some way of identifying it in the URL, and then the name of the graph,
and then the unit of measure that we're keeping track of. For example,
if I'm keeping track of my cycling,
then that's going to be kilometers that I've cycled,
or if I want to keep track of how long I'm coding
then that might be minutes or hours.
And then the data type of the unit of measure.
If I was tracking the number of pages I've read then they'll be whole numbers,
if I'm tracking the number of kilometers I've ridden
then it's probably going to be a floating point number. And then finally,
we're going to specify the color of our graph. Here
you have to be a little bit careful because these are strings
which are actually in Japanese. So for example,
momiji is autumn leaves.
shibafu is grass, sara is sky,
and they've got a whole bunch of poetic names for each of these colors,
which wonderful, but it also prone to errors.
So you have to be careful when you're typing in the color that you want.
And if in doubt, just simply copy it from the documentation.
So let's go ahead and create our graph configuration.
To start off, I have to specify an ID. And in this case,
it has to start off with some letters from A to Z,
and then it could be letters or numbers.
And it has to be between 1 and 16 characters.
Let's say that the id of my graph is going to be graph1,
notice how we can't have the number just at the beginning because of these rules.
Next is the name of the graph.
So mine is going to be my cycling graph,
and then it's the unit of measure.
So because I'm tracking my cycling, it's going to be in kilometers.
And then it's going to be the type of data that I'm going to be posting in this
unit. Because it's kilometers,
I'm probably going to have some numbers after the decimal point.
So I'm going to keep it as a float rather than an int.
And finally, we're going to pick a color.
I'm going to pick purple, ajisai, which is hydrangea flower.
And just to make sure I don't make a mistake,
I'm going to copy that and paste it in.
That's all of my required graph config. But right now,
if I do the same thing as I did before
which is to put in my graph endpoint as the URL
and then the JSON is set to equal the graph config,
if I actually post this right now, it's not going to work.
And if I print the response.text, you'll see the reason.
The message tells me that the user angela does not exist.
I know it exists because I created in this step and I got a successful response,
or it's telling me that the token is wrong. So think about it.
Where have we actually provided our password?
Where if we provided our token in this request? No where.
It's not one of the request body keys.
So how do we provide our authentication? Well,
it's done using the request header.
We have to provide this key
and it's gonna look for the token that we used to register with Pixela.
You might have even noticed previously when we were working with the news API
that when we were looking at the authentication,
even though there's three methods for authentication, via a API key, query string
parameter
which is what we used or via the X-Api-Key HTTP header or via the authorization
HTEP header.
And you can see that they actually say we strongly recommend either of the
latter two, so using the HTTP header.
And the reason is so that your API key isn't visible to others in logs
or if they're doing requests sniffing.
So what exactly are these headers anyways?
Well if you think about a letter that you might get in the mail,
the header is the part that contains some relevant pieces of information,
like, um,
the phone numbers of the company or their website of the company or their logo.
And then the body is the actual message part sesentially.
It's the part that changes from letter to letter. Now,
back in the previous project where we created our stock news alert application,
you can see that we're simply sending our API key as a parameter
along with the get request. And what this effectively looks like
if we're doing it in the browser is we're taking the news endpoint,
we're putting it into the URL bar and then we're adding our parameters.
So the first one is the API key.
And then the next one is the actual query.
So we were querying for the company name in the title of the news piece
and we were looking for Tesla.
This is effectively exactly the same as our code here.
We make a request to that URL and pass over these parameters.
There's effectively no difference between this and that.
As you can imagine, all our secret stuff in the request itself.
So if anybody's monitoring this,
then they will be able to see all of this information and they might be able to
steal your API key. Now,
one of the things that's reassuring is that this API is accessed via HTTPS and
the S stands for secure.
So this request is actually encrypted,
but it doesn't stop somebody from installing something on your browser to look
at what requests you're making
and there are certain cases where this might leak your API key to somebody that
you don't want to. So for some API providers,
you'll see that they want you to provide the authentication in the header.
Let's go ahead and do that. So let's take the key that they're expecting
and let's create a dictionary called headers,
and let's make that the key,
and then the value is going to be our token.
This is our top secret password. Now,
in addition to our URL on the JSON that we're sending over to Pixela,
we're also going to add a optional keyword argument which is called headers.
This is not going to show up when you start typing, not like JSON or URL.
And this is because it's one of the kwargs or keyword arguments.
And that means you also have to spell it exactly right
and make sure you don't make any typos. It's headers with an S at the end.
And then we're going to pass our variable headers into this parameter.
Now what we're going to make our request once more,
and you can see this time when we print the response.text,
it gives us success and we've now managed to set up our brand new cycling graph.
So if we go back to step three,
we can actually go ahead and look at our cycling graph by going to this URL and
replacing this part which is the creator's username with our own,
so mine was angela. Yours will be something else.
And we're also going to replace this final part with our ID,
which in my case is just graph1.
Now we're going to finally tag on .html to the end,
and when I hit enter,
you'll see it go to my brand new cycling graph right here.
And it's going to be colored in purple and it's going to look beautiful and it's
going to start tracking my cycling data.
So this is how we can authenticate ourselves using a header
and the requests module makes passing over headers just as easy as passing over
other parameters or other parts of the message body. In the next lesson,
we're going to be moving on to step 4 so that we can finally add a
the pixel of data to our graph.
So head over to the next lesson and give that a go.