Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,13 @@

import datahelper

# HOST & PORT are the values used to run the current application
HOST = os.getenv("HOST")
PORT = os.getenv("PORT")

# OUTPUT_URL is the url which receives all the output messages after they are processed by the app
OUTPUT_URL = os.getenv("OUTPUT_URL")

# Filepath for the JSON schema which represents
# the schema for the expected input messages to the app
SCHEMA_FILEPATH = os.getenv("SCHEMA_FILEPATH")


class Socket(BaseHTTPRequestHandler):
"""Handles HTTP requests that come to the server."""

schema_filepath = ''
output_url = ''

def _set_headers(self):
"""Sets common headers when returning an OK HTTPStatus. """
self.send_response(200)
Expand All @@ -45,7 +37,7 @@ def do_POST(self):
data = data.decode("utf-8")

try:
datahelper.validate_schema(data, SCHEMA_FILEPATH)
datahelper.validate_schema(data, self.schema_filepath)
except BaseException:
self.send_error(
400, 'Incorrect data format. Please check JSON schema.')
Expand All @@ -54,41 +46,62 @@ def do_POST(self):

try:
transformed_data = datahelper.transform(data)
output_message(transformed_data)
self.__output_message(transformed_data)
self._set_headers()
self.wfile.write(bytes("Data successfully consumed", 'utf8'))
except BaseException:
self.send_error(400, 'Error when sending output message')
logging.error('Error when sending output message')
raise

def __output_message(self, data: object):
"""Outputs the transformed payload to the specified HTTP endpoint

def output_message(data: object):
"""Outputs the transformed payload to the specified HTTP endpoint

Args:
data: transformed json object to send to output writer
"""
request = requests.post(OUTPUT_URL, data=json.dumps(data))
if request.status_code != 200:
Args:
data: transformed json object to send to output writer
"""
request = requests.post(self.output_url, data=json.dumps(data))
if request.status_code != 200:

logging.error("Error with a request %s and message not sent was %s",
request.status_code, data)
else:
logging.info("%s Response received from output writer",
request.status_code)
logging.error("Error with a request %s and message not sent was %s",
request.status_code, data)
else:
logging.info("%s Response received from output writer",
request.status_code)


def run(server_class=HTTPServer, handler_class=Socket):
def run(args, server_class=HTTPServer, handler_class=Socket):
"""Run the server on specified host and port, using our
custom Socket class to receive and process requests.
"""

server_address = (HOST, int(PORT))
server_address = (args.host, int(args.port))
handler_class.output_url = args.output_url
handler_class.schema_filepath = args.schema_filepath
httpd = server_class(server_address, handler_class)
logging.info('Running server on host ${HOST} and port ${PORT}')
httpd.serve_forever()


def cli():
from argparse import ArgumentParser

parser = ArgumentParser(description=__doc__)

# HOST & PORT are the values used to run the current application
parser.add_argument('--host', default=os.getenv('HOST'))
parser.add_argument('--port', default=os.getenv('PORT'))

# OUTPUT_URL is the url which receives all the output messages after they are processed by the app
parser.add_argument('--output_url', default=os.getenv('OUTPUT_URL'))

# Filepath for the JSON schema which represents
# the schema for the expected input messages to the app
parser.add_argument('--schema_filepath', default=os.getenv('SCHEMA_FILEPATH'))

args = parser.parse_args()

run(args)


if __name__ == "__main__":
run()
cli()