mirror of
https://github.com/holidayapi/holidayapi-go.git
synced 2025-06-21 04:26:33 +00:00
Finished up wrapper
This commit is contained in:
parent
1adc8d6f90
commit
6556bc0cb8
2 changed files with 83 additions and 21 deletions
38
README.md
38
README.md
|
@ -1,2 +1,38 @@
|
||||||
# go-holidayapi
|
# go-holidayapi
|
||||||
Official Go library for Holiday API
|
Official Go library for [Holiday API](https://holidayapi.com)
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/joshtronic/go-holidayapi"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
hapi := holidayapi.NewV1("_MY_API_KEY_")
|
||||||
|
|
||||||
|
holidays, err := hapi.Holidays(map[string]interface{}{
|
||||||
|
// Required
|
||||||
|
"country": "US",
|
||||||
|
"year": "2016",
|
||||||
|
// Optional
|
||||||
|
// "month": "7",
|
||||||
|
// "day": "4",
|
||||||
|
// "previous": "true",
|
||||||
|
// "upcoming": "true",
|
||||||
|
// "public": "true",
|
||||||
|
// "pretty": "true",
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
// Error handling...
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("%#v\n", holidays)
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
|
@ -1,36 +1,62 @@
|
||||||
//package holidayapi
|
package holidayapi
|
||||||
package main
|
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
|
"encoding/json"
|
||||||
// TODO: import ( encoding/json net/http net/url )
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
)
|
||||||
|
|
||||||
type V1 struct {
|
type V1 struct {
|
||||||
|
Url string
|
||||||
Key string
|
Key string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewV1(key string) *V1 {
|
func NewV1(key string) *V1 {
|
||||||
v1 := &V1{
|
v1 := &V1{
|
||||||
Key: key,
|
Key: key,
|
||||||
|
Url: "https://holidayapi.com/v1/holidays?",
|
||||||
}
|
}
|
||||||
|
|
||||||
return v1
|
return v1
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v1 *V1) Holidays(parameters map[string]interface{}) map[string]interface{} {
|
func (v1 *V1) Holidays(args map[string]interface{}) (map[string]interface{}, error) {
|
||||||
return map[string]interface{}{
|
var data map[string]interface{}
|
||||||
"test": "ing",
|
|
||||||
|
if _, ok := args["key"]; !ok {
|
||||||
|
args["key"] = v1.Key
|
||||||
|
}
|
||||||
|
|
||||||
|
params := url.Values{}
|
||||||
|
|
||||||
|
for k, v := range args {
|
||||||
|
params.Add(k, v.(string))
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := http.Get(v1.Url + params.Encode())
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
json.Unmarshal([]byte(string(body)), &data)
|
||||||
|
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
_, ok := data["error"]
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
data["error"] = "Unknown error."
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
return data, nil
|
||||||
hapi := NewV1("_MY_API_KEY_")
|
|
||||||
|
|
||||||
fmt.Println(hapi.Key)
|
|
||||||
|
|
||||||
holidays := hapi.Holidays(map[string]interface{}{
|
|
||||||
"country": "US",
|
|
||||||
})
|
|
||||||
|
|
||||||
fmt.Println("%#v\n", holidays)
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue