Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dubeyji10/c518db23e6271c27e2eb43c83d4b853f to your computer and use it in GitHub Desktop.
Save dubeyji10/c518db23e6271c27e2eb43c83d4b853f to your computer and use it in GitHub Desktop.
conversation push with a refresh token function - since conversation payload count is very high - refreshing of access tokens is needed
'''
running a refresh thread in background
'''
import threading
import logging
import os
import logging
import time
from datetime import datetime , timedelta
import json
import requests
now = datetime.now()
fileName = now.strftime('%Y_%m_%d_%H_%M_%S')
logging.basicConfig(filename="logs\\conversation_push_Production_withoutThread_"+fileName+"_LOGS.log", level=logging.INFO, datefmt='%Y-%m-%d %H:%M:%S')
startTime = now
print('script started at : {}'.format(startTime.strftime('%Y-%m-%d %H:%M:%S')))
logging.info('script started at : {}\n'.format(startTime.strftime('%Y-%m-%d %H:%M:%S')))
''' refresh token function to refresh access token every 30 minutes
refreshed at 18:46 with 1000.17fecd0a30a4eb79151abfb75b816de5.4fc94f964c7150c9d2bef813fda54bd3
{
}
'''
accessTokenFile = 'accessToken.json'
def refreshToken():
url = "https://accounts.zoho.in/oauth/v2/token?"
print("->refreshing the access tokens")
logging.info(" -> refreshing tokens at {} ".format(time.ctime()))
print("1. without thread ")
# at every 25 seconds thread runs
logging.info(" writing refresh token request response at {} ".format(time.ctime()))
# timer = threading.Timer(25, refreshToken) # # Call `refreshTokens` in 25 seconds.
# timer.start()
print("3. make api calls \n make changes in access_token.json")
files=[]
headers = {}
print("1. creating payload for refresh request")
payloadRefresh={
'client_id': "",
'client_secret': "",
'refresh_token':"",
'grant_type': 'refresh_token'
}
print(payloadRefresh,'\n...\n...\n')
refreshResponse = requests.request("POST", url, headers=headers, data=payloadRefresh, files=files)
print("\t 3.1 response to refresh token request : \n")
print("response : ---- \n\n",refreshResponse,'\n\n')
print("4. writing response to accessToken.json ")
logging.info("\n --------------------------------------------------- \n")
logging.info("\n --------- response to refresh request ---------------- \n{}".format(refreshResponse.text))
logging.info("\n --------------------------------------------------- \n")
with open(accessTokenFile,'w') as refresh:
refresh.write(refreshResponse.text)
print("-done-")
print('\\/'*25)
''' function to push data '''
# now = datetime.now()
# fileName = now.strftime('%Y_%m_%d_%H_%M_%S')
# logging.basicConfig(filename="outputs2/requests_"+fileName+"_testing_3"+".log", level=logging.INFO)
# logging.info("-- operations started at {} (test time = 2015 Jan 6 10:00) --".format(time.ctime()))
'''
sending json.dumps({"data":list_of_records})
'''
def pushInto(moduleName , payload):
url = "https://www.zohoapis.in/crm/v2/"
# will return responseGenerated
# logging.info("|| calling api to insert {} at {} ||".format(moduleName,time.ctime()))
'''
add try except here later
'''
print(">make request")
print(">write request response to log")
print(">reading accessToken.json")
accessTokenFile = "accessToken.json"
with open(accessTokenFile,'r') as client:
tokens = json.load(client)
accessToken = tokens['access_token']
url_module = url+moduleName
print("url used to make api calls ->",url_module)
headers = {
'Authorization': 'Bearer '+accessToken,
'Content-Type': 'application/json',
}
response = requests.request("POST", url_module, headers=headers, data=payload)
# print(response.text)
# responseText = ''
# responseText = line+"\n -- reponse recieved at {} -- \n {} ".format(time.ctime(),response.text)+line
# logging.info(responseText)
return response
''' read files in conversation directory '''
path = 'conversations'
print('refresh token function runs in background as a thread ')
logging.info('\n\nrefresh token function runs in background as a thread\n\n')
files = [f for f in os.listdir(path)]
#
for index,i in enumerate(files):
''' using index +1 to skip 0indexed record refreshing '''
if (index+1)%161==0:
print('refreshing tokens')
refreshToken()
logging.info(' _____________- refreshed at {}- ______________'.format(time.ctime()))
payloadPath = ''
responsePath = ''
currPath = ''
print(' ----------- index : {} --------------'.format(index))
payloadPath = i
print('now payloadPath : {}'.format(i))
responsePath = 'responses\\conversations\\'+i[:-5] + "_response.json"
logging.info(f'--> payload #{index} : {i}')
print('making api request')
logging.info('making api request')
# data = None
currPath = os.path.join(path,i)
print('payload path : ',currPath)
with open(currPath,'r') as clientPayload:
data = clientPayload.read()
print(len(data))
res = pushInto('conversation',data)
with open(responsePath,'w') as responseClient:
responseClient.write(res.text)
print('\t -> response in : {}'.format(responsePath))
logging.info('\t -> response in : {}\n'.format(responsePath))
print('-'*50)
print('3 second wait')
time.sleep(3)
logging.info(' {0} {0} '.format(' - - - - - - - - - - - - - - - - - - - - - - - - - - - '))
now = datetime.now()
endTime = now
print('script ended at : {}'.format(endTime.strftime('%Y-%m-%d %H:%M:%S')))
logging.info('script ended at : {}'.format(endTime.strftime('%Y-%m-%d %H:%M:%S')))
print('total files read : ',len(files))
print('last file in directory : ',files[-1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment