Skip to content

Commit

Permalink
Create make_response wrapper and login context manager.
Browse files Browse the repository at this point in the history
  • Loading branch information
amh-mw committed Dec 16, 2024
1 parent e9b26a5 commit 54059be
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions vlamb.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,59 @@
import functools
import hashlib
import http
import json
import logging
import os
import urllib.parse
import urllib.request

import boto3

_logger = logging.getLogger(__name__)

ssm = None


# https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-output-format
def make_response(func):
@functools.wraps(func)
def wrapper(event, context):
_logger.debug("request %s", event)
try:
message = func(event, context) or http.HTTPStatus.OK.phrase
code = http.HTTPStatus.OK.value
except VtapiError as e:
_logger.error(e.message)
message = e.message
code = e.status
except Exception as e:
_logger.exception(str(e))
message = http.HTTPStatus.INTERNAL_SERVER_ERROR.phrase
code = http.HTTPStatus.INTERNAL_SERVER_ERROR.value
response = {
'body': json.dumps({'message': message}),
'statusCode': code,
}
_logger.debug("response %s", response)
return response
return wrapper


def login():
global ssm
host = os.environ['VTIGER_HOST']
password = os.environ['VTIGER_PASS']
user = os.environ['VTIGER_USER']

if password.startswith('/') or password.startswith('arn:'):
if not ssm:
ssm = boto3.client('ssm')
password = ssm.get_parameter(Name=password, WithDecryption=True)['Parameter']['Value']

api = Vtapi(host)
api.login(user, password)
return api


class Vtapi:
def __init__(self, url):
Expand Down

0 comments on commit 54059be

Please sign in to comment.