Documentation ¶
Overview ¶
Package rail provides a client for using RailwayAPI.com's API.
You can read the API server documentation at https://railwayapi.com/api
Usage ¶
Construct a new rail client, then use the various methods on the client to access different parts of the RailwayAPI.
For demonstration:
package main import ( "context" "github.com/go-india/rail" ) var ctx = context.Background() func main() { client := rail.NewClient(API_KEY) // Gets PNR status details. pnr, err := client.PNRStatus(ctx, 2124289856) // Gets Live running status of Train. live, err := client.LiveTrainStatus(ctx, 14311, time.Now()) // Gets fares of train. fare, err := client.TrainFare(ctx, 14311, "BE", "ADI", 24, "SL", "GN", time.Now()) }
Notes:
* Using the https://godoc.org/context package for passing context.
* Look at tests(*_test.go) files for more sample usage.
Authentication ¶
If you are using concrete Client, then you need to assign client.Auth field to make the client methods use authenticator for requests.
client := rail.Client{ Auth: rail.NewAuth(API_KEY), }
This will add API Key to each request made by client methods.
Index ¶
- Constants
- Variables
- func NewAuth(APIKey string) func(Requester) Requester
- type Available
- type CancelledTrainsReq
- type CancelledTrainsResp
- type CheckSeatReq
- type CheckSeatResp
- type Class
- type Client
- func (c Client) CancelledTrains(ctx context.Context, Date time.Time) (CancelledTrainsResp, error)
- func (c Client) CheckSeat(ctx context.Context, TrainNumber uint32, FromStationCode string, ...) (CheckSeatResp, error)
- func (c Client) Do(r Requester, intoPtr interface{}) error
- func (c Client) LiveTrainStatus(ctx context.Context, TrainNumber uint32, Date time.Time) (LiveTrainStatusResp, error)
- func (c Client) PNRStatus(ctx context.Context, PNRNumber uint64) (PNRStatusResp, error)
- func (c Client) RescheduledTrains(ctx context.Context, Date time.Time) (RescheduledTrainsResp, error)
- func (c Client) StationCodeToName(ctx context.Context, StationCode string) (Stations, error)
- func (c Client) StationNameToCode(ctx context.Context, StationName string) (Stations, error)
- func (c Client) SuggestStation(ctx context.Context, StationName string) (Stations, error)
- func (c Client) SuggestTrainByCode(ctx context.Context, TrainCode uint32) (Trains, error)
- func (c Client) SuggestTrainByName(ctx context.Context, TrainName string) (Trains, error)
- func (c Client) TrainArrivals(ctx context.Context, StationCode string, Hours WindowHour) (TrainArrivalsResp, error)
- func (c Client) TrainBetweenStations(ctx context.Context, FromStationCode string, ToStationCode string, ...) (TrainBetweenStationsResp, error)
- func (c Client) TrainByName(ctx context.Context, TrainName string) (TrainResp, error)
- func (c Client) TrainByNumber(ctx context.Context, TrainNumber uint32) (TrainResp, error)
- func (c Client) TrainFare(ctx context.Context, TrainNumber uint32, FromStationCode string, ...) (TrainFareResp, error)
- func (c Client) TrainRoute(ctx context.Context, TrainNumber uint32) (TrainRouteResp, error)
- type Day
- type ErrAPI
- type ExtendedTrain
- type LiveTrainStatusReq
- type LiveTrainStatusResp
- type PNRStatusReq
- type PNRStatusResp
- type Passenger
- type Quota
- type Requester
- type RequesterFunc
- type RescheduledTrain
- type RescheduledTrainsReq
- type RescheduledTrainsResp
- type Response
- type Route
- type Station
- type StationCodeToNameReq
- type StationNameToCodeReq
- type Stations
- type SuggestStationReq
- type SuggestTrainByCodeReq
- type SuggestTrainByNameReq
- type Train
- type TrainArrivalsReq
- type TrainArrivalsResp
- type TrainBetweenStationsReq
- type TrainBetweenStationsResp
- type TrainByNameReq
- type TrainByNumberReq
- type TrainFareReq
- type TrainFareResp
- type TrainResp
- type TrainRouteReq
- type TrainRouteResp
- type TrainSemi
- type TrainWithTimings
- type Trains
- type WindowHour
Constants ¶
const ( // DefaultBaseURL is the default base server URL of the API. DefaultBaseURL = "https://api.railwayapi.com" // DefaultUserAgent is the default user agent used by client. DefaultUserAgent = "go-india/rail" )
const ( // WindowHour2 refers to window hour 2 of indian railway. WindowHour2 = 1 + iota // WindowHour4 refers to window hour 4 of indian railway. WindowHour4 )
Variables ¶
var ( // ErrNoAuth is returned when auth is not defined for a client. ErrNoAuth = errors.New("rail: no authenticator in client") )
Functions ¶
Types ¶
type Available ¶
Available holds an available item
func (*Available) UnmarshalJSON ¶
UnmarshalJSON convert JSON data to struct
type CancelledTrainsReq ¶
type CancelledTrainsReq struct { // Specifies the date for which result is required. Date time.Time `validate:"required"` }
CancelledTrainsReq parameters
type CancelledTrainsResp ¶
type CancelledTrainsResp struct { Trains []TrainSemi `json:"trains,omitempty"` Total *int `json:"total,omitempty"` *Response }
CancelledTrainsResp holds cancelled trains details
type CheckSeatReq ¶
type CheckSeatReq struct { // Specifies the train number. TrainNumber uint32 `validate:"required"` // Specifies the source station code. FromStationCode string `validate:"required"` // Specifies the destination station code. ToStationCode string `validate:"required"` // Specifies the date for which result is required. Date time.Time `validate:"required"` // Specifies the class code. Ex: SL/AC/2S Class string `validate:"required"` // Specifies the quota code. Ex: GN etc Quota string `validate:"required"` }
CheckSeatReq parameters
type CheckSeatResp ¶
type CheckSeatResp struct { Train *Train `json:"train,omitempty"` FromStation *Station `json:"from_station,omitempty"` ToStation *Station `json:"to_station,omitempty"` Quota *Quota `json:"quota,omitempty"` JourneyClass *Class `json:"journey_class,omitempty"` Availability []Available `json:"availability,omitempty"` *Response }
CheckSeatResp holds seat availability response
type Class ¶
type Class struct { Available *bool // `json:"available,omitempty"` Name string `json:"name,omitempty"` Code string `json:"code,omitempty"` }
Class holds class details
func (*Class) UnmarshalJSON ¶
UnmarshalJSON convert JSON data to struct
type Client ¶
type Client struct { // BaseURL is the base URL of the API server. BaseURL *url.URL // User agent used when communicating with the API. UserAgent string // HTTPClient is a reusable http client instance. HTTPClient *http.Client // Auth holds authenticator function used to authenticate requests. // // Client methods uses Auth to add APIKey to requests. // Use NewAuth(apikey) to generate a new authenticator. Auth func(Requester) Requester }
Client is an railwayapi's HTTP REST API client instance.
Its zero value is usable client that uses http.DefaultTransport. Client is safe for use by multiple go routines.
func NewClient ¶
NewClient returns a new RailwayAPI.com authenticated API client.
Use returned client's methods to access various API functions.
func (Client) CancelledTrains ¶
CancelledTrains gets list of all cancelled trains on a particular day.
func (Client) CheckSeat ¶
func (c Client) CheckSeat(ctx context.Context, TrainNumber uint32, FromStationCode string, ToStationCode string, Class string, Quota string, Date time.Time, ) (CheckSeatResp, error)
CheckSeat gets train seat availability.
func (Client) LiveTrainStatus ¶
func (c Client) LiveTrainStatus(ctx context.Context, TrainNumber uint32, Date time.Time, ) (LiveTrainStatusResp, error)
LiveTrainStatus gets live running status of a Train.
func (Client) RescheduledTrains ¶
func (c Client) RescheduledTrains(ctx context.Context, Date time.Time) (RescheduledTrainsResp, error)
RescheduledTrains gets list of all rescheduled trains on a particular date.
func (Client) StationCodeToName ¶
StationCodeToName gets station details of the given station and its nearby stations using partial station name. Station’s name is autocompleted.
func (Client) StationNameToCode ¶
StationNameToCode gets station details of the given station and its nearby stations using partial station name. Station’s name is autocompleted.
func (Client) SuggestStation ¶
SuggestStation suggests full station names given a partial station name.
func (Client) SuggestTrainByCode ¶
SuggestTrainByCode suggests full train names or numbers given a partial train code.
func (Client) SuggestTrainByName ¶
SuggestTrainByName suggests full train names or numbers given a partial train name.
func (Client) TrainArrivals ¶
func (c Client) TrainArrivals(ctx context.Context, StationCode string, Hours WindowHour, ) (TrainArrivalsResp, error)
TrainArrivals get list of trains arriving at a station within a window period along with their live status.
Window time in hours to search, valid values are 2 or 4.
func (Client) TrainBetweenStations ¶
func (c Client) TrainBetweenStations(ctx context.Context, FromStationCode string, ToStationCode string, Date time.Time, ) (TrainBetweenStationsResp, error)
TrainBetweenStations gets trains running between stations.
func (Client) TrainByName ¶
TrainByName gets train details by its number.
func (Client) TrainByNumber ¶
TrainByNumber gets train details by its number.
func (Client) TrainFare ¶
func (c Client) TrainFare(ctx context.Context, TrainNumber uint32, FromStationCode string, ToStationCode string, Age uint8, Class string, Quota string, Date time.Time, ) (TrainFareResp, error)
TrainFare gets fares of a train.
func (Client) TrainRoute ¶
TrainRoute gets details about all the stations in the train’s route.
type Day ¶
Day holds day details
func (*Day) UnmarshalJSON ¶
UnmarshalJSON convert JSON data to struct
type ExtendedTrain ¶
type ExtendedTrain struct { *Train ToStation *Station `json:"to_station,omitempty"` FromStation *Station `json:"from_station,omitempty"` SourceDepartureTime *time.Time // `json:"src_departure_time,omitempty"` DestinationArrivalTime *time.Time // `json:"dest_arrival_time,omitempty"` TravelDuration *time.Duration // `json:"travel_time,omitempty"` }
ExtendedTrain holds extended train details
func (*ExtendedTrain) UnmarshalJSON ¶
func (et *ExtendedTrain) UnmarshalJSON(data []byte) error
UnmarshalJSON convert JSON data to struct
type LiveTrainStatusReq ¶
type LiveTrainStatusReq struct { // Specifies the train number. TrainNumber uint32 `validate:"required"` // Specifies the date for which result is required. Date time.Time `validate:"required"` }
LiveTrainStatusReq parameters
type LiveTrainStatusResp ¶
type LiveTrainStatusResp struct { Train *Train `json:"train,omitempty"` CurrentStation *Station `json:"current_station,omitempty"` Route []Route `json:"route,omitempty"` StartDate *time.Time // `json:"start_date,omitempty"` PositionRemark *string `json:"position,omitempty"` *Response }
LiveTrainStatusResp of the request
func (*LiveTrainStatusResp) UnmarshalJSON ¶
func (s *LiveTrainStatusResp) UnmarshalJSON(data []byte) error
UnmarshalJSON convert JSON data to struct
type PNRStatusReq ¶
type PNRStatusReq struct {
PNRNumber uint64 `validate:"required"` // Specifies the pnr number.
}
PNRStatusReq parameters
type PNRStatusResp ¶
type PNRStatusResp struct { ChartPrepared *bool `json:"chart_prepared,omitempty"` DateOfJourney *time.Time // `json:"doj,omitempty"` BoardingPoint *Station `json:"boarding_point,omitempty"` FromStation *Station `json:"from_station,omitempty"` ToStation *Station `json:"to_station,omitempty"` TotalPassengers *int `json:"total_passengers,omitempty"` JourneyClass *Class `json:"journey_class,omitempty"` Train *Train `json:"train,omitempty"` Passengers []Passenger `json:"passengers,omitempty"` PNR *uint64 `json:"pnr,string,omitempty"` ReservationUpto *Station `json:"reservation_upto,omitempty"` *Response }
PNRStatusResp is the response for a PNRReq
func (*PNRStatusResp) UnmarshalJSON ¶
func (p *PNRStatusResp) UnmarshalJSON(data []byte) error
UnmarshalJSON convert JSON data to struct
type Passenger ¶
type Passenger struct { Number *uint16 `json:"no,omitempty"` CurrentStatus *string `json:"current_status,omitempty"` BookingStatus *string `json:"booking_status,omitempty"` }
Passenger holds passenger details
type Requester ¶
type Requester interface { // Request should generate an HTTP request from parameters. Request() (*http.Request, error) }
Requester is implemented by any value that has a Request method.
type RequesterFunc ¶
RequesterFunc implements Requester
type RescheduledTrain ¶
type RescheduledTrain struct { FromStation *Station `json:"from_station,omitempty"` ToStation *Station `json:"to_station,omitempty"` TimeDifference *time.Duration // `json:"time_diff,omitempty"` RescheduledDate *time.Time // `json:"rescheduled_date,omitempty"` RescheduledTime *time.Time // `json:"rescheduled_time,omitempty"` *Train }
RescheduledTrain holds rescheduled train detail
func (*RescheduledTrain) UnmarshalJSON ¶
func (s *RescheduledTrain) UnmarshalJSON(data []byte) error
UnmarshalJSON convert JSON data to struct
type RescheduledTrainsReq ¶
type RescheduledTrainsReq struct { // Specifies the date for which result is required. Date time.Time `validate:"required"` }
RescheduledTrainsReq parameters
type RescheduledTrainsResp ¶
type RescheduledTrainsResp struct { Trains []RescheduledTrain `json:"trains,omitempty"` *Response }
RescheduledTrainsResp holds rescheduled trains
type Response ¶
type Response struct { // The debit key is included in each API response and it contains // the value by which the user’s credit was debited. Whether the user will // be debited or not depends upon the ResponseCode. Debit int `json:"debit"` // ResponseCode key included in each response contains the status // of the result returned. ResponseCode int `json:"response_code"` }
Response is the standard response field that comes with every response from API.
Refer to following URL for more details. https://railwayapi.com/api/
type Route ¶
type Route struct { ActualArrivalDate *time.Time //`json:"actarr_date,omitempty"` ScheduledArrivalDate *time.Time //`json:"scharr_date,omitempty"` ScheduledArrivalTime *time.Time //`json:"scharr,omitempty"` ScheduledDepartureTime *time.Time //`json:"schdep,omitempty"` ActualDepartureTime *time.Time //`json:"actdep,omitempty"` ActualArrivalTime *time.Time //`json:"actarr,omitempty"` HasArrived *bool `json:"has_arrived,omitempty"` HasDeparted *bool `json:"has_departed,omitempty"` Station *Station `json:"station,omitempty"` Status *string `json:"status,omitempty"` LateByMinutes *int `json:"latemin,omitempty"` Distance *float64 `json:"distance,omitempty"` Day *int `json:"day,omitempty"` Number *int `json:"no,omitempty"` Halt *int `json:"halt,omitempty"` }
Route holds route details
func (*Route) UnmarshalJSON ¶
UnmarshalJSON convert JSON data to struct
type Station ¶
type Station struct { Latitude float64 `json:"lat"` Longitude float64 `json:"lng"` Name string `json:"name"` Code string `json:"code"` }
Station holds station details
type StationCodeToNameReq ¶
type StationCodeToNameReq struct { // Specifies the source station code. StationCode string `validate:"required"` }
StationCodeToNameReq parameters
type StationNameToCodeReq ¶
type StationNameToCodeReq struct { // Specifies the source station name. StationName string `validate:"required"` }
StationNameToCodeReq parameters
type SuggestStationReq ¶
type SuggestStationReq struct { // Specifies the source station name. StationName string `validate:"required"` }
SuggestStationReq parameters
type SuggestTrainByCodeReq ¶
type SuggestTrainByCodeReq struct { // Specifies the Train code. TrainCode uint32 `validate:"required"` }
SuggestTrainByCodeReq parameters
type SuggestTrainByNameReq ¶
type SuggestTrainByNameReq struct { // Specifies the Train name. TrainName string `validate:"required"` }
SuggestTrainByNameReq parameters
type Train ¶
type Train struct { Name string `json:"name"` Number uint32 `json:"number,string"` Classes []Class `json:"classes,omitempty"` Days []Day `json:"days,omitempty"` }
Train holds train details
type TrainArrivalsReq ¶
type TrainArrivalsReq struct { // Specifies the source station code. StationCode string `validate:"required"` // Specifies the windows hours to search. // // Window time in hours to search, valid values are 2 or 4. Hours WindowHour `validate:"required"` }
TrainArrivalsReq parameters
type TrainArrivalsResp ¶
type TrainArrivalsResp struct { Trains []TrainWithTimings `json:"trains,omitempty"` Total *int `json:"total,omitempty"` *Response }
TrainArrivalsResp holds train arrivals details
type TrainBetweenStationsReq ¶
type TrainBetweenStationsReq struct { // Specifies the source station code. FromStationCode string `validate:"required"` // Specifies the destination station code. ToStationCode string `validate:"required"` // Specifies the date for which result is required. Date time.Time `validate:"required"` }
TrainBetweenStationsReq parameters
type TrainBetweenStationsResp ¶
type TrainBetweenStationsResp struct { Trains []ExtendedTrain `json:"trains,omitempty"` Total *int `json:"total,omitempty"` *Response }
TrainBetweenStationsResp holds trains between stations
type TrainByNameReq ¶
type TrainByNameReq struct {
TrainName string `validate:"required"` // Specifies the train name.
}
TrainByNameReq parameters
type TrainByNumberReq ¶
type TrainByNumberReq struct {
TrainNumber uint32 `validate:"required"` // Specifies the train number.
}
TrainByNumberReq parameters
type TrainFareReq ¶
type TrainFareReq struct { // Specifies the train number. TrainNumber uint32 `validate:"required"` // Specifies the source station code. FromStationCode string `validate:"required"` // Specifies the destination station code. ToStationCode string `validate:"required"` // Specifies the age code of passenger Age uint8 `url:"age" validate:"required"` // Specifies the date for which result is required. Date time.Time `validate:"required"` // Specifies the class code. Ex: SL/AC/2S Class string `validate:"required"` // Specifies the quota code. Ex: GN etc Quota string `validate:"required"` }
TrainFareReq parameters
type TrainFareResp ¶
type TrainFareResp struct { FromStation *Station `json:"from_station,omitempty"` ToStation *Station `json:"to_station,omitempty"` Quota *Quota `json:"quota,omitempty"` Train *Train `json:"train,omitempty"` Fare *float64 `json:"fare,omitempty"` JourneyClass *Class `json:"journey_class,omitempty"` Availability []Available `json:"availability,omitempty"` *Response }
TrainFareResp holds fare details for a train journey
type TrainRouteReq ¶
type TrainRouteReq struct {
TrainNumber uint32 `validate:"required"` // Specifies the train number.
}
TrainRouteReq parameters
type TrainRouteResp ¶
type TrainRouteResp struct { Train *Train `json:"train,omitempty"` Route []Route `json:"route,omitempty"` *Response }
TrainRouteResp holds route information of a train
type TrainSemi ¶
type TrainSemi struct { Source *Station `json:"source,omitempty"` Destination *Station `json:"dest,omitempty"` Type *string `json:"type,omitempty"` StartDate *time.Time //`json:"start_time,omitempty"` *Train }
TrainSemi holds semi train information
func (*TrainSemi) UnmarshalJSON ¶
UnmarshalJSON convert JSON data to struct
type TrainWithTimings ¶
type TrainWithTimings struct { *Train DelayArrivalTime *time.Time //`json:"delayarr,omitempty"` DelayDepartureTime *time.Time //`json:"delaydep,omitempty"` ScheduledArrivalTime *time.Time //`json:"scharr,omitempty"` ScheduledDepartureTime *time.Time //`json:"schdep,omitempty"` ActualDepartureTime *time.Time //`json:"actdep,omitempty"` ActualArrivalTime *time.Time //`json:"actarr,omitempty"` }
TrainWithTimings holds train timings
func (*TrainWithTimings) UnmarshalJSON ¶
func (r *TrainWithTimings) UnmarshalJSON(data []byte) error
UnmarshalJSON convert JSON data to struct