WEBVTT

1
00:00.000 --> 00:03.930
Now in a previous skill when we first talked about REST APIs, I happen to

2
00:03.930 --> 00:04.960
highlight a very

3
00:04.960 --> 00:10.180
popular utility for interacting with REST APIs known as Postman. This is really

4
00:10.180 --> 00:10.640
good because

5
00:10.640 --> 00:14.370
you can ultimately save your endpoints, you can easily browse them. It really

6
00:14.370 --> 00:14.800
is very,

7
00:14.800 --> 00:20.000
very handy for testing purposes, but that said, if you want to interact with a

8
00:20.000 --> 00:20.960
REST API and

9
00:20.960 --> 00:24.960
receive the data, like JSON data, and then work with it in a programmatic

10
00:24.960 --> 00:26.960
manner, utilizing Python,

11
00:26.960 --> 00:30.960
then what we would like to have would be a dedicated Python library that is
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

11
00:26.960 --> 00:30.960
then what we would like to have would be a dedicated Python library that is

12
00:30.960 --> 00:31.680
effectively going to

13
00:31.680 --> 00:37.010
allow us to talk HTTP. Well, the good news is that there is such a library and

14
00:37.010 --> 00:38.320
this one is called

15
00:38.320 --> 00:42.480
the Request Library. So let's walk through the basic installation of Request

16
00:42.480 --> 00:42.960
and then we'll do

17
00:42.960 --> 00:46.580
some testing. So very similar to what I did when I happened to install NetMeko,

18
00:46.580 --> 00:47.040
what I will do

19
00:47.040 --> 00:50.150
is I will create a little virtual environment here. This is going to allow me

20
00:50.150 --> 00:51.360
some segmentation

21
00:51.360 --> 00:55.580
or isolation should I say. So now I've created it, let me just activate it. And

22
00:55.580 --> 00:56.400
now we can see here

23
00:56.400 --> 01:00.720
in the corner my virtual environment has been activated. So this means anything
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

23
00:56.400 --> 01:00.720
in the corner my virtual environment has been activated. So this means anything

24
01:00.720 --> 01:01.840
I install is not going to

25
01:01.840 --> 01:05.270
be installed system-wide instead it's going to be installed with fitness

26
01:05.270 --> 01:06.640
environment only. So I

27
01:06.640 --> 01:10.420
will now say it's pit 3 install request and to answer give a moment and as we

28
01:10.420 --> 01:11.040
can see here,

29
01:11.040 --> 01:14.640
I have successfully installed a request library. So let me just create a little

30
01:14.640 --> 01:15.520
test folder, just

31
01:15.520 --> 01:19.310
called test, keep it nice and simple. And let's go inside it. And what I will

32
01:19.310 --> 01:20.240
do is I will create a

33
01:20.240 --> 01:26.480
little file called script1.py. So remember we were using Postman, but we were

34
01:26.480 --> 01:28.000
using Postman to send

35
01:28.000 --> 01:31.620
requests to this little test-reached API. So let's click on this here once
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

35
01:28.000 --> 01:31.620
requests to this little test-reached API. So let's click on this here once

36
01:31.620 --> 01:32.880
again. So what we can see

37
01:32.880 --> 01:37.590
here is we have our endpoints such as getting information on the actual users.

38
01:37.590 --> 01:38.320
So what I'm going to do

39
01:38.320 --> 01:42.620
here is I'm going to take this information but as opposed to connecting to the

40
01:42.620 --> 01:44.080
rest API over Postman,

41
01:44.080 --> 01:47.720
I'm going to use the request library to do the same thing. So the first thing I

42
01:47.720 --> 01:48.480
will do is I will

43
01:48.480 --> 01:52.260
grab this URL right here. So I've got a copy of it. And let me go in and open

44
01:52.260 --> 01:53.440
up this script here.

45
01:53.440 --> 01:56.740
All right, so first thing is first what I'm going to do is to import the

46
01:56.740 --> 01:58.000
request library. And what

47
01:58.000 --> 02:01.490
I will now do is I will create a little dictionary. Let me just pull this down
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

47
01:58.000 --> 02:01.490
I will now do is I will create a little dictionary. Let me just pull this down

48
02:01.490 --> 02:02.480
a little touch. And I'll

49
02:02.480 --> 02:05.750
call the dictionary headers okay. And this is going to allow me to define the

50
02:05.750 --> 02:07.040
headers that I want.

51
02:07.040 --> 02:11.520
So in this case here, I'm going to have an accept header and the corresponding

52
02:11.520 --> 02:12.560
value is going to be

53
02:12.560 --> 02:16.920
application forward slash JSON. And remember, if you recall, this is just

54
02:16.920 --> 02:18.320
similar to what we did when

55
02:18.320 --> 02:23.290
we made a request via Postman. Remember, I can make my get request paste in the

56
02:23.290 --> 02:24.080
URL that I want to

57
02:24.080 --> 02:28.210
target. And in my headers, what I did was specify an accept header. And one of

58
02:28.210 --> 02:29.360
the valid values is

59
02:29.360 --> 02:33.610
like I say, it's application forward slash JSON right here. So if I just happen
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

59
02:29.360 --> 02:33.610
like I say, it's application forward slash JSON right here. So if I just happen

60
02:33.610 --> 02:34.640
to send this off,

61
02:34.640 --> 02:38.180
as we can see, we get the response, let's do the same thing with our Python

62
02:38.180 --> 02:38.960
code. So we'll

63
02:38.960 --> 02:42.970
define our headers. And now what I will do is I will create a little variable

64
02:42.970 --> 02:44.160
to store my result. So

65
02:44.160 --> 02:47.630
I'll just say response is equal to, and then I'm going to take the request

66
02:47.630 --> 02:48.880
library that I'm just

67
02:48.880 --> 02:53.620
importing. And then I'm going to say dot get this will be the get method to

68
02:53.620 --> 02:55.200
actually send a get

69
02:55.200 --> 02:59.140
request. So what I also want to do is I want to define the URL that I want to

70
02:59.140 --> 03:00.480
target. So I'll just say
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

70
02:59.140 --> 03:00.480
target. So I'll just say

71
03:00.480 --> 03:05.740
URL is equal to and paste in this particular value right here. And all I'll do

72
03:05.740 --> 03:07.040
is also the URL

73
03:07.040 --> 03:11.150
is equal to the URL. I've just defined above. I'll say the headers that I want

74
03:11.150 --> 03:12.400
are equal to the

75
03:12.400 --> 03:16.830
headers library or library. The header dictionary I've just defined above

76
03:16.830 --> 03:18.000
clearly I'm not getting

77
03:18.000 --> 03:22.300
enough sleep these days. Now what I'm also going to say is verify is equal to

78
03:22.300 --> 03:23.280
false pretty much.

79
03:23.280 --> 03:28.500
I'm not looking to verify my SSL certificates or my TLS certificates. Now check

80
03:28.500 --> 03:29.360
this out. What we've

81
03:29.360 --> 03:34.640
done here is we've taken the library going to send a HTTP get request, but
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

81
03:29.360 --> 03:34.640
done here is we've taken the library going to send a HTTP get request, but

82
03:34.640 --> 03:35.840
specify the URL and the

83
03:35.840 --> 03:39.350
headers and we're going to save the response and this particular variable here.

84
03:39.350 --> 03:39.840
So what this

85
03:39.840 --> 03:43.550
therefore means is if I just print out the response, which I actually see some

86
03:43.550 --> 03:44.400
pretty interesting

87
03:44.400 --> 03:48.330
information. So let's just save this. And if I just go and attempt to run my

88
03:48.330 --> 03:49.280
little script or

89
03:49.280 --> 03:54.330
I'll say Python 3 script 1.py and to enter, look at what we actually see here

90
03:54.330 --> 03:55.760
response 200. This

91
03:55.760 --> 04:00.270
is as seeing the actual status code. And what I'm also getting here is this
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

91
03:55.760 --> 04:00.270
is as seeing the actual status code. And what I'm also getting here is this

92
04:00.270 --> 04:01.600
particular warning.

93
04:01.600 --> 04:04.430
So in order just to suppress that warning. So it's not going to be quite so

94
04:04.430 --> 04:05.200
annoying. I'm just

95
04:05.200 --> 04:11.130
going to disable it by saying requests dot packages URL lib3 and I'll say dot

96
04:11.130 --> 04:12.640
disable warnings.

97
04:12.640 --> 04:16.700
So let's try it again. Arrow up, hit enter. Now we're just seeing the response,

98
04:16.700 --> 04:17.280
but this is not

99
04:17.280 --> 04:21.250
actually showing us the data we got back just mainly the response code. However

100
04:21.250 --> 04:22.800
, this particular

101
04:22.800 --> 04:26.450
variable is going to have some particular attributes associated with it. So

102
04:26.450 --> 04:28.240
what I can say here is show

103
04:28.240 --> 04:32.970
me the text attribute, which ultimately shows me the content of the response in
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

103
04:28.240 --> 04:32.970
me the text attribute, which ultimately shows me the content of the response in

104
04:32.970 --> 04:33.920
Unicode as we see

105
04:33.920 --> 04:38.010
right here. So what this means then is if I actually run this one more time,

106
04:38.010 --> 04:39.040
now we're actually seeing

107
04:39.040 --> 04:43.270
all of the data that's coming back here are all of the names, the emails, the

108
04:43.270 --> 04:44.160
gender, the status,

109
04:44.160 --> 04:47.770
so on, so forth. Now as it stands right now, this type of information is just

110
04:47.770 --> 04:48.880
regular string data.

111
04:48.880 --> 04:52.010
Okay, so it's not programmatic, even though it does look like it. So if I just

112
04:52.010 --> 04:52.960
print the type and

113
04:52.960 --> 04:56.540
hit enter, indeed, we can see this is just a regular Python string. So we can't

114
04:56.540 --> 04:57.360
go and work with a

115
04:57.360 --> 05:01.420
list or call any of the dictionary case, but what we can do is we also have an
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

115
04:57.360 --> 05:01.420
list or call any of the dictionary case, but what we can do is we also have an

116
05:01.420 --> 05:02.640
additional attribute

117
05:02.640 --> 05:07.240
here. So I just happened to print this out and I just say print the response

118
05:07.240 --> 05:08.560
dot json and then I

119
05:08.560 --> 05:13.060
use my parentheses to actually call the JSON object. If I run the script, the

120
05:13.060 --> 05:14.320
information looks the same,

121
05:14.320 --> 05:17.980
but this time, if I actually check the type one more time, look at this right

122
05:17.980 --> 05:19.440
here, the outer object

123
05:19.440 --> 05:23.130
is actually a Python list as the note of via the square brackets. So what this

124
05:23.130 --> 05:24.160
means is we can now

125
05:24.160 --> 05:28.440
access the data programmatically so I can grab list index position zero, run

126
05:28.440 --> 05:29.680
this, enter,

127
05:29.680 --> 05:33.100
this now gives us the first item in the list, which is this right here. And now
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

127
05:29.680 --> 05:33.100
this now gives us the first item in the list, which is this right here. And now

128
05:33.100 --> 05:33.680
we can grab the

129
05:33.680 --> 05:37.320
particular keys. Let's maybe say want to get the email. Let's go and grab this

130
05:37.320 --> 05:38.880
to call the email key.

131
05:38.880 --> 05:42.600
And now we're able to program it to access the very first email within all of

132
05:42.600 --> 05:43.360
this data.

133
05:43.360 --> 05:46.880
So unlike what we were doing with postman, we're not just retrieving the actual

134
05:46.880 --> 05:47.760
information back

135
05:47.760 --> 05:52.310
and testing the response now with Python, but able to access this data as

136
05:52.310 --> 05:53.520
Python objects and

137
05:53.520 --> 05:57.720
begin manipulating it programmatically. This is very, very important when

138
05:57.720 --> 05:58.960
working with networking

139
05:58.960 --> 06:02.800
information, particularly when you want to conduct network tests.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

139
05:58.960 --> 06:02.800
information, particularly when you want to conduct network tests.
