Skip to content

Commit

Permalink
Lint & cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
westy92 committed May 8, 2024
1 parent 89570bb commit eb17c42
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 129 deletions.
26 changes: 26 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
linters:
enable-all: true
disable:
- execinquery # Deprecated
- gomnd # Deprecated
- exhaustruct
# TODO re-enable these
- depguard
- err113
- forbidigo
- funlen
- godox
- gofumpt
- lll
- nlreturn
- noctx
- paralleltest
- testifylint
- testpackage
- tparallel
linters-settings:
tagliatelle:
case:
use-field-name: true
rules:
json: snake
53 changes: 28 additions & 25 deletions api_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,45 @@ import (

// TODO docs

type ApiProvider int
type APIProvider int

const (
ApiLayer ApiProvider = iota
RapidApi
APILayer APIProvider = iota
RapidAPI
// TODO APIMarket.
)

func (api ApiProvider) isValid() bool {
func (api APIProvider) isValid() bool {
switch api {
case ApiLayer:
case APILayer:
return true
case RapidApi:
case RapidAPI:
return true
default:
return false

Check warning on line 26 in api_provider.go

View check run for this annotation

Codecov / codecov/patch

api_provider.go#L23-L26

Added lines #L23 - L26 were not covered by tests
}
}

func (api ApiProvider) apiKeySource() string {
func (api APIProvider) apiKeySource() string {
switch api {
case ApiLayer:
case APILayer:
return "https://apilayer.com/marketplace/checkiday-api#pricing"
case RapidApi:
case RapidAPI:
return "https://rapidapi.com/westy92-llc-westy92-llc-default/api/checkiday/pricing"
default:
return ""

Check warning on line 37 in api_provider.go

View check run for this annotation

Codecov / codecov/patch

api_provider.go#L34-L37

Added lines #L34 - L37 were not covered by tests
}
}

func (api ApiProvider) baseUrl() url.URL {
func (api APIProvider) baseURL() url.URL {
switch api {
case ApiLayer:
case APILayer:
return url.URL{
Scheme: "https",
Host: "api.apilayer.com",
Path: "checkiday",
}
case RapidApi:
case RapidAPI:
return url.URL{
Scheme: "https",
Host: "checkiday.p.rapidapi.com",

Check warning on line 52 in api_provider.go

View check run for this annotation

Codecov / codecov/patch

api_provider.go#L49-L52

Added lines #L49 - L52 were not covered by tests
Expand All @@ -55,30 +56,32 @@ func (api ApiProvider) baseUrl() url.URL {
}
}

func (api ApiProvider) extractRateLimitInfo(headers http.Header) RateLimit {
func (api APIProvider) extractRateLimitInfo(headers http.Header) RateLimit {
var limit, remaining int

switch api {
case ApiLayer:
limit, _ = strconv.Atoi(headers.Get("x-ratelimit-limit-month"))
remaining, _ = strconv.Atoi(headers.Get("x-ratelimit-remaining-month"))
case RapidApi:
limit, _ = strconv.Atoi(headers.Get("x-ratelimit-requests-limit"))
remaining, _ = strconv.Atoi(headers.Get("x-ratelimit-requests-remaining"))
case APILayer:
limit, _ = strconv.Atoi(headers.Get("X-Ratelimit-Limit-Month"))
remaining, _ = strconv.Atoi(headers.Get("X-Ratelimit-Remaining-Month"))
case RapidAPI:
limit, _ = strconv.Atoi(headers.Get("X-Ratelimit-Requests-Limit"))
remaining, _ = strconv.Atoi(headers.Get("X-Ratelimit-Requests-Remaining"))
default:

Check warning on line 69 in api_provider.go

View check run for this annotation

Codecov / codecov/patch

api_provider.go#L66-L69

Added lines #L66 - L69 were not covered by tests
}

return RateLimit{
Limit: limit,
Remaining: remaining,
}
}

func (api ApiProvider) attachRequestHeaders(headers *http.Header, apiKey string) {
func (api APIProvider) attachRequestHeaders(headers *http.Header, apiKey string) {
switch api {
case ApiLayer:
headers.Set("apikey", apiKey)
case RapidApi:
headers.Set("X-RapidAPI-Key", apiKey)
headers.Set("X-RapidAPI-Host", "checkiday.p.rapidapi.com")
case APILayer:
headers.Set("Apikey", apiKey)
case RapidAPI:
headers.Set("X-Rapidapi-Key", apiKey)
headers.Set("X-Rapidapi-Host", "checkiday.p.rapidapi.com")
default:

Check warning on line 85 in api_provider.go

View check run for this annotation

Codecov / codecov/patch

api_provider.go#L82-L85

Added lines #L82 - L85 were not covered by tests
}
}
6 changes: 3 additions & 3 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func main() {
// Get a FREE API key from https://apilayer.com/marketplace/checkiday-api#pricing
client, err := holidays.New(holidays.ApiLayer, "<your API key>")
client, err := holidays.New(holidays.APILayer, "<your API key>")

Check warning on line 11 in example/main.go

View check run for this annotation

Codecov / codecov/patch

example/main.go#L11

Added line #L11 was not covered by tests

if err != nil {
fmt.Println(err)
Expand All @@ -29,12 +29,12 @@ func main() {
}

event := events.Events[0]
fmt.Printf("Today is %s! Find more information at: %s.\n", event.Name, event.Url)
fmt.Printf("Today is %s! Find more information at: %s.\n", event.Name, event.URL)
fmt.Printf("Rate limit remaining: %d/%d (billing cycle).\n", events.RateLimit.Remaining, events.RateLimit.Limit)

Check warning on line 33 in example/main.go

View check run for this annotation

Codecov / codecov/patch

example/main.go#L32-L33

Added lines #L32 - L33 were not covered by tests

// Get Event Information
eventInfo, err := client.GetEventInfo(holidays.GetEventInfoRequest{
Id: event.Id,
ID: event.ID,

Check warning on line 37 in example/main.go

View check run for this annotation

Codecov / codecov/patch

example/main.go#L37

Added line #L37 was not covered by tests
// These parameters can be specified to calculate the range of eventInfo.Event.Occurrences
// Start: 2020,
// End: 2030,
Expand Down
37 changes: 23 additions & 14 deletions holidays.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ import (
"strconv"
)

// The API Client
var ErrAPIProviderRequired = errors.New("please provide a valid API provider")
var ErrEventIDRequired = errors.New("event id is required")
var ErrSearchQueryRequired = errors.New("search query is required")

// The API Client.
type Client struct {
apiKey string
apiProvider ApiProvider
apiProvider APIProvider
}

const (
Expand All @@ -25,9 +29,9 @@ const (
// Creates a New Client using the provided API key.
// TODO update docs
// Get a FREE API key from https://apilayer.com/marketplace/checkiday-api#pricing
func New(apiProvider ApiProvider, apiKey string) (*Client, error) {
func New(apiProvider APIProvider, apiKey string) (*Client, error) {
if !apiProvider.isValid() {
return nil, errors.New("please provide a valid API provider")
return nil, ErrAPIProviderRequired

Check warning on line 34 in holidays.go

View check run for this annotation

Codecov / codecov/patch

holidays.go#L34

Added line #L34 was not covered by tests
}

if apiKey == "" {
Expand All @@ -40,7 +44,7 @@ func New(apiProvider ApiProvider, apiKey string) (*Client, error) {
}, nil
}

// Gets the Events for the provided Date
// Gets the Events for the provided Date.
func (c *Client) GetEvents(req GetEventsRequest) (*GetEventsResponse, error) {
var params = url.Values{
"adult": {strconv.FormatBool(req.Adult)},
Expand All @@ -64,14 +68,15 @@ func (c *Client) GetEvents(req GetEventsRequest) (*GetEventsResponse, error) {
return res, nil
}

// Gets the Event Info for the provided Event
// Gets the Event Info for the provided Event.
func (c *Client) GetEventInfo(req GetEventInfoRequest) (*GetEventInfoResponse, error) {
var params = url.Values{}

if req.Id == "" {
return nil, errors.New("event id is required")
if req.ID == "" {
return nil, ErrEventIDRequired
}
params["id"] = []string{req.Id}

params["id"] = []string{req.ID}

if req.Start != 0 {
params["start"] = []string{strconv.Itoa(req.Start)}
Expand All @@ -91,15 +96,16 @@ func (c *Client) GetEventInfo(req GetEventInfoRequest) (*GetEventInfoResponse, e
return res, nil
}

// Searches for Events with the given criteria
// Searches for Events with the given criteria.
func (c *Client) Search(req SearchRequest) (*SearchResponse, error) {
var params = url.Values{
"adult": {strconv.FormatBool(req.Adult)},
}

if req.Query == "" {
return nil, errors.New("search query is required")
return nil, ErrSearchQueryRequired
}

params["query"] = []string{req.Query}

res, rateLimit, err := request[SearchResponse](c, "search", params)
Expand All @@ -112,19 +118,20 @@ func (c *Client) Search(req SearchRequest) (*SearchResponse, error) {
return res, nil
}

// Gets the API Client Version
// Gets the API Client Version.
func (c *Client) GetVersion() string {
return version
}

func request[R StandardResponseInterface](client *Client, urlPath string, params url.Values) (*R, *RateLimit, error) {
url := client.apiProvider.baseUrl()
url := client.apiProvider.baseURL()
url.Path = path.Join(url.Path, urlPath)

if params != nil {
url.RawQuery = params.Encode()
}

req, err := http.NewRequest("GET", url.String(), nil)
req, err := http.NewRequest(http.MethodGet, url.String(), nil) // TODO pass context lint(noctx)
if err != nil {
return nil, nil, fmt.Errorf("can't create request: %w", err)
}
Expand All @@ -139,11 +146,13 @@ func request[R StandardResponseInterface](client *Client, urlPath string, params
}

defer res.Body.Close()

if res.StatusCode != http.StatusOK {
var errBody errorResponse
if err := json.NewDecoder(res.Body).Decode(&errBody); err == nil && errBody.Error != "" {
return nil, nil, errors.New(errBody.Error)
}

return nil, nil, errors.New(res.Status)
}

Expand Down
Loading

0 comments on commit eb17c42

Please sign in to comment.