diff --git a/connection/connections/plaid_client.py b/connection/connections/plaid_client.py index 98382b8..bb3445c 100755 --- a/connection/connections/plaid_client.py +++ b/connection/connections/plaid_client.py @@ -45,11 +45,12 @@ class Connection(AbstractConnectionClient): environment=self.PLAID_ENV, api_version='2019-05-29') print("Getting Public Key") - public_key = self.credentials.get('public_key') + public_key = self.credentials.get('public_token') print("Retrieving auth token") if not self.credentials.get('auth_token') and public_key: self.credentials['auth_token'] = self.get_auth_token(public_key) print("Plaid Connection successful") + print(self.credentials) def get_auth_token(self, public_token): try: @@ -61,6 +62,18 @@ class Connection(AbstractConnectionClient): return format_error(e) access_token = exchange_response['access_token'] return access_token + + def get_accounts(self, auth_token=None): + if not auth_token: + auth_token = self.credentials.get('auth_token') + if not auth_token: + raise Exception("Missing Auth Token") + try: + accounts = self.client.Accounts.get(auth_token) + except Exception as e: + print(e) + accounts = None + return accounts def get_transactions( self, diff --git a/connection/views.py b/connection/views.py index 0a8f5b0..3be5914 100644 --- a/connection/views.py +++ b/connection/views.py @@ -4,8 +4,11 @@ from rest_framework.response import Response from .models import Connection, ConnectionType from .serializers import ConnectionSerializer from rest_framework.decorators import action +from rest_framework.decorators import permission_classes from qrtr_account.models import Account +from rest_framework.permissions import IsAuthenticated import importlib +import json # Create your views here. @@ -13,6 +16,7 @@ import importlib class ConnectionViewSet(viewsets.ModelViewSet): """API endpoint that allows connections to be seen or created """ + # permission_classes = [IsAuthenticated] queryset = Connection.objects.all() serializer_class = ConnectionSerializer # Make connections somewhat immutable from the users perspective @@ -47,10 +51,17 @@ class ConnectionViewSet(viewsets.ModelViewSet): print(request) plaid = importlib.import_module(f"connection.connections.plaid_client") conn_type = ConnectionType.objects.get(name="Plaid") - conn = Connection.objects.create(name=name, type=conn_type, - credentials=request.data, - account=account) + conn, created = Connection.objects \ + .get_or_create(name=name, type=conn_type, + defaults={ + "credentials": request.data, + "account": account + }) plaid_client = plaid.Connection(request.data) conn.credentials = plaid_client.credentials conn.save() - return Response(200) + return Response(plaid_client.get_accounts()) + + @action(detail=True, methods=['get'], url_path='accounts') + def get_accounts(self): + pass \ No newline at end of file