Wrap API calls in helper and validate response
This commit is contained in:
@@ -39,7 +39,15 @@ class AccountInfo(LoginRequiredMixin, View):
|
||||
}
|
||||
return render(request, template_name, context)
|
||||
|
||||
live_info = dict(account.client.get_account())
|
||||
success, live_info = account.client.get_account()
|
||||
if not success:
|
||||
context = {
|
||||
"message": "Could not get account info",
|
||||
"class": "danger",
|
||||
"window_content": self.window_content,
|
||||
}
|
||||
return render(request, template_name, context)
|
||||
live_info = live_info
|
||||
account_info = account.__dict__
|
||||
account_info = {
|
||||
k: v for k, v in account_info.items() if k in self.VIEWABLE_FIELDS_MODEL
|
||||
|
||||
@@ -4,13 +4,13 @@ import orjson
|
||||
from django.conf import settings
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.http import HttpResponse, HttpResponseBadRequest
|
||||
from pydantic import ValidationError
|
||||
from rest_framework.parsers import JSONParser
|
||||
from rest_framework.views import APIView
|
||||
from serde import ValidationError
|
||||
|
||||
from core.forms import HookForm
|
||||
from core.lib import market
|
||||
from core.lib.serde import drakdoo_s
|
||||
from core.lib.schemas.drakdoo_s import DrakdooCallback
|
||||
from core.models import Callback, Hook
|
||||
from core.util import logs
|
||||
from core.views import ObjectCreate, ObjectDelete, ObjectList, ObjectUpdate
|
||||
@@ -44,7 +44,7 @@ class HookAPI(APIView):
|
||||
|
||||
# Try validating the JSON
|
||||
try:
|
||||
hook_resp = drakdoo_s.BaseDrakdoo.from_dict(request.data)
|
||||
hook_resp = DrakdooCallback(**request.data)
|
||||
except ValidationError as e:
|
||||
log.error(f"HookAPI POST: {e}")
|
||||
return HttpResponseBadRequest(e)
|
||||
|
||||
@@ -17,8 +17,10 @@ def get_positions(user, account_id=None):
|
||||
items = []
|
||||
accounts = Account.objects.filter(user=user)
|
||||
for account in accounts:
|
||||
positions = account.client.get_all_positions()
|
||||
print("positions", positions)
|
||||
success, positions = account.client.get_all_positions()
|
||||
if not success:
|
||||
items.append({"name": account.name, "status": "error"})
|
||||
continue
|
||||
|
||||
for item in positions:
|
||||
items.append(item)
|
||||
|
||||
Reference in New Issue
Block a user