Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.

Commit 10e3556

Browse files
ajustes lambda_api
1 parent c35dc54 commit 10e3556

File tree

25 files changed

+476
-62
lines changed

25 files changed

+476
-62
lines changed

examples/lambda_api/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ Service routes map.
3232
* Redis
3333
* Swagger
3434

35+
## Kong configuration
36+
Configure Kong API Gateway to work compatible with API Gateway.
37+
38+
3539
## Installation
3640

3741
### Running Locally

examples/lambda_api/app.py

Lines changed: 105 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
from lambda_app.boot import load_dot_env,register_vendor
44
register_vendor()
55

6+
from lambda_app import helper
7+
8+
env = helper.get_environment()
9+
load_dot_env(env)
10+
611
# load env
712
from lambda_app.config import get_config
813
from lambda_app.enums.events import EventType
@@ -13,10 +18,6 @@
1318
from lambda_app.http_resources.response import ApiResponse
1419
from lambda_app.services.v1.ocoren_event_service import OcorenEventService
1520
from lambda_app.vos.events import EventVO
16-
17-
env = os.environ['ENVIRONMENT_NAME'] if 'ENVIRONMENT_NAME' in os.environ else None
18-
load_dot_env(env)
19-
2021
from lambda_app.logging import get_logger
2122
from lambda_app import APP_NAME, APP_VERSION, http_helper, helper
2223
from lambda_app.helper import open_vendor_file, print_routes
@@ -198,9 +199,108 @@ def event_create(event_type):
198199
return response.get_response(status_code)
199200

200201

202+
@app.route('/v1/event/<event_type>', methods=['GET'])
203+
def event_list(event_type):
204+
"""
205+
:param event_type:
206+
:return:
207+
---
208+
get:
209+
summary: List event
210+
parameters:
211+
- in: path
212+
name: event_type
213+
description: "Event type"
214+
required: true
215+
schema:
216+
type: string
217+
example: ocoren-event
218+
responses:
219+
200:
220+
content:
221+
application/json:
222+
schema: EventListResponseSchema
223+
4xx:
224+
content:
225+
application/json:
226+
schema: EventListErrorResponseSchema
227+
"""
228+
request = ApiRequest().parse_request(app)
229+
logger.info('event_type: {}'.format(event_type))
230+
logger.info('request: {}'.format(request))
231+
232+
# event_tracker = EventTracker(logger)
233+
#
234+
status_code = 200
235+
response = ApiResponse(request)
236+
response.set_hateos(False)
237+
# try:
238+
# # event_type validation
239+
# if EventType.from_value(event_type) not in EventType.get_public_events():
240+
# exception = ApiException(MessagesEnum.EVENT_TYPE_UNKNOWN_ERROR)
241+
# exception.set_message_params([event_type])
242+
# raise exception
243+
#
244+
# event_vo = EventVO(event_type=event_type, data=request.where)
245+
# # if EventType.from_value(event_type) == EventType.OCOREN_EVENT:
246+
# # event_service = OcorenEventService()
247+
# # else:
248+
# # event_service = ProductEventService()
249+
# event_service = OcorenEventService()
250+
# service = EventManager(logger=logger, event_service=event_service)
251+
# result = service.process(event_vo)
252+
# event_hash = event_vo.hash
253+
#
254+
# event_tracker.track(event_hash, event_vo.to_dict())
255+
#
256+
# if result:
257+
# code = MessagesEnum.EVENT_REGISTERED_WITH_SUCCESS.code
258+
# label = MessagesEnum.EVENT_REGISTERED_WITH_SUCCESS.label
259+
# message = MessagesEnum.EVENT_REGISTERED_WITH_SUCCESS.message
260+
# params = None
261+
# else:
262+
# if isinstance(service.exception, ApiException):
263+
# raise service.exception
264+
# else:
265+
# raise ApiException(MessagesEnum.INTERNAL_SERVER_ERROR)
266+
# except Exception as err:
267+
# logger.error(err)
268+
# result = False
269+
# event_hash = None
270+
# if isinstance(err, ApiException):
271+
# api_ex = err
272+
# status_code = 400
273+
# else:
274+
# api_ex = ApiException(MessagesEnum.CREATE_ERROR)
275+
# status_code = 500
276+
#
277+
# code = api_ex.code
278+
# label = api_ex.label
279+
# message = api_ex.message
280+
# params = api_ex.params
281+
#
282+
# data = {
283+
# "result": result,
284+
# "event_hash": event_hash,
285+
# "code": code,
286+
# "label": label,
287+
# "message": message,
288+
# "params": params
289+
# }
290+
291+
292+
data = {}
293+
294+
response.set_data(data)
295+
response.set_total()
296+
297+
# event_tracker.track(event_hash, data)
298+
return response.get_response(status_code)
299+
300+
201301
# doc
202302
spec.path(view=alive, path="/alive", operations=get_doc(alive))
203-
# spec.path(view=event_list, path="/v1/event/{event_type}", operations=get_doc(event_list))
303+
spec.path(view=event_list, path="/v1/event/{event_type}", operations=get_doc(event_list))
204304
spec.path(view=event_create, path="/v1/event/{event_type}", operations=get_doc(event_create))
205305

206306
print_routes(app, logger)

examples/lambda_api/lambda_app/helper.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,14 @@ def print_routes(app, logger):
164164
methods = list(dict_route.keys())
165165
for method in methods:
166166
logger.info('Route: %s - %s', method, path)
167+
168+
169+
def get_environment():
170+
environment = 'development'
171+
if 'ENVIRONMENT' in os.environ:
172+
environment = os.environ['ENVIRONMENT']
173+
elif 'ENVIRONMENT_NAME' in os.environ:
174+
environment = os.environ['ENVIRONMENT_NAME']
175+
elif 'APP_ENV' in os.environ:
176+
environment = os.environ['APP_ENV']
177+
return environment

examples/lambda_api/public/swagger/openapi.yml

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@ components:
66
example: I'm alive!
77
type: string
88
type: object
9+
Event:
10+
properties:
11+
data:
12+
type: object
13+
date:
14+
example: '2021-05-03T19:41:36.315842-03:00'
15+
format: date-time
16+
type: string
17+
hash:
18+
example: 406cce9743906f7b8d7dd5d5c5d8c95d820eeefd72a3a554a4a726d022d8fa19
19+
type: string
20+
type:
21+
type: string
22+
type: object
923
EventCreateErrorResponse:
1024
properties:
1125
code:
@@ -65,6 +79,61 @@ components:
6579
example: true
6680
type: boolean
6781
type: object
82+
EventListResponse:
83+
properties:
84+
control:
85+
$ref: '#/components/schemas/RequestControl'
86+
data:
87+
items:
88+
$ref: '#/components/schemas/Event'
89+
type: array
90+
links:
91+
items:
92+
$ref: '#/components/schemas/Link'
93+
type: array
94+
meta:
95+
$ref: '#/components/schemas/Meta'
96+
type: object
97+
Link:
98+
properties:
99+
href:
100+
type: string
101+
method:
102+
type: string
103+
rel:
104+
type: string
105+
type: object
106+
Meta:
107+
properties:
108+
first:
109+
format: url
110+
type: string
111+
href:
112+
format: url
113+
type: string
114+
last:
115+
format: url
116+
type: string
117+
next:
118+
format: url
119+
type: string
120+
previous:
121+
format: url
122+
type: string
123+
type: object
124+
RequestControl:
125+
properties:
126+
count:
127+
type: integer
128+
limit:
129+
type: integer
130+
offset:
131+
type: integer
132+
total:
133+
type: integer
134+
required:
135+
- limit
136+
type: object
68137
info:
69138
title: template-serverless-lambda-python-lambda-api
70139
version: 1.0.0
@@ -81,6 +150,27 @@ paths:
81150
description: Success response
82151
summary: Service Health Method
83152
/v1/event/{event_type}:
153+
get:
154+
parameters:
155+
- description: Event type
156+
in: path
157+
name: event_type
158+
required: true
159+
schema:
160+
example: ocoren-event
161+
type: string
162+
responses:
163+
'200':
164+
content:
165+
application/json:
166+
schema:
167+
$ref: '#/components/schemas/EventListResponse'
168+
4xx:
169+
content:
170+
application/json:
171+
schema:
172+
$ref: '#/components/schemas/EventListErrorResponseSchema'
173+
summary: List event
84174
post:
85175
parameters:
86176
- description: Event type

0 commit comments

Comments
 (0)