Last.fm API core: signatures, XML/JSON responses and dispatcher
- /2.0/ dispatcher backed by a method registry that declares each method's params and signature/session requirements - api_sig verification per the Last.fm auth spec - XML (default) and Last.fm-style JSON rendering, including error shapes and HTTP statuses per error code - Last.fm request, error and auth-failure metrics with bounded labels Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ae8d3b65db
commit
9af8a4be8b
11 files changed
+585
No files matched your search
@@ -51,3 +51,80 @@ class MetricDelta:
|
||||
@pytest.fixture
|
||||
def metric_delta():
|
||||
return MetricDelta
|
||||
|
||||
|
||||
# --- Factories ------------------------------------------------------------------
|
||||
|
||||
import secrets # noqa: E402
|
||||
from datetime import UTC, datetime, timedelta # noqa: E402
|
||||
|
||||
from werkzeug.security import generate_password_hash # noqa: E402
|
||||
|
||||
from scrobbler.lastfm.signature import sign # noqa: E402
|
||||
from scrobbler.models import ApiApp, Session, User # noqa: E402
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def make_user():
|
||||
def make(username="alice", password="hunter22", email=None):
|
||||
user = User(
|
||||
username=username,
|
||||
email=email or f"{username}@example.com",
|
||||
password_hash=generate_password_hash(password),
|
||||
)
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
return user
|
||||
|
||||
return make
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def make_app(make_user):
|
||||
def make(owner=None, name="Test Player"):
|
||||
app_ = ApiApp(
|
||||
api_key=secrets.token_hex(16),
|
||||
shared_secret=secrets.token_hex(16),
|
||||
name=name,
|
||||
owner=owner or make_user(username=f"owner{secrets.token_hex(3)}"),
|
||||
)
|
||||
db.session.add(app_)
|
||||
db.session.commit()
|
||||
return app_
|
||||
|
||||
return make
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def make_session():
|
||||
def make(user, api_app):
|
||||
session = Session(session_key=secrets.token_hex(16), user=user, api_app=api_app)
|
||||
db.session.add(session)
|
||||
db.session.commit()
|
||||
return session
|
||||
|
||||
return make
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def user(make_user):
|
||||
return make_user()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def api_app(make_app, user):
|
||||
return make_app(owner=user)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def session_key(make_session, user, api_app):
|
||||
return make_session(user, api_app).session_key
|
||||
|
||||
|
||||
def signed(params: dict, secret: str) -> dict:
|
||||
"""Return params plus a valid api_sig."""
|
||||
return {**params, "api_sig": sign(params, secret)}
|
||||
|
||||
|
||||
def days_ago(days: float) -> int:
|
||||
return int((datetime.now(UTC) - timedelta(days=days)).timestamp())
|
||||
Reference in new issue
Block a user