Skip to content

Refactor duplicated code into shared utilities - #4

Open
devin-ai-integration[bot] wants to merge 1 commit into
mainfrom
devin/1787719524-dedupe-shared-utilities
Open

Refactor duplicated code into shared utilities#4
devin-ai-integration[bot] wants to merge 1 commit into
mainfrom
devin/1787719524-dedupe-shared-utilities

Conversation

@devin-ai-integration

Copy link
Copy Markdown

Summary

Deduplicates repeated patterns across the backend routes/services and the frontend components into shared utilities.

New backend/utils.py:

  • @require_spotify_auth — replaces the if 'access_token' not in session: return 401 guard repeated in all 4 playlist routes
  • get_spotify_service() — replaces the 6x repeated SpotifyService(Config.SPOTIFY_CLIENT_ID, Config.SPOTIFY_CLIENT_SECRET)
  • store_token_session(token_info) — token/expiry session storage previously duplicated in callback and auth_status
  • format_track(track) / format_duration(ms) — the raw-Spotify-track → frontend dict mapping (default image + m:ss duration) previously duplicated in generate_preview and Search_Track

backend/services/spotify.py:

  • _token_request(data, error_label) — shared Basic-auth token POST used by exchange_code_for_token and refresh_token
  • _search_request(token, query, limit, timeout) — shared search GET used by both the strict and relaxed passes of search_track (a RATE_LIMITED sentinel preserves the original 429 short-circuit vs. fall-through-on-error behavior)
  • New search_tracks() and get_user_playlists() methods so Search_Track and Get_Playlists routes call the service instead of inlining raw requests calls — this also fixes a latent NameError: playlist.py used requests without importing it, so both routes would crash at runtime

Frontend (UI_Front/src/lib/playlist.ts):

  • generatePlaylistName and calculateDuration were duplicated verbatim in SpotifyPlaylistGenerator.tsx and PreferenceInput.tsx; they now live in the shared module (the copies in SpotifyPlaylistGenerator were dead code and are simply removed)

.gitignore fix: lib//lib/ (and lib64/). The unanchored Python-venv pattern was ignoring UI_Front/src/lib/, which is why src/lib/api.ts / src/lib/utils.ts (imported throughout the frontend) were never committed. Note: those files still need to be committed from a machine that has them — tsc currently fails with TS2307 on @/lib/api / @/lib/utils on main and this PR alike.

Verification

  • python -c "from backend.app import create_app" — app builds; all routes register unchanged
  • pyflakes backend/ — no new warnings (remaining unused-import warnings are preexisting)
  • eslint on touched frontend files — no new errors; tsc errors are all preexisting TS2307 missing @/lib/api/@/lib/utils

Link to Devin session: https://app.devin.ai/sessions/c11c29e6d62940faa80941b4e3b536bc
Requested by: @praneels2005

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration

Copy link
Copy Markdown
Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment, CI, and merge conflict monitoring

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant