1 00:00:00,180 --> 00:00:03,180 We've been using the requests module for a while now, 2 00:00:03,270 --> 00:00:07,590 and we've been using it to make HTTP requests across the internet. 3 00:00:08,189 --> 00:00:10,260 We've already seen the get request, 4 00:00:10,410 --> 00:00:14,940 and this is a way for us to get pieces of data from somebody else, 5 00:00:14,970 --> 00:00:16,350 like an API provider. 6 00:00:17,070 --> 00:00:22,070 Now there's also four other common types of requests that we should probably know 7 00:00:22,170 --> 00:00:24,630 about; post put and delete. 8 00:00:25,440 --> 00:00:30,440 We can use the request module to complete a get request by simply writing 9 00:00:30,480 --> 00:00:31,920 requests.get 10 00:00:32,400 --> 00:00:37,400 and then we put our parameters inside the parentheses. For the other three 11 00:00:37,470 --> 00:00:38,303 requests, 12 00:00:38,460 --> 00:00:42,900 the code looks pretty much the same. It's .post, .put, and .delete. 13 00:00:44,010 --> 00:00:49,010 Whereas a get request is made where we ask an external system for a 14 00:00:49,560 --> 00:00:53,610 particular piece of data and they give that to us in the response. 15 00:00:54,270 --> 00:00:59,220 a post request is where we give the external system some piece of data 16 00:00:59,820 --> 00:01:03,660 and we're not so interested in the response we're getting back other than 17 00:01:03,660 --> 00:01:06,960 whether if it was successful or not. For example, 18 00:01:06,990 --> 00:01:10,950 if you wanted to save a piece of data in Google sheets, 19 00:01:11,220 --> 00:01:13,440 then you might use the Google sheets API 20 00:01:13,680 --> 00:01:17,190 to you post your data into your Google sheet. 21 00:01:18,060 --> 00:01:21,450 Or maybe you want to use the Twitter API to post a tweet 22 00:01:21,780 --> 00:01:26,780 then your program is going to send your tweet through a post request to the 23 00:01:27,150 --> 00:01:29,580 Twitter API. So that's how get 24 00:01:29,610 --> 00:01:34,610 and post works. Put is where you simply update a piece of data in the 25 00:01:34,680 --> 00:01:38,100 external service. So if you had a spreadsheet in Google sheets 26 00:01:38,100 --> 00:01:41,430 and maybe you want to update some of the values in the spreadsheet, 27 00:01:41,640 --> 00:01:43,590 then you would use a put request. 28 00:01:44,010 --> 00:01:48,330 And finally delete is simply where you want to delete a piece of data in the 29 00:01:48,330 --> 00:01:49,350 external service 30 00:01:49,350 --> 00:01:53,550 like a tweet that you posted or a Facebook post. Today 31 00:01:53,550 --> 00:01:57,360 we're going to be building our habit tracker using the Pixela API. 32 00:01:57,990 --> 00:02:00,030 And in order to use this API, 33 00:02:00,090 --> 00:02:04,380 we're going to post our habit tracking data, for example, 34 00:02:04,380 --> 00:02:08,460 how many pages have a book we've read, how many kilometers we've cycled, 35 00:02:08,729 --> 00:02:11,940 and we're going to be posting that data to Pixela 36 00:02:12,030 --> 00:02:14,040 to be tracked in our graph. 37 00:02:14,730 --> 00:02:17,610 If you head over to their website and you scroll down, 38 00:02:17,850 --> 00:02:21,090 you can see that it tells you how to get started. 39 00:02:21,450 --> 00:02:24,810 And it's a series of six steps. 40 00:02:25,200 --> 00:02:27,480 So we're going to be completing these in order 41 00:02:27,930 --> 00:02:30,360 and we're going to be pairing this short form 42 00:02:30,360 --> 00:02:33,810 get started guide with their documentation. 43 00:02:34,380 --> 00:02:38,490 So we're going to have that side by side in order to see all of the 44 00:02:38,490 --> 00:02:42,300 documentation on each of the steps. Step one 45 00:02:42,300 --> 00:02:47,300 involves creating a user account. And it says you have to hit up this particular 46 00:02:48,240 --> 00:02:52,290 end point using the HTTP post request. 47 00:02:52,680 --> 00:02:55,860 So this is the first time we're making a post request. 48 00:02:56,640 --> 00:03:00,100 Now I've set up a new project in PyCharm habit tracker 49 00:03:00,490 --> 00:03:05,110 and in my main.py, as always, that I'm going to start off by importing the 50 00:03:05,110 --> 00:03:07,090 requests module. Now, 51 00:03:07,090 --> 00:03:11,650 because this is a brand new project we're going to have to install that package 52 00:03:11,710 --> 00:03:14,020 to make this red underline go away. 53 00:03:14,620 --> 00:03:19,330 And once it's been installed successfully, we can tap into that method requests 54 00:03:19,860 --> 00:03:22,290 .post. What are we going to 55 00:03:22,410 --> 00:03:27,060 put into this parentheses, what are the parameters, what are the inputs? 56 00:03:27,420 --> 00:03:31,350 Well, the first thing is the URL endpoint, right? 57 00:03:31,440 --> 00:03:34,020 So this is the Pixela endpoint, 58 00:03:34,740 --> 00:03:38,130 and that is going to be all of this. 59 00:03:38,820 --> 00:03:42,690 Let's copy that over and paste it in as a string. 60 00:03:44,070 --> 00:03:46,410 Now, if we head over to the API document, 61 00:03:46,560 --> 00:03:50,220 you can see that there's an index of the API on the right, 62 00:03:50,580 --> 00:03:52,650 and also at the beginning of the docs. 63 00:03:53,010 --> 00:03:56,130 So what we want to do is first create a user, 64 00:03:56,130 --> 00:03:58,350 That's step one. 65 00:03:58,440 --> 00:04:01,200 Now, this API is actually really well-documented, 66 00:04:01,590 --> 00:04:04,950 especially given the fact that it's actually not a big development team behind 67 00:04:04,950 --> 00:04:08,790 this. If we take a look, this is the end point, 68 00:04:09,090 --> 00:04:12,150 and this is what we've got in our code. Next, 69 00:04:12,180 --> 00:04:14,160 we're going to make our post request 70 00:04:14,190 --> 00:04:18,240 and we're going to add some parameters into the request body. 71 00:04:18,959 --> 00:04:23,100 So you can see that some of these are required and others are optional. 72 00:04:23,610 --> 00:04:28,200 So we're going to add a value for each of the required parameters; 73 00:04:28,470 --> 00:04:32,550 token, username, agreedTermsOfService, and that we're not a minor. 74 00:04:35,760 --> 00:04:35,880 Cool. 75 00:04:35,880 --> 00:04:40,380 So let's create our user_params. And the first key is our token. 76 00:04:40,920 --> 00:04:42,780 Now let's see what this token needs to be. 77 00:04:42,990 --> 00:04:47,190 It has to be a string that's used to authenticate your user 78 00:04:47,670 --> 00:04:51,510 and we're going to use it later on as well when we access our graph 79 00:04:51,540 --> 00:04:55,020 and when we add to it. So make sure you save this securely. 80 00:04:55,110 --> 00:04:59,430 This is basically like an API key that you're going to generate yourself. 81 00:05:00,090 --> 00:05:02,370 This token can contain any characters, 82 00:05:02,370 --> 00:05:05,940 so you can make up a key like this, 83 00:05:06,120 --> 00:05:10,650 but the length of the token has to be between 8 characters and 128 84 00:05:10,650 --> 00:05:14,310 characters. So I think I've definitely got more than eight characters there, 85 00:05:14,580 --> 00:05:18,270 so I can move on to the next key, which is my username. 86 00:05:19,350 --> 00:05:23,730 I'm going to try and see if I can get away with just my first name. If it fails, 87 00:05:23,730 --> 00:05:26,340 we can always try it again. Next, 88 00:05:26,370 --> 00:05:31,370 we've got agreeTermsOfService and this is going to be either yes or no. 89 00:05:32,700 --> 00:05:34,200 We're probably going to have to say yes 90 00:05:34,200 --> 00:05:36,120 if we actually want to use this service. 91 00:05:36,420 --> 00:05:39,240 And also we're going to have to say yes that we're not a minor. 92 00:05:40,680 --> 00:05:44,760 Let's make sure that we don't make any typos by simply pasting this in. 93 00:05:45,660 --> 00:05:50,660 So it's a yes for agreed to terms of service and a yes 94 00:05:51,240 --> 00:05:56,130 to the fact that I'm not a minor, sadly. Not a minor anymore. 95 00:05:56,840 --> 00:06:01,070 Once we've got our user parameters and our Pixela endpoint, 96 00:06:01,190 --> 00:06:04,910 then we're ready to make our post request. Again, 97 00:06:04,910 --> 00:06:09,710 we're going to start off with the URL which is going to be our Pixela endpoint, 98 00:06:10,370 --> 00:06:13,790 and then we're going to add a new keyword argument. 99 00:06:14,270 --> 00:06:15,770 And this is called json. 100 00:06:16,250 --> 00:06:21,250 Notice how all the data that we're posting over to Pixela is pretty much in 101 00:06:21,260 --> 00:06:24,860 the format of a JSON. String and string. 102 00:06:25,310 --> 00:06:28,670 This is basically a piece of JSON data that we're going to send over. 103 00:06:29,570 --> 00:06:33,080 And that's all there is to it. The endpoint 104 00:06:33,380 --> 00:06:36,020 and also the JSON data that we want to send over. 105 00:06:36,560 --> 00:06:41,060 So let's go ahead and save this inside a response variable just as we did 106 00:06:41,060 --> 00:06:44,060 before. And then once this is completed, 107 00:06:44,120 --> 00:06:46,910 let's go ahead and print what the response is. 108 00:06:47,420 --> 00:06:50,270 Now you can either tap into the response as a JSON, 109 00:06:50,630 --> 00:06:54,560 but given that in this case we're not really looking to do anything with the 110 00:06:54,560 --> 00:06:58,160 response. We just wanna check if it's actually successful or not. 111 00:06:58,580 --> 00:07:01,070 We can actually tap into a property called text. 112 00:07:02,060 --> 00:07:05,180 So it'll give you back the response as a piece of text. 113 00:07:05,690 --> 00:07:09,230 Let's go ahead and run this and see if I can get away with setting my username 114 00:07:09,350 --> 00:07:12,500 as angela. Wow. 115 00:07:12,650 --> 00:07:16,130 So there's actually no user called angela at the moment 116 00:07:16,550 --> 00:07:19,790 and I was able to nab that username. 117 00:07:20,360 --> 00:07:23,090 So the reason why we're interested in looking at the response is 118 00:07:23,180 --> 00:07:27,650 if there were some issues, for example, if I tried to run this again 119 00:07:27,680 --> 00:07:32,210 now that I've taken this username, I think it's probably going to fail 120 00:07:32,300 --> 00:07:36,740 and it tells us this user already exists. Now, alternatively, if we said 121 00:07:36,770 --> 00:07:40,430 no to one of these, agreed to terms of service or not minor, 122 00:07:40,670 --> 00:07:45,110 it's probably also going to give us a message telling us why this post request 123 00:07:45,170 --> 00:07:49,490 failed. But now that I've completed this step, 124 00:07:49,550 --> 00:07:54,550 I can actually comment it out because I've now created my user. 125 00:07:55,370 --> 00:08:00,370 We've now set up ourselves with a new account on Pixela with a username and also 126 00:08:02,120 --> 00:08:06,110 a secret token that we're gonna use in the future to access this account. 127 00:08:06,560 --> 00:08:11,060 It's effectively our user name and password. So in the next lesson 128 00:08:11,120 --> 00:08:14,960 we're going to create our graph and we're going to learn how to use a more 129 00:08:14,960 --> 00:08:17,570 secure method of authentication. 130 00:08:18,080 --> 00:08:21,110 So for all of that and more, I'll see you on the next lesson.