-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/fylein/fyle-sage-desktop-api
- Loading branch information
Showing
6 changed files
with
160 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from typing import Tuple | ||
|
||
from fyle_rest_auth.models import AuthToken | ||
from fyle_integrations_platform_connector import PlatformConnector | ||
|
||
from apps.workspaces.models import FyleCredential | ||
from apps.fyle.helpers import get_cluster_domain | ||
|
||
|
||
def get_cluster_domain_and_refresh_token(user) -> Tuple[str, str]: | ||
""" | ||
Get cluster domain and refresh token from User | ||
""" | ||
fyle_credentials = FyleCredential.objects.filter(workspace__user=user).first() | ||
|
||
if fyle_credentials: | ||
refresh_token = fyle_credentials.refresh_token | ||
cluster_domain = fyle_credentials.cluster_domain | ||
else: | ||
refresh_token = AuthToken.objects.get(user__user_id=user).refresh_token | ||
cluster_domain = get_cluster_domain(refresh_token) | ||
|
||
return cluster_domain, refresh_token | ||
|
||
|
||
def get_user_profile(request): | ||
""" | ||
Get user profile | ||
""" | ||
refresh_token = AuthToken.objects.get(user__user_id=request.user).refresh_token | ||
cluster_domain, _ = get_cluster_domain_and_refresh_token(request.user) | ||
|
||
fyle_credentials = FyleCredential( | ||
cluster_domain=cluster_domain, | ||
refresh_token=refresh_token | ||
) | ||
|
||
platform = PlatformConnector(fyle_credentials) | ||
employee_profile = platform.connection.v1beta.spender.my_profile.get() | ||
|
||
return employee_profile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from rest_framework import serializers | ||
|
||
from .models import User | ||
|
||
|
||
class UserSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = User | ||
fields = '__all__' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
"""fyle_intacct_api URL Configuration | ||
The `urlpatterns` list routes URLs to views. For more information please see: | ||
https://docs.djangoproject.com/en/3.0/topics/http/urls/ | ||
Examples: | ||
Function views | ||
1. Add an import: from my_app import views | ||
2. Add a URL to urlpatterns: path('', views.home, name='home') | ||
Class-based views | ||
1. Add an import: from other_app.views import Home | ||
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') | ||
Including another URLconf | ||
1. Import the include() function: from django.urls import include, path | ||
2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) | ||
""" | ||
from django.urls import path | ||
|
||
from .views import UserProfileView, FyleOrgsView | ||
|
||
urlpatterns = [ | ||
path('profile/', UserProfileView.as_view()), | ||
path('orgs/', FyleOrgsView.as_view()) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from rest_framework import generics, status | ||
from rest_framework.permissions import IsAuthenticated | ||
from rest_framework.response import Response | ||
|
||
from apps.fyle.helpers import get_fyle_orgs | ||
from apps.users.helpers import get_cluster_domain_and_refresh_token, get_user_profile | ||
|
||
|
||
class UserProfileView(generics.RetrieveAPIView): | ||
|
||
permission_classes = [IsAuthenticated] | ||
|
||
def get(self, request, *args, **kwargs): | ||
""" | ||
Get User Details | ||
""" | ||
employee_profile = get_user_profile(request) | ||
return Response( | ||
data=employee_profile, | ||
status=status.HTTP_200_OK | ||
) | ||
|
||
|
||
class FyleOrgsView(generics.ListCreateAPIView): | ||
""" | ||
FyleOrgs view | ||
""" | ||
|
||
permission_classes = [IsAuthenticated] | ||
|
||
def get(self, request, *args, **kwargs): | ||
""" | ||
Get cluster domain from Fyle | ||
""" | ||
cluster_domain, refresh_token = get_cluster_domain_and_refresh_token(request.user) | ||
fyle_orgs = get_fyle_orgs(refresh_token, cluster_domain) | ||
|
||
return Response( | ||
data=fyle_orgs, | ||
status=status.HTTP_200_OK | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters