api

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 22, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// BaseURL is the base URL for the bahn.de API
	BaseURL = "https://www.bahn.de/web/api"

	// EndpointDepartures returns departures at a station
	// Required params: datum, zeit, ortExtId, ortId, mitVias, maxVias, verkehrsmittel[]
	EndpointDepartures = "/reiseloesung/abfahrten"

	// EndpointArrivals returns arrivals at a station
	// Required params: datum, zeit, ortExtId, ortId, mitVias, maxVias, verkehrsmittel[]
	EndpointArrivals = "/reiseloesung/ankuenfte"

	// EndpointLocations searches for stations by name
	// Required params: suchbegriff, typ, limit
	EndpointLocations = "/reiseloesung/orte"

	// EndpointNearby searches for stations by coordinates
	// Required params: lat, long, radius, maxNo
	EndpointNearby = "/reiseloesung/orte/nearby"

	// EndpointJourney returns journey/trip details
	// Required params: journeyId, poly
	EndpointJourney = "/reiseloesung/fahrt"

	// EndpointFormation returns train carriage formation
	// Required params: administrationId, category, date, evaNumber, number, time
	EndpointFormation = "/reisebegleitung/wagenreihung/vehicle-sequence"
)

Variables

View Source
var (
	// ErrNotFound indicates the requested resource was not found
	ErrNotFound = errors.New("not found")

	// ErrInvalidRequest indicates the request parameters are invalid
	ErrInvalidRequest = errors.New("invalid request")

	// ErrServerError indicates a server-side error
	ErrServerError = errors.New("server error")

	// ErrTimeout indicates the request timed out
	ErrTimeout = errors.New("request timed out")

	// ErrNoResults indicates no results were found
	ErrNoResults = errors.New("no results found")
)

Common errors

View Source
var ModesOfTransit = []string{
	"ICE",
	"EC_IC",
	"IR",
	"REGIONAL",
	"SBAHN",
	"BUS",
	"SCHIFF",
	"UBAHN",
	"TRAM",
	"ANRUFPFLICHTIG",
}

ModesOfTransit contains all supported transport modes

Functions

func ErrInvalidFormat

func ErrInvalidFormat(field, expected string) error

func ErrInvalidValue

func ErrInvalidValue(field string, value interface{}) error

func ErrMissingField

func ErrMissingField(field string) error

Common validation errors

Types

type APIError

type APIError struct {
	StatusCode int
	Status     string
	Endpoint   string
	Message    string
}

APIError represents an error returned by the bahn.de API

func NewAPIError

func NewAPIError(statusCode int, status, endpoint string) *APIError

NewAPIError creates a new API error

func NewAPIErrorWithMessage

func NewAPIErrorWithMessage(statusCode int, endpoint, message string) *APIError

NewAPIErrorWithMessage creates a new API error with a custom message

func (*APIError) Error

func (e *APIError) Error() string

func (*APIError) Is

func (e *APIError) Is(target error) bool

Is implements errors.Is for APIError

type Cache

type Cache interface {
	Get(key string) ([]byte, bool)
	Set(key string, value []byte) error
}

Cache interface for caching HTTP responses

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is the API client for bahn.de

func NewClient

func NewClient(opts ...ClientOption) (*Client, error)

NewClient creates a new API client

func (*Client) GetArrivals

func (c *Client) GetArrivals(ctx context.Context, req StationBoardRequest) ([]models.Departure, error)

GetArrivals fetches arrivals for a station

func (*Client) GetArrivalsRaw

func (c *Client) GetArrivalsRaw(ctx context.Context, req StationBoardRequest) (json.RawMessage, error)

GetArrivalsRaw fetches arrivals and returns raw JSON

func (*Client) GetDepartures

func (c *Client) GetDepartures(ctx context.Context, req StationBoardRequest) ([]models.Departure, error)

GetDepartures fetches departures for a station

func (*Client) GetDeparturesRaw

func (c *Client) GetDeparturesRaw(ctx context.Context, req StationBoardRequest) (json.RawMessage, error)

GetDeparturesRaw fetches departures and returns raw JSON

func (*Client) GetFormation

func (c *Client) GetFormation(ctx context.Context, req FormationRequest) (*models.Formation, error)

GetFormation fetches train formation/composition data

func (*Client) GetFormationRaw

func (c *Client) GetFormationRaw(ctx context.Context, req FormationRequest) (json.RawMessage, error)

GetFormationRaw fetches train formation data and returns raw JSON

func (*Client) GetJourney

func (c *Client) GetJourney(ctx context.Context, journeyID string, withPolyline bool) (*models.Journey, error)

GetJourney fetches journey details by journey ID

func (*Client) GetJourneyRaw

func (c *Client) GetJourneyRaw(ctx context.Context, journeyID string, withPolyline bool) (json.RawMessage, error)

GetJourneyRaw fetches journey details and returns raw JSON

func (*Client) SearchLocations

func (c *Client) SearchLocations(ctx context.Context, query string) ([]models.Location, error)

SearchLocations searches for stations by name

func (*Client) SearchLocationsRaw

func (c *Client) SearchLocationsRaw(ctx context.Context, query string) (json.RawMessage, error)

SearchLocationsRaw searches for stations and returns raw JSON

func (*Client) SearchNearby

func (c *Client) SearchNearby(ctx context.Context, req NearbyRequest) ([]models.Location, error)

SearchNearby searches for stations near a location

func (*Client) SearchNearbyRaw

func (c *Client) SearchNearbyRaw(ctx context.Context, req NearbyRequest) (json.RawMessage, error)

SearchNearbyRaw searches for nearby stations and returns raw JSON

func (*Client) Timezone

func (c *Client) Timezone() *time.Location

Timezone returns the client's timezone

type ClientOption

type ClientOption func(*Client)

ClientOption configures the Client

func WithCache

func WithCache(cache Cache) ClientOption

WithCache enables caching with the provided cache implementation

func WithDefaultCache

func WithDefaultCache() ClientOption

WithDefaultCache enables caching with the default file cache

func WithHTTPClient

func WithHTTPClient(hc *http.Client) ClientOption

WithHTTPClient sets a custom HTTP client

func WithTimeout

func WithTimeout(d time.Duration) ClientOption

WithTimeout sets the HTTP client timeout

type DepartureRequest

type DepartureRequest = StationBoardRequest

DepartureRequest is an alias for StationBoardRequest for backward compatibility

type FormationRequest

type FormationRequest struct {
	EVA         int64     // Station EVA number
	TrainType   string    // Train type (e.g., "ICE")
	TrainNumber string    // Train number (e.g., "623")
	Departure   time.Time // Departure time
}

FormationRequest contains parameters for a formation query

type NearbyRequest

type NearbyRequest struct {
	Latitude  float64 // Latitude (required)
	Longitude float64 // Longitude (required)
	Radius    int     // Search radius in meters (default: 9999)
	MaxNo     int     // Maximum number of results (default: 100)
}

NearbyRequest contains parameters for a nearby search

type StationBoardRequest

type StationBoardRequest struct {
	EVA            int64     // Station EVA number (required)
	StationID      string    // Station ID (required)
	DateTime       time.Time // Query time (defaults to now)
	NumVias        int       // Number of via stations (default: 5)
	ModesOfTransit []string  // Filter by transport mode (default: all)
}

StationBoardRequest contains parameters for a departure/arrival query

type ValidationError

type ValidationError struct {
	Field   string
	Message string
}

ValidationError represents a validation error for request parameters

func NewValidationError

func NewValidationError(field, message string) *ValidationError

NewValidationError creates a new validation error

func (*ValidationError) Error

func (e *ValidationError) Error() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL