Skip to content

Commit

Permalink
Updating and simplifying the gethistory endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiehewitt15 committed Sep 6, 2023
1 parent 63bd3fc commit d189673
Showing 1 changed file with 24 additions and 32 deletions.
56 changes: 24 additions & 32 deletions server/oceandbs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -704,39 +704,31 @@ def get(self, request):
except Exception as e:
return Response(f"Error while retrieving possible storages: {str(e)}", status=500)

histories = []
errors = [] # To collect errors from each micro-service

for storage in storages:
if storage.type != storage_type:
continue

# Request status of quote from micro-service
try:
query_params = {
'page': page,
'pageSize': pageSize,
'userAddress': userAddress,
'nonce': params['nonce'][0],
'signature': params['signature'][0],
}
absolute_url = urljoin(storage.url, f'getHistory?{urlencode(query_params)}')
response = requests.get(absolute_url)

if response.status_code == 200:
histories.extend(response.json())
else:
errors.append(f"Error from storage {storage.type}: {json.loads(response.content)}")
try:
storage = Storage.objects.filter(is_active=True, type=storage_type).first()
if storage is None:
print(f'No matching storage type found at {datetime.datetime.now()}')
return Response("No matching storage type found", status=404)

query_params = {
'page': page,
'pageSize': pageSize,
'userAddress': userAddress,
'nonce': params['nonce'],
'signature': params['signature'],
}
absolute_url = urljoin(storage.url, f'getHistory?{urlencode(query_params)}')
response = requests.get(absolute_url)
print(f'Got response from microservice at {datetime.datetime.now()}')
print(f'Response: {response}')

except Exception as e:
errors.append(f"Exception while calling history endpoint from storage {storage.type}: {str(e)}")
if response.status_code == 200:
return Response(response.json(), status=200)
else:
return Response(response.json(), status=500)

if histories:
return Response(histories, status=200)

if errors:
return Response({"errors": errors}, status=500)

return Response("No matching storage type found", status=404)
except Exception as e:
print(f"An error occurred: {str(e)}")
return Response(f"An error occurred: {str(e)}", status=500)


0 comments on commit d189673

Please sign in to comment.