Documentation ¶
Overview ¶
Package trafikinfo provides the necessary primitives to interact with the Trafikinfo API. It contains a query builder that can be used to build up a Request object. You can then encoding/xml.Marshal it yourself or call Request.Build. It should be passed as the body to an HTTP request. You can encoding/xml.Unmarshal the response into a struct.
Each package in trv provides a trv.ObjectType that you can pass to NewQuery to create a Request for that object type. It also provides a Response object that you can use to decode the response, instead of having to specify your own. A package's Response type can only be used if all Query in a Request are for the same trv.ObjectType. For other cases you'll need to craft a response struct yourself.
Requests should be done against the v2 Endpoint. Earlier versions of the API are deprecated by Trafikverket and not supported in this library. All requests must be POST requests, for example using net/http.NewRequestWithContext together with net/http.MethodPost.
A number of endpoints return IconID elements, either at the root of the object, or in one of its embedded objects. Each of these IconID strings can be plugged into a query for the code.dny.dev/trafikinfo/trv/icon/v1dot1.Icon resource. It in turn will give you access to a base64 encoded PNG in the response, as well as a URL at which the icon can be retrieved.
When using certain filters, for example Equal, querying for an attribute with a value that doesn't exist will not result in an error by the API. Instead you'll get an empty response.
When creating a query, a Change ID of 0 is automatically included, unless explicitly set to something else with Query.ChangeID. The query also always requests for a trv.LastModified to be returned in the response.
Package design ¶
Much of the code in this library is automatically generated using the XSD schemas provided by Trafikverket. This results in some pointer-heavy structs in code.dny.dev/trafikinfo/internal/trv as all fields are effectively optional.
In order to make it easier to drill down into nested structs, the public structs in trv expose all fields as methods instead which perform nil-checks themselves when returning nested values. In the case of nested structs, they'll instead return a pointer to an empty struct. This results in a fluent-style API, letting you write something like:
Observation().Aggregated5minutes().Precipitation().TotalWaterEquivalent().Value()
You will get a value back which may be nil, no matter if Observation, Aggregated5minutes, Precipitation and TotalWaterEquivalent elements where present in the response or not.
When a method returns a slice, that slice may be nil or empty. You can always safely iterate over those using a for loop, or check the length of the returned slice. A nil slice always has a length of 0.
XML to Go type mapping ¶
The data types are mapped to their closest representation in Go:
- xs:long and :integer are mapped to int64 (to ensure they still decode correctly on 32-bit systems)
- xs:int to int (as int is always at least 32 bits)
- xs:unsignedByte to uint8
- xs:dateTime to time.Time
- xs:float, :double and :decimal to float64 (for xs:float float32 is in theory enough but this avoids needing to deal with the extra type, whereas xs:decimal could in theory exceed a float64 but this never happens with the API in practice as it's only used for sensor values)
Data License ¶
The data returned by the Trafikinfo API is licensed under Creative Commons CC0:
Index ¶
- Constants
- type Evaluation
- type Filter
- func And(filters ...Filter) Filter
- func ElementMatch(filters ...Filter) Filter
- func Equal(name, value string) Filter
- func Exists(name string, exists bool) Filter
- func GreaterThan(name, value string) Filter
- func GreaterThanOrEqual(name, value string) Filter
- func In(name, value string) Filter
- func Intersects(name, value string) Filter
- func LessThan(name, value string) Filter
- func LessThanOrEqual(name, value string) Filter
- func Like(name, value string) Filter
- func Near(name, value string, minDistance, maxDistance int) Filter
- func Not(filters ...Filter) Filter
- func NotEqual(name, value string) Filter
- func NotIn(name, value string) Filter
- func NotLike(name, value string) Filter
- func Or(filters ...Filter) Filter
- func Within(name, value, shape string, radius float64) Filter
- type Query
- func (q Query) ChangeID(opt string) Query
- func (q Query) Distinct(field string) Query
- func (q Query) Eval(evaluations ...Evaluation) Query
- func (q Query) Exclude(fields ...string) Query
- func (q Query) Filter(filters ...Filter) Query
- func (q Query) ID(opt string) Query
- func (q Query) Include(fields ...string) Query
- func (q Query) IncludeDeletedObjects(opt bool) Query
- func (q Query) Limit(opt int) Query
- func (q Query) MarshalXML(e *xml.Encoder, start xml.StartElement) error
- func (q Query) OrderBy(opt string) Query
- func (q Query) SSEURL(opt bool) Query
- func (q Query) Skip(opt int) Query
- type Request
Constants ¶
const (
Endpoint = "https://api.trafikinfo.trafikverket.se/v2/data.xml"
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Evaluation ¶ added in v0.3.0
type Evaluation struct {
// contains filtered or unexported fields
}
func Eval ¶ added in v0.3.0
func Eval(alias, function string) Evaluation
func (Evaluation) MarshalXML ¶ added in v0.3.0
func (v Evaluation) MarshalXML(e *xml.Encoder, start xml.StartElement) error
type Filter ¶
type Filter struct {
// contains filtered or unexported fields
}
Filter represents a filter element in the query.
func And ¶
And builds an AND filter
This filter should be called with at least 2 filters. With only one filter the AND doesn't make much sense and is equivalent to adding the child filter on the Query directly. Despite this, an AND filter with only one child is still considered valid by the API.
func ElementMatch ¶
ElementMatch builds an ELEMENTMATCH filter
This filter should be called with at least 2 filters. Using any less than 2 filters will result in the API returning an error.
func GreaterThan ¶
GreaterThan filters by whether the field specified by name is greater than the specified value
func GreaterThanOrEqual ¶
GreaterThanOrEqual filters by whether the field specified by name is greater than or equal to the specified value
func Intersects ¶
Intersects filters by if te field specified in name intersects with the coordinates provided in value
func LessThan ¶
LessThan filters by whether the field specified by name is less than the specified value
func LessThanOrEqual ¶
LessThanOrEqual filters by whether the field specified by name is less than or equal to the specified value
func Near ¶
Near filters by if the field specified in name is within the specified min/max dinstance from the point coordinates sepcified in value
func Or ¶
Or builds an OR filter
This filter should be called with at least 2 filters. With only one filter the OR doesn't make much sense and is equivalent to adding the child filter on the Query directly. Despite this, an OR filter with only one child is still considered valid by the API.
func Within ¶
Within filters by if the field specified in name falls within the specified shape, radius and the coordinates in value
func (Filter) MarshalXML ¶
type Query ¶
type Query struct {
// contains filtered or unexported fields
}
Query is used to request information from the Trafikinfo API
func NewQuery ¶
func NewQuery(obj trv.ObjectType) Query
NewQuery returns a query that other methods can be chained on to further customise the request.
func (Query) ChangeID ¶ added in v0.1.1
ChangeID sets the change ID for the request. This should initially be 0 to request all data, and then be set to the value of the change ID in the response to only get updated/deleted objects since the previous change ID.
func (Query) Eval ¶ added in v0.3.0
func (q Query) Eval(evaluations ...Evaluation) Query
func (Query) ID ¶ added in v0.1.1
ID is an arbitrary value which will be echoed in the response. It can be used to associate queries with responses, especially when a request includes multiple queries.
func (Query) IncludeDeletedObjects ¶ added in v0.1.1
IncludeDeletedObjects requests that deleted objects also be returned. This defaults to false.
func (Query) Limit ¶ added in v0.1.1
Limit sets the limit for the amount of items to be returned. This can be used together with Skip to implement pagination. A Limit of 0 means no limit at all, i.e return everything.
func (Query) MarshalXML ¶
type Request ¶
type Request struct { XMLName string `xml:"REQUEST"` Login *login `xml:"LOGIN"` Queries []Query `xml:"QUERY"` }
Request tells the API what we're interested in
It must include the Login information and at least one Query.
func NewRequest ¶
func NewRequest() *Request
NewRequest returns a Request using the specified API authentication key and the data to be retrieved and filtered by the specified queries. At least 1 query needs to be provided.
func (*Request) Build ¶
Build returns the XML encoded request as an array of bytes. It can be passed as http.NewRequest's body by wrapping it in a call to bytes.NewBuffer().
The Build() method is final when used in a fluent API style, you can't chain additional methods on it that continue to modify the request.
Directories ¶
Path | Synopsis |
---|---|
cmd
|
|
trafikgen
This is a helper CLI to download all the schemas from Trafikverket, uncompress them and then generate the code.
|
This is a helper CLI to download all the schemas from Trafikverket, uncompress them and then generate the code. |
examples
|
|
client
An example on how to use the trafikinfo library to request weather data.
|
An example on how to use the trafikinfo library to request weather data. |
internal
|
|
meta
Package meta contains some metadata information about the schemas and associated helpers to extract that information.
|
Package meta contains some metadata information about the schemas and associated helpers to extract that information. |
tree
Package tree contains a bunch of awful code that transforms the XSD into an internal representation that we can then template into Go code.
|
Package tree contains a bunch of awful code that transforms the XSD into an internal representation that we can then template into Go code. |
trv
Package trv contains all the automatically generated pointer-structs from the Trafikverket XSDs.
|
Package trv contains all the automatically generated pointer-structs from the Trafikverket XSDs. |
trv/camera/v1
Package v1 contains the type definitions for Camera v1.
|
Package v1 contains the type definitions for Camera v1. |
trv/ferryannouncement/v1dot2
Package v1dot2 contains the type definitions for FerryAnnouncement v1.2.
|
Package v1dot2 contains the type definitions for FerryAnnouncement v1.2. |
trv/ferryroute/v1dot2
Package v1dot2 contains the type definitions for FerryRoute v1.2.
|
Package v1dot2 contains the type definitions for FerryRoute v1.2. |
trv/icon/v1dot1
Package v1dot1 contains the type definitions for Icon v1.1.
|
Package v1dot1 contains the type definitions for Icon v1.1. |
trv/measurementdata100/v1
Package v1 contains the type definitions for MeasurementData100 v1.
|
Package v1 contains the type definitions for MeasurementData100 v1. |
trv/measurementdata20/v1
Package v1 contains the type definitions for MeasurementData20 v1.
|
Package v1 contains the type definitions for MeasurementData20 v1. |
trv/parking/v1dot4
Package v1dot4 contains the type definitions for Parking v1.4.
|
Package v1dot4 contains the type definitions for Parking v1.4. |
trv/pavementdata/v1
Package v1 contains the type definitions for PavementData v1.
|
Package v1 contains the type definitions for PavementData v1. |
trv/railcrossing/v1dot5
Package v1dot5 contains the type definitions for RailCrossing v1.5.
|
Package v1dot5 contains the type definitions for RailCrossing v1.5. |
trv/reasoncode/v1
Package v1 contains the type definitions for ReasonCode v1.
|
Package v1 contains the type definitions for ReasonCode v1. |
trv/roadcondition/v1dot2
Package v1dot2 contains the type definitions for RoadCondition v1.2.
|
Package v1dot2 contains the type definitions for RoadCondition v1.2. |
trv/roaddata/v1
Package v1 contains the type definitions for RoadData v1.
|
Package v1 contains the type definitions for RoadData v1. |
trv/roadgeometry/v1
Package v1 contains the type definitions for RoadGeometry v1.
|
Package v1 contains the type definitions for RoadGeometry v1. |
trv/situation/v1dot5
Package v1dot5 contains the type definitions for Situation v1.5.
|
Package v1dot5 contains the type definitions for Situation v1.5. |
trv/trafficflow/v1dot4
Package v1dot4 contains the type definitions for TrafficFlow v1.4.
|
Package v1dot4 contains the type definitions for TrafficFlow v1.4. |
trv/trafficsafetycamera/v1
Package v1 contains the type definitions for TrafficSafetyCamera v1.
|
Package v1 contains the type definitions for TrafficSafetyCamera v1. |
trv/trainannouncement/v1dot9
Package v1dot9 contains the type definitions for TrainAnnouncement v1.9.
|
Package v1dot9 contains the type definitions for TrainAnnouncement v1.9. |
trv/trainmessage/v1dot7
Package v1dot7 contains the type definitions for TrainMessage v1.7.
|
Package v1dot7 contains the type definitions for TrainMessage v1.7. |
trv/trainposition/v1dot1
Package v1dot1 contains the type definitions for TrainPosition v1.1.
|
Package v1dot1 contains the type definitions for TrainPosition v1.1. |
trv/trainstation/v1dot4
Package v1dot4 contains the type definitions for TrainStation v1.4.
|
Package v1dot4 contains the type definitions for TrainStation v1.4. |
trv/trainstationmessage/v1
Package v1 contains the type definitions for TrainStationMessage v1.
|
Package v1 contains the type definitions for TrainStationMessage v1. |
trv/traveltimeroute/v1dot5
Package v1dot5 contains the type definitions for TravelTimeRoute v1.5.
|
Package v1dot5 contains the type definitions for TravelTimeRoute v1.5. |
trv/weathermeasurepoint/v2
Package v2 contains the type definitions for WeatherMeasurepoint v2.
|
Package v2 contains the type definitions for WeatherMeasurepoint v2. |
trv/weatherobservation/v2
Package v2 contains the type definitions for WeatherObservation v2.
|
Package v2 contains the type definitions for WeatherObservation v2. |
xsd
Package xsd parses an XML Schema Definition.
|
Package xsd parses an XML Schema Definition. |
camera/v1
Package v1 contains the type definitions for Camera v1.
|
Package v1 contains the type definitions for Camera v1. |
ferryannouncement/v1dot2
Package v1dot2 contains the type definitions for FerryAnnouncement v1.2.
|
Package v1dot2 contains the type definitions for FerryAnnouncement v1.2. |
ferryroute/v1dot2
Package v1dot2 contains the type definitions for FerryRoute v1.2.
|
Package v1dot2 contains the type definitions for FerryRoute v1.2. |
icon/v1dot1
Package v1dot1 contains the type definitions for Icon v1.1.
|
Package v1dot1 contains the type definitions for Icon v1.1. |
measurementdata100/v1
Package v1 contains the type definitions for MeasurementData100 v1.
|
Package v1 contains the type definitions for MeasurementData100 v1. |
measurementdata20/v1
Package v1 contains the type definitions for MeasurementData20 v1.
|
Package v1 contains the type definitions for MeasurementData20 v1. |
parking/v1dot4
Package v1dot4 contains the type definitions for Parking v1.4.
|
Package v1dot4 contains the type definitions for Parking v1.4. |
pavementdata/v1
Package v1 contains the type definitions for PavementData v1.
|
Package v1 contains the type definitions for PavementData v1. |
railcrossing/v1dot5
Package v1dot5 contains the type definitions for RailCrossing v1.5.
|
Package v1dot5 contains the type definitions for RailCrossing v1.5. |
reasoncode/v1
Package v1 contains the type definitions for ReasonCode v1.
|
Package v1 contains the type definitions for ReasonCode v1. |
roadcondition/v1dot2
Package v1dot2 contains the type definitions for RoadCondition v1.2.
|
Package v1dot2 contains the type definitions for RoadCondition v1.2. |
roaddata/v1
Package v1 contains the type definitions for RoadData v1.
|
Package v1 contains the type definitions for RoadData v1. |
roadgeometry/v1
Package v1 contains the type definitions for RoadGeometry v1.
|
Package v1 contains the type definitions for RoadGeometry v1. |
situation/v1dot5
Package v1dot5 contains the type definitions for Situation v1.5.
|
Package v1dot5 contains the type definitions for Situation v1.5. |
trafficflow/v1dot4
Package v1dot4 contains the type definitions for TrafficFlow v1.4.
|
Package v1dot4 contains the type definitions for TrafficFlow v1.4. |
trafficsafetycamera/v1
Package v1 contains the type definitions for TrafficSafetyCamera v1.
|
Package v1 contains the type definitions for TrafficSafetyCamera v1. |
trainannouncement/v1dot9
Package v1dot9 contains the type definitions for TrainAnnouncement v1.9.
|
Package v1dot9 contains the type definitions for TrainAnnouncement v1.9. |
trainmessage/v1dot7
Package v1dot7 contains the type definitions for TrainMessage v1.7.
|
Package v1dot7 contains the type definitions for TrainMessage v1.7. |
trainposition/v1dot1
Package v1dot1 contains the type definitions for TrainPosition v1.1.
|
Package v1dot1 contains the type definitions for TrainPosition v1.1. |
trainstation/v1dot4
Package v1dot4 contains the type definitions for TrainStation v1.4.
|
Package v1dot4 contains the type definitions for TrainStation v1.4. |
trainstationmessage/v1
Package v1 contains the type definitions for TrainStationMessage v1.
|
Package v1 contains the type definitions for TrainStationMessage v1. |
traveltimeroute/v1dot5
Package v1dot5 contains the type definitions for TravelTimeRoute v1.5.
|
Package v1dot5 contains the type definitions for TravelTimeRoute v1.5. |
weathermeasurepoint/v2
Package v2 contains the type definitions for WeatherMeasurepoint v2.
|
Package v2 contains the type definitions for WeatherMeasurepoint v2. |
weatherobservation/v2
Package v2 contains the type definitions for WeatherObservation v2.
|
Package v2 contains the type definitions for WeatherObservation v2. |