Skip to content

Commit

Permalink
Merge pull request #9 from Netflix/response-formating
Browse files Browse the repository at this point in the history
exception handling
  • Loading branch information
ferras authored May 19, 2020
2 parents c9e1366 + 00376ae commit ca016b6
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 64 deletions.
2 changes: 2 additions & 0 deletions metadata_service/api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
METADATA_SERVICE_VERSION = 'v1.0'
METADATA_SERVICE_HEADER = 'METADATA_SERVICE_VERSION'
5 changes: 4 additions & 1 deletion metadata_service/api/admin.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import boto3
import json
import os
from multidict import MultiDict
from aiohttp import web
from botocore.client import Config
from .utils import get_traceback_str
from . import METADATA_SERVICE_VERSION, METADATA_SERVICE_HEADER


class AuthApi(object):
Expand All @@ -25,7 +27,8 @@ async def ping(self, request):
"405":
description: invalid HTTP Method
"""
return web.Response(text="pong")
return web.Response(text="pong", headers=MultiDict(
{METADATA_SERVICE_HEADER: METADATA_SERVICE_VERSION}))

async def get_authorization_token(self, request):
"""
Expand Down
16 changes: 9 additions & 7 deletions metadata_service/api/artifact.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from aiohttp import web
from ..data.postgres_async_db import AsyncPostgresDB
from .utils import read_body
from .utils import read_body, format_response, handle_exceptions
import json


Expand Down Expand Up @@ -39,6 +39,8 @@ def __init__(self, app):
)
self._async_table = AsyncPostgresDB.get_instance().artifact_table_postgres

@format_response
@handle_exceptions
async def get_artifact(self, request):
"""
---
Expand Down Expand Up @@ -85,12 +87,9 @@ async def get_artifact(self, request):
task_id = request.match_info.get("task_id")
artifact_name = request.match_info.get("artifact_name")

artifact = await self._async_table.get_artifact(
return await self._async_table.get_artifact(
flow_name, run_number, step_name, task_id, artifact_name
)
return web.Response(
status=artifact.response_code, body=json.dumps(artifact.body)
)

async def get_artifacts_by_task(self, request):
"""
Expand Down Expand Up @@ -135,8 +134,9 @@ async def get_artifacts_by_task(self, request):
artifacts = await self._async_table.get_artifact_in_task(
flow_name, run_number, step_name, task_id
)

filtered_body = ArtificatsApi._filter_artifacts_by_attempt_id(
artifacts)
artifacts.body)
return web.Response(
status=artifacts.response_code, body=json.dumps(filtered_body)
)
Expand Down Expand Up @@ -178,11 +178,13 @@ async def get_artifacts_by_step(self, request):
artifacts = await self._async_table.get_artifact_in_steps(
flow_name, run_number, step_name
)

filtered_body = ArtificatsApi._filter_artifacts_by_attempt_id(artifacts.body)
return web.Response(
status=artifacts.response_code, body=json.dumps(filtered_body)
)


async def get_artifacts_by_run(self, request):
"""
---
Expand Down Expand Up @@ -212,7 +214,7 @@ async def get_artifacts_by_run(self, request):
run_number = request.match_info.get("run_number")

artifacts = await self._async_table.get_artifacts_in_runs(flow_name, run_number)
filtered_body = ArtificatsApi._filter_artifacts_by_attempt_id(artifacts)
filtered_body = ArtificatsApi._filter_artifacts_by_attempt_id(artifacts.body)
return web.Response(
status=artifacts.response_code, body=json.dumps(filtered_body)
)
Expand Down
21 changes: 10 additions & 11 deletions metadata_service/api/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import json
from ..data.models import FlowRow
from ..data.postgres_async_db import AsyncPostgresDB
from .utils import read_body
from .utils import read_body, format_response, handle_exceptions
import asyncio


Expand All @@ -17,6 +17,8 @@ def __init__(self, app):
app.router.add_route("POST", "/flows/{flow_id}", self.create_flow)
self._async_table = AsyncPostgresDB.get_instance().flow_table_postgres

@format_response
@handle_exceptions
async def create_flow(self, request):
"""
---
Expand Down Expand Up @@ -60,11 +62,10 @@ async def create_flow(self, request):
flow = FlowRow(
flow_id=flow_name, user_name=user, tags=tags, system_tags=system_tags
)
response = await self._async_table.add_flow(flow)
return web.Response(
status=response.response_code, body=json.dumps(response.body)
)
return await self._async_table.add_flow(flow)

@format_response
@handle_exceptions
async def get_flow(self, request):
"""
---
Expand All @@ -89,11 +90,10 @@ async def get_flow(self, request):
"""

flow_name = request.match_info.get("flow_id")
flow_response = await self._async_table.get_flow(flow_name)
return web.Response(
status=flow_response.response_code, body=json.dumps(flow_response.body)
)
return await self._async_table.get_flow(flow_name)

@format_response
@handle_exceptions
async def get_all_flows(self, request):
"""
---
Expand All @@ -108,5 +108,4 @@ async def get_all_flows(self, request):
"405":
description: invalid HTTP Method
"""
flows = await self._async_table.get_all_flows()
return web.Response(status=flows.response_code, body=json.dumps(flows.body))
return await self._async_table.get_all_flows()
19 changes: 8 additions & 11 deletions metadata_service/api/metadata.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from aiohttp import web
import json
from .utils import read_body
from .utils import read_body, format_response, handle_exceptions
import asyncio
from ..data.postgres_async_db import AsyncPostgresDB


class MetadataApi(object):
_metadata_table = None
lock = asyncio.Lock()
Expand All @@ -30,6 +29,9 @@ def __init__(self, app):

self._async_table = AsyncPostgresDB.get_instance().metadata_table_postgres


@format_response
@handle_exceptions
async def get_metadata(self, request):
"""
---
Expand Down Expand Up @@ -69,14 +71,12 @@ async def get_metadata(self, request):
run_number = request.match_info.get("run_number")
step_name = request.match_info.get("step_name")
task_id = request.match_info.get("task_id")
metadata_response = await self._async_table.get_metadata(
return await self._async_table.get_metadata(
flow_name, run_number, step_name, task_id
)
return web.Response(
status=metadata_response.response_code,
body=json.dumps(metadata_response.body),
)

@format_response
@handle_exceptions
async def get_metadata_by_run(self, request):
"""
---
Expand Down Expand Up @@ -104,12 +104,9 @@ async def get_metadata_by_run(self, request):
"""
flow_name = request.match_info.get("flow_id")
run_number = request.match_info.get("run_number")
metadata_runs = await self._async_table.get_metadata_in_runs(
return await self._async_table.get_metadata_in_runs(
flow_name, run_number
)
return web.Response(
status=metadata_runs.response_code, body=json.dumps(metadata_runs.body)
)

async def create_metadata(self, request):
"""
Expand Down
24 changes: 10 additions & 14 deletions metadata_service/api/run.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import asyncio
import json
from aiohttp import web
from .utils import read_body
from .utils import read_body, format_response, handle_exceptions
from ..data.models import RunRow
from ..data.postgres_async_db import AsyncPostgresDB

Expand All @@ -16,6 +14,8 @@ def __init__(self, app):
app.router.add_route("POST", "/flows/{flow_id}/run", self.create_run)
self._async_table = AsyncPostgresDB.get_instance().run_table_postgres

@format_response
@handle_exceptions
async def get_run(self, request):
"""
---
Expand Down Expand Up @@ -45,11 +45,10 @@ async def get_run(self, request):
"""
flow_name = request.match_info.get("flow_id")
run_number = request.match_info.get("run_number")
run_response = await self._async_table.get_run(flow_name, run_number)
return web.Response(
status=run_response.response_code, body=json.dumps(run_response.body)
)
return await self._async_table.get_run(flow_name, run_number)

@format_response
@handle_exceptions
async def get_all_runs(self, request):
"""
---
Expand All @@ -71,10 +70,10 @@ async def get_all_runs(self, request):
description: invalid HTTP Method
"""
flow_name = request.match_info.get("flow_id")
runs = await self._async_table.get_all_runs(flow_name)

return web.Response(status=runs.response_code, body=json.dumps(runs.body))
return await self._async_table.get_all_runs(flow_name)

@format_response
@handle_exceptions
async def create_run(self, request):
"""
---
Expand Down Expand Up @@ -118,7 +117,4 @@ async def create_run(self, request):
flow_id=flow_name, user_name=user, tags=tags, system_tags=system_tags
)

run_response = await self._async_table.add_run(run_row)
return web.Response(
status=run_response.response_code, body=json.dumps(run_response.body)
)
return await self._async_table.add_run(run_row)
19 changes: 10 additions & 9 deletions metadata_service/api/step.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from aiohttp import web
from ..data.models import StepRow
import json
from .utils import read_body
from .utils import read_body, format_response, handle_exceptions
from ..data.postgres_async_db import AsyncPostgresDB


Expand All @@ -23,6 +21,8 @@ def __init__(self, app):
)
self._async_table = AsyncPostgresDB.get_instance().step_table_postgres

@format_response
@handle_exceptions
async def get_steps(self, request):
"""
---
Expand Down Expand Up @@ -50,9 +50,10 @@ async def get_steps(self, request):
"""
flow_name = request.match_info.get("flow_id")
run_number = request.match_info.get("run_number")
steps = await self._async_table.get_steps(flow_name, run_number)
return web.Response(status=steps.response_code, body=json.dumps(steps.body))
return await self._async_table.get_steps(flow_name, run_number)

@format_response
@handle_exceptions
async def get_step(self, request):
"""
---
Expand Down Expand Up @@ -88,9 +89,10 @@ async def get_step(self, request):
flow_name = request.match_info.get("flow_id")
run_number = request.match_info.get("run_number")
step_name = request.match_info.get("step_name")
step = await self._async_table.get_step(flow_name, run_number, step_name)
return web.Response(status=step.response_code, body=json.dumps(step.body))
return await self._async_table.get_step(flow_name, run_number, step_name)

@format_response
@handle_exceptions
async def create_step(self, request):
"""
---
Expand Down Expand Up @@ -147,5 +149,4 @@ async def create_step(self, request):
flow_name, run_number, user, step_name, tags=tags, system_tags=system_tags
)

step = await self._async_table.add_step(step_row)
return web.Response(status=step.response_code, body=json.dumps(step.body))
return await self._async_table.add_step(step_row)
21 changes: 10 additions & 11 deletions metadata_service/api/task.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from aiohttp import web
from ..data.models import TaskRow
from ..data.postgres_async_db import AsyncPostgresDB
import json
from .utils import read_body
from .utils import read_body, format_response, handle_exceptions
import asyncio


Expand All @@ -28,6 +26,8 @@ def __init__(self, app):
)
self._async_table = AsyncPostgresDB.get_instance().task_table_postgres

@format_response
@handle_exceptions
async def get_tasks(self, request):
"""
---
Expand Down Expand Up @@ -62,9 +62,10 @@ async def get_tasks(self, request):
run_number = request.match_info.get("run_number")
step_name = request.match_info.get("step_name")

tasks = await self._async_table.get_tasks(flow_name, run_number, step_name)
return web.Response(status=tasks.response_code, body=json.dumps(tasks.body))
return await self._async_table.get_tasks(flow_name, run_number, step_name)

@format_response
@handle_exceptions
async def get_task(self, request):
"""
---
Expand Down Expand Up @@ -104,11 +105,12 @@ async def get_task(self, request):
run_number = request.match_info.get("run_number")
step_name = request.match_info.get("step_name")
task_id = request.match_info.get("task_id")
task = await self._async_table.get_task(
return await self._async_table.get_task(
flow_name, run_number, step_name, task_id
)
return web.Response(status=task.response_code, body=json.dumps(task.body))

@format_response
@handle_exceptions
async def create_task(self, request):
"""
---
Expand Down Expand Up @@ -168,7 +170,4 @@ async def create_task(self, request):
tags=tags,
system_tags=system_tags,
)
task_response = await self._async_table.add_task(task)
return web.Response(
status=task_response.response_code, body=json.dumps(task_response.body)
)
return await self._async_table.add_task(task)
Loading

0 comments on commit ca016b6

Please sign in to comment.