diff --git a/api/chrome/cli.py b/api/chrome/cli.py index fe2b72f..9eb54a0 100644 --- a/api/chrome/cli.py +++ b/api/chrome/cli.py @@ -22,7 +22,7 @@ class Chrome: def __init__(self): self.ep = "https://localhost:8443" - def __requestget__(self, data: str): + def __request_get__(self, data: str): """ ## HTTP GET @@ -46,17 +46,17 @@ class Chrome: else: return res.text - def __requestpost__(self, data: str, object: dict): + def __request_json_post__(self, path: str, object: dict): """ - ## HTTP POST + ## HTTP JSON POST --- - Este método é responsável por realizar requisições HTTP do tipo POST. + Este método é responsável por realizar requisições HTTP do tipo POST para objetos JSON. Ele retorna o corpo de resposta da requisição, ou uma mensagem de erro, que indica qual foi a irregularidade ocorrida ao chamar a API. """ - url = self.ep + data + url = self.ep + path print("Calling: " + url) apikey = os.environ.get('REPLAY_APIKEY') @@ -69,6 +69,30 @@ class Chrome: return json.loads(res.text) else: return res.text + + def __request_raw_post__(self, path: str, data: str): + + """ + ## HTTP RAW POST + --- + Este método é responsável por realizar requisições HTTP do tipo POST para objetos RAW. + + Ele retorna o corpo de resposta da requisição, ou uma mensagem de erro, que indica qual foi a irregularidade ocorrida ao chamar a API. + """ + + url = self.ep + path + print("Calling: " + url) + + apikey = os.environ.get('REPLAY_APIKEY') + headers = {"X-API-KEY": apikey} + res = requests.post(url, data = data, headers = headers, verify = False) + + if res.status_code >= 400: + raise Exception(f"HTTP ERROR: {str(res.status_code)} - {res.text}") + if res.headers.get("Content-Type") != None and res.headers.get("Content-Type").find("json") != -1: + return json.loads(res.text) + else: + return res.text def start (self, to: int = 120): """ @@ -87,7 +111,7 @@ class Chrome: "" -> String vazia """ - return self.__requestget__(f"/ipc/chrome/start?to={to}") + return self.__request_get__(f"/ipc/chrome/start?to={to}") def start_headless (self): """ @@ -107,7 +131,7 @@ class Chrome: "" -> String vazia """ - return self.__requestget__("/ipc/chrome/startHeadless") + return self.__request_get__("/ipc/chrome/startHeadless") def stop (self): """ @@ -127,7 +151,7 @@ class Chrome: "" -> String vazia """ - return self.__requestget__("/ipc/chrome/stop") + return self.__request_get__("/ipc/chrome/stop") def new (self, url: str = "https://www.google.com"): """ @@ -145,7 +169,7 @@ class Chrome: """ url = urllib.parse.quote(url, "") - return self.__requestget__(f"/ipc/chrome/new?url={url}") + return self.__request_get__(f"/ipc/chrome/new?url={url}") def close (self, id: str): """ @@ -162,4 +186,20 @@ class Chrome: "" -> String vazia """ - return self.__requestget__("/ipc/chrome/close/"+id) \ No newline at end of file + return self.__request_get__("/ipc/chrome/close/"+id) + + def eval (self, id: str, command: str) -> dict: + """ + ## Chrome Eval + Digita o comando recebido no Console JavaScript da página cujo ID foi recebido. + + --- + #### Parâmetros: + - id: Identificador da aba em que se quer acessar o console. + - command: Comando DOM JavaScript + --- + #### Retorna: + -> ? + """ + + return self.__request_raw_post__("/ipc/chrome/eval/"+id, command) \ No newline at end of file