Skip to content

Commit

Permalink
Support for cluster domain GET by code (#36)
Browse files Browse the repository at this point in the history
* Support for cluster domain GET by code

* Handle local dev as well

* fix lint

* replace hard coded url
  • Loading branch information
ashwin1111 authored Jan 9, 2024
1 parent 63ac083 commit 6267582
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
25 changes: 25 additions & 0 deletions fyle_rest_auth/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,31 @@
logger.level = logging.INFO


def get_cluster_domain_by_code(request):
authorization_code = request.data.get('code')
try:
if not authorization_code:
raise ValidationError('authorization code not found')

tokens = auth.generate_fyle_refresh_token(authorization_code=authorization_code)

cluster_domain = settings.API_URL.split('/api')[0] if settings.DEBUG \
else get_cluster_domain(tokens['access_token'], auth.get_origin_address(request))

return {
'cluster_domain': cluster_domain,
'tokens': tokens
}

except ValidationError as error:
logger.info(error)
raise

except Exception as error:
logger.error(traceback.format_exc())
raise ValidationError(error)


def validate_code_and_login(request):
authorization_code = request.data.get('code')
try:
Expand Down
3 changes: 2 additions & 1 deletion fyle_rest_auth/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
"""
from django.urls import path

from .views import LoginView, RefreshView, LoginWithRefreshTokenView
from .views import LoginView, RefreshView, LoginWithRefreshTokenView, ClusterDomainView

urlpatterns = [
path('login/', LoginView.as_view()),
path('refresh/', RefreshView.as_view()),
path('login_with_refresh_token/', LoginWithRefreshTokenView.as_view()),
path('cluster_domain/', ClusterDomainView.as_view()),
]
19 changes: 18 additions & 1 deletion fyle_rest_auth/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from .helpers import (
validate_code_and_login,
validate_and_refresh_token,
validate_refresh_token_and_login
validate_refresh_token_and_login,
get_cluster_domain_by_code
)


Expand Down Expand Up @@ -63,3 +64,19 @@ def post(self, request):
data=tokens,
status=status.HTTP_200_OK
)


class ClusterDomainView(APIView):
"""
Get Cluster Domain
"""
authentication_classes = []
permission_classes = []

def post(self, request):
response = get_cluster_domain_by_code(request)

return Response(
data=response,
status=status.HTTP_200_OK
)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setuptools.setup(
name='fyle-rest-auth',
version='1.5.0',
version='1.6.0',
author='Shwetabh Kumar',
author_email='[email protected]',
description='Django application to implement OAuth 2.0 using Fyle in Django rest framework',
Expand Down

0 comments on commit 6267582

Please sign in to comment.