Skip to content

feat(api): JSON API via Content Negotiation#1

Merged
pandeiro merged 32 commits intomainfrom
json-api
Mar 3, 2026
Merged

feat(api): JSON API via Content Negotiation#1
pandeiro merged 32 commits intomainfrom
json-api

Conversation

@pandeiro
Copy link
Copy Markdown
Owner

@pandeiro pandeiro commented Mar 2, 2026

Summary

Implements JSON API support via content negotiation for various endpoints in Nitter. Clients can now request JSON responses by sending Accept: application/json header.

Completed Endpoints

Phase 1: Core Read-Only Endpoints

  • GET / - Home feed
  • GET /<username> - User profile
  • GET /<username>/with_replies - User replies
  • GET /<username>/media - User media

Phase 2: Discovery & Search

  • GET /search - Search results
  • GET /following - Following lists
  • GET /i/lists/<id> - List profile

Phase 3: User Content & Actions

  • GET /<username>/status/<id> - Single tweet
  • GET /pinned - Pinned tweets

Implementation Details

  • Content negotiation via Accept: application/json header
  • JSON structure mirrors embedded data in HTML pages
  • Existing HTML responses remain unchanged
  • All responses include appropriate error handling

Files Changed

  • src/routes/*.nim - Added content negotiation to route handlers
  • src/json_api.nim - Added toJson helpers for various types
  • src/nitter.nim - Fixed pre-existing build issues, added pinned route
  • tests/test_api_json.py - Added/updated tests

Testing

Run the test suite:

pytest tests/test_api_json.py --headless

Notes

  • Remaining tasks: User Lists, Follow Actions, Pin Actions, List Members
  • Track status: Phase 1-3 implementation complete, Phase 4 pending

- Add content negotiation to search.nim for Accept: application/json
- Add toJson helper for Result[User] in json_api.nim
- Add test for search JSON endpoint
- Fix pre-existing build issues with httpcore imports and string conversion
- Add content negotiation to follow.nim for GET /following
- Add toJson helper for following lists in json_api.nim
- Add test for following lists JSON endpoint
- Add content negotiation to list.nim for GET /i/lists/<id>
- Add toJson helper for List and Timeline in json_api.nim
- Add test for list profile JSON endpoint
- Add content negotiation to status.nim for GET /<username>/status/<id>
- Add toJson helper for Conversation in json_api.nim
- Add test for single tweet JSON endpoint
- Add content negotiation to GET /pinned in nitter.nim
- Add pinnedTweetsJson helper in json_api.nim
- Add test for pinned tweets JSON endpoint
- Add acceptJson() template in router_utils.nim for consistent header detection
- Replace 49 duplicate header check patterns across all route files
- Simplifies maintenance - single source of truth for Accept header logic
pandeiro added 2 commits March 2, 2026 20:10
- Remove duplicate POST /pin and POST /unpin handlers from nitter.nim
  (these are handled by the extended pinned router in routes/pinned.nim)
- Remove GET /pinned handler from nitter.nim (handled by pinned router)
- Add renderTweetHtml helper proc to json_api.nim
- Add html field to tweet JSON output using renderTweet
- Add toJson(tweet: Tweet; prefs: Prefs) overload for custom prefs
- Update existing toJson(tweet: Tweet) to include html using defaultPrefs
@pandeiro pandeiro merged commit 2fd0767 into main Mar 3, 2026
0 of 4 checks passed
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