From b3896afd96e5dc4b4b1e69cc988214dcdf3ae542 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Tue, 28 Mar 2023 21:03:52 +0800 Subject: [PATCH] - update caiyunapp --- service/caiyunapp/realtime.go | 103 ++++++++++++++++++++++++++++++++++ service/caiyunapp/weather.go | 61 ++++++++++++++++++++ 2 files changed, 164 insertions(+) create mode 100644 service/caiyunapp/realtime.go create mode 100644 service/caiyunapp/weather.go diff --git a/service/caiyunapp/realtime.go b/service/caiyunapp/realtime.go new file mode 100644 index 00000000..56df9c8c --- /dev/null +++ b/service/caiyunapp/realtime.go @@ -0,0 +1,103 @@ +package caiyunapp + +import ( + "context" + "fmt" + "github.com/dtapps/go-library/utils/gojson" + "github.com/dtapps/go-library/utils/gorequest" + "net/http" +) + +type RealtimeResponse struct { + Status string `json:"status"` + ApiVersion string `json:"api_version"` + ApiStatus string `json:"api_status"` + Lang string `json:"lang"` + Unit string `json:"unit"` + Tzshift float64 `json:"tzshift"` + Timezone string `json:"timezone"` + ServerTime float64 `json:"server_time"` + Location []float64 `json:"location"` + Result struct { + Realtime struct { + Status string `json:"status"` + Temperature float64 `json:"temperature"` + Humidity float64 `json:"humidity"` + Cloudrate float64 `json:"cloudrate"` + Skycon string `json:"skycon"` + Visibility float64 `json:"visibility"` + Dswrf float64 `json:"dswrf"` + Wind struct { + Speed float64 `json:"speed"` + Direction float64 `json:"direction"` + } `json:"wind"` + Pressure float64 `json:"pressure"` + ApparentTemperature float64 `json:"apparent_temperature"` + Precipitation struct { + Local struct { + Status string `json:"status"` + Datasource string `json:"datasource"` + Intensity float64 `json:"intensity"` + } `json:"local"` + Nearest struct { + Status string `json:"status"` + Distance float64 `json:"distance"` + Intensity float64 `json:"intensity"` + } `json:"nearest"` + } `json:"precipitation"` + AirQuality struct { + Pm25 float64 `json:"pm25"` + Pm10 float64 `json:"pm10"` + O3 float64 `json:"o3"` + So2 float64 `json:"so2"` + No2 float64 `json:"no2"` + Co float64 `json:"co"` + Aqi struct { + Chn float64 `json:"chn"` + Usa float64 `json:"usa"` + } `json:"aqi"` + Description struct { + Chn string `json:"chn"` + Usa string `json:"usa"` + } `json:"description"` + } `json:"air_quality"` + LifeIndex struct { + Ultraviolet struct { + Index float64 `json:"index"` + Desc string `json:"desc"` + } `json:"ultraviolet"` + Comfort struct { + Index float64 `json:"index"` + Desc string `json:"desc"` + } `json:"comfort"` + } `json:"life_index"` + } `json:"realtime"` + Primary float64 `json:"primary"` + } `json:"result"` +} + +type RealtimeResult struct { + Result RealtimeResponse // 结果 + Body []byte // 内容 + Http gorequest.Response // 请求 +} + +func newRealtimeResult(result RealtimeResponse, body []byte, http gorequest.Response) *RealtimeResult { + return &RealtimeResult{Result: result, Body: body, Http: http} +} + +// Realtime 实况 +// https://docs.caiyunapp.com/docs/realtime +func (c *Client) Realtime(ctx context.Context, locationLatitude, locationLongitude string, notMustParams ...gorequest.Params) (*RealtimeResult, error) { + // 参数 + params := gorequest.NewParamsWith(notMustParams...) + // 请求 + request, err := c.request(ctx, c.getApiUrl()+fmt.Sprintf("/%s,%s/realtime", locationLatitude, locationLongitude), params, http.MethodGet) + if err != nil { + return newRealtimeResult(RealtimeResponse{}, request.ResponseBody, request), err + } + // 定义 + var response RealtimeResponse + err = gojson.Unmarshal(request.ResponseBody, &response) + return newRealtimeResult(response, request.ResponseBody, request), err +} diff --git a/service/caiyunapp/weather.go b/service/caiyunapp/weather.go new file mode 100644 index 00000000..ec81f1bb --- /dev/null +++ b/service/caiyunapp/weather.go @@ -0,0 +1,61 @@ +package caiyunapp + +import ( + "context" + "fmt" + "github.com/dtapps/go-library/utils/gojson" + "github.com/dtapps/go-library/utils/gorequest" + "net/http" +) + +type WeatherResponse struct { + Status string `json:"status"` + ApiVersion string `json:"api_version"` + ApiStatus string `json:"api_status"` + Lang string `json:"lang"` + Unit string `json:"unit"` + Tzshift int `json:"tzshift"` + Timezone string `json:"timezone"` + ServerTime int `json:"server_time"` + Location []float64 `json:"location"` + Result struct { + Alert struct { + } `json:"alert"` + Realtime struct { + } `json:"realtime"` + Minutely struct { + } `json:"minutely"` + Hourly struct { + } `json:"hourly"` + Daily struct { + } `json:"daily"` + Primary int `json:"primary"` + ForecastKeypoint string `json:"forecast_keypoint"` + } `json:"result"` +} + +type WeatherResult struct { + Result WeatherResponse // 结果 + Body []byte // 内容 + Http gorequest.Response // 请求 +} + +func newWeatherResult(result WeatherResponse, body []byte, http gorequest.Response) *WeatherResult { + return &WeatherResult{Result: result, Body: body, Http: http} +} + +// Weather 综合 +// https://docs.caiyunapp.com/docs/weather +func (c *Client) Weather(ctx context.Context, locationLatitude, locationLongitude string, notMustParams ...gorequest.Params) (*WeatherResult, error) { + // 参数 + params := gorequest.NewParamsWith(notMustParams...) + // 请求 + request, err := c.request(ctx, c.getApiUrl()+fmt.Sprintf("/%s,%s/weather", locationLatitude, locationLongitude), params, http.MethodGet) + if err != nil { + return newWeatherResult(WeatherResponse{}, request.ResponseBody, request), err + } + // 定义 + var response WeatherResponse + err = gojson.Unmarshal(request.ResponseBody, &response) + return newWeatherResult(response, request.ResponseBody, request), err +}