Update app.py
Browse files
app.py
CHANGED
|
@@ -3,15 +3,15 @@ import requests
|
|
| 3 |
import os
|
| 4 |
from requests.auth import HTTPBasicAuth
|
| 5 |
|
| 6 |
-
def
|
| 7 |
"""
|
| 8 |
-
Função para
|
| 9 |
"""
|
| 10 |
try:
|
| 11 |
# Recupera as variáveis de ambiente seguras
|
| 12 |
wp_app_password = os.getenv("WP_APP_PASSWORD")
|
| 13 |
wp_user = os.getenv("WP_USER")
|
| 14 |
-
wp_url = os.getenv("
|
| 15 |
|
| 16 |
# Verifica se as variáveis estão definidas
|
| 17 |
if not wp_app_password:
|
|
@@ -19,53 +19,40 @@ def execute_wp_cli(command, args):
|
|
| 19 |
if not wp_user:
|
| 20 |
return "Erro: Variável de ambiente WP_USER não definida."
|
| 21 |
if not wp_url:
|
| 22 |
-
return "Erro: Variável de ambiente
|
| 23 |
|
| 24 |
-
# URL da API REST
|
| 25 |
-
api_url = f"{wp_url}/wp-json/wp/v2"
|
| 26 |
|
| 27 |
# Configura a autenticação
|
| 28 |
auth = HTTPBasicAuth(wp_user, wp_app_password)
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
# Adiciona argumento fixo para teste
|
| 34 |
-
args = "test"
|
| 35 |
-
|
| 36 |
-
# Executa o comando WP-CLI via API REST
|
| 37 |
-
response = requests.post(command_url, auth=auth, json={"args": args})
|
| 38 |
|
| 39 |
if response.status_code != 200:
|
| 40 |
-
return f"Erro ao
|
| 41 |
|
| 42 |
return response.json()
|
| 43 |
except Exception as e:
|
| 44 |
return f"Erro: {str(e)}"
|
| 45 |
|
| 46 |
-
# Lista de
|
| 47 |
-
|
| 48 |
-
"
|
| 49 |
-
"
|
| 50 |
-
"
|
| 51 |
-
"theme list",
|
| 52 |
-
"theme activate",
|
| 53 |
-
"post list",
|
| 54 |
-
"post create",
|
| 55 |
-
"post update",
|
| 56 |
-
"post delete",
|
| 57 |
]
|
| 58 |
|
| 59 |
# Cria a interface Gradio
|
| 60 |
iface = gr.Interface(
|
| 61 |
-
fn=
|
| 62 |
inputs=[
|
| 63 |
-
gr.Dropdown(choices=
|
| 64 |
-
gr.Textbox(lines=1, placeholder="Digite os argumentos (se necessário)", label="Argumentos") # Entrada de texto para argumentos
|
| 65 |
],
|
| 66 |
outputs="text", # Saída de texto com o resultado
|
| 67 |
-
title="
|
| 68 |
-
description="Selecione um
|
| 69 |
)
|
| 70 |
|
| 71 |
# Inicia a interface
|
|
|
|
| 3 |
import os
|
| 4 |
from requests.auth import HTTPBasicAuth
|
| 5 |
|
| 6 |
+
def test_wp_rest_api(endpoint):
|
| 7 |
"""
|
| 8 |
+
Função para testar endpoints básicos da API REST do WordPress.
|
| 9 |
"""
|
| 10 |
try:
|
| 11 |
# Recupera as variáveis de ambiente seguras
|
| 12 |
wp_app_password = os.getenv("WP_APP_PASSWORD")
|
| 13 |
wp_user = os.getenv("WP_USER")
|
| 14 |
+
wp_url = os.getenv("WP_URL")
|
| 15 |
|
| 16 |
# Verifica se as variáveis estão definidas
|
| 17 |
if not wp_app_password:
|
|
|
|
| 19 |
if not wp_user:
|
| 20 |
return "Erro: Variável de ambiente WP_USER não definida."
|
| 21 |
if not wp_url:
|
| 22 |
+
return "Erro: Variável de ambiente WP_URL não definida."
|
| 23 |
|
| 24 |
+
# URL do endpoint da API REST
|
| 25 |
+
api_url = f"{wp_url}/wp-json/wp/v2/{endpoint}"
|
| 26 |
|
| 27 |
# Configura a autenticação
|
| 28 |
auth = HTTPBasicAuth(wp_user, wp_app_password)
|
| 29 |
|
| 30 |
+
# Executa a requisição GET no endpoint da API REST
|
| 31 |
+
response = requests.get(api_url, auth=auth)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
if response.status_code != 200:
|
| 34 |
+
return f"Erro ao acessar o endpoint: {response.text}"
|
| 35 |
|
| 36 |
return response.json()
|
| 37 |
except Exception as e:
|
| 38 |
return f"Erro: {str(e)}"
|
| 39 |
|
| 40 |
+
# Lista de endpoints básicos da API REST
|
| 41 |
+
endpoints = [
|
| 42 |
+
"posts",
|
| 43 |
+
"users",
|
| 44 |
+
"categories"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
]
|
| 46 |
|
| 47 |
# Cria a interface Gradio
|
| 48 |
iface = gr.Interface(
|
| 49 |
+
fn=test_wp_rest_api, # Função que será chamada
|
| 50 |
inputs=[
|
| 51 |
+
gr.Dropdown(choices=endpoints, label="Endpoint da API REST") # Menu suspenso para selecionar o endpoint
|
|
|
|
| 52 |
],
|
| 53 |
outputs="text", # Saída de texto com o resultado
|
| 54 |
+
title="Testador de API REST do WordPress", # Título do painel
|
| 55 |
+
description="Selecione um endpoint da API REST para testar." # Descrição do painel
|
| 56 |
)
|
| 57 |
|
| 58 |
# Inicia a interface
|