def main(argv): parser = argparse.ArgumentParser() parser.add_argument("apikey", type=str, help="Mashery V2 API Key") parser.add_argument("secret", type=str, help="Mashery V2 API Secret") parser.add_argument("siteId", type=str, help="Mashery Area/Site ID") parser.add_argument("startDate", type=str, help="Start Date of reporting data") parser.add_argument("endDate", type=str, help="End Date of reporting data") parser.add_argument( "keyCreateDate", type=str, help= "Key Create Date, i.e. keys created before this date will be considered for disablement/deletion" ) parser.add_argument("outputFile", type=str, help="Output Filename") args = parser.parse_args() apikey = args.apikey secret = args.secret siteId = args.siteId startDate = args.startDate endDate = args.endDate keyCreateDate = args.keyCreateDate outputFile = args.outputFile if masheryDate.dayGap(startDate, endDate) < 1: print 'ERROR: endDate must be at least 1 day past startDate' return all_keys = fetchAllKeys(siteId, apikey, secret) all_applications = transform_keys_map_to_applications_map(all_keys) all_active_keys = fetchAllActiveKeys(siteId, apikey, secret, startDate, endDate) inactive_applications = [] for application in all_applications: active = False application = all_applications[application] if (application['is_packaged'] == True): continue for key in application['keys']: if (masheryDate.dayGap(key['created'], keyCreateDate) > 0): if hasActivity(key['apikey'], all_active_keys ) == True and key['apikey'] != 'noapikey': active = True if active == False: inactive_applications.append(application) writeToFile(outputFile, inactive_applications)
def main(argv): parser = argparse.ArgumentParser() parser.add_argument("apikey", type=str, help="Mashery V2 API Key") parser.add_argument("secret", type=str, help="Mashery V2 API Secret") parser.add_argument("siteId", type=str, help="Mashery Area/Site ID") parser.add_argument("startDate", type=str, help="Start Date of reporting data") parser.add_argument("endDate", type=str, help="End Date of reporting data") parser.add_argument("keyCreateDate", type=str, help="Key Create Date, i.e. keys created before this date will be considered for disablement/deletion") parser.add_argument('--delete', action='store_true', default=False, help='specify to delete, leave off command to disable') parser.add_argument('--nodryrun', action='store_true', default=False, help='specify to perform work, leave off command for dry run') parser.add_argument('--archive', type=str, help="Name of file to use for archiving deleted fileds") args = parser.parse_args() apikey = args.apikey secret = args.secret siteId = args.siteId startDate = args.startDate endDate = args.endDate keyCreateDate = args.keyCreateDate delete = args.delete dryrun = args.nodryrun archive = args.archive if masheryDate.dayGap(startDate, endDate) < 1: print 'ERROR: endDate must be at least 1 day past startDate' return masheryV2 = Base('https', 'api.mashery.com', siteId, apikey, secret) allKeys = fetchAllKeys(siteId, apikey, secret) writeToFile('keys.csv', allKeys) allActiveKeys = fetchAllActiveKeys(siteId, apikey, secret, startDate, endDate) writeToFile('active_keys.csv', allActiveKeys) for key in allKeys: if (masheryDate.dayGap(key['created'], keyCreateDate) > 0): if hasActivity(key['apikey'], allActiveKeys) == False and key['apikey'] != 'noapikey': actionType = 'disable' if delete == True: actionType = 'delete' if dryrun == False: print 'Need to {actionType} {apikey}, created on {created} for user: {username} ({email})'.format(actionType= actionType, apikey= key['apikey'], created= key['created'], username= key['username'], email= key['member']['email'] if key['member'] != None else '' ) else: if key['application'] == None or key['application']['is_packaged'] == False: print '{actionType}: {apikey}, created on {created} for user: {username} ({email})'.format(actionType= actionType, apikey= key['apikey'], created= key['created'], username= key['username'], email= key['member']['email'] if key['member'] != None else '' ) print processKey(siteId, apikey, secret, key, delete, archive)
def main(argv): parser = argparse.ArgumentParser() parser.add_argument("apikey", type=str, help="Mashery V2 API Key") parser.add_argument("secret", type=str, help="Mashery V2 API Secret") parser.add_argument("siteId", type=str, help="Mashery Area/Site ID") parser.add_argument("startDate", type=str, help="Start Date of reporting data") parser.add_argument("endDate", type=str, help="End Date of reporting data") parser.add_argument("keyCreateDate", type=str, help="Key Create Date, i.e. keys created before this date will be considered for disablement/deletion") parser.add_argument("outputFile", type=str, help="Output Filename") args = parser.parse_args() apikey = args.apikey secret = args.secret siteId = args.siteId startDate = args.startDate endDate = args.endDate keyCreateDate = args.keyCreateDate outputFile = args.outputFile if masheryDate.dayGap(startDate, endDate) < 1: print 'ERROR: endDate must be at least 1 day past startDate' return all_keys = fetchAllKeys(siteId, apikey, secret) all_applications = transform_keys_map_to_applications_map(all_keys) all_active_keys = fetchAllActiveKeys(siteId, apikey, secret, startDate, endDate) inactive_applications = [] for application in all_applications: active = False application = all_applications[application] if (application['is_packaged'] == True): continue for key in application['keys']: if (masheryDate.dayGap(key['created'], keyCreateDate) > 0): if hasActivity(key['apikey'], all_active_keys) == True and key['apikey'] != 'noapikey': active = True if active == False: inactive_applications.append(application) writeToFile(outputFile, inactive_applications)
def main(argv): parser = argparse.ArgumentParser() parser.add_argument("apikey", type=str, help="Mashery V2 API Key") parser.add_argument("secret", type=str, help="Mashery V2 API Secret") parser.add_argument("siteId", type=str, help="Mashery Area/Site ID") parser.add_argument("startDate", type=str, help="Start Date") parser.add_argument("endDate", type=str, help="End Date") parser.add_argument("outputFile", type=str, help="Output Filename") parser.add_argument('--apis', nargs='+', help='List of APIs by name, space separated') parser.add_argument('--keys', nargs='+', help='List of keys to include in results, space separated') parser.add_argument('--additionalfields', nargs='+', help='List of key/app fields to retrieve, space separated') args = parser.parse_args() apikey = args.apikey secret = args.secret siteId = args.siteId startDate = args.startDate endDate = args.endDate outputFile = args.outputFile if masheryDate.dayGap(startDate, endDate) < 1: print 'ERROR: endDate must be at least 1 day past startDate' return masheryV2 = MasheryV2('https', 'api.mashery.com') apis = args.apis if args.apis == None: apis = [] # get list of APIs all_apis = masheryV2.post(siteId, apikey, secret, '{"method":"object.query","id":1,"params":["select * from services ITEMS 1000"]}') keys = args.keys if args.keys == None: keys = [] results = [] dates = masheryDate.daysToReportOn(startDate, endDate) for api in all_apis['result']['items']: try: if api['name'] in apis or len(apis) == 0: print 'Processing...' + api['name'] for date in dates: print 'Processing...' + date[0] urlParams = '&start_date=' + urllib.quote_plus(date[0]) + '&end_date=' + urllib.quote_plus(date[1]) + '&format=json&limit=1000' results.extend(masheryV2.get(siteId, apikey, secret, '/reports/calls/developer_activity/service/' + api['service_key'], urlParams)) except ValueError, e: print e
def main(argv): parser = argparse.ArgumentParser() parser.add_argument("apikey", type=str, help="Mashery V2 API Key") parser.add_argument("secret", type=str, help="Mashery V2 API Secret") parser.add_argument("siteId", type=str, help="Mashery Area/Site ID") parser.add_argument("startDate", type=str, help="Start Date") parser.add_argument("endDate", type=str, help="End Date") parser.add_argument("outputFile", type=str, help="Output Filename") parser.add_argument('--apis', nargs='+', help='List of APIs by name, space separated') parser.add_argument( '--keys', nargs='+', help='List of keys to include in results, space separated') parser.add_argument( '--additionalfields', nargs='+', help='List of key/app fields to retrieve, space separated') args = parser.parse_args() apikey = args.apikey secret = args.secret siteId = args.siteId startDate = args.startDate endDate = args.endDate outputFile = args.outputFile if masheryDate.dayGap(startDate, endDate) < 1: print 'ERROR: endDate must be at least 1 day past startDate' return masheryV2 = MasheryV2('https', 'api.mashery.com') apis = args.apis if args.apis == None: apis = [] # get list of APIs all_apis = masheryV2.post( siteId, apikey, secret, '{"method":"object.query","id":1,"params":["select * from services ITEMS 1000"]}' ) keys = args.keys if args.keys == None: keys = [] results = [] dates = masheryDate.daysToReportOn(startDate, endDate) for api in all_apis['result']['items']: try: if api['name'] in apis or len(apis) == 0: print 'Processing...' + api['name'] for date in dates: print 'Processing...' + date[0] urlParams = '&start_date=' + urllib.quote_plus( date[0]) + '&end_date=' + urllib.quote_plus( date[1]) + '&format=json&limit=1000' results.extend( masheryV2.get( siteId, apikey, secret, '/reports/calls/developer_activity/service/' + api['service_key'], urlParams)) except ValueError, e: print e
def main(argv): parser = argparse.ArgumentParser() parser.add_argument("apikey", type=str, help="Mashery V2 API Key") parser.add_argument("secret", type=str, help="Mashery V2 API Secret") parser.add_argument("siteId", type=str, help="Mashery Area/Site ID") parser.add_argument("startDate", type=str, help="Start Date") parser.add_argument("endDate", type=str, help="End Date") parser.add_argument("outputFile", type=str, help="Output Filename") parser.add_argument('--apis', nargs='+', help='List of APIs by name, space separated') args = parser.parse_args() apikey = args.apikey secret = args.secret siteId = args.siteId startDate = args.startDate endDate = args.endDate outputFile = args.outputFile if masheryDate.dayGap(startDate, endDate) < 1: print 'ERROR: endDate must be at least 1 day past startDate' return apis = args.apis if args.apis == None: apis = [] all_apis = masheryV2.post( siteId, apikey, secret, '{"method":"object.query","id":1,"params":["select * from services ITEMS 1000"]}' ) results = [] dates = masheryDate.daysToReportOn(startDate, endDate) for api in all_apis['result']['items']: try: if api['name'] in apis or len(apis) == 0: print 'Processing...' + api['name'] for date in dates: urlParams = '&start_date=' + urllib.quote_plus( date[0]) + '&end_date=' + urllib.quote_plus( date[1]) + '&format=json&limit=1000' apiReportingResults = masheryV2.get( siteId, apikey, secret, '/reports/calls/developer_activity/service/' + api['service_key'], urlParams) for apiReportingResult in apiReportingResults: if (apiReportingResult['serviceDevKey'] != 'unknown'): developerReportingResult = masheryV2.get( siteId, apikey, secret, '/reports/calls/methods/service/' + api['service_key'] + '/developer/' + apiReportingResult['serviceDevKey'], urlParams) if (developerReportingResult != None): results.extend(developerReportingResult) except ValueError: pass f = open(outputFile, 'w') f.write( 'Application,API ID,API Name,API Method,Key,Start Date,End Date,Calls\n' ) for result in results: try: key = masheryV2.post( siteId, apikey, secret, '{"method":"object.query","id":1,"params":["select *, application from keys where apikey = \'' + result['serviceDevKey'] + '\'"]}') application_name = '<UNKNOWN>' if (key['result']['total_items'] > 0): application_name = key['result']['items'][0]['application'][ 'name'] f.write('"' + application_name + '",' + result['serviceKey'] + ',"' + apiName(all_apis, result['serviceKey']) + '","' + result['apiMethod'] + '",' + result['serviceDevKey'] + ',' + result['startDate'] + ',' + result['endDate'] + ',' + str(result['methodCount']) + '\n') except TypeError: pass f.close()
def main(argv): parser = argparse.ArgumentParser() parser.add_argument("apikey", type=str, help="Mashery V2 API Key") parser.add_argument("secret", type=str, help="Mashery V2 API Secret") parser.add_argument("siteId", type=str, help="Mashery Area/Site ID") parser.add_argument("startDate", type=str, help="Start Date") parser.add_argument("endDate", type=str, help="End Date") parser.add_argument("outputFile", type=str, help="Output Filename") parser.add_argument('--apis', nargs='+', help='List of APIs by name, space separated') args = parser.parse_args() apikey = args.apikey secret = args.secret siteId = args.siteId startDate = args.startDate endDate = args.endDate outputFile = args.outputFile if masheryDate.dayGap(startDate, endDate) < 1: print 'ERROR: endDate must be at least 1 day past startDate' return apis = args.apis if args.apis == None: apis = [] all_apis = masheryV2.post(siteId, apikey, secret, '{"method":"object.query","id":1,"params":["select * from services ITEMS 1000"]}') results = [] dates = masheryDate.daysToReportOn(startDate, endDate) for api in all_apis['result']['items']: try: if api['name'] in apis or len(apis) == 0: print 'Processing...' + api['name'] for date in dates: urlParams = '&start_date=' + urllib.quote_plus(date[0]) + '&end_date=' + urllib.quote_plus(date[1]) + '&format=json&limit=1000' apiReportingResults = masheryV2.get(siteId, apikey, secret, '/reports/calls/developer_activity/service/' + api['service_key'], urlParams) for apiReportingResult in apiReportingResults: if (apiReportingResult['serviceDevKey'] != 'unknown'): developerReportingResult = masheryV2.get(siteId, apikey, secret, '/reports/calls/methods/service/' + api['service_key'] + '/developer/' + apiReportingResult['serviceDevKey'], urlParams) if (developerReportingResult != None): results.extend(developerReportingResult) except ValueError: pass f = open(outputFile,'w') f.write('Application,API ID,API Name,API Method,Key,Start Date,End Date,Calls\n') for result in results: try: key = masheryV2.post(siteId, apikey, secret, '{"method":"object.query","id":1,"params":["select *, application from keys where apikey = \'' + result['serviceDevKey'] + '\'"]}') application_name = '<UNKNOWN>' if (key['result']['total_items'] > 0) : application_name = key['result']['items'][0]['application']['name'] f.write('"' + application_name + '",' + result['serviceKey'] + ',"' + apiName(all_apis, result['serviceKey']) + '","'+ result['apiMethod']+ '",'+ result['serviceDevKey'] + ',' + result['startDate'] + ',' + result['endDate'] + ','+ str(result['methodCount']) + '\n') except TypeError: pass f.close()
def main(argv): parser = argparse.ArgumentParser() parser.add_argument("apikey", type=str, help="Mashery V2 API Key") parser.add_argument("secret", type=str, help="Mashery V2 API Secret") parser.add_argument("siteId", type=str, help="Mashery Area/Site ID") parser.add_argument("startDate", type=str, help="Start Date") parser.add_argument("endDate", type=str, help="End Date") parser.add_argument("outputFile", type=str, help="Output Filename") parser.add_argument('--apis', nargs='+', help='List of APIs by name, space separated') parser.add_argument('--keys', nargs='+', help='List of keys to include in results, space separated') parser.add_argument('--additionalfields', nargs='+', help='List of key/app fields to retrieve, space separated') args = parser.parse_args() apikey = args.apikey secret = args.secret siteId = args.siteId startDate = args.startDate endDate = args.endDate outputFile = args.outputFile if masheryDate.dayGap(startDate, endDate) < 1: print 'ERROR: endDate must be at least 1 day past startDate' return apis = args.apis if args.apis == None: apis = [] # get list of APIs all_apis = masheryV2.post(siteId, apikey, secret, '{"method":"object.query","id":1,"params":["select * from services ITEMS 1000"]}') keys = args.keys if args.keys == None: keys = [] results = [] dates = masheryDate.daysToReportOn(startDate, endDate) for api in all_apis['result']['items']: try: if api['name'] in apis or len(apis) == 0: print 'Processing...' + api['name'] for date in dates: urlParams = '&start_date=' + urllib.quote_plus(date[0]) + '&end_date=' + urllib.quote_plus(date[1]) + '&format=json&limit=1000' results.extend(masheryV2.get(siteId, apikey, secret, '/reports/calls/developer_activity/service/' + api['service_key'], urlParams)) except ValueError: pass UTF8Writer = codecs.getwriter('utf8') f = UTF8Writer(open(outputFile,'w')) customFields = u'application.name' if (args.additionalfields != None): customFields = ','.join(args.additionalfields) headers = customFields + ',service.id,service.name,key.apikey,startDate,endDate,successful,blocked,other,total\n' f.write(headers) for result in results: if result['serviceDevKey'] in keys or len(keys) == 0: time.sleep(1) # adding slight delay so as to decrease chances of hitting mashery api qps limits query = '{"method":"object.query","id":1,"params":["select ' + customFields + ' from keys where apikey = \'' + result['serviceDevKey'] + '\'"]}' key = masheryV2.post(siteId, apikey, secret, query) unknown_field = '<UNKNOWN>' if (key['result']['total_items'] > 0) : customFieldValues = '' for field in customFields.split(','): splitFields = field.split('.') try: if key['result']['items'][0][splitFields[0]] != None: if len(splitFields) == 2: customFieldValues = customFieldValues + '"' + str(key['result']['items'][0][splitFields[0]][splitFields[1]]) + '",' else: customFieldValues = customFieldValues + '"' + str(key['result']['items'][0][splitFields[0]]) + '",' else: customFieldValues = customFieldValues + '"' + unknown_field + '",' except TypeError: customFieldValues = customFieldValues + '"' + unknown_field + '",' pass outputString = customFieldValues + result['serviceKey'] + ',"' + apiName(all_apis, result['serviceKey']) + '",'+ result['serviceDevKey'] + ',' + result['startDate'] + ',' + result['endDate'] + ','+ str(result['callStatusSuccessful']) + ',' + str(result['callStatusBlocked']) + ',' + str(result['callStatusOther']) + ',' + str(result['callStatusSuccessful'] + result['callStatusBlocked'] + result['callStatusOther']) f.write(outputString + '\n') f.close()