You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.2 KiB
58 lines
1.2 KiB
package holidays
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"net/http"
|
|
"net/url"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
const hostUrl = "http://api.tianapi.com/jiejiari/index?"
|
|
const key = "f2599751017c50b91d6f31261ce6dbc0"
|
|
const yearType = 1
|
|
const mouthTyoe = 2
|
|
|
|
var HolidaysApi = &holidaysApi{}
|
|
|
|
type holidaysApi struct {
|
|
}
|
|
|
|
func (h *holidaysApi) GetHolidays(year string) (string, error) {
|
|
request, err := h.request("get", hostUrl+url.Values{
|
|
"key": {key},
|
|
"type": {strconv.Itoa(yearType)},
|
|
"date": {year + "-01-01"},
|
|
}.Encode(), "")
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return request, nil
|
|
}
|
|
|
|
// @Style 网络请求
|
|
func (h *holidaysApi) request(method string, host string, data string, headers ...*map[string]string) (string, error) {
|
|
client := &http.Client{}
|
|
method = strings.ToUpper(method)
|
|
|
|
req, err := http.NewRequest(method, host, strings.NewReader(data))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if len(headers) > 0 {
|
|
for key, value := range *headers[0] {
|
|
req.Header.Add(key, value)
|
|
}
|
|
}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer resp.Body.Close()
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return string(body), nil
|
|
}
|