WEBVTT

0
00:00.180 --> 00:03.180
We've been using the requests module for a while now,

1
00:03.270 --> 00:07.590
and we've been using it to make HTTP requests across the internet.

2
00:08.190 --> 00:10.260
We've already seen the get request,

3
00:10.410 --> 00:14.940
and this is a way for us to get pieces of data from somebody else,

4
00:14.970 --> 00:16.350
like an API provider.

5
00:17.070 --> 00:22.070
Now there's also four other common types of requests that we should probably know

6
00:22.170 --> 00:24.630
about; post put and delete.

7
00:25.440 --> 00:30.440
We can use the request module to complete a get request by simply writing

8
00:30.480 --> 00:31.920
requests.get

9
00:32.400 --> 00:37.400
and then we put our parameters inside the parentheses. For the other three

10
00:37.470 --> 00:38.303
requests,

11
00:38.460 --> 00:42.900
the code looks pretty much the same. It's .post, .put, and .delete.

12
00:44.010 --> 00:49.010
Whereas a get request is made where we ask an external system for a

13
00:49.560 --> 00:53.610
particular piece of data and they give that to us in the response.

14
00:54.270 --> 00:59.220
a post request is where we give the external system some piece of data

15
00:59.820 --> 01:03.660
and we're not so interested in the response we're getting back other than

16
01:03.660 --> 01:06.960
whether if it was successful or not. For example,

17
01:06.990 --> 01:10.950
if you wanted to save a piece of data in Google sheets,

18
01:11.220 --> 01:13.440
then you might use the Google sheets API

19
01:13.680 --> 01:17.190
to you post your data into your Google sheet.

20
01:18.060 --> 01:21.450
Or maybe you want to use the Twitter API to post a tweet

21
01:21.780 --> 01:26.780
then your program is going to send your tweet through a post request to the

22
01:27.150 --> 01:29.580
Twitter API. So that's how get

23
01:29.610 --> 01:34.610
and post works. Put is where you simply update a piece of data in the

24
01:34.680 --> 01:38.100
external service. So if you had a spreadsheet in Google sheets

25
01:38.100 --> 01:41.430
and maybe you want to update some of the values in the spreadsheet,

26
01:41.640 --> 01:43.590
then you would use a put request.

27
01:44.010 --> 01:48.330
And finally delete is simply where you want to delete a piece of data in the

28
01:48.330 --> 01:49.350
external service

29
01:49.350 --> 01:53.550
like a tweet that you posted or a Facebook post. Today

30
01:53.550 --> 01:57.360
we're going to be building our habit tracker using the Pixela API.

31
01:57.990 --> 02:00.030
And in order to use this API,

32
02:00.090 --> 02:04.380
we're going to post our habit tracking data, for example,

33
02:04.380 --> 02:08.460
how many pages have a book we've read, how many kilometers we've cycled,

34
02:08.730 --> 02:11.940
and we're going to be posting that data to Pixela

35
02:12.030 --> 02:14.040
to be tracked in our graph.

36
02:14.730 --> 02:17.610
If you head over to their website and you scroll down,

37
02:17.850 --> 02:21.090
you can see that it tells you how to get started.

38
02:21.450 --> 02:24.810
And it's a series of six steps.

39
02:25.200 --> 02:27.480
So we're going to be completing these in order

40
02:27.930 --> 02:30.360
and we're going to be pairing this short form

41
02:30.360 --> 02:33.810
get started guide with their documentation.

42
02:34.380 --> 02:38.490
So we're going to have that side by side in order to see all of the

43
02:38.490 --> 02:42.300
documentation on each of the steps. Step one

44
02:42.300 --> 02:47.300
involves creating a user account. And it says you have to hit up this particular

45
02:48.240 --> 02:52.290
end point using the HTTP post request.

46
02:52.680 --> 02:55.860
So this is the first time we're making a post request.

47
02:56.640 --> 03:00.100
Now I've set up a new project in  PyCharm habit tracker

48
03:00.490 --> 03:05.110
and in my main.py, as always, that I'm going to start off by importing the

49
03:05.110 --> 03:07.090
requests module. Now,

50
03:07.090 --> 03:11.650
because this is a brand new project we're going to have to install that package

51
03:11.710 --> 03:14.020
to make this red underline go away.

52
03:14.620 --> 03:19.330
And once it's been installed successfully, we can tap into that method requests

53
03:19.860 --> 03:22.290
<v 1>.post. What are we going to</v>

54
03:22.410 --> 03:27.060
<v 0>put into this parentheses, what are the parameters, what are the inputs?</v>

55
03:27.420 --> 03:31.350
Well, the first thing is the URL endpoint, right?

56
03:31.440 --> 03:34.020
So this is the Pixela endpoint,

57
03:34.740 --> 03:38.130
and that is going to be all of this.

58
03:38.820 --> 03:42.690
Let's copy that over and paste it in as a string.

59
03:44.070 --> 03:46.410
Now, if we head over to the API document,

60
03:46.560 --> 03:50.220
you can see that there's an index of the API on the right,

61
03:50.580 --> 03:52.650
and also at the beginning of the docs.

62
03:53.010 --> 03:56.130
So what we want to do is first create a user,

63
03:56.130 --> 03:58.350
<v 1> That's step one.</v>

64
03:58.440 --> 04:01.200
<v 0>Now, this API is actually really well-documented,</v>

65
04:01.590 --> 04:04.950
especially given the fact that it's actually not a big development team behind

66
04:04.950 --> 04:08.790
this. If we take a look, this is the end point,

67
04:09.090 --> 04:12.150
and this is what we've got in our code. Next,

68
04:12.180 --> 04:14.160
we're going to make our post request

69
04:14.190 --> 04:18.240
and we're going to add some parameters into the request body.

70
04:18.960 --> 04:23.100
So you can see that some of these are required and others are optional.

71
04:23.610 --> 04:28.200
So we're going to add a value for each of the required parameters;

72
04:28.470 --> 04:32.550
token, username, agreedTermsOfService, and that we're not a minor.

73
04:35.760 --> 04:35.880
<v 1>Cool.</v>

74
04:35.880 --> 04:40.380
<v 0>So let's create our user_params. And the first key is our token.</v>

75
04:40.920 --> 04:42.780
Now let's see what this token needs to be.

76
04:42.990 --> 04:47.190
It has to be a string that's used to authenticate your user

77
04:47.670 --> 04:51.510
and we're going to use it later on as well when we access our graph

78
04:51.540 --> 04:55.020
and when we add to it. So make sure you save this securely.

79
04:55.110 --> 04:59.430
This is basically like an API key that you're going to generate yourself.

80
05:00.090 --> 05:02.370
This token can contain any characters,

81
05:02.370 --> 05:05.940
so you can make up a key like this,

82
05:06.120 --> 05:10.650
but the length of the token has to be between 8 characters and 128

83
05:10.650 --> 05:14.310
characters. So I think I've definitely got more than eight characters there,

84
05:14.580 --> 05:18.270
so I can move on to the next key, which is my username.

85
05:19.350 --> 05:23.730
I'm going to try and see if I can get away with just my first name. If it fails,

86
05:23.730 --> 05:26.340
we can always try it again. Next,

87
05:26.370 --> 05:31.370
we've got agreeTermsOfService and this is going to be either yes or no.

88
05:32.700 --> 05:34.200
We're probably going to have to say yes

89
05:34.200 --> 05:36.120
if we actually want to use this service.

90
05:36.420 --> 05:39.240
And also we're going to have to say yes that we're not a minor.

91
05:40.680 --> 05:44.760
Let's make sure that we don't make any typos by simply pasting this in.

92
05:45.660 --> 05:50.660
So it's a yes for agreed to terms of service and a yes

93
05:51.240 --> 05:56.130
to the fact that I'm not a minor, sadly. Not a minor anymore.

94
05:56.840 --> 06:01.070
Once we've got our user parameters and our Pixela endpoint,

95
06:01.190 --> 06:04.910
then we're ready to make our post request. Again,

96
06:04.910 --> 06:09.710
we're going to start off with the URL which is going to be our Pixela endpoint,

97
06:10.370 --> 06:13.790
and then we're going to add a new keyword argument.

98
06:14.270 --> 06:15.770
And this is called json.

99
06:16.250 --> 06:21.250
Notice how all the data that we're posting over to Pixela is pretty much in

100
06:21.260 --> 06:24.860
the format of a JSON. String and string.

101
06:25.310 --> 06:28.670
This is basically a piece of JSON data that we're going to send over.

102
06:29.570 --> 06:33.080
And that's all there is to it. The endpoint

103
06:33.380 --> 06:36.020
and also the JSON data that we want to send over.

104
06:36.560 --> 06:41.060
So let's go ahead and save this inside a response variable just as we did

105
06:41.060 --> 06:44.060
before. And then once this is completed,

106
06:44.120 --> 06:46.910
let's go ahead and print what the response is.

107
06:47.420 --> 06:50.270
Now you can either tap into the response as a JSON,

108
06:50.630 --> 06:54.560
but given that in this case we're not really looking to do anything with the

109
06:54.560 --> 06:58.160
response. We just wanna check if it's actually successful or not.

110
06:58.580 --> 07:01.070
We can actually tap into a property called text.

111
07:02.060 --> 07:05.180
So it'll give you back the response as a piece of text.

112
07:05.690 --> 07:09.230
Let's go ahead and run this and see if I can get away with setting my username

113
07:09.350 --> 07:12.500
as angela. Wow.

114
07:12.650 --> 07:16.130
So there's actually no user called angela at the moment

115
07:16.550 --> 07:19.790
and I was able to nab that username.

116
07:20.360 --> 07:23.090
So the reason why we're interested in looking at the response is

117
07:23.180 --> 07:27.650
if there were some issues, for example, if I tried to run this again

118
07:27.680 --> 07:32.210
now that I've taken this username, I think it's probably going to fail

119
07:32.300 --> 07:36.740
and it tells us this user already exists. Now, alternatively, if we said

120
07:36.770 --> 07:40.430
no to one of these, agreed to terms of service or not minor,

121
07:40.670 --> 07:45.110
it's probably also going to give us a message telling us why this post request

122
07:45.170 --> 07:49.490
failed. But now that I've completed this step,

123
07:49.550 --> 07:54.550
I can actually comment it out because I've now created my user.

124
07:55.370 --> 08:00.370
We've now set up ourselves with a new account on Pixela with a username and also

125
08:02.120 --> 08:06.110
a secret token that we're gonna use in the future to access this account.

126
08:06.560 --> 08:11.060
It's effectively our user name and password. So in the next lesson

127
08:11.120 --> 08:14.960
we're going to create our graph and we're going to learn how to use a more

128
08:14.960 --> 08:17.570
secure method of authentication.

129
08:18.080 --> 08:21.110
So for all of that and more, I'll see you on the next lesson.