weather

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: May 1, 2020 License: Apache-2.0 Imports: 11 Imported by: 5

Documentation

Overview

Package weather provides a client for YOLP Weather API, described as https://developer.yahoo.co.jp/webapi/map/openlocalplatform/v1/weather.html

Index

Examples

Constants

This section is empty.

Variables

View Source
var Endpoint = "https://map.yahooapis.jp/weather/V1/place"
View Source
var Timezone = time.FixedZone("Asia/Tokyo", 9*60*60)

Timezone represents the timezone used in Weather API.

Functions

This section is empty.

Types

type Client

type Client struct {
	ClientID string       // API Client ID (required)
	Client   *http.Client // Default to http.DefaultClient
	Endpoint string       // Default to Endpoint
}

Client provides access to Weather API.

func NewClient

func NewClient(clientID string) *Client

NewClient returns a Client.

func (*Client) Get

func (c *Client) Get(req *Request) (*Response, error)

Get sends the request and returns the response.

Example
package main

import (
	"log"
	"os"

	"github.com/int128/go-yahoo-weather/weather"
)

func main() {
	c := weather.NewClient(os.Getenv("YAHOO_CLIENT_ID"))
	resp, err := c.Get(&weather.Request{
		Coordinates: []weather.Coordinates{
			{Latitude: 35.663613, Longitude: 139.732293},
		},
	})
	if err != nil {
		log.Fatalf("Could not get weather: %s", err)
	}
	log.Printf("Weather response: %+v", resp)
}
Output:

type Coordinates

type Coordinates struct {
	Latitude  float64
	Longitude float64
}

Coordinates represents a coordinates in WGS84.

type CoordinatesString

type CoordinatesString string

CoordinatesString represents a coordinates in API specific format.

func (CoordinatesString) Parse

func (s CoordinatesString) Parse() (Coordinates, error)

Parse returns a coordinates corresponding to the string.

type DateString

type DateString string

DateString represents a time in API specific format.

func (DateString) Parse

func (t DateString) Parse() (time.Time, error)

Parse returns a time.Time corresponding to the string.

type ErrorResponse added in v1.2.0

type ErrorResponse interface {
	Code() int       // Error code in response body or status code, such as 400, 401 or 403
	Message() string // Error message
}

ErrorResponse provides details of error response.

func GetErrorResponse added in v1.2.0

func GetErrorResponse(err error) ErrorResponse

GetErrorResponse returns ErrorResponse if API returned an error response.

Example
package main

import (
	"log"
	"os"

	"github.com/int128/go-yahoo-weather/weather"
)

func main() {
	c := weather.NewClient(os.Getenv("YAHOO_CLIENT_ID"))
	resp, err := c.Get(&weather.Request{})
	if err != nil {
		if errResp := weather.GetErrorResponse(err); errResp != nil {
			if errResp.Code() >= 500 {
				// you can retry here
			}
		}
		log.Fatalf("Could not get weather: %s", err)
	}
	log.Printf("Weather response: %+v", resp)
}
Output:

type Event added in v1.1.0

type Event struct {
	Time     time.Time
	Forecast bool
	Rainfall float64
}

Event represents an observation or forecast at time.

type Request

type Request struct {
	Coordinates     []Coordinates // list of coordinates, up to 10 (required)
	DateTime        time.Time     // default to current time
	PastHours       int           // 0 (default), 1 or 2
	IntervalMinutes int           // 10 (default) or 5
}

Request represents a request for Weather API.

func (*Request) QueryString added in v1.0.2

func (r *Request) QueryString() string

QueryString returns the query string. Note that spaces need to encoded as %20 instead of +.

func (*Request) Values

func (r *Request) Values() url.Values

Values returns query parameters corresponding to the request.

type Response

type Response struct {
	Body    ResponseBody
	Expires time.Time // expires header
}

Response represents a response from Weather API.

type ResponseBody

type ResponseBody struct {
	ResultInfo struct {
		Count       int     `json:"Count"`
		Total       int     `json:"Total"`
		Start       int     `json:"Start"`
		Status      int     `json:"Status"`
		Latency     float64 `json:"Latency"`
		Description string  `json:"Description"`
		Copyright   string  `json:"Copyright"`
	} `json:"ResultInfo"`
	Feature []struct {
		ID       string `json:"Id"`
		Name     string `json:"Name"`
		Geometry struct {
			Type        string            `json:"Type"`
			Coordinates CoordinatesString `json:"Coordinates"`
		} `json:"Geometry"`
		Property struct {
			WeatherAreaCode int `json:"WeatherAreaCode"`
			WeatherList     struct {
				Weather []struct {
					Type     string     `json:"Type"`
					Date     DateString `json:"Date"`
					Rainfall float64    `json:"Rainfall"`
				} `json:"Weather"`
			} `json:"WeatherList"`
		} `json:"Property"`
	} `json:"Feature"`
	Error errorResponse `json:"Error"`
}

ResponseBody represents body of Response.

type Weather added in v1.1.0

type Weather struct {
	Coordinates Coordinates
	Events      []Event
}

Weather represents weather at the coordinates.

func Parse added in v1.1.0

func Parse(resp *Response) ([]Weather, error)

Parse returns a list of Weather corresponding to the Response.

Example
package main

import (
	"log"
	"os"

	"github.com/int128/go-yahoo-weather/weather"
)

func main() {
	c := weather.NewClient(os.Getenv("YAHOO_CLIENT_ID"))
	resp, err := c.Get(&weather.Request{
		Coordinates: []weather.Coordinates{
			{Latitude: 35.663613, Longitude: 139.732293},
		},
	})
	if err != nil {
		log.Fatalf("Error while getting weather: %s", err)
	}
	weathers, err := weather.Parse(resp)
	if err != nil {
		log.Fatalf("Error while parsing weather response: %s", err)
	}
	log.Printf("Weathers: %+v", weathers)
}
Output:

Jump to

Keyboard shortcuts

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