1
00:00:05,250 --> 00:00:10,270
Welcome back everyone in this lecture we're going to build a very simple rest API and use placement

2
00:00:10,270 --> 00:00:12,630
to actually request data.

3
00:00:12,700 --> 00:00:15,800
So we're going to learn how to send back basic information using arrest.

4
00:00:15,850 --> 00:00:21,610
Keep in mind the most common payload format to send information back from a request is these Jasen which

5
00:00:21,610 --> 00:00:26,200
if you haven't seen it before it's very similar to key value formatting and pretty much looks exactly

6
00:00:26,200 --> 00:00:27,730
like a python dictionary.

7
00:00:27,760 --> 00:00:28,700
So keep that in mind.

8
00:00:28,900 --> 00:00:30,570
Let's go ahead and begin.

9
00:00:30,610 --> 00:00:31,180
I'll see you at the.

10
00:00:31,190 --> 00:00:32,460
And text editor.

11
00:00:32,620 --> 00:00:34,160
OK here I am with the text editor.

12
00:00:34,180 --> 00:00:39,570
I'm going to create a new file and I'm going to call it simple API pie.

13
00:00:39,610 --> 00:00:45,980
You can call it apple pie really whatever you want just any simple basic python file.

14
00:00:46,030 --> 00:00:52,040
So I've created this file and then save from flask import flask.

15
00:00:52,100 --> 00:00:54,280
So we're going create a basic class application.

16
00:00:54,350 --> 00:01:04,410
So say app is equal to flask and then we'll pass in the name variable and then we'll say if name is

17
00:01:04,410 --> 00:01:05,090
equal to me.

18
00:01:05,100 --> 00:01:10,270
Go ahead and take the app run it and we'll say debug mode is equal to true.

19
00:01:10,290 --> 00:01:10,500
All right.

20
00:01:10,500 --> 00:01:13,070
This is pretty much the most basic floss application we create.

21
00:01:13,080 --> 00:01:17,400
We're not even connected to any templates where we're going to do is show you how you can create a resource

22
00:01:17,460 --> 00:01:21,950
using flask restful but in order to do that when you actually install flask restful.

23
00:01:22,140 --> 00:01:25,490
So go ahead and activate your environment whatever you happen to have called it.

24
00:01:25,920 --> 00:01:29,590
So I'll say activate my flask in the.

25
00:01:29,680 --> 00:01:35,280
So here my environment is activated and I'm going to zoom out just a little bit so we can get the whole

26
00:01:35,670 --> 00:01:45,510
detail here and say Pipp install a capital F flask restful.

27
00:01:45,560 --> 00:01:49,180
So go ahead and let the install so that is the are is also capitalized.

28
00:01:49,220 --> 00:01:51,470
I already had it installed so I'm good to go.

29
00:01:51,500 --> 00:01:58,970
Once you've installed that you can come back to the python script and then say from Lotus the import

30
00:01:58,970 --> 00:02:02,570
is going to be flask underscore restful lowercase.

31
00:02:02,930 --> 00:02:04,550
We're going to import two things.

32
00:02:04,820 --> 00:02:12,050
We're going to import resource at API and basically the way this works is resource is actually going

33
00:02:12,050 --> 00:02:16,430
to allow you to create a resource to connect to using the rest API.

34
00:02:16,430 --> 00:02:21,650
An API is just going to simply be a wrapper around your entire application that allows the resource

35
00:02:21,650 --> 00:02:22,730
to actually connect.

36
00:02:22,730 --> 00:02:28,850
So a lot is going on to the backend here for you and the API calls it's going to seem really similar

37
00:02:28,850 --> 00:02:35,570
to when we use things like flask migrate or we need this API is equal to and then call API and then

38
00:02:35,580 --> 00:02:37,210
pasand our application.

39
00:02:37,310 --> 00:02:42,500
So that's all we need to do and then create a resource the most basic way is to just create a class

40
00:02:43,100 --> 00:02:44,750
and I'm going to say hello world.

41
00:02:45,650 --> 00:02:52,730
And then we inherit from resource and then these resource classes they basically take in different methods

42
00:02:52,730 --> 00:02:55,510
like get Post put delete.

43
00:02:55,540 --> 00:03:01,510
So the most basic method we are going to provide is the gets and takes and the self keyword.

44
00:03:01,820 --> 00:03:03,910
And then this method is just going to return

45
00:03:06,890 --> 00:03:12,570
a python dictionary that when we actually read it in the post meant side it's going to be Jason.

46
00:03:12,600 --> 00:03:13,970
So say hello world.

47
00:03:15,110 --> 00:03:16,090
And then save that.

48
00:03:16,310 --> 00:03:21,410
So the key thing to note here is that we're wrapping our currently existing application with this API

49
00:03:21,410 --> 00:03:28,160
call that we imported from flask restful and then we created a class which inherited from resource which

50
00:03:28,160 --> 00:03:32,690
we also imported and then we just have the most basic method which is get self.

51
00:03:32,690 --> 00:03:34,520
And then we return something.

52
00:03:34,520 --> 00:03:39,560
The last thing we need to do before running or application is connect this resource to an actual u r

53
00:03:39,570 --> 00:03:40,120
l.

54
00:03:40,340 --> 00:03:46,390
So the way you do that is remember we assign here on line 6 this API to connect the application.

55
00:03:46,610 --> 00:03:55,070
But I'm going to do is notice is outside of the class I will say API and then say add underscore resource

56
00:03:55,790 --> 00:03:57,710
and then I'm going to add the name of the class here.

57
00:03:57,710 --> 00:03:59,240
So say hello world.

58
00:03:59,480 --> 00:04:05,080
Go ahead and copy that and then pass in the class comma and Lotusphere and passing in the class.

59
00:04:05,200 --> 00:04:08,080
It by itself in the actually creating an instance of it.

60
00:04:08,090 --> 00:04:09,340
So it is passing that class.

61
00:04:09,350 --> 00:04:10,160
Hello world.

62
00:04:10,400 --> 00:04:15,740
And then here I'm going to pass in the Ural extension that I want to connect this particular resource

63
00:04:15,740 --> 00:04:16,310
with.

64
00:04:16,610 --> 00:04:21,590
So I'm just going to say forward slash so that you can actually make a request directly to the home

65
00:04:21,590 --> 00:04:22,300
page.

66
00:04:22,370 --> 00:04:29,260
Later on we're going to see things like explaining this is something like request or create slash puppy

67
00:04:29,270 --> 00:04:29,900
and so on.

68
00:04:29,960 --> 00:04:34,250
Right now just a single forward slash that's connected to the very home site.

69
00:04:34,340 --> 00:04:35,250
Go ahead and save that.

70
00:04:35,300 --> 00:04:39,470
And we're just adding a resource passing in that class passing and single forward slash.

71
00:04:39,630 --> 00:04:43,670
And now that API has that resource and there we're going to run this file.

72
00:04:43,940 --> 00:04:46,780
So I'll say Python simply API enter.

73
00:04:46,880 --> 00:04:47,680
Now it's running.

74
00:04:47,840 --> 00:04:53,900
Let's go ahead and open up postman to see how that works and how we can create a collection in Postman.

75
00:04:53,900 --> 00:04:54,930
All right so I just opened up.

76
00:04:54,930 --> 00:05:00,800
Postman what I'm going to do is say create new collection and then go ahead and give this collection

77
00:05:00,830 --> 00:05:01,210
a name.

78
00:05:01,220 --> 00:05:06,110
Basically a collection is going to be a folder holding a bunch of requests.

79
00:05:06,110 --> 00:05:11,910
So we're going to say this is our basic collection really whatever you want to call it.

80
00:05:11,950 --> 00:05:15,930
And if you want you can give it a description since it is just documenting what you're doing here.

81
00:05:15,970 --> 00:05:21,970
So I'll just say training that will hit create and I you see this is essentially a folder that you can

82
00:05:21,970 --> 00:05:27,490
share if other people you can run the entire collection of get hoes put delete requests.

83
00:05:27,480 --> 00:05:30,900
So right now we're going to have one single request collection.

84
00:05:31,060 --> 00:05:35,680
So go ahead and close that little window and then right here we see these three dots we're just going

85
00:05:35,680 --> 00:05:42,970
to say add a request so you can give this request a specific name.

86
00:05:43,260 --> 00:05:50,330
So I'll say my first request if you want you can out with ascriptions not necessary and we'll save the

87
00:05:50,340 --> 00:05:51,610
basic collection.

88
00:05:51,630 --> 00:05:55,160
That means if you expand this collection you can see my first request.

89
00:05:55,170 --> 00:06:00,420
So this allows you to really start developing kind of a more test driven approach using postman because

90
00:06:00,420 --> 00:06:04,950
you can have this collection of requests and then just run all of them at the same time a click still

91
00:06:05,010 --> 00:06:05,740
play button.

92
00:06:05,940 --> 00:06:11,170
That way if you begin to edit your website you can just run a collection almost as like a testing.

93
00:06:11,190 --> 00:06:16,320
So we're going to get my first two quest and right here on this button is where we can select which

94
00:06:16,330 --> 00:06:18,800
you want to get PPOs put delete.

95
00:06:18,810 --> 00:06:26,700
So I just want to get your all then is just HTP Colan forward slash 4 slash 1 to seven point zero point

96
00:06:26,700 --> 00:06:30,870
zero point one colon 5000 forward slash.

97
00:06:30,900 --> 00:06:32,790
So right now we're just dealing with the home page.

98
00:06:32,790 --> 00:06:35,670
And then what I didn't do is just send that get request.

99
00:06:36,060 --> 00:06:40,780
So when I send it what it does is it connects to the hello world resource.

100
00:06:40,980 --> 00:06:46,950
And then inside the class it the get method and so it calls the get method from the resource.

101
00:06:46,960 --> 00:06:51,120
So we added to the API and then just returns whatever it was called to return.

102
00:06:51,120 --> 00:06:54,950
In this case it just returns hello world which you see right here.

103
00:06:55,020 --> 00:06:55,410
All right.

104
00:06:55,500 --> 00:06:56,280
That's it.

105
00:06:56,280 --> 00:07:03,600
So successfully we've been able to return some basic piece of information using rest API and also using

106
00:07:03,600 --> 00:07:06,240
the postman graphical interface tool.

107
00:07:06,270 --> 00:07:11,280
Coming up next we're going to begin to expand this example with basic credit commands creating reading

108
00:07:11,310 --> 00:07:12,650
and deleting.

109
00:07:12,660 --> 00:07:13,260
All right.

110
00:07:13,260 --> 00:07:14,250
We'll see at the next lecture.
