-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi_gateway.py
More file actions
42 lines (32 loc) · 1.96 KB
/
api_gateway.py
File metadata and controls
42 lines (32 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import boto3
class ApiGateway:
"""docstring for LexRunTimeApi"""
def __init__(self):
self.client = boto3.client('apigateway')
def create_rest_api(self, name):
return self.client.create_rest_api(name=name)
def get_resources(self, restApiId):
return self.client.get_resources(restApiId=restApiId)
def create_resource(self, restApiId, parentId, res):
return self.client.create_resource(restApiId=restApiId, parentId=parentId, pathPart=res)
def put_method(self, restApiId, resourceId, method):
return self.client.put_method(restApiId=restApiId, resourceId=resourceId,httpMethod=method, authorizationType='NONE')
def put_integration(self, restApiId, resourceId, httpMethod, uri):
return self.client.put_integration(restApiId=restApiId, resourceId=resourceId,httpMethod=httpMethod, type="AWS_PROXY",uri=uri, integrationHttpMethod='POST')
def get_integration(self, restApiId, resourceId):
return self.client.get_integration(restApiId=restApiId, resourceId=resourceId, httpMethod='GET')
def create_deployment(self, restApiId, stageName):
return self.client.create_deployment(restApiId=restApiId,stageName=stageName)
def set_up_dwight_gateway(self, api_name, aws_region, aws_acct_id, lambda_function):
new_api = self.create_rest_api(api_name)
restApiId = new_api["id"]
resources = self.get_resources(restApiId)
root_id = resources["items"][0]["id"]
dwight_resources = ["spotify" ,"connect-spotify" ,"gmail" ,"connect-gmail" ,"uber" ,"connect-uber"]
for resource_name in dwight_resources:
new_resource = self.create_resource(restApiId, root_id, resource_name)
self.put_method(restApiId, new_resource["id"], "GET")
uri = "arn:aws:apigateway:{0}:lambda:path/2015-03-31/functions/arn:aws:lambda:{0}:{1}:function:{2}/invocations".format(aws_region, aws_acct_id, lambda_function)
self.put_integration(restApiId, new_resource["id"], "GET", uri)
self.create_deployment(restApiId, "prod")
return restApiId