api done
This commit is contained in:
62
backend/thirdparty/trading212/views.py
vendored
62
backend/thirdparty/trading212/views.py
vendored
@@ -1,38 +1,58 @@
|
||||
# thirdparty/trading212/views.py
|
||||
import os
|
||||
import base64
|
||||
import logging
|
||||
import requests
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from .serializers import Trading212AccountCashSerializer
|
||||
from rest_framework.permissions import IsAuthenticated, AllowAny
|
||||
from django.conf import settings
|
||||
from django.core.cache import cache
|
||||
|
||||
from drf_spectacular.utils import extend_schema
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
api_key = os.getenv("ID_API_KEY_TRADING212")
|
||||
api_secret = os.getenv("API_KEY_TRADING212")
|
||||
|
||||
base_url = "https://live.trading212.com"
|
||||
|
||||
class Trading212AccountCashView(APIView):
|
||||
permission_classes = [IsAuthenticated]
|
||||
permission_classes = [AllowAny]
|
||||
authentication_classes = []
|
||||
|
||||
@extend_schema(
|
||||
tags=["trading212"],
|
||||
summary="Get Trading212 account cash",
|
||||
responses=Trading212AccountCashSerializer
|
||||
summary="Get Trading212 account cash"
|
||||
)
|
||||
def get(self, request):
|
||||
api_key = os.getenv("API_KEY_TRADING212")
|
||||
|
||||
headers = {
|
||||
"Authorization": f"Bearer {api_key}",
|
||||
"Accept": "application/json",
|
||||
}
|
||||
url = f"{base_url}/api/v0/equity/account/cash"
|
||||
|
||||
url = "https://api.trading212.com/api/v0/equity/account/cash"
|
||||
response = requests.get(url, auth=(api_key, api_secret))
|
||||
|
||||
try:
|
||||
resp = requests.get(url, headers=headers, timeout=10)
|
||||
resp.raise_for_status()
|
||||
except requests.RequestException as exc:
|
||||
return Response({"error": str(exc)}, status=400)
|
||||
logger.info("Trading212 Account Cash Response:")
|
||||
logger.info(response)
|
||||
|
||||
data = resp.json()
|
||||
serializer = Trading212AccountCashSerializer(data=data)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
return Response(serializer.data)
|
||||
data = response.json()
|
||||
|
||||
return Response(data)
|
||||
|
||||
class Trading212SummaryView(APIView):
|
||||
permission_classes = [AllowAny]
|
||||
authentication_classes = []
|
||||
|
||||
@extend_schema(
|
||||
tags=["trading212"],
|
||||
summary="Get Trading212 account summary"
|
||||
)
|
||||
def get(self, request):
|
||||
url = f"{base_url}/api/v0/equity/portfolio"
|
||||
response = requests.get(url, auth=(api_key, api_secret))
|
||||
|
||||
logger.info("Trading212 Account Summary Response:")
|
||||
logger.info(response)
|
||||
|
||||
data = response.json()
|
||||
|
||||
return Response(data)
|
||||
Reference in New Issue
Block a user