39 lines
1.2 KiB
Python
39 lines
1.2 KiB
Python
# thirdparty/trading212/views.py
|
|
import os
|
|
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 drf_spectacular.utils import extend_schema
|
|
|
|
class Trading212AccountCashView(APIView):
|
|
permission_classes = [IsAuthenticated]
|
|
|
|
@extend_schema(
|
|
tags=["trading212"],
|
|
summary="Get Trading212 account cash",
|
|
responses=Trading212AccountCashSerializer
|
|
)
|
|
def get(self, request):
|
|
api_key = os.getenv("API_KEY_TRADING212")
|
|
|
|
headers = {
|
|
"Authorization": f"Bearer {api_key}",
|
|
"Accept": "application/json",
|
|
}
|
|
|
|
url = "https://api.trading212.com/api/v0/equity/account/cash"
|
|
|
|
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)
|
|
|
|
data = resp.json()
|
|
serializer = Trading212AccountCashSerializer(data=data)
|
|
serializer.is_valid(raise_exception=True)
|
|
return Response(serializer.data)
|