Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save NicolasGeraud/44c65baa7975cd8ea322c0f4f4fdb772 to your computer and use it in GitHub Desktop.
Save NicolasGeraud/44c65baa7975cd8ea322c0f4f4fdb772 to your computer and use it in GitHub Desktop.
liste des apps par apis
import sys
import requests
import getopt
baseURL = ""
headers = {
"Content-Type": "application/json",
"Authorization": "",
}
def get_authorization_token():
url = baseURL + "/user/login"
response = requests.post(url, params=None, headers=headers, verify=False)
return response.json()["token"]
def get_apis(api_id=None):
url = baseURL + "/apis"
if api_id is not None:
url += ("/%s" % api_id)
response = requests.get(url, headers=headers, verify=False)
return response.json()
def get_subscribers(api_id):
url = "{}/apis/{}/subscribers".format(baseURL, api_id)
response = requests.get(url, headers=headers, verify=False)
return response.json()
def print_csv_header():
print("API Id; API Name; API ContextPath; APP Id; APP Name")
def or_empty(maybe):
if maybe is not None and maybe is not '':
return maybe
return "-"
def print_api_as_csv(api, subscribers):
delimiter = ";"
for subscriber in subscribers:
row = or_empty(api["id"]) + delimiter
row += or_empty(api["name"]) + delimiter
if "proxy" in api:
row += or_empty(api["proxy"]["context_path"]) + delimiter
else:
row += or_empty(None) + delimiter
row += or_empty(subscriber["id"]) + delimiter
row += or_empty(subscriber["name"]) + delimiter
print(u''.join(row).encode("utf-8"))
def main():
global filters
global baseURL
global headers
try:
opts, args = getopt.getopt(sys.argv[1:], "", ["auth=", "url="])
except getopt.GetoptError:
print("ERROR")
sys.exit(-1)
for opt, arg in opts:
if opt == '--auth':
headers["Authorization"] = arg
elif opt == "--url":
baseURL = arg
token = get_authorization_token()
headers["Authorization"] = "Bearer {}".format(token)
print(headers)
apis = get_apis()
print_csv_header()
for api in apis:
api = get_apis(api['id'])
subscribers = get_subscribers(api['id'])
print_api_as_csv(api, subscribers)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment