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 ¶
var Endpoint = "https://map.yahooapis.jp/weather/V1/place"
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 (*Client) Get ¶
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 ¶
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 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 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
QueryString returns the query string. Note that spaces need to encoded as %20 instead of +.
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
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: