Skip to content

Commit

Permalink
Added static JSON responses to test server
Browse files Browse the repository at this point in the history
  • Loading branch information
jamespeterschinner committed Dec 26, 2017
1 parent bf96abd commit f82c407
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 25 deletions.
4 changes: 2 additions & 2 deletions async_v20/definitions/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@
'guaranteed_execution_fee': 'guaranteed_execution_fee', 'halfSpreadCost': 'half_spread_cost',
'half_spread_cost': 'half_spread_cost', 'description': 'description', 'list': 'list',
'current-event': 'current_event', 'url': 'url', 'sid': 'sid', 'message': 'message',
'informational': 'informational', 'level': 'level', 'image': 'image'}
'informational': 'informational', 'level': 'level', 'image': 'image', 'icon_set': 'icon_set'}

json_attributes = {'long': 'long', 'short': 'short', 'default': 'default', 'reduce_first': 'reduceFirst',
'reduce_only': 'reduceOnly', 'open_only': 'openOnly', 'timestamp': 'timestamp',
Expand Down Expand Up @@ -252,4 +252,4 @@
'guaranteed_execution_fee': 'guaranteedExecutionFee', 'half_spread_cost': 'halfSpreadCost',
'description': 'description', 'list': 'list',
'current_event':'current-event', 'url': 'url', 'sid': 'sid', 'message': 'message',
'informational': 'informational', 'level': 'level', 'image': 'image'}
'informational': 'informational', 'level': 'level', 'image': 'image', 'icon_set': 'icon_set'}
11 changes: 11 additions & 0 deletions async_v20/definitions/health_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,14 @@ def __new__(cls, id: str, name: str, description: str,

class ArrayService(Array):
_contains = Service

class Image(Model):
"""An Image to be displayed to the end user.
"""

def __new__(cls, name: str, icon_set: str, url: str):
return super().__new__(**Image._preset_arguments, **locals())


class ArrayImage(Array):
_contains = Image
4 changes: 2 additions & 2 deletions async_v20/endpoints/health.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class GETStatuses(EndPoint):
path = ('/api/v1/statuses',)

# valid responses
responses = {200: {'status': ArrayStatus}}
responses = {200: {'statuses': ArrayStatus}}


class GETStatus(EndPoint):
Expand Down Expand Up @@ -147,4 +147,4 @@ class GETImages(EndPoint):
path = ('/api/v1/status-images',)

# valid responses
responses = {200: str}
responses = {200: {'images': ArrayImage}}
11 changes: 7 additions & 4 deletions async_v20/interface/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@ async def _create_response(json_body, endpoint, schema, status, boolean, datetim
# Here we iterate through all the json objects returned in the response
# and construct the corresponding async_v20 type as determined by the endpoints
# Schema
def create_data():
if isinstance(schema, dict):
data = []
for json_object, json_field in json_body.items():
yield json_object, create_attribute(schema.get(json_object), json_field)

return Response(tuple(create_data()), status, boolean, datetime_format)
data.append((json_object, create_attribute(schema.get(json_object), json_field)))
else:
obj = schema(**json_body)
data = [(obj.__class__.__name__.lower(), obj)]
return Response(data, status, boolean, datetime_format)


async def _rest_response(self, response, endpoint, enable_rest):
Expand Down
19 changes: 10 additions & 9 deletions tests/fixtures/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@
('GET', '/v3/accounts/123-123-1234567-123/transactions/stream'): None,
('GET', '/v3/users/123-123-1234567-123'): None,
('GET', '/v3/users/123-123-1234567-123/externalInfo'): None,
('GET', '/api/v1/services'): None,
('GET', '/api/v1/services/0000'): None,
('GET', '/api/v1/service-lists'): None,
('GET', '/api/v1/service-lists/0000'): None,
('GET', '/api/v1/services/0000/events'): None,
('GET', '/api/v1/services/0000/events/current'): None,
('GET', '/api/v1/services/0000/events/0000'): None,
('GET', '/api/v1/statuses'): None,
('GET', '/api/v1/status-images'): None
('GET', '/api/v1/services'): list_services_response,
('GET', '/api/v1/services/0000'): get_service_response,
('GET', '/api/v1/service-lists'): list_service_lists_response,
('GET', '/api/v1/service-lists/0000'): get_service_list_response,
('GET', '/api/v1/services/0000/events'): list_events_response,
('GET', '/api/v1/services/0000/events/current'): get_current_event_response,
('GET', '/api/v1/services/0000/events/0000'): get_event_response,
('GET', '/api/v1/statuses'): list_statuses_response,
('GET', '/api/v1/statuses/0000'): get_status_response,
('GET', '/api/v1/status-images'): list_images_response
}
20 changes: 12 additions & 8 deletions tests/fixtures/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,27 @@
from .routes import routes

rest_headers = {'Access-Control-Allow-Headers': 'Authorization, Content-Type, Accept-Datetime-Format',
'Access-Control-Allow-Methods': 'PUT, PATCH, POST, GET, OPTIONS, DELETE', 'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json', 'RequestID': '42359369470976686', 'Content-Encoding': 'gzip',
'Vary': 'Accept-Encoding', 'Connection': 'Keep-Alive'}
'Access-Control-Allow-Methods': 'PUT, PATCH, POST, GET, OPTIONS, DELETE',
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json', 'RequestID': '42359369470976686', 'Content-Encoding': 'gzip',
'Vary': 'Accept-Encoding', 'Connection': 'Keep-Alive'}

stream_headers = {'Access-Control-Allow-Headers': 'Authorization, Content-Type, Accept-Datetime-Format',
'Access-Control-Allow-Methods': 'PUT, PATCH, POST, GET, OPTIONS, DELETE', 'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json', 'RequestID': '42359369470976686',
'Vary': 'Accept-Encoding', 'Connection': 'Keep-Alive'}
'Access-Control-Allow-Methods': 'PUT, PATCH, POST, GET, OPTIONS, DELETE',
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json', 'RequestID': '42359369470976686',
'Vary': 'Accept-Encoding', 'Connection': 'Keep-Alive'}

status = 200
received = ''
sleep_time = 0


def get_id_from_path(path):
print(path)
try:
path_id = re.findall(r'(?<=\/)(\d+)(?=[/\s])', path)[0]
path = re.sub(r'(((orders)|(trades)|(transactions))\/)(\d+)', '\g<1>0000', path)
path_id = re.findall(r'(?<=\/)(\d+)(?=(\/|\Z))', path)[0][0]
path = re.sub(r'(?<=\/)(\d+)(?=(\/|\Z))', '0000', path)
except:
return path, None
else:
Expand Down Expand Up @@ -87,6 +90,7 @@ async def handler(request):
await asyncio.sleep(sleep_time)

await asyncio.sleep(sleep_time)
print(response_data)
return web.Response(body=gzip.compress(bytes(response_data, encoding='utf8')), headers=rest_headers,
status=response_status)

Expand Down
20 changes: 20 additions & 0 deletions tests/fixtures/static.py

Large diffs are not rendered by default.

0 comments on commit f82c407

Please sign in to comment.