"""=========================aiohttp_middlewares.utils=========================Various utility functions for ``aiohttp_middlewares`` library."""fromyarlimportURLfromaiohttp_middlewares.annotationsimportUrl,Urls
[docs]defmatch_path(item:Url,path:str)->bool:"""Check whether current path is equal to given URL str or regexp. :param item: URL to compare with request path. :param path: Request path string. """ifisinstance(item,URL):item=str(item)ifisinstance(item,str):returnitem==pathtry:returnbool(item.match(path))except(AttributeError,TypeError):returnFalse
defmatch_request(urls:Urls,method:str,path:str)->bool:"""Check whether request method and path matches given URLs or not."""found=[itemforiteminurlsifmatch_path(item,path)]ifnotfound:returnFalseifnotisinstance(urls,dict):returnTruefound_item=urls[found[0]]method=method.lower()ifisinstance(found_item,str):returnfound_item.lower()==methodreturnany(Trueforiteminfound_itemifitem.lower()==method)