calendarific

package module
v0.0.0-...-30c5173 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 15, 2022 License: MIT Imports: 6 Imported by: 1

README

go-calendarific

Official Go library for Calendarific Global Holiday API

Installation

dep ensure -add github.com/calendarific/go-calendarific

Usage / Example

  • Create a file called main.go & insert a script like below

    package main
    
    import (
    	"fmt"
    	"github.com/calendarific/go-calendarific"
    	"os"
    )
    
    func main() {
    
        // Loading the paramater struct
    	cp := calendarific.CalParameters{
    		ApiKey:  "<MY SECRET API KEY>",
    		Country: "US",
    		Year:    2019,
    	}
    
    	// It returns a response struct
    	holidays, err := cp.CalData()
    	if err != nil {
    		fmt.Println(err)
    		os.Exit(1)
    	}
    
    	fmt.Println(holidays)
    }
    

    Check out the file for parameter struct and response struct.

  • Run the program for data

    go run main.go
    

Bonus

In case your project needs the states data, you might use the below example on how to Interpretate a state interface type.

```
package main

import (
    "fmt"
    "github.com/calendarific/go-calendarific"
    "github.com/ryanuber/columnize"
    "os"
)

func main() {

    // Initialize a column format display
    var output []string

    // Parameters
    cp := calendarific.CalParameters{
        ApiKey:  "MY SECRET",
        Country: "US",
        Year:    2019,
    }

    // Response
    holidays, err := cp.CalData()
    if err != nil {
        fmt.Printf("Encountered error: %v", err)
        os.Exit(1)
    }

    // Reading the response and formatting the state struct
    // State struct is arbitrary datatype, some of the data is of
    // string format and some are of array, so we use the below procedure 
    // to check what kind datatype is it and take necessary action.
    for _, pv := range holidays.Response.Holidays {
        s := pv.States
        switch val := s.(type) {
        case string:
            // Its a string
            output = append(output, fmt.Sprintf("%s|%s|%s", pv.Name, pv.Date.Iso, pv.States))
        case int:
            // Its an integer
            output = append(output, fmt.Sprintf("%s|%s|%d", pv.Name, pv.Date.Iso, pv.States))
        case []interface{}:
            // Its an array
            for _, v := range val {
                j := v.(map[string]interface{})
                output = append(output, fmt.Sprintf("%s%s|%s|%s", " ├── ", pv.Name, pv.Date.Iso, j["name"]))
            }
        default:
            output = append(output, fmt.Sprintf("%s|%s|%s", pv.Name, pv.Date.Iso, "Unknown Type"))
        }
    }

    // Format configuration to print
    // the data in a column forat
    config := columnize.DefaultConfig()
    config.NoTrim = true

    // Print the output on column format
    result := columnize.Format(output, config)
    fmt.Println(result)
}
```

Documentation

Index

Constants

View Source
const (
	CalAPI = "https://calendarific.com/api/v2/holidays?"
)

The API URL to get all the holidays

Variables

This section is empty.

Functions

This section is empty.

Types

type CalParameters

type CalParameters struct {
	ApiKey   string `url:"api_key,omitempty"`
	Country  string `url:"country,omitempty"`
	Year     int32  `url:"year,omitempty"`
	Day      int32  `url:"day,omitempty"`
	Month    int32  `url:"month,omitempty"`
	Location string `url:"location,omitempty"`
	Type     string `url:"type,omitempty"`
	Language string `url:"language,omitempty"`
	Uuid     bool   `url:"uuid,omitempty"`
}

Calendarific Parameters

func (*CalParameters) CalData

func (p *CalParameters) CalData() (*CalResponse, error)

Request the data from Calendarific

type CalResponse

type CalResponse struct {
	Meta struct {
		Code int `json:"code"`
	} `json:"meta"`
	Response struct {
		Holidays []Holiday `json:"holidays"`
	} `json:"response"`
}

Calendarific Response

type Holiday

type Holiday struct {
	Name        string `json:"name"`
	Description string `json:"description"`
	Date        struct {
		Iso      string `json:"iso"`
		Datetime struct {
			Year  int `json:"year"`
			Month int `json:"month"`
			Day   int `json:"day"`
		} `json:"datetime"`
	} `json:"date"`
	GoDate    time.Time   `json:"-"` // This field is not in the original response, so ignore it when parsing JSON
	Type      []string    `json:"type"`
	Locations string      `json:"locations"`
	States    interface{} `json:"states"` // sometimes its a struct, sometime its a string, so use interface
}

type States

type States []struct {
	ID        int         `json:"id"`
	Abbrev    string      `json:"abbrev"`
	Name      string      `json:"name"`
	Exception interface{} `json:"exception"`
	Iso       string      `json:"iso"`
}

We don't use this struct, since the states response json is not always a JSON it is sometimes a string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL