This is a pretty big undertaking for me with my basic skills, and I was kind of flying blind. It's basic in its intent: get a bunch of case info from a helpdesk app and put it into a database. There are >10k records, so I had to overcome some pagination challenges. It works, and I would love your opinions regarding best practices or making it more pythonic.
import time
import collections
from desk_auth import BASE_URL, service, session
import MySQLdb
import mysql.connector
from mysql.connector import errorcode
from db_info import uname,pw,host,db
import dateutil.parser
PAGER = 100
def get_all_cases():
"""
pages through items,
returns a dict of dicts of case, cust, date
#cases[case] = {cust:date}
"""
i = 1
all_cases = []
is_next = ""
while is_next is not None:
endpoint_url = '/api/v2/cases/search'
endpoint = ''.join([BASE_URL, endpoint_url])
query_params = {
'page':i,
'per_page': PAGER,
'status': 'new,open,pending', #,resolved,closed',
'fields': 'created_at,status,labels,subject,updated_at'
}
response = session.get(endpoint, params=query_params)
items = response.json()
data = items['_embedded']['entries']
is_next = items['_links']['next']
for item in data:
#case id, customer id
case = get_clean_id(item['_links']['self']['href'])
cust = get_clean_id(item['_links']['customer']['href'])
#create date
raw_date = item['created_at']
p = dateutil.parser.parse(raw_date)
date = '%s-%s-%s %s:%s:%s' % (p.year, p.month, p.day, p.hour, p.minute, '00')
#last update
raw_update = item['updated_at']
l = dateutil.parser.parse(raw_update)
last_update = '%s-%s-%s %s:%s:%s' % (l.year, l.month, l.day, l.hour, l.minute, '00')
#subject, status
subj = item['subject']
status = item['status']
#get plan from labels
plan = get_plan(item['labels'])
#get group id or set to 0
if item['_links']['assigned_group'] is not None:
group = get_clean_id(item['_links']['assigned_group']['href'])
else:
group = '0'
#get user id or set to 0
if item['_links']['assigned_user'] is not None:
assigned_to = get_clean_id(item['_links']['assigned_user']['href'])
else:
assigned_to = 0
cases = (case, cust, date, last_update, subj, status, plan, group, assigned_to)
all_cases.append(cases)
i += 1
#prevents rate limiting over 60 calls/minute
time.sleep(1)
return all_cases
This inserts into the database:
def add_cases(cases_dict):
"""
Takes the list of known kanji and inserts into database.
"""
cnx = mysql.connector.connect(user=uname, password=pw, host=host, database=db, charset='utf8')
cursor = cnx.cursor()
try:
for data in cases_dict:
print data
sql = """INSERT INTO cases(id, customer_id, date, last_update, subject, status, plan, group_id, assigned_id)
VALUES (%s, %s, %s , %s, %s, %s, %s, %s, %s);"""
cursor.execute(sql, data)
cnx.commit()
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
cnx.rollback()
cursor.close()
cnx.close()