fisk/core/views/hooks.py

153 lines
4.7 KiB
Python
Raw Normal View History

import re
2022-10-14 06:20:30 +00:00
import orjson
from django.conf import settings
2022-10-14 06:20:30 +00:00
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import HttpResponse, HttpResponseBadRequest
from pydantic import ValidationError
from rest_framework.parsers import JSONParser
2022-10-12 06:22:22 +00:00
from rest_framework.views import APIView
from core.forms import HookForm
from core.lib import market
from core.lib.schemas.drakdoo_s import DrakdooCallback
from core.models import Callback, Hook
2022-10-15 22:36:04 +00:00
from core.util import logs
from core.views import ObjectCreate, ObjectDelete, ObjectList, ObjectUpdate
2022-10-15 22:24:03 +00:00
2022-10-15 22:34:55 +00:00
log = logs.get_logger(__name__)
2022-10-12 06:22:22 +00:00
def extract_price(message):
result = re.findall("\d+\.\d+", message) # noqa
if len(result) != 1:
log.error(f"Could not extract price from message: {message}")
return False
try:
log.debug(f"Extracted {result[0]} from '{message}'")
return float(result[0])
except ValueError:
return False
class HookAPI(APIView):
parser_classes = [JSONParser]
def post(self, request, hook_name):
2022-10-15 22:24:03 +00:00
log.debug(f"HookAPI POST: {request.data}")
# Try loading the JSON
2022-10-16 13:37:49 +00:00
# try:
# loaded_json = orjson.loads(request.data)
# except orjson.JSONDecodeError:
# return HttpResponseBadRequest("Invalid JSON")
2022-10-15 22:24:03 +00:00
# Try validating the JSON
try:
hook_resp = DrakdooCallback(**request.data)
2022-10-15 22:24:03 +00:00
except ValidationError as e:
log.error(f"HookAPI POST: {e}")
return HttpResponseBadRequest(e)
price = extract_price(hook_resp.message)
if not price:
log.debug(f"Could not extract price from message: {hook_resp.message}")
return HttpResponseBadRequest("Could not extract price from message")
base = hook_resp.market.item
quote = hook_resp.market.currency
symbol = f"{base.upper()}/{quote.upper()}"
if symbol not in settings.ASSET_FILTER:
log.debug(f"Skipping {symbol} because it is not in the asset filter")
return HttpResponseBadRequest("Invalid symbol")
data = {
"title": hook_resp.title,
"message": hook_resp.message,
"period": hook_resp.period,
"sent": hook_resp.timestamp.sent,
"trade": hook_resp.timestamp.trade,
"exchange": hook_resp.market.exchange,
"base": hook_resp.market.item,
"quote": hook_resp.market.currency,
"symbol": symbol,
"contract": hook_resp.market.contract,
"price": price,
}
log.debug("HookAPI callback: data: %s", data)
2022-10-15 22:24:03 +00:00
# Try getting the hook
2022-10-15 21:08:13 +00:00
try:
hook = Hook.objects.get(hook=hook_name)
2022-10-15 21:08:13 +00:00
except Hook.DoesNotExist:
return HttpResponseBadRequest("Hook does not exist.")
2022-10-15 22:24:03 +00:00
# Create the callback object
callback = Callback.objects.create(hook=hook, **data)
callback.save()
market.process_callback(callback)
2022-10-15 22:24:03 +00:00
# Bump received count
hook.received = hook.received + 1
2022-10-15 22:10:18 +00:00
hook.save()
return HttpResponse("OK")
def get(self, request, hook_name):
2022-11-03 15:59:27 +00:00
try:
hook = Hook.objects.get(name=hook_name)
except Hook.DoesNotExist:
return HttpResponseBadRequest("Hook does not exist.")
return_data = {"name": hook.name, "hook": hook.hook, "hook_id": hook.id}
return HttpResponse(orjson.dumps(return_data), content_type="application/json")
class HookList(LoginRequiredMixin, ObjectList):
# window_content = "window-content/hooks.html"
list_template = "partials/hook-list.html"
model = Hook
context_object_name = "hooks"
2022-10-29 13:05:01 +00:00
context_object_name_singular = "hook"
title = "Hooks"
title_singular = "Hook"
page_title = "List of active URL endpoints for receiving hooks."
page_subtitle = "Add URLs here to receive Drakdoo callbacks. Make then unique!"
2022-10-12 06:22:22 +00:00
2022-10-29 13:05:01 +00:00
list_url_name = "hooks"
list_url_args = ["type"]
submit_url_name = "hook_create"
2022-10-15 17:45:25 +00:00
class HookCreate(LoginRequiredMixin, ObjectCreate):
model = Hook
form_class = HookForm
context_object_name = "hooks"
2022-10-29 13:05:01 +00:00
context_object_name_singular = "hook"
list_url_name = "hooks"
list_url_args = ["type"]
submit_url_name = "hook_create"
class HookUpdate(LoginRequiredMixin, ObjectUpdate):
model = Hook
form_class = HookForm
context_object_name = "hooks"
2022-10-29 13:05:01 +00:00
context_object_name_singular = "hook"
list_url_name = "hooks"
list_url_args = ["type"]
submit_url_name = "hook_update"
2022-10-15 17:45:25 +00:00
class HookDelete(LoginRequiredMixin, ObjectDelete):
model = Hook
2022-10-29 13:05:01 +00:00
context_object_name = "hooks"
context_object_name_singular = "hook"
list_url_name = "hooks"
list_url_args = ["type"]