package httpClient import ( "PersonApp/models" "encoding/json" "fmt" "io" "net/http" "time" ) type Client interface { GetPersonTasks(id int) ([]models.Task, error) TestConnection() (bool, error) } type client struct { BaseUrl string Timeout time.Duration } func (c *client) TestConnection() (bool, error) { client := &http.Client{Timeout: c.Timeout} url := fmt.Sprintf("%s/", c.BaseUrl) resp, err := client.Get(url) if err != nil { return false, err } defer func(Body io.ReadCloser) { err := Body.Close() if err != nil { return } }(resp.Body) if resp.StatusCode != http.StatusOK { return false, fmt.Errorf("bad status code: %d", resp.StatusCode) } return true, nil } func (c *client) GetPersonTasks(id int) ([]models.Task, error) { client := &http.Client{Timeout: c.Timeout * time.Second} url := fmt.Sprintf("%s/f/%d", c.BaseUrl, id) resp, err := client.Get(url) if err != nil { return nil, err } defer func(Body io.ReadCloser) { err := Body.Close() if err != nil { } }(resp.Body) body, _ := io.ReadAll(resp.Body) var tasks []models.Task if err := json.Unmarshal(body, &tasks); err != nil { fmt.Printf("Unmarshal error: %s", err) return []models.Task{}, err } return tasks, nil } func NewClient(baseUrl string, timeout time.Duration) Client { return &client{BaseUrl: baseUrl, Timeout: timeout} }