trafikinfo

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2022 License: MIT Imports: 2 Imported by: 0

README

🚦 Trafikinfo 🦺

A Go library for the Trafikinfo API from Trafikverket

Build Status Coverage Status Go report card GoDoc

This library 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 xml.Marshal it and pass it on to your favourite HTTP client to retrieve it. The API endpoint is available through the Endpoint constant.

This library is under construction and currently lacks the struct types to decode the response into.

Usage

package main

import (
	"context"
	"fmt"
	"net/http"

	"code.dny.dev/trafikinfo"
)

func main() {
	req, err := trafikinfo.NewRequest().
		APIKey("YOUR_API_KEY").
		Query(
			trafikinfo.NewQuery(
				trafikinfo.WeatherStation,
				1.0,
			).Filter(
				trafikinfo.Equal("Id", "YOUR_STATION_ID"),
			),
		).Build()

	if err != nil {
		panic(err)
	}

	// Rest of the code here to do the request, handle non-200 error
	// responses etc.
}

More complete code can be found in the examples/ directory.

Multiple queries can be passed by either passing multiple NewQuery() into a single Query() call, or chaining .Query() multiple times on the result of NewRequest().

Calling .Filter() multiple times on a Query will replace the whole filter, as a query can only have one filter block.

Documentation

Index

Constants

View Source
const Endpoint = "https://api.trafikinfo.trafikverket.se/v2/data.json"

Endpoint is the current recommended endpoint for the Trafikinfo API

Variables

This section is empty.

Functions

This section is empty.

Types

type Filter

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

Filter represents a filter element in the query.

func And

func And(f1, f2 Filter, rest ...Filter) Filter

And builds an AND filter

func ElementMatch

func ElementMatch(f1, f2 Filter, rest ...Filter) Filter

ElementMatch builds an ELEMENTMATCH filter

func Equal

func Equal(name, value string) Filter

Equal filters by if the field provided by name matches the specified value

func Exists

func Exists(name string, exists bool) Filter

Exists filters by whether the field provided by name exists or not

func GreaterThan

func GreaterThan(name, value string) Filter

GreaterThan filters by whether the field specified by name is greater than the specified value

func GreaterThanOrEqual

func GreaterThanOrEqual(name, value string) Filter

GreaterThanOrEqual filters by whether the field specified by name is greater than or equal to the specified value

func In

func In(name, value string) Filter

In filters by if the field specified by name matches any of the provided values

func Intersects

func Intersects(name, value string) Filter

Intersects filters by if te field specified in name intersects with the coordinates provided in value

func LessThan

func LessThan(name, value string) Filter

LessThan filters by whether the field specified by name is less than the specified value

func LessThanOrEqual

func LessThanOrEqual(name, value string) Filter

LessThanOrEqual filters by whether the field specified by name is less than or equal to the specified value

func Like

func Like(name, value string) Filter

Like filters by if the field provided by name matches the regex provided by value

func Near

func Near(name, value string, minDistance, maxDistance int) Filter

Near filters by if the field specified in name is within the specified min/max dinstance from the point coordinates sepcified in value

func Not

func Not(f1, f2 Filter, rest ...Filter) Filter

Not builds an NOT filter

func NotEqual

func NotEqual(name, value string) Filter

NotEqual is the inverse of Equal

func NotIn

func NotIn(name, value string) Filter

NotIn is the inverse of In

func NotLike

func NotLike(name, value string) Filter

NotLIke is the inverse of Like

func Or

func Or(f1, f2 Filter, rest ...Filter) Filter

Or builds an OR filter

func Within

func Within(name, value, shape string, radius float64) Filter

Within filters by if the field specified in name falls within the specified shape, radius and the coordinates in value

func (*Filter) MarshalXML

func (f *Filter) MarshalXML(e *xml.Encoder, start xml.StartElement) error

type ObjectType

type ObjectType string

ObjectType is a type of object you can retrieve from the API

const (
	RailCrossing        ObjectType = "RailCrossing"
	ReasonCode          ObjectType = "ReasonCode"
	TrainAnnouncement   ObjectType = "TrainAnnouncement"
	TrainMessage        ObjectType = "TrainMessage"
	TrainStation        ObjectType = "TrainStation"
	TrainStationMessage ObjectType = "TrainStationMessage"
	TrainPosition       ObjectType = "TrainPosition"

	Camera                ObjectType = "Camera"
	FerryAnnouncement     ObjectType = "FerryAnnouncement"
	FerryRoute            ObjectType = "FerryRoute"
	Icon                  ObjectType = "Icon"
	Parking               ObjectType = "Parking"
	RoadCondition         ObjectType = "RoadCondition"
	RoadConditionOverview ObjectType = "RoadConditionOverview"
	Situation             ObjectType = "Situation"
	TrafficFlow           ObjectType = "TrafficFlow"
	TrafficSafetyCamera   ObjectType = "TrafficSafetyCamera"
	TravelTimeRoute       ObjectType = "TravelTimeRoute"
	WeatherMeasurePoint   ObjectType = "WeatherMeasurePoint"
	WeatherObservation    ObjectType = "WeatherObservation"
	WeatherStation        ObjectType = "WeatherStation"

	MeasurementData100 ObjectType = "MeasurementData100"
	MeasurementData20  ObjectType = "MeasurementData20"
	PavementData       ObjectType = "PavementData"
	RoadData           ObjectType = "RoadData"
	RoadGeometry       ObjectType = "RoadGeometry"
)

type Query

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

Query is used to request information from the Trafikinfo API

func NewQuery

func NewQuery(objectType ObjectType, schemaVersion float64) *Query

NewQuery returns a query with the provided filters

func (*Query) Distinct

func (q *Query) Distinct(field string) *Query

Distinct returns an array of unique values of this field

func (*Query) Exclude

func (q *Query) Exclude(fields ...string) *Query

Exclude ensures element matching the specifields fields are ommitted

func (*Query) Filter

func (q *Query) Filter(filters ...Filter) *Query

func (*Query) Include

func (q *Query) Include(fields ...string) *Query

Include ensures only elements matching the specified fields are returned

func (*Query) MarshalXML

func (q *Query) MarshalXML(e *xml.Encoder, start xml.StartElement) error

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) APIKey

func (r *Request) APIKey(key string) *Request

APIKey sets the API key to use for this request

func (*Request) Build

func (r *Request) Build() ([]byte, error)

Build returns the XML encoded request as an array of bytes. It can be passed directly as http.NewRequest's body

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.

func (*Request) Query

func (r *Request) Query(query *Query, rest ...*Query) *Request

Query adds one or more queries to the request

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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