meli-python-sdk 1.0.0a1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- meli_python_sdk-1.0.0a1.dist-info/METADATA +449 -0
- meli_python_sdk-1.0.0a1.dist-info/RECORD +18 -0
- meli_python_sdk-1.0.0a1.dist-info/WHEEL +5 -0
- meli_python_sdk-1.0.0a1.dist-info/licenses/LICENSE +21 -0
- meli_python_sdk-1.0.0a1.dist-info/top_level.txt +1 -0
- meli_sdk/__init__.py +43 -0
- meli_sdk/_version.py +1 -0
- meli_sdk/actions.py +85 -0
- meli_sdk/bills.py +123 -0
- meli_sdk/client.py +523 -0
- meli_sdk/exceptions.py +34 -0
- meli_sdk/items.py +257 -0
- meli_sdk/metrics.py +77 -0
- meli_sdk/orders.py +222 -0
- meli_sdk/py.typed +0 -0
- meli_sdk/session.py +92 -0
- meli_sdk/shipments.py +26 -0
- meli_sdk/storage.py +119 -0
meli_sdk/storage.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import contextlib
|
|
2
|
+
import json
|
|
3
|
+
import os
|
|
4
|
+
import threading
|
|
5
|
+
from typing import Callable, ContextManager, Optional
|
|
6
|
+
|
|
7
|
+
_FILE_LOCKS = {}
|
|
8
|
+
_FILE_LOCKS_GUARD = threading.Lock()
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _file_lock_for_path(file_path: str) -> threading.Lock:
|
|
12
|
+
"""Return the process-wide lock shared by every storage for this path."""
|
|
13
|
+
absolute = os.path.abspath(file_path)
|
|
14
|
+
with _FILE_LOCKS_GUARD:
|
|
15
|
+
lock = _FILE_LOCKS.get(absolute)
|
|
16
|
+
if lock is None:
|
|
17
|
+
lock = threading.Lock()
|
|
18
|
+
_FILE_LOCKS[absolute] = lock
|
|
19
|
+
return lock
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class TokenStorage:
|
|
23
|
+
"""Clase base abstracta (interfaz) para el almacenamiento y recuperación de tokens."""
|
|
24
|
+
|
|
25
|
+
def load_token(self) -> dict:
|
|
26
|
+
"""Carga y devuelve los datos del token actual (ej. access_token, refresh_token)."""
|
|
27
|
+
raise NotImplementedError("Debe implementar el método load_token.")
|
|
28
|
+
|
|
29
|
+
def save_token(self, token_data: dict) -> None:
|
|
30
|
+
"""Guarda la información actualizada del token."""
|
|
31
|
+
raise NotImplementedError("Debe implementar el método save_token.")
|
|
32
|
+
|
|
33
|
+
def lock(self) -> ContextManager[None]:
|
|
34
|
+
"""Return the lock that serializes token refreshes for this storage.
|
|
35
|
+
|
|
36
|
+
The default is a no-op context manager, so existing subclasses keep working.
|
|
37
|
+
"""
|
|
38
|
+
return contextlib.nullcontext()
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class FileTokenStorage(TokenStorage):
|
|
42
|
+
"""Almacena el token en un archivo JSON local. Admite múltiples sesiones usando rutas
|
|
43
|
+
diferentes.
|
|
44
|
+
|
|
45
|
+
Instances that resolve to the same absolute path share one lock inside this process. It does
|
|
46
|
+
not coordinate separate processes.
|
|
47
|
+
"""
|
|
48
|
+
|
|
49
|
+
def __init__(self, file_path: str) -> None:
|
|
50
|
+
self.file_path = file_path
|
|
51
|
+
|
|
52
|
+
def load_token(self) -> dict:
|
|
53
|
+
if not os.path.exists(self.file_path):
|
|
54
|
+
return {}
|
|
55
|
+
try:
|
|
56
|
+
with open(self.file_path, "r", encoding="utf-8") as f:
|
|
57
|
+
return json.load(f)
|
|
58
|
+
except (json.JSONDecodeError, OSError):
|
|
59
|
+
return {}
|
|
60
|
+
|
|
61
|
+
def save_token(self, token_data: dict) -> None:
|
|
62
|
+
# Crear directorios padres si no existen
|
|
63
|
+
dir_name = os.path.dirname(self.file_path)
|
|
64
|
+
if dir_name and not os.path.exists(dir_name):
|
|
65
|
+
os.makedirs(dir_name, exist_ok=True)
|
|
66
|
+
|
|
67
|
+
with open(self.file_path, "w", encoding="utf-8") as f:
|
|
68
|
+
json.dump(token_data, f, ensure_ascii=False, indent=2)
|
|
69
|
+
|
|
70
|
+
def lock(self) -> ContextManager[None]:
|
|
71
|
+
"""Return the lock shared by every storage for the same absolute path."""
|
|
72
|
+
return _file_lock_for_path(self.file_path)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
class MemoryTokenStorage(TokenStorage):
|
|
76
|
+
"""Guarda el token únicamente en memoria RAM. Útil para scripts efímeros y testing."""
|
|
77
|
+
|
|
78
|
+
def __init__(self, initial_token: Optional[dict] = None) -> None:
|
|
79
|
+
self.token = initial_token or {}
|
|
80
|
+
self._lock = threading.Lock()
|
|
81
|
+
|
|
82
|
+
def load_token(self) -> dict:
|
|
83
|
+
return self.token
|
|
84
|
+
|
|
85
|
+
def save_token(self, token_data: dict) -> None:
|
|
86
|
+
self.token = token_data
|
|
87
|
+
|
|
88
|
+
def lock(self) -> ContextManager[None]:
|
|
89
|
+
"""Return the per-instance lock, shared by every client using this storage."""
|
|
90
|
+
return self._lock
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
class CallbackTokenStorage(TokenStorage):
|
|
94
|
+
"""
|
|
95
|
+
Guarda y carga el token delegando en callbacks definidos por el usuario.
|
|
96
|
+
Ideal para bases de datos relacionales u ORMs (SQLAlchemy, Django, Redis, etc.).
|
|
97
|
+
"""
|
|
98
|
+
|
|
99
|
+
def __init__(
|
|
100
|
+
self,
|
|
101
|
+
load_callback: Callable[[], dict],
|
|
102
|
+
save_callback: Callable[[dict], None],
|
|
103
|
+
lock_callback: Optional[Callable[[], ContextManager]] = None,
|
|
104
|
+
) -> None:
|
|
105
|
+
self.load_callback = load_callback
|
|
106
|
+
self.save_callback = save_callback
|
|
107
|
+
self.lock_callback = lock_callback
|
|
108
|
+
|
|
109
|
+
def load_token(self) -> dict:
|
|
110
|
+
return self.load_callback() or {}
|
|
111
|
+
|
|
112
|
+
def save_token(self, token_data: dict) -> None:
|
|
113
|
+
self.save_callback(token_data)
|
|
114
|
+
|
|
115
|
+
def lock(self) -> ContextManager[None]:
|
|
116
|
+
"""Return the user-provided lock, for example a database transaction."""
|
|
117
|
+
if self.lock_callback is None:
|
|
118
|
+
return contextlib.nullcontext()
|
|
119
|
+
return self.lock_callback()
|