Skip to content

Commit

Permalink
user logger to print
Browse files Browse the repository at this point in the history
  • Loading branch information
khavinshankar committed Jul 20, 2023
1 parent e96fc0c commit 09ff7b2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
2 changes: 0 additions & 2 deletions care/abdm/api/viewsets/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@ class OnAddContextsView(GenericAPIView):
authentication_classes = [ABDMAuthentication]

def post(self, request, *args, **kwargs):
data = request.data
print(data)
return Response({}, status=status.HTTP_202_ACCEPTED)


Expand Down
2 changes: 0 additions & 2 deletions care/abdm/api/viewsets/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,4 @@ class SMSOnNotifyView(GenericAPIView):
authentication_classes = [ABDMAuthentication]

def post(self, request, *args, **kwargs):
data = request.data
print(data)
return Response(status=status.HTTP_202_ACCEPTED)
39 changes: 21 additions & 18 deletions care/abdm/utils/api_call.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import logging
import uuid
from base64 import b64encode
from datetime import datetime, timedelta, timezone
Expand All @@ -21,6 +22,8 @@

# TODO: Exception handling for all api calls, need to gracefully handle known exceptions

logger = logging.getLogger(__name__)


def encrypt_with_public_key(a_message):
rsa_public_key = RSA.importKey(
Expand Down Expand Up @@ -60,7 +63,7 @@ def add_user_header(self, headers, user_token):
def add_auth_header(self, headers):
token = cache.get(ABDM_TOKEN_CACHE_KEY)
if not token:
print("No Token in Cache")
logger.info("No Token in Cache")
data = {
"clientId": settings.ABDM_CLIENT_ID,
"clientSecret": settings.ABDM_CLIENT_SECRET,
Expand All @@ -72,29 +75,29 @@ def add_auth_header(self, headers):
resp = requests.post(
ABDM_TOKEN_URL, data=json.dumps(data), headers=auth_headers
)
print("Token Response Status: {}".format(resp.status_code))
logger.info("Token Response Status: {}".format(resp.status_code))
if resp.status_code < 300:
# Checking if Content-Type is application/json
if resp.headers["Content-Type"] != "application/json":
print(
logger.info(
"Unsupported Content-Type: {}".format(
resp.headers["Content-Type"]
)
)
print("Response: {}".format(resp.text))
logger.info("Response: {}".format(resp.text))
return None
else:
data = resp.json()
token = data["accessToken"]
expires_in = data["expiresIn"]
print("New Token: {}".format(token))
print("Expires in: {}".format(expires_in))
logger.info("New Token: {}".format(token))
logger.info("Expires in: {}".format(expires_in))
cache.set(ABDM_TOKEN_CACHE_KEY, token, expires_in)
else:
print("Bad Response: {}".format(resp.text))
logger.info("Bad Response: {}".format(resp.text))
return None
# print("Returning Authorization Header: Bearer {}".format(token))
print("Adding Authorization Header")
# logger.info("Returning Authorization Header: Bearer {}".format(token))
logger.info("Adding Authorization Header")
auth_header = {"Authorization": "Bearer {}".format(token)}
return {**headers, **auth_header}

Expand All @@ -107,9 +110,9 @@ def get(self, path, params=None, auth=None):
headers = self.add_auth_header(headers)
if auth:
headers = self.add_user_header(headers, auth)
print("Making GET Request to: {}".format(url))
logger.info("Making GET Request to: {}".format(url))
response = requests.get(url, headers=headers, params=params)
print("{} Response: {}".format(response.status_code, response.text))
logger.info("{} Response: {}".format(response.status_code, response.text))
return response

def post(self, path, data=None, auth=None, additional_headers=None):
Expand All @@ -128,10 +131,10 @@ def post(self, path, data=None, auth=None, additional_headers=None):
# ['-H "{}: {}"'.format(k, v) for k, v in headers.items()]
# )
data_json = json.dumps(data)
# print("curl -X POST {} {} -d {}".format(url, headers_string, data_json))
print("Posting Request to: {}".format(url))
# logger.info("curl -X POST {} {} -d {}".format(url, headers_string, data_json))
logger.info("Posting Request to: {}".format(url))
response = requests.post(url, headers=headers, data=data_json)
print("{} Response: {}".format(response.status_code, response.text))
logger.info("{} Response: {}".format(response.status_code, response.text))
return response


Expand All @@ -142,7 +145,7 @@ def __init__(self):
def generate_aadhaar_otp(self, data):
path = "/v1/registration/aadhaar/generateOtp"
response = self.api.post(path, data)
print("{} Response: {}".format(response.status_code, response.text))
logger.info("{} Response: {}".format(response.status_code, response.text))
return response.json()

def resend_aadhaar_otp(self, data):
Expand Down Expand Up @@ -174,7 +177,7 @@ def verify_mobile_otp(self, data):
# /v1/registration/aadhaar/createHealthIdWithPreVerified
def create_health_id(self, data):
path = "/v1/registration/aadhaar/createHealthIdWithPreVerified"
print("Creating Health ID with data: {}".format(data))
logger.info("Creating Health ID with data: {}".format(data))
# data.pop("healthId", None)
response = self.api.post(path, data)
return response.json()
Expand Down Expand Up @@ -299,9 +302,9 @@ def get_abha_card_pdf(self, data):
def get_qr_code(self, data, auth):
path = "/v1/account/qrCode"
access_token = self.generate_access_token(data)
print("Getting QR Code for: {}".format(data))
logger.info("Getting QR Code for: {}".format(data))
response = self.api.get(path, {}, access_token)
print("QR Code Response: {}".format(response.text))
logger.info("QR Code Response: {}".format(response.text))
return response.json()


Expand Down

0 comments on commit 09ff7b2

Please sign in to comment.