Welcome back to BackSpace Academy. In this lab I'm going to run through how to 1
use the AWS api gateway service to create a REST API and then we'll look at 2
connecting to this REST API and sending HTTP requests to it from our desktop 3
computer. From the services menu we go to application services and API gateway. 4
If we haven't already created an API we're going to be presented with the welcome 5
screen. So we just click on get started, the message comes up saying that there's 6
already an example API we can use to get started, and that's 7
what we'll do, so there's our example API and there's the swagger definition as 8
well, if we want to have a look at that for that example API. We're just going to 9
import that. Ok we can see there it has created that API for us, So we are 10
currently in the resources section of our API and we can see there we've 11
got a number of methods that have been defined for how our API responds to 12
different types of requests and what we've got set up here is how it 13
responds to "get" requests to the route endpoint and also how it responds to the 14
- "pets" additional to that and also "pets" followed by a "pet ID" so if we want to 15
get the feedback or the the data concerning a "pet" an individual "pet" with 16
a "pet ID" we can put that out there as a get request and it will be returned with 17
that information if it's available, now before we can actually use this we need 18
to deploy it so what we do is, we go to actions and then we go to deploy API and 19
that will create a stage, so our deployment stage will be a new stage and 20
we're just going to call it demo that will be fine 21
we're not going to worry about having a description and a deployment description 22
but in a production environment you would definitely put that in but it's 23
not necessary 24
so now we have very quickly deployed our API and what we have here is an invoke 25
URL and so that is the the root of our our API endpoint so we can click on that 26
and it will give us a response, and its given us a response in the form of 27
some some HTML there, so we're not going to be using the console to send out our 28
HTTP request, we're going to be using it from our desktop side using an 29
application but just to get started we can use this this postman 30
application, just go to getpostman.com and download that, it's available for Mac 31
Windows and Linux and then we can use that to to test our API and send 32
requests and see what comes back. Ok so once you're downloaded and installed 33
postman and you've got it up and running you'll be presented in with a screen 34
similar to this, so what I'm going to do is just give you a quick idea of how to 35
use it, so we've got here the different types of HTTP requests we can send to 36
our API, you've got "get", "post", "put", "delete", we put in our URL for our endpoint and 37
then we click on send, and then what will come back will be the response from that 38
so I'm going to start off with our deployed stage and the invoke URL for 39
that, so I'm just going to copy that URL, and then paste that in here, now just 40
looking here we've got "no auth" so this doesn't require any authorization for a 41
"get" you just getting information so we haven't setup authentication for 42
that, but there are a number of options that we can do and one of them is AWS 43
signature and although that's a pretty cool thing that they've put that in 44
there never ever use it and there's a reason why because you never ever want 45
to write code that contains your access key and your secret access key because 46
that is just plain, stupid, so please don't do it and you'll be exposing 47
yourself big time to access to your AWS account so 48
the best way to do it is if you want authentication for your API use an iam 49
roll the same as you would with with any website, so use an 50
IAM role and verify or authenticate your end user using Facebook or Google 51
or Microsoft Active Directory or whatever you want to use, so that is the 52
way that you would do it you would never put in your access key and your secret 53
access key in code, especially if it's going on the client-side so you've been 54
warned about that and we'll go back to "NoAuth" and we're going to put in our 55
endpoint and we're going to click on send, so what is returned from a "get" 56
request to the root of their endpoint is that HTML web page that the introduction 57
to to the API and that's what we would expect Now there is also, if we have a 58
look in the methods, so if we go back to resources or if you actually see it from 59
here I believe, so we've actually got a "get" for "pets" we can use, so let's go 60
into a "get" request on "pets" so I just put "pets" on the end here and we'll send it 61
and see what comes back, and so what gets returned from that method is a list of 62
all the pets and they come back as a JSON objects an array of pet objects 63
here which have an ID a type and a price. We've got three pets there we've got 64
dog cat and fish. We can also there is another method there which is a "get" 65
which followed by the "pet ID" and that's when we can also use that one, so if we 66
put a "pet ID" of say three it should come back as a fish so put in three and we 67
click send and there you go so we've got the object for ID number three which is 68
type fish and a price of 99. So everything seems to be working fine from 69
this end and now what we can do is we can look at writing an application, a 70
real application in JavaScript that can do this and access all this 71
information programmatically and we'll do that in the next lab