1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
// fmi_client.go - FMI API client library
package main
import (
"fmt"
"io"
"net/http"
"net/url"
"time"
)
// FMIClient handles FMI API interactions
type FMIClient struct {
BaseURL string
HTTPClient *http.Client
WeatherMapper *WeatherMapper
}
// NewFMIClient creates a new FMI client
func NewFMIClient() *FMIClient {
return &FMIClient{
BaseURL: "https://opendata.fmi.fi/wfs",
HTTPClient: &http.Client{Timeout: 30 * time.Second},
WeatherMapper: NewWeatherMapper(),
}
}
// GetForecast fetches and parses forecast for a location
func (c *FMIClient) GetForecast(location string) (*OWMResponse, error) {
// Build request URL
url, err := c.buildURL(location)
if err != nil {
return nil, fmt.Errorf("building URL: %w", err)
}
// Fetch XML data
xmlData, err := c.fetchXML(url)
if err != nil {
return nil, fmt.Errorf("fetching XML: %w", err)
}
// Parse XML
forecastData, err := ParseForecastXML(xmlData)
if err != nil {
return nil, fmt.Errorf("parsing XML: %w", err)
}
// Convert to OWM format
response, err := forecastData.ToOWMResponse(c.WeatherMapper)
if err != nil {
return nil, fmt.Errorf("converting to OWM format: %w", err)
}
return response, nil
}
func (c *FMIClient) buildURL(location string) (string, error) {
baseURL, err := url.Parse(c.BaseURL)
if err != nil {
return "", err
}
q := baseURL.Query()
q.Set("service", "WFS")
q.Set("version", "2.0.0")
q.Set("request", "getFeature")
q.Set("storedquery_id", "fmi::forecast::harmonie::surface::point::multipointcoverage")
q.Set("place", location)
q.Set("parameters", "Temperature,PrecipitationRate,Humidity,Pressure,dewPoint,visibility,WindSpeedMS,WindDirection,WindGust,TotalCloudCover,WeatherSymbol3")
baseURL.RawQuery = q.Encode()
return baseURL.String(), nil
}
func (c *FMIClient) fetchXML(url string) ([]byte, error) {
resp, err := c.HTTPClient.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("HTTP %d: %s", resp.StatusCode, string(body))
}
return io.ReadAll(resp.Body)
}
|