43 lines
1.5 KiB
Python
43 lines
1.5 KiB
Python
from django.shortcuts import render
|
|
from rest_framework import status, viewsets
|
|
from rest_framework.response import Response
|
|
from .models import Connection, ConnectionType
|
|
from .serializers import ConnectionSerializer
|
|
from rest_framework.decorators import action
|
|
import importlib
|
|
|
|
# Create your views here.
|
|
|
|
|
|
class ConnectionViewSet(viewsets.ModelViewSet):
|
|
"""API endpoint that allows connections to be seen or created
|
|
"""
|
|
queryset = Connection.objects.all()
|
|
serializer_class = ConnectionSerializer
|
|
# Make connections somewhat immutable from the users perspective
|
|
http_method_names = [
|
|
'get',
|
|
'post',
|
|
'delete',
|
|
'options']
|
|
|
|
@action(detail=False, methods=['post'], url_path='plaid')
|
|
def authenticate(self, request):
|
|
print(request.data)
|
|
print(request.data.keys())
|
|
public_key = request.data.get("public_key")
|
|
name = request.data.get("name", "dummyName")
|
|
if public_key is None:
|
|
return Response(
|
|
status=status.HTTP_400_BAD_REQUEST,
|
|
data="ERROR: missing public_key")
|
|
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)
|
|
plaid_client = plaid.Connection(request.data)
|
|
conn.credentials = plaid_client.credentials
|
|
conn.save()
|
|
return Response(200)
|