Not for your specific case, unfortunately. But perhaps you can modify this to siut your needs. #!/usr/bin/env python3
from pprint import pprint
import os, time, sys
import argparse
import requests
from requests import Session
import json
MAX_RETRIES = 5
API_KEY = None
apiVersion = 'v1'
baseURL = 'https://api.meraki.com/api/'+apiVersion
class NoRebuildAuthSession(Session):
def rebuild_auth(self, prepared_request, response):
'''
No code here means requests will always preserve the Authorization header when redirected.
Be careful not to leak your credentials to untrusted hosts!
'''
session = NoRebuildAuthSession()
def GetOrgs(p_apiKey):
for _ in range(MAX_RETRIES):
try:
r = session.get(
baseURL+'/organizations',
headers = {
'Authorization': f'Bearer {p_apiKey}',
'Content-Type': 'application/json',
"Accept": "application/json",
}
)
if r.status_code == 200:
return r
elif r.status_code == 429:
print(f'Rate limited - Retrying after {r.headers["Retry-After"]}.')
time.sleep(int(r.headers['Retry-After']))
continue
else:
raise SystemExit(f'Unexpected status code: {r.status_code} returned from server.')
except Exception as e:
pprint(e)
# Main routine function
def main(arg_org_id,arg_get_nw,arg_debug):
if arg_debug is not None:
print(locals())
print()
API_KEY = os.environ.get('MERAKI_API_KEY', None)
if API_KEY is None:
raise SystemExit('Please set environment variable MERAKI_API_KEY')
else:
API_KEY = os.environ['MERAKI_API_KEY']
if arg_org is None:
print('Getting list of organisations...')
r = GetOrgs(API_KEY)
rjson = r.json()
print('Success!')
pprint(rjson)
def run(args):
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
parser.add_argument('-o', '--org-id', dest='arg_org_id', help='Set the Organisation ID')
group.add_argument('-n', '--get-networks', dest='arg_get_nw', help='Get Networks')
parser.add_argument('-d', '--debug', dest='arg_debug')
options = parser.parse_args(sys.argv[1:])
main(**vars(options))
if __name__ == "__main__":
run(sys.argv[1:]) Edit: Added sys package.
... View more