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 import os.path #test for parallel requests import asyncio #import aiohttp import concurrent.futures import time class apibase: def __init__(self,user_name=None,user_password=None): #self.baseurl="https://.../api" self.username = user_name self.password = user_password pass def quote(self,string): return requests.utils.quote(string) def get(self,url): # print("url:",url) if isinstance(url,dict): u=url["url"] else: u=url try: verify=False auth=None dbg={"http_code":0,"total_time":0} headers={} data={} if self.username: auth=HTTPBasicAuth(self.username,self.password) #, timeout=10 #connect,read: timeout=(3.05, 27) #response = requests.get(url, verify=verify, auth=HTTPBasicAuth(self.username,self.password)) #else: #response = requests.get(url, verify=verify) # print("requesting url:",u) response = requests.get(u, verify=verify, auth=auth) #requests.post('https://httpbin.org/post', data={'key':'value'}) #requests.put('https://httpbin.org/put', data={'key':'value'}) #requests.delete('https://httpbin.org/delete') #requests.head('https://httpbin.org/get') #requests.patch('https://httpbin.org/patch', data={'key':'value'}) #requests.options('https://httpbin.org/get') # 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') dbg["msg"]="The request timed out" except requests.ConnectionError as e: #print(f"connection-error {e}") dbg["msg"]=f"connection-error {e}" except Exception as e: #print(f'Other error occurred: {e}') # Python 3.6 dbg["msg"]=f'Other error occurred: {e}' else: #print('Success!') #print(response.text) #print("Status:",response.status_code,response.reason,response.ok) #print (response.elapsed.total_seconds()) #print(response.headers) 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 dbg={"http_code":response.status_code,"total_time":response.elapsed.total_seconds()} headers=response.headers ret={"url":u,"headers":headers,"data":data,"debug":dbg} if isinstance(url,dict) and "id" in url: ret["id"]=url["id"] return ret def handleurl(self,url,**args): # print("handleurl-args:",args) if "action" in args: if args["action"]=="returnurl": r=url r={"url":url} if "id" in args: r["id"]=args["id"] # print("handleurl - r:",r) return r # else: return self.get(url) 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): # print("mq urls:",urls) loop = asyncio.get_event_loop() data=loop.run_until_complete(self.multiquery_helper(urls)) return data def readconfig(self,configfile,inisection="api",userkey="user",pwkey="password"): try: u=None p=None config = configparser.ConfigParser() if config.read(configfile): print(config.sections()) #print(config.items()) u=config[inisection][userkey].strip('\"') p_enc=config[inisection][pwkey].strip('\"') p_bin=base64.b64decode(p_enc.encode('ascii')) p=p_bin.decode('ascii') else: print("cannot read config!") except Exception as e: print(e) else: self.username = u self.password = p class ghapi(apibase): def __init__(self,username="",password=""): self.baseurl="https://api.github.com/" apibase.__init__(self,username,password) def getBranches(self,ghuser,ghrepo,**args): url=self.baseurl+"repos/"+ghuser+"/"+ghrepo+"/branches" return self.handleurl(url,**args) def getUserInfo(self,ghuser,**args): url=self.baseurl+"users/"+ghuser return self.handleurl(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",id=1) #,returnurl=True) # url_b2=api.getBranches("frank-w","bpi-r2-ssd1306-display",id=2) #,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",action="returnurl",id=1) url_b2=api.getBranches("frank-w","bpi-r2-ssd1306-display",action="returnurl",id=2) res=api.multiquery([url_b1,url_b2]) et = time.perf_counter() print("outside:", # res, f"\nDownload in {et - st:0.4f} seconds") for r in res: print("id:",r["id"],"url:",r["url"],"debug:",r["debug"])