44
55import json
66import requests
7+ from rest_framework import status
8+ from rest_framework .response import Response
79from core .serializers import UserSerializer
810from core .models import ApiInfo
911
@@ -13,11 +15,24 @@ def update_api_info(api):
1315 url = api ['public_hostname' ]+ '/api/accounts/describe/'
1416 token = api ['token' ]
1517 header = {'Authorization' : f'Token { token } ' }
16- try :
18+
19+ try :
1720 response = requests .post (url , headers = header )
1821 except requests .exceptions .ConnectionError :
19- return 'ConncetionError'
20- return json .loads (response .text )
22+ return Response (status = status .HTTP_503_SERVICE_UNAVAILABLE ,
23+ data = 'ConnectionError'
24+ )
25+
26+ if response .status_code == 401 :
27+ return Response (status = status .HTTP_401_UNAUTHORIZED ,
28+ data = json .loads (response .text )
29+ )
30+ if response .status_code is 400 :
31+ return Response (status = status .HTTP_400_BAD_REQUEST ,
32+ data = json .loads (response .text )
33+ )
34+
35+ return Response (status = status .HTTP_200_OK , data = json .loads (response .text ))
2136
2237def my_jwt_response_handler (token , user = None , request = None ):
2338 """JWT
@@ -29,13 +44,16 @@ def my_jwt_response_handler(token, user=None, request=None):
2944
3045 user_info = UserSerializer (user , context = {'request' : request }).data
3146 for api in user_info ['apiinfo' ]:
32- updated_api = update_api_info (api )
33- if updated_api != 'ConncetionError' :
34- new = ApiInfo .objects .get (token = updated_api ['token' ])
35- new .other_info = updated_api ['other_info' ]
36- new .save ()
37- # print(new.other_info)
47+ api_object = ApiInfo .objects .get (token = api ['token' ])
48+ api_update = update_api_info (api )
49+ if api_update .status_code == 200 :
50+ # import pdb; pdb.set_trace()
51+ api_object .other_info = api_update .data ['other_info' ]
52+
53+ api_object .other_info ['status' ] = api_update .status_code
54+ api_object .save ()
3855
56+ # print(api_object.other_info)
3957 user_info ['groups' ] = [list (i .items ())[0 ][1 ] for i in user_info ['groups' ]]
4058
4159 return {'token' : token , 'user' : user_info }
0 commit comments