|
2 | 2 |
|
3 | 3 | This module contains the handler method |
4 | 4 | """ |
5 | | -import inspect |
6 | | -import os |
7 | 5 | import base64 |
| 6 | +import os |
| 7 | + |
8 | 8 | import boot |
9 | | -from lambda_app.services.product_manager import ProductManager |
10 | | -from lambda_app.services.v1.healthcheck import HealthCheckSchema |
11 | | -from lambda_app.services.v1.healthcheck.resources import \ |
12 | | - MysqlConnectionHealthCheck, RedisConnectionHealthCheck, \ |
13 | | - SQSConnectionHealthCheck, SelfConnectionHealthCheck |
14 | | -from lambda_app.services.v1.healthcheck_service import HealthCheckService |
| 9 | +from lambda_app import APP_NAME, APP_VERSION, http_helper |
| 10 | +from lambda_app import helper |
15 | 11 | from lambda_app.config import get_config |
16 | | -from lambda_app.enums.events import EventType |
17 | 12 | from lambda_app.enums.messages import MessagesEnum |
18 | | -from lambda_app.events.tracker import EventTracker |
19 | 13 | from lambda_app.exceptions import ApiException |
20 | | -from lambda_app.http_resources.request import ApiRequest |
21 | | -from lambda_app.http_resources.response import ApiResponse |
22 | | -from lambda_app.services.v1.product_service import ProductService as ProductServiceV1 |
23 | | -from lambda_app.vos.events import EventVO |
24 | | -from lambda_app.logging import get_logger |
25 | | -from lambda_app import APP_NAME, APP_VERSION, http_helper |
26 | 14 | from lambda_app.helper import open_vendor_file, print_routes |
27 | 15 | from lambda_app.http_helper import CUSTOM_DEFAULT_HEADERS |
| 16 | +from lambda_app.http_resources.request import ApiRequest |
| 17 | +from lambda_app.http_resources.response import ApiResponse |
28 | 18 | from lambda_app.lambda_flask import LambdaFlask |
| 19 | +from lambda_app.logging import get_logger |
29 | 20 | from lambda_app.openapi import spec, get_doc, generate_openapi_yml |
30 | | -from lambda_app.openapi import api_schemas |
31 | | -from lambda_app import helper |
| 21 | +from lambda_app.services.product_manager import ProductManager |
| 22 | +from lambda_app.services.v1.healthcheck import HealthCheckResult |
| 23 | +from lambda_app.services.v1.healthcheck.resources import \ |
| 24 | + MysqlConnectionHealthCheck, RedisConnectionHealthCheck, \ |
| 25 | + SQSConnectionHealthCheck, SelfConnectionHealthCheck |
| 26 | +from lambda_app.services.v1.healthcheck_service import HealthCheckService |
| 27 | +from lambda_app.services.v1.product_service import ProductService as ProductServiceV1 |
32 | 28 |
|
33 | 29 | # load env |
34 | 30 | ENV = helper.get_environment() |
@@ -85,6 +81,7 @@ def alive(): |
85 | 81 | LOGGER, CONFIG), ["redis"]) |
86 | 82 | service.add_check("queue", SQSConnectionHealthCheck( |
87 | 83 | LOGGER, CONFIG), ["queue"]) |
| 84 | + service.add_check("test", lambda: HealthCheckResult.healthy("connected"), ["example"]) |
88 | 85 |
|
89 | 86 | return service.get_response() |
90 | 87 |
|
@@ -224,108 +221,6 @@ def product_delete(): |
224 | 221 | pass |
225 | 222 |
|
226 | 223 |
|
227 | | -@APP.route('/v1/event/<event_type>', methods=['POST']) |
228 | | -def event_create(event_type): |
229 | | - """ |
230 | | - :param event_type: |
231 | | - :return: |
232 | | - --- |
233 | | - post: |
234 | | - summary: Create event |
235 | | - parameters: |
236 | | - - in: path |
237 | | - name: event_type |
238 | | - description: "Event type" |
239 | | - required: true |
240 | | - schema: |
241 | | - type: string |
242 | | - example: ocoren-event |
243 | | - requestBody: |
244 | | - description: 'Event to be created' |
245 | | - required: true |
246 | | - content: |
247 | | - application/json: |
248 | | - schema: EventCreateRequest |
249 | | - responses: |
250 | | - 200: |
251 | | - content: |
252 | | - application/json: |
253 | | - schema: EventCreateResponseSchema |
254 | | - 4xx: |
255 | | - content: |
256 | | - application/json: |
257 | | - schema: EventCreateErrorResponseSchema |
258 | | - """ |
259 | | - request = ApiRequest().parse_request(APP) |
260 | | - LOGGER.info('event_type: {}'.format(event_type)) |
261 | | - LOGGER.info('request: {}'.format(request)) |
262 | | - |
263 | | - event_tracker = EventTracker(LOGGER) |
264 | | - |
265 | | - status_code = 200 |
266 | | - response = ApiResponse(request) |
267 | | - response.set_hateos(False) |
268 | | - try: |
269 | | - # event_type validation |
270 | | - if EventType.from_value( |
271 | | - event_type) not in EventType.get_public_events(): |
272 | | - exception = ApiException(MessagesEnum.EVENT_TYPE_UNKNOWN_ERROR) |
273 | | - exception.set_message_params([event_type]) |
274 | | - raise exception |
275 | | - |
276 | | - event_vo = EventVO(event_type=event_type, data=request.where) |
277 | | - # if EventType.from_value(event_type) == EventType.OCOREN_EVENT: |
278 | | - # event_service = OcorenEventService() |
279 | | - # else: |
280 | | - # event_service = ProductEventService() |
281 | | - event_service = OcorenEventServiceV1() |
282 | | - service = EventManager(logger=LOGGER, event_service=event_service) |
283 | | - result = service.process(event_vo) |
284 | | - event_hash = event_vo.hash |
285 | | - |
286 | | - event_tracker.track(event_hash, event_vo.to_dict()) |
287 | | - |
288 | | - if result: |
289 | | - code = MessagesEnum.EVENT_REGISTERED_WITH_SUCCESS.code |
290 | | - label = MessagesEnum.EVENT_REGISTERED_WITH_SUCCESS.label |
291 | | - message = MessagesEnum.EVENT_REGISTERED_WITH_SUCCESS.message |
292 | | - params = None |
293 | | - else: |
294 | | - if isinstance(service.exception, ApiException): |
295 | | - raise service.exception |
296 | | - else: |
297 | | - raise ApiException(MessagesEnum.INTERNAL_SERVER_ERROR) |
298 | | - except Exception as err: |
299 | | - LOGGER.error(err) |
300 | | - result = False |
301 | | - event_hash = None |
302 | | - if isinstance(err, ApiException): |
303 | | - api_ex = err |
304 | | - status_code = 400 |
305 | | - else: |
306 | | - api_ex = ApiException(MessagesEnum.CREATE_ERROR) |
307 | | - status_code = 500 |
308 | | - |
309 | | - code = api_ex.code |
310 | | - label = api_ex.label |
311 | | - message = api_ex.message |
312 | | - params = api_ex.params |
313 | | - |
314 | | - data = { |
315 | | - "result": result, |
316 | | - "event_hash": event_hash, |
317 | | - "code": code, |
318 | | - "label": label, |
319 | | - "message": message, |
320 | | - "params": params |
321 | | - } |
322 | | - |
323 | | - response.set_data(data) |
324 | | - |
325 | | - event_tracker.track(event_hash, data) |
326 | | - return response.get_response(status_code) |
327 | | - |
328 | | - |
329 | 224 | # ************* |
330 | 225 | # doc |
331 | 226 | # ************* |
|
0 commit comments