feat: implement ORCID API client and update gitignore to exclude environment files

This commit is contained in:
Alexis
2026-05-15 12:23:36 +02:00
parent a31e0e6137
commit 5ecac7ecf6
2 changed files with 33 additions and 20 deletions
+7
View File
@@ -44,6 +44,13 @@ docker-data/
postgres_data/ postgres_data/
redis_data/ redis_data/
# --- ENVIRONMENT VARIABLES ---
# Secret files shouldn't be committed
.env
.env.*
!.env.example
!.env.template
# --- OS / EDITOR --- # --- OS / EDITOR ---
.DS_Store .DS_Store
Thumbs.db Thumbs.db
+26 -20
View File
@@ -1,18 +1,28 @@
import os
import urllib.parse import urllib.parse
from pathlib import Path
from typing import Any, Optional from typing import Any, Optional
from dotenv import load_dotenv
import httpx import httpx
TOKEN_URL_SANDBOX = "https://sandbox.orcid.org/oauth/token" from app.core.config import settings
AUTHORIZATION_URL_SANDBOX = "https://sandbox.orcid.org/oauth/authorize"
BASE_URL_SANDBOX = "https://pub.sandbox.orcid.org/v3.0" ORCID_ENDPOINTS = {
"sandbox": {
"token_url": "https://sandbox.orcid.org/oauth/token",
"authorization_url": "https://sandbox.orcid.org/oauth/authorize",
"api_base_url": "https://pub.sandbox.orcid.org/v3.0",
},
"production": {
"token_url": "https://orcid.org/oauth/token",
"authorization_url": "https://orcid.org/oauth/authorize",
"api_base_url": "https://pub.orcid.org/v3.0",
},
}
def _orcid_endpoints() -> dict[str, str]:
key = "production" if settings.is_production else "sandbox"
return ORCID_ENDPOINTS[key]
# Si en algún momento pasas a producción, cambiarías a:
# TOKEN_URL_PROD = "https://orcid.org/oauth/token"
# BASE_URL_PROD = "https://pub.orcid.org/v3.0"
# --------------------------------------------------------- # ---------------------------------------------------------
# Clase de cliente de ORCID # Clase de cliente de ORCID
@@ -23,17 +33,13 @@ class ORCIDClient:
# Función auxiliar: inicializar el cliente de ORCID # Función auxiliar: inicializar el cliente de ORCID
# --------------------------------------------------------- # ---------------------------------------------------------
def __init__(self): def __init__(self):
# Asegura que al ejecutar `uvicorn` local también se carga `backend/.env`. endpoints = _orcid_endpoints()
# (En docker `ORCID_REDIRECT_URI` y secretos llegan por env_file, así que esto no molesta.) self.client_id = settings.ORCID_CLIENT_ID
_env_path = Path(__file__).resolve().parents[2] / ".env" self.client_secret = settings.ORCID_CLIENT_SECRET
load_dotenv(dotenv_path=_env_path, override=False)
self.client_id = os.getenv("ORCID_CLIENT_ID")
self.client_secret = os.getenv("ORCID_CLIENT_SECRET")
self._token_cache: Optional[str] = None self._token_cache: Optional[str] = None
self.token_url = TOKEN_URL_SANDBOX self.token_url = endpoints["token_url"]
self.authorization_url = AUTHORIZATION_URL_SANDBOX self.authorization_url = endpoints["authorization_url"]
self.base_url = BASE_URL_SANDBOX self.base_url = endpoints["api_base_url"]
# --------------------------------------------------------- # ---------------------------------------------------------
# 1. Obtener token público # 1. Obtener token público
@@ -204,4 +210,4 @@ def get_display_name(orcid_id: str) -> str | None:
record = get_record(orcid_id) record = get_record(orcid_id)
except Exception: except Exception:
return None return None
return extract_display_name(record) return extract_display_name(record)