tankerkoenig

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2020 License: MIT Imports: 8 Imported by: 3 modules, 3 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

tankerkoenig-go

test tag

A wrapper for the Tankerkönig-API in Go

Usage

import "github.com/alexruf/tankerkoenig-go"

Create a new client, then use the exposed services to access the API.

Example

Note that the Demo-API-Key won't return any actual data results! So when using the API make sure to register for a real API-Key.

The API is under Creative Commons (CC BY 4.0) license. It is used by many clients so please restrict API calls to the minimum. An API call every 15 minutes is OK.

To return a list of stations within a radius of a specific location:

package main

import (
	"fmt"
	"github.com/alexruf/tankerkoenig-go"
	"time"
)

func main() {
	client := tankerkoenig.NewClient("00000000-0000-0000-0000-000000000002", nil)
	stations, _, err := client.Station.List(52.52099975265203, 13.43803882598877, 4)

	if err != nil {
		fmt.Printf("Something bad happened: %s\n\n", err)
		return
	}

	fmt.Printf("Prices %s\n\n", time.Now().Format(time.RFC822))
	for _, station := range stations {
		fmt.Printf("Brand: %s\n", station.Brand)
		fmt.Printf("Name: %s\n", station.Name)
		fmt.Printf("Adress: %s %s, %d %s\n", station.Street, station.HouseNumber, station.PostCode, station.Place)
		fmt.Printf("Diesel:\t%f EUR/l\n", station.Diesel)
		fmt.Printf("E5:\t%f EUR/l\n", station.E5)
		fmt.Printf("E10:\t%f EUR/l\n\n", station.E10)
	}
}

To get price information for one or more known gas stations:

package main

import (
	"fmt"
	"github.com/alexruf/tankerkoenig-go"
	"time"
)

func main() {
	client := tankerkoenig.NewClient("00000000-0000-0000-0000-000000000002", nil)

	ids := []string{"1c4f126b-1f3c-4b38-9692-05c400ea8e61", "51d4b6a9-a095-1aa0-e100-80009459e03a", "579d25fd-acb9-445a-9494-f7fe0fa7ce4a", "51d4b660-a095-1aa0-e100-80009459e03a"}
	prices, _, err := client.Prices.Get(ids...)

	if err != nil {
		fmt.Printf("Something bad happened: %s\n\n", err)
		return
	}

	fmt.Printf("Prices %s\n\n", time.Now().Format(time.RFC822))
	for id, price := range prices {
		fmt.Println(id)
		fmt.Printf("Status: %s\n", price.Status)
		fmt.Printf("Diesel:\t%v EUR/l\n", price.Diesel)
		fmt.Printf("E5:\t%v EUR/l\n", price.E5)
		fmt.Printf("E10:\t%v EUR/l\n\n", price.E10)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckResponse

func CheckResponse(r *http.Response) error

CheckResponse checks the API response for errors, and returns them if present. A response is considered an error if it has a status code outside the 200 range. API error responses are expected to have either no response body, or a JSON response body that maps to ErrorResponse. Any other response body will be silently ignored.

Types

type Client

type Client struct {

	// Base URL for API requests
	BaseURL *url.URL

	// APIKey used for authentication with the API
	APIKey string

	// User agent for client
	UserAgent string

	// Services used for communicating with the API
	Station StationService
	Prices  PricesService
	// contains filtered or unexported fields
}

Client communicates with Tankerkönig-API.

func NewClient

func NewClient(apiKey string, httpClient *http.Client) *Client

NewClient returns a new Tankerkönig-API client.

func (*Client) Do

func (c *Client) Do(req *http.Request, v interface{}) (*Response, error)

Do sends an API request and returns the API response. The API response is JSON decoded and stored in the value pointed to by v, or returned as an error if an API error has occurred. If v implements the io.Writer interface, the raw response will be written to v, without attempting to decode it.

func (*Client) NewRequest

func (c *Client) NewRequest(method, urlStr string, query url.Values, body interface{}) (*http.Request, error)

NewRequest creates an API request. A relative URL can be provided in urlStr, which will be resolved to the BaseURL of the Client. Relative URLs should always be specified without a preceding slash. If specified, the value pointed to by body is JSON encoded and included in as the request body.

type ErrorResponse

type ErrorResponse struct {
	// HTTP response that caused this error
	Response *http.Response

	Ok      bool   `json:"ok"`
	Message string `json:"message"`
}

An ErrorResponse reports the error caused by an API request.

func (*ErrorResponse) Error

func (r *ErrorResponse) Error() string

type Price

type Price struct {
	Status string      `json:"status"` // Open-status
	Diesel interface{} `json:"diesel"` // Price for diesel fuel type
	E5     interface{} `json:"e5"`     // Price for E5 fuel type
	E10    interface{} `json:"e10"`    // Price for E10 fuel type
}

Price represents a price data structure.

type PricesService

type PricesService interface {
	// Get returns a map of Price items for one or multiple station IDs.
	Get(ids ...string) (map[string]Price, *Response, error)
}

PricesService is an interface to query price information from the Tankerkönig-API.

type PricesServiceOp

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

PricesServiceOp handles communication with the price related methods of the Tankerkönig-API.

func (*PricesServiceOp) Get

func (p *PricesServiceOp) Get(ids ...string) (map[string]Price, *Response, error)

type Response

type Response struct {
	*http.Response
}

Response is a Tankerkönig-API response. This wraps the standard http.Response returned from Tankerkönig-API.

type Station

type Station struct {
	Brand       string      `json:"brand"`       // Brand
	Dist        float64     `json:"dist"`        // Distance (air line) from the search point to the gas station
	HouseNumber string      `json:"houseNumber"` // House number
	Id          string      `json:"id"`          // ID
	IsOpen      bool        `json:"isOpen"`      // Open-status
	Lat         float64     `json:"lat"`         // Latitude
	Lng         float64     `json:"Lng"`         // Longitude
	Name        string      `json:"name"`        // Name
	Place       string      `json:"place"`       // Place
	PostCode    int         `json:"postCode"`    // Post code
	Diesel      interface{} `json:"diesel"`      // Price for diesel fuel type
	E5          interface{} `json:"e5"`          // Price for E5 fuel type
	E10         interface{} `json:"e10"`         // Price for E10 fuel type
	Street      string      `json:"street"`      // Street

	Overrides    []string      `json:"overrides"`
	WholeDay     bool          `json:"wholeDay"`
	State        string        `json:"state"`
	OpeningTimes []openingTime `json:"openingTimes"`
}

Station represents a gas station.

type StationService

type StationService interface {
	// Detail returns the station for the given ID
	Detail(id string) (Station, *Response, error)
	// List returns all stations within a radius of a location.
	List(lat float64, lng float64, rad int) ([]Station, *Response, error)
}

StationService is an interface to query station information from the Tankerkönig-API.

type StationServiceOp

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

StationServiceOp handles communication with the station related methods of the Tankerkönig-API.

func (*StationServiceOp) Detail

func (s *StationServiceOp) Detail(id string) (Station, *Response, error)

func (*StationServiceOp) List

func (s *StationServiceOp) List(lat float64, lng float64, rad int) ([]Station, *Response, error)

Jump to

Keyboard shortcuts

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