mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 02:17:05 +00:00
more improvements to the x/client new sub-package
This commit is contained in:
46
_examples/http-client/weatherapi/client/client.go
Normal file
46
_examples/http-client/weatherapi/client/client.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
"github.com/kataras/iris/v12/x/client"
|
||||
)
|
||||
|
||||
// The BaseURL of our API client.
|
||||
const BaseURL = "https://api.weatherapi.com/v1"
|
||||
|
||||
type (
|
||||
Options struct {
|
||||
APIKey string `json:"api_key" yaml:"APIKey" toml:"APIKey"`
|
||||
}
|
||||
|
||||
Client struct {
|
||||
*client.Client
|
||||
}
|
||||
)
|
||||
|
||||
func NewClient(opts Options) *Client {
|
||||
apiKeyParameterSetter := client.RequestParam("key", opts.APIKey)
|
||||
|
||||
c := client.New(
|
||||
client.Debug,
|
||||
client.BaseURL(BaseURL),
|
||||
client.PersistentRequestOptions(apiKeyParameterSetter),
|
||||
)
|
||||
|
||||
return &Client{c}
|
||||
}
|
||||
|
||||
func (c *Client) GetCurrentByCity(ctx context.Context, city string) (resp Response, err error) {
|
||||
urlpath := "/current.json"
|
||||
// ?q=Athens&aqi=no
|
||||
params := client.RequestQuery(url.Values{
|
||||
"q": []string{city},
|
||||
"aqi": []string{"no"},
|
||||
})
|
||||
|
||||
err = c.Client.ReadJSON(ctx, &resp, iris.MethodGet, urlpath, nil, params)
|
||||
return
|
||||
}
|
||||
43
_examples/http-client/weatherapi/client/response.go
Normal file
43
_examples/http-client/weatherapi/client/response.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package client
|
||||
|
||||
type Response struct {
|
||||
Location struct {
|
||||
Name string `json:"name"`
|
||||
Region string `json:"region"`
|
||||
Country string `json:"country"`
|
||||
Lat float64 `json:"lat"`
|
||||
Lon float64 `json:"lon"`
|
||||
TzID string `json:"tz_id"`
|
||||
LocaltimeEpoch int `json:"localtime_epoch"`
|
||||
Localtime string `json:"localtime"`
|
||||
} `json:"location"`
|
||||
Current struct {
|
||||
LastUpdatedEpoch int `json:"last_updated_epoch"`
|
||||
LastUpdated string `json:"last_updated"`
|
||||
TempC float64 `json:"temp_c"`
|
||||
TempF float64 `json:"temp_f"`
|
||||
IsDay int `json:"is_day"`
|
||||
Condition struct {
|
||||
Text string `json:"text"`
|
||||
Icon string `json:"icon"`
|
||||
Code int `json:"code"`
|
||||
} `json:"condition"`
|
||||
WindMph float64 `json:"wind_mph"`
|
||||
WindKph float64 `json:"wind_kph"`
|
||||
WindDegree int `json:"wind_degree"`
|
||||
WindDir string `json:"wind_dir"`
|
||||
PressureMb float64 `json:"pressure_mb"`
|
||||
PressureIn float64 `json:"pressure_in"`
|
||||
PrecipMm float64 `json:"precip_mm"`
|
||||
PrecipIn float64 `json:"precip_in"`
|
||||
Humidity int `json:"humidity"`
|
||||
Cloud int `json:"cloud"`
|
||||
FeelslikeC float64 `json:"feelslike_c"`
|
||||
FeelslikeF float64 `json:"feelslike_f"`
|
||||
VisKm float64 `json:"vis_km"`
|
||||
VisMiles float64 `json:"vis_miles"`
|
||||
Uv float64 `json:"uv"`
|
||||
GustMph float64 `json:"gust_mph"`
|
||||
GustKph float64 `json:"gust_kph"`
|
||||
} `json:"current"`
|
||||
}
|
||||
21
_examples/http-client/weatherapi/main.go
Normal file
21
_examples/http-client/weatherapi/main.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/kataras/iris/v12/_examples/http-client/weatherapi/client"
|
||||
)
|
||||
|
||||
func main() {
|
||||
c := client.NewClient(client.Options{
|
||||
APIKey: "{YOUR_API_KEY_HERE}",
|
||||
})
|
||||
|
||||
resp, err := c.GetCurrentByCity(context.Background(), "Xanthi/GR")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Printf("Temp: %.2f(C), %.2f(F)\n", resp.Current.TempC, resp.Current.TempF)
|
||||
}
|
||||
Reference in New Issue
Block a user