How sanketsaurav/pnrapi works
1. Repository shape
- This is a small, single-purpose Python API service for Indian Railways PNR status lookup.
- There is no multi-service backend, no database, and no frontend. The architecture is basically one HTTP entrypoint plus a few support files.
2. Public API boundary
- The service exposes one main route: GET /pnr/<pnr>.
- The input is a 10-digit PNR number.
- The output is a JSON payload with a top-level status field and a data object containing train, route, and passenger status details.
3. Request handling layer
- api.py is the application entrypoint and likely contains the web route/controller logic.
- Its job is to accept the PNR request, decide whether to serve a dummy/test response or query live data, and return the final JSON response.
4. External data dependency
- The core business function depends on an upstream Indian Railways PNR information source.
- This repo acts as a proxy/adapter over that external system, normalizing whatever raw response it gets into a stable JSON contract for callers.
5. Transformation and normalization
- utils.py appears to hold helper logic for parsing, cleaning, or reshaping fetched PNR data.
- The README’s sample response suggests the service converts upstream data into a consistent structure with fields like train number, train name, travel date, boarding/alighting stations, class, and passenger status.
6. Station metadata boundary
- station_codes.json is a local lookup table for station code-to-name mapping.
- This indicates a normalization step in the response pipeline: raw station codes from upstream are resolved into human-readable station names before returning JSON.
7. Test/dummy response path
- dummy_response.json is a fixture used for testing and development.
- The README explicitly documents PNR 0123456789 as a special test input that returns this dummy payload, which means the service has a deterministic offline/fixture branch separate from live lookups.
8. Configuration
- settings.py likely contains runtime configuration such as upstream URLs, timeouts, environment-specific toggles, or constants used by the request pipeline.
- This separates deploy-time settings from request-handling code.
9. Deployment/runtime
- Procfile suggests the service is designed to run on a process manager / PaaS-style host using a web worker command.
- requirements.txt defines the Python runtime dependencies for the API and any scraping/parsing libraries it relies on.
10. Response contract
- The API always returns a status plus a structured data payload.
- Notable result states documented in the README are:
- OK for successful lookups
- PNR FLUSHED / SERVICE UNAVAILABLE when the PNR is unavailable or upstream is down
- ERROR for unexpected fetch/parse failures
- This makes the service a stable adapter around an unreliable upstream source.
11. Data flow summary
- Client sends PNR request → api.py handles route → helper/config/lookup files enrich or normalize the request → upstream PNR source is queried or dummy_response.json is returned → response is transformed into the documented JSON schema → JSON is sent back to the client.
12. Architectural takeaway
- The repo is best diagrammed as a thin API façade over an external rail PNR source, with local support files for configuration, station metadata, and deterministic test fixtures.
Main entry points: README.md Response contract · api.py Lookup flow · utils.py Normalizer · dummy_response.json Dummy payload
Components 16
HTTP API
Public request handling and response boundary
- API entrypoint · Handles GET /pnr/<pnr> and returns the JSON response
api.py - PNR route · Validates request input and dispatches lookup flow
api.py - Response contract · Top-level status plus structured data payload
README.md - Docs · Describes API behavior, status codes, and examples
README.md - Train details · Train number, name, date, class, and route fields
README.md - Passenger status · Passenger-wise reservation and boarding/alighting details
README.md
Core logic
Lookup, normalization, and status shaping
- Lookup flow · Chooses dummy response or live upstream lookup
api.py - Rail source · Indian Railways PNR information source
- Parse utils · Cleans and reshapes raw PNR data
utils.py - Normalizer · Converts upstream fields into stable API output
utils.py - Status mapper · Produces OK, SERVICE UNAVAILABLE, and ERROR states
utils.py
Local data
Bundled fixture and station metadata
- Station codes · Maps station codes to human-readable names
station_codes.json - Dummy payload · Deterministic offline response for test PNR
dummy_response.json
Runtime
Deployment and dependency packaging
- Settings · Upstream URL, timeouts, and environment toggles
settings.py - Dependencies · Runtime and parsing libraries for the service
requirements.txt - Procfile · Process manager command for the web worker
Procfile
Connections 16
- API entrypoint → PNR route: routes · HTTP entrypoint hands requests to the PNR controller
- PNR route → Lookup flow: dispatches · Route decides whether to use fixture or live lookup
- Lookup flow → Dummy payload: special-case · PNR 0123456789 returns deterministic test data
- Lookup flow → Rail source: queries · Live requests are proxied to the external rail source
- Lookup flow → Settings: configures · Uses runtime settings for upstream and behavior toggles
- Rail source → Parse utils: raw data · Fetched upstream payload is parsed and cleaned
- Parse utils → Normalizer: shapes · Parsed values are mapped into the API contract
- Normalizer → Station codes: resolves · Station codes are expanded to human-readable names
- Normalizer → Status mapper: formats · Lookup outcome is converted into documented status values
- Status mapper → Response contract: emits · Final result conforms to the JSON response shape
- Dummy payload → Response contract: matches · Fixture follows the same response contract as live data
- Response contract → Train details: includes · Documented payload contains train and route fields
- Response contract → Passenger status: includes · Documented payload contains passenger status fields
- Docs → Response contract: documents · README describes the output schema and status codes
- Dependencies → API entrypoint: supports · Python dependencies make the service runnable
- Procfile → API entrypoint: launches · Deployment command starts the web process