# Este script es una plantilla de como se podria consultar a la API # Integra funcinoes que envuelven la capa del requeset # Para GET sin autenticacion # Y tambien a los que requieren AUTH (con el token, previamnte obtenido) #+------------------------------------------------------------------+ #| Imports | #+------------------------------------------------------------------+ import json import os import urllib.error import urllib.request #+------------------------------------------------------------------+ #| Defines | #+------------------------------------------------------------------+ BASE_URL = "https://forge.mql5.io/api/v1" #+------------------------------------------------------------------+ #| Functions | #+------------------------------------------------------------------+ def needs_token(path: str) -> bool: """ Returns True if the endpoint answered 401 (token required), False if it's public (200). Don't assume from the endpoint's name/category test it. """ try: urllib.request.urlopen(f"{BASE_URL}{path}") return False except urllib.error.HTTPError as e: return e.code == 401 def get_public(path: str, params: dict | None = None) -> dict: """ Return json for GET (not-authorization) requerid query """ url : str = f"{BASE_URL}{path}" # En caso haya params.. armamos el query y lo intrudcimos a la url base if params: query : str = "&".join(f"{k}={v}" for k, v in params.items()) url = f"{url}?{query}" with urllib.request.urlopen(url) as resp: return json.load(resp) def get_authenticated(path: str, params: dict | None = None) -> dict: """ Reads FORGEJO_TOKEN from the environment never hardcode it here. The user should have set it themselves (see SKILL.md, token section) via env var or a local .env loaded with python-dotenv. """ # Obtenemso el token token = os.environ.get("FORGEJO_TOKEN") if not token: raise RuntimeError( "FORGEJO_TOKEN not set in the environment. " "Ask the user to set it first (see SKILL.md)." ) # Armamos la URL url : str = f"{BASE_URL}{path}" # Si hay params los agregamos if params: query : str = "&".join(f"{k}={v}" for k, v in params.items()) url = f"{url}?{query}" # Hacemos el reg req = urllib.request.Request( url, headers={"Authorization": f"token {token}"} ) # Cargamos el json with urllib.request.urlopen(req) as resp: return json.load(resp) #+------------------------------------------------------------------+ #| Main | #+------------------------------------------------------------------+ if __name__ == "__main__": # Public example: search repos instance-wide results = get_public("/repos/search", {"q": "example", "limit": 5}) print(json.dumps(results, indent=2)[:500]) # Authenticated example (only if the endpoint actually needs it # check with needs_token() first): # data = get_authenticated("/user/repos") # print(data)