feat(auth): Allow non-interactive Anilist authentication#175
feat(auth): Allow non-interactive Anilist authentication#175Benexl merged 5 commits intoviu-media:masterfrom
Conversation
This enhances the `anilist auth` command by allowing the authentication token to be provided directly as an argument or from a file. This provides a non-interactive way to authenticate, which is useful for scripting or for users who have issues with the interactive browser-based flow. The `auth` command now accepts an optional `token_input` argument. - If the argument is a valid file path, the token is read from the file. - Otherwise, the argument is treated as the token itself. - If no argument is provided, the command falls back to the previous interactive method. The README is also updated to document these new authentication options. This commit also includes: - `test`: Unit tests for the new authentication logic (``test_auth.py``) and for the mapper fix (``test_mapper.py``). - `fix(api)`: A fix in the Anilist API mapper to handle potentially missing data in the API response, making it more robust. - `style`: Minor code style and formatting fixes throughout the codebase.
There was a problem hiding this comment.
Pull request overview
This PR enhances the anilist auth command to support non-interactive authentication by allowing tokens to be provided directly as arguments or from files, useful for scripting and automation. The changes also include a robustness fix for the AniList API mapper and various code style improvements.
- Added non-interactive authentication support with token from argument or file
- Fixed potential null reference issues in AniList API response mapper
- Improved code formatting consistency across provider files
Reviewed changes
Copilot reviewed 8 out of 12 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
| viu_media/cli/commands/anilist/commands/auth.py | Enhanced authentication command to accept token as argument or file path, refactored token retrieval into separate function |
| viu_media/libs/media_api/anilist/mapper.py | Added defensive null checks for data and Viewer fields to handle missing API response data |
| tests/cli/commands/anilist/commands/test_auth.py | Added comprehensive unit tests for new authentication methods (direct token, file-based, empty file scenarios) |
| tests/libs/media_api/anilist/test_mapper.py | Added tests for mapper edge cases including missing data and viewer keys |
| viu_media/libs/provider/anime/animepahe/provider.py | Minor formatting fix for trailing comma in headers dictionary |
| viu_media/libs/provider/anime/animepahe/mappers.py | Reformatted multi-line Server return statement |
| viu_media/libs/provider/anime/animepahe/constants.py | Updated string concatenation to use double quotes consistently |
| viu_media/cli/cli.py | Fixed spacing around assignment operator |
| README.md | Updated authentication documentation to mention new token input options |
| tests/cli/commands/anilist/init.py | Added empty init file for test package structure |
| tests/libs/init.py | Added empty init file for test package structure |
| tests/libs/media_api/init.py | Added empty init file for test package structure |
| tests/libs/media_api/anilist/init.py | Added empty init file for test package structure |
| except Exception as e: | ||
| feedback.error(f"Error reading token from file: {e}") | ||
| return None |
There was a problem hiding this comment.
The broad exception handler catches all exceptions, which could mask unexpected errors. Consider catching specific exceptions (e.g., PermissionError, OSError, UnicodeDecodeError) to provide more helpful error messages to users.
| except Exception as e: | |
| feedback.error(f"Error reading token from file: {e}") | |
| return None | |
| except (OSError, PermissionError) as e: | |
| feedback.error(f"Error reading token from file '{path}': {e}") | |
| return None | |
| except UnicodeDecodeError as e: | |
| feedback.error(f"Token file '{path}' is not valid text: {e}") | |
| return None |
| except Exception as e: | ||
| feedback.error(f"Error reading token from file: {e}") | ||
| return None |
There was a problem hiding this comment.
Missing test coverage for the file read error path. Consider adding a test case that simulates a file read exception (e.g., permission error) to ensure the error handling works correctly.
There was a problem hiding this comment.
Should be implemented. Not going to be implemented by me though
| if not token: | ||
| feedback.error("Login cancelled.") | ||
| if not token_input: | ||
| feedback.error("Login cancelled.") | ||
| return |
There was a problem hiding this comment.
Missing test coverage for the case when a user cancels the interactive token prompt (returns None or empty string). Consider adding a test where selector.ask returns None to ensure the "Login cancelled." message is shown correctly.
|
yoh @komposer-aml just fix the pyright errors and will be good to go |
Updated mock data in `test_to_generic_user_profile_success` to conform to `AnilistViewerData` requirements. Adjusted type annotations in tests with intentionally malformed data to `Any` to prevent pyright errors, ensuring proper validation of error handling.
1b7fc69 to
efa6f4d
Compare
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>


This enhances the
anilist authcommand by allowing the authentication token to be provided directly as an argument or from a file. This provides a non-interactive way to authenticate, which is useful for scripting or for users who have issues with the interactive browser-based flow.The
authcommand now accepts an optionaltoken_inputargument.The README is also updated to document these new authentication options.
This commit also includes:
test: Unit tests for the new authentication logic (test_auth.py) and for the mapper fix (test_mapper.py).fix(api): A fix in the Anilist API mapper to handle potentially missing data in the API response, making it more robust.style: Minor code style and formatting fixes throughout the codebase.