feat: finalize root api.py with X-Auth-Secret auth security and clean pure sales cohort
HIFFI Multi-Platform CI / test (3.12) (push) Successful in 50s
HIFFI Multi-Platform CI / test (3.11) (push) Successful in 50s

This commit is contained in:
Srilekha committed 2026-11-04 16:49:39 +05:30
1 parent 85e6395770
commit 78a46a8dbe
629 files changed
+123965 -755

No files matched your search

+19 -70
View File
@@ -1,85 +1,34 @@
"""
collectors/test_spotify_endpoints.py
Direct diagnostic to test Spotify OAuth2 Client Credentials flow
and probe endpoint permissions for Artist data, Top Tracks, and Search.
Manual diagnostic utility for testing public and authenticated Spotify API endpoints.
Excluded from automated pytest discovery.
"""
import base64
import os
from pathlib import Path
import sys
import requests
from dotenv import load_dotenv
PROJECT_ROOT = Path(__file__).resolve().parent.parent
if str(PROJECT_ROOT) not in sys.path:
sys.path.insert(0, str(PROJECT_ROOT))
load_dotenv(PROJECT_ROOT / ".env")
CLIENT_ID = os.getenv("SPOTIFY_CLIENT_ID", "").strip()
CLIENT_SECRET = os.getenv("SPOTIFY_CLIENT_SECRET", "").strip()
__test__ = False
def get_token(client_id: str, client_secret: str):
"""Requests a bearer token via standard OAuth2 Client Credentials."""
if not client_id or not client_secret:
return 0, {"error": "SPOTIFY_CLIENT_ID or SPOTIFY_CLIENT_SECRET not configured"}
auth_header = base64.b64encode(f"{client_id}:{client_secret}".encode()).decode()
url = "https://accounts.spotify.com/api/token"
res = requests.post(
url,
data={"grant_type": "client_credentials"},
headers={"Authorization": f"Basic {auth_header}"},
timeout=10,
)
return res.status_code, res.json()
def test_endpoints(token: str):
def run_manual_endpoint_test(token: str):
"""Manual probe for validating token scopes against official Spotify Web API endpoints."""
headers = {"Authorization": f"Bearer {token}"}
pilot_artist_id = "3TVXtAsR1Inumwj472S9r4" # Drake
sample_id = "06HL4z0CvFAxyc27GXpf02" # Taylor Swift
endpoints = {
"Artist Metadata (GET /v1/artists/{id})": f"https://api.spotify.com/v1/artists/{pilot_artist_id}",
"Artist Top Tracks (GET /v1/artists/{id}/top-tracks)": f"https://api.spotify.com/v1/artists/{pilot_artist_id}/top-tracks?market=US",
"Search Endpoint (GET /v1/search)": "https://api.spotify.com/v1/search?q=Drake&type=artist&limit=1",
}
results = {}
for name, url in endpoints.items():
res = requests.get(url, headers=headers, timeout=10)
results[name] = {
"status_code": res.status_code,
"response_preview": res.text[:300],
}
return results
url = f"https://api.spotify.com/v1/artists/{sample_id}"
resp = requests.get(url, headers=headers, timeout=10)
print(f"Status Code: {resp.status_code}")
if resp.status_code == 200:
data = resp.json()
print(f"Artist Name: {data.get('name')}")
print(f"Followers: {data.get('followers', {}).get('total')}")
else:
print(f"Error Payload: {resp.text}")
if __name__ == "__main__":
print("=" * 80)
print("SPOTIFY API AUTHENTICATION & ENDPOINT PERMISSION DIAGNOSTIC")
print("=" * 80)
if not CLIENT_ID or not CLIENT_SECRET:
print("Error: Missing credentials in environment. Set SPOTIFY_CLIENT_ID and SPOTIFY_CLIENT_SECRET in .env.")
sys.exit(1)
status, token_resp = get_token(CLIENT_ID, CLIENT_SECRET)
print(f"Token Endpoint Status: {status}")
if status == 200:
token = token_resp.get("access_token")
print(f"Bearer Token Acquired: {token[:15]}... [expires_in: {token_resp.get('expires_in')}s]")
print("-" * 80)
diagnostics = test_endpoints(token)
for ep, out in diagnostics.items():
print(f"\nEndpoint: {ep}")
print(f"Status: {out['status_code']}")
print(f"Payload: {out['response_preview']}")
test_token = os.getenv("SPOTIFY_ACCESS_TOKEN", "").strip()
if not test_token:
print("Usage: Set SPOTIFY_ACCESS_TOKEN in environment before executing.")
else:
print(f"Failed to obtain token: {token_resp}")
print("\n" + "=" * 80)
run_manual_endpoint_test(test_token)