import configparser import base64 import requests from requests.auth import HTTPBasicAuth import warnings warnings.filterwarnings('ignore', message='Unverified HTTPS request') #import urllib #parse.quote (urlencode) #requests.utils.quote #test for parallel requests import asyncio #import aiohttp import concurrent.futures import time class apibase: def __init__(self,user_name='',user_password=''): #self.baseurl="https://.../api" self.username = user_name self.password = user_password pass def get(self,url): try: verify=False if self.username: response = requests.get(url, verify=verify, auth=HTTPBasicAuth(self.username,self.password)) else: response = requests.get(url, verify=verify) # If the response was successful, no Exception will be raised response.raise_for_status() except requests.HTTPError as e: #print(f'HTTP error occurred: {e}') status_code = e.response.status_code print("Status:",status_code) except requests.Timeout as e: print('The request timed out') except requests.ConnectionError as e: print(f"connection-error {e}") except Exception as e: print(f'Other error occurred: {e}') # Python 3.6 else: #print('Success!') #print(response.text) ctype=response.headers['content-type'] #print(ctype) #application/json; charset=utf-8 t=ctype.split("; ") if t[0]=="application/json": data=response.json() else: data=response.text ret={"url":url,"type":ctype,"data":data} return ret def getorreturn(self,url,**args): if "returnurl" in args and args["returnurl"]: return url else: return self.get(url) # def basequery(self,url): # print(url) # response=requests.get(url) # #print(response) # ctype=response.headers['content-type'] # #print(ctype) # data=response.text # #print(data) # ret={"url":url,"type":ctype,"data":data} # #print("basequery",ret) # return ret #{"type":ctype,"data":data} async def multiquery_helper(self,urls): results=[] with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor: loop = asyncio.get_event_loop() futures = [ loop.run_in_executor( executor, self.get, u ) for u in urls ] for response in await asyncio.gather(*futures): #pass results.append(response) #print("mq_helper:",results) return results def multiquery(self,urls): loop = asyncio.get_event_loop() data=loop.run_until_complete(self.multiquery_helper(urls)) return data class secapi(apibase): def __init__(self,configfile=""): #self.baseurl="https://.../api" inisection="api" try: #if configfile: config = configparser.ConfigParser() config.read(configfile) u=config[inisection]['user'].strip('\"') # löscht umfassende "" bei strings p_enc=config[inisection]['password'].strip('\"') # löscht umfassende "" bei strings p_bin=base64.b64decode(p_enc.encode('ascii')) p=p_bin.decode('ascii') apibase.__init__(self,u,p) except: apibase.__init__(self) class ghapi(secapi): def __init__(self,configfile=""): self.baseurl="https://api.github.com/" secapi.__init__(self,configfile) def getBranches(self,ghuser,ghrepo,**args): url=self.baseurl+"repos/"+ghuser+"/"+ghrepo+"/branches" return self.getorreturn(url,**args) def getUserInfo(self,ghuser,**args): url=self.baseurl+"users/"+ghuser return self.getorreturn(url,**args) if __name__ == "__main__": #api=apibase() #res=api.multiquery(["https://api.github.com/repos/frank-w/buildroot/branches","https://api.github.com/users/frank-w"]) api=ghapi() st = time.perf_counter() url_b1=api.getBranches("frank-w","buildroot")#,returnurl=True) url_b2=api.getBranches("frank-w","bpi-r2-ssd1306-display")#,returnurl=True) et = time.perf_counter() print("urls:",url_b1,url_b2,f"Download in {et - st:0.4f} seconds") st = time.perf_counter() url_b1=api.getBranches("frank-w","buildroot",returnurl=True) url_b2=api.getBranches("frank-w","bpi-r2-ssd1306-display",returnurl=True) res=api.multiquery([url_b1,url_b2]) et = time.perf_counter() print("outside:",res,f"Download in {et - st:0.4f} seconds")