github.com/wtfutil/wtf@v0.43.0/modules/football/client.go (about)

     1  package football
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  )
     7  
     8  var (
     9  	footballAPIUrl = "https://api.football-data.org/v2"
    10  )
    11  
    12  type leagueInfo struct {
    13  	id      int
    14  	caption string
    15  }
    16  
    17  type Client struct {
    18  	apiKey string
    19  }
    20  
    21  func NewClient(apiKey string) *Client {
    22  	client := Client{
    23  		apiKey: apiKey,
    24  	}
    25  
    26  	return &client
    27  }
    28  
    29  func (client *Client) footballRequest(path string, id int) (*http.Response, error) {
    30  
    31  	url := fmt.Sprintf("%s/competitions/%d/%s", footballAPIUrl, id, path)
    32  	req, err := http.NewRequest("GET", url, http.NoBody)
    33  	req.Header.Add("Accept", "application/json")
    34  	req.Header.Add("Content-Type", "application/json")
    35  	req.Header.Add("X-Auth-Token", client.apiKey)
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  	httpClient := &http.Client{}
    40  	resp, err := httpClient.Do(req)
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  	defer func() { _ = resp.Body.Close() }()
    45  
    46  	return resp, nil
    47  }