def __init__( self, eth_address: str = None, password: str = None, access_token: str = None, refresh_token: str = None, handler: APIHandler = None, staging: bool = False, no_cache: bool = False, middlewares: List[BaseMiddleware] = None, ): self.eth_address = eth_address self.password = password if not middlewares: # initialize without custom middlewares middlewares = [ ClientToolNameMiddleware(), AnalysisCacheMiddleware(no_cache), ] else: # add tool name and analysis cache middleware type_list = [type(m) for m in middlewares] if ClientToolNameMiddleware not in type_list: middlewares.append(ClientToolNameMiddleware()) if AnalysisCacheMiddleware not in type_list: middlewares.append(AnalysisCacheMiddleware(no_cache)) self.handler = handler or APIHandler(middlewares=middlewares, staging=staging) self.access_token = access_token self.refresh_token = refresh_token
def test_custom_middlewares(): assert_middlewares(Client()) assert_middlewares(Client(middlewares=None)) assert_middlewares(Client(middlewares=[])) assert_middlewares(Client(middlewares=[AnalysisCacheMiddleware()])) assert_middlewares(Client(middlewares=[ClientToolNameMiddleware()])) assert_middlewares( Client(middlewares=[AnalysisCacheMiddleware(), ClientToolNameMiddleware()]) )
def main(): parser = argparse.ArgumentParser( description="Submit analysis to MythX API") create_parser(parser) args = parser.parse_args() config["endpoints"]["production"] = args.api_url handler = APIHandler(middlewares=[ ClientToolNameMiddleware(name="ApiTests"), AnalysisCacheMiddleware(no_cache=args.no_cache), ]) logging.info(f"Running MythX API tests without cache: {args.no_cache}") c = Client(eth_address=ETH_ADDRESS, password=PASSWORD, handler=handler) logging.info(f"Submit analysis to API: {args.api_url}") resp = c.analyze(**API_PAYLOAD) logging.info(resp.to_dict()) while not c.analysis_ready(resp.uuid): logging.info("Checking analysis status") time.sleep(1) issues = c.report(resp.uuid) detected_issues = issues.to_dict() logging.info(json.dumps(detected_issues, indent=2)) return detected_issues
import pytest from pythx.middleware.analysiscache import AnalysisCacheMiddleware from pythx.middleware.toolname import ClientToolNameMiddleware from . import common as testdata FALSE_CACHE_MIDDLEWARE = AnalysisCacheMiddleware(no_cache=False) TRUE_CACHE_MIDDLEWARE = AnalysisCacheMiddleware(no_cache=True) @pytest.mark.parametrize( "middleware,request_dict,flag_added,lookup_value", [ ( TRUE_CACHE_MIDDLEWARE, testdata.generate_request_dict( testdata.ANALYSIS_LIST_REQUEST_OBJECT), False, True, ), ( TRUE_CACHE_MIDDLEWARE, testdata.generate_request_dict( testdata.DETECTED_ISSUES_REQUEST_OBJECT), False, True, ), ( TRUE_CACHE_MIDDLEWARE, testdata.generate_request_dict(