diff --git a/backend/api/urls.py b/backend/api/urls.py index 7d9a366..a9f0dce 100644 --- a/backend/api/urls.py +++ b/backend/api/urls.py @@ -17,4 +17,5 @@ urlpatterns = [ path('profile/', views.UserProfileDetail.as_view()), path('token/', TokenObtainPairView.as_view(), name='token_obtain_pair'), path('token/refresh/', TokenRefreshView.as_view(), name='token_refresh'), + path('robinhood/fetch/', views.FetchRobinhoodStocks.as_view()) ] diff --git a/backend/api/views.py b/backend/api/views.py index 35896bc..7b5b2aa 100644 --- a/backend/api/views.py +++ b/backend/api/views.py @@ -1,6 +1,12 @@ from django.shortcuts import render, get_object_or_404 from django.http import QueryDict +from math import ceil + +import robin_stocks as r + +import os + from rest_framework import status, permissions from rest_framework.views import APIView from rest_framework.viewsets import ModelViewSet @@ -48,4 +54,26 @@ class UserProfileDetail(APIView): class UserProfileCreate(CreateAPIView): model = User permission_classes = [permissions.AllowAny] - serializer_class = UserCreateSerializer \ No newline at end of file + serializer_class = UserCreateSerializer + +class FetchRobinhoodStocks(APIView): + permission_classes = [permissions.AllowAny] + def get(self, request, format=None): + login = r.login(os.getenv("ROBINHOOD_USER"), os.getenv("ROBINHOOD_PASS")) + stonks = r.build_holdings() + for key, value in stonks.items(): + if Stock.objects.filter(uuid=value['id']).count() != 1: + stock = Stock.objects.create(user=request.user.profile, quantity=ceil(float(value['quantity'])), ticker=key, buy_price=value['price'], uuid=value['id']) + queryset = request.user.profile.stocks.all() + serializer = StockSerializer(queryset, many=True) + return Response(serializer.data, status=status.HTTP_200_OK) + +class BuyRobinhoodStock(APIView): + permission_classes = [permissions.AllowAny] + def get(self, request, format=None): + login = r.login(os.getenv("ROBINHOOD_USER"), os.getenv("ROBINHOOD_PASS")) + stonks = r.build_holdings() + for key, value in stonks.items(): + print(key) + print(value) + return Response(stonks) \ No newline at end of file