Skip to content

Commit

Permalink
Add debug logging. (#6)
Browse files Browse the repository at this point in the history
* add debug logger
* capture response code for log too
* fix up tests
* use status text
* safety net
  • Loading branch information
davidnewhall authored Aug 21, 2021
1 parent a8bf42e commit e67a084
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ language: go
go:
- 1.16.x
install:
- curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin v1.38.0
- curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin v1.41.1
script:
- make test
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ test: lint nopollution

lint:
# Test lint on four platforms.
GOOS=linux golangci-lint run --enable-all -D maligned,scopelint,interfacer
GOOS=darwin golangci-lint run --enable-all -D maligned,scopelint,interfacer
GOOS=windows golangci-lint run --enable-all -D maligned,scopelint,interfacer
GOOS=freebsd golangci-lint run --enable-all -D maligned,scopelint,interfacer
GOOS=linux golangci-lint run --enable-all -D maligned,scopelint,interfacer,golint,tagliatelle
GOOS=darwin golangci-lint run --enable-all -D maligned,scopelint,interfacer,golint,tagliatelle
GOOS=windows golangci-lint run --enable-all -D maligned,scopelint,interfacer,golint,tagliatelle
GOOS=freebsd golangci-lint run --enable-all -D maligned,scopelint,interfacer,golint,tagliatelle

nopollution:
# Avoid cross pollution.
Expand Down
16 changes: 8 additions & 8 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ import (

/* The methods in this file provided assumption-ridden HTTP calls for Starr apps. */

func (c *Config) req(path, method string, params url.Values, body io.Reader) ([]byte, error) {
func (c *Config) req(path, method string, params url.Values, body io.Reader) (int, []byte, error) {
if c.Client == nil { // we must have an http client.
return nil, ErrNilClient
return 0, nil, ErrNilClient
}

ctx, cancel := context.WithTimeout(context.Background(), c.Timeout.Duration)
defer cancel()

req, err := http.NewRequestWithContext(ctx, method, c.setPathParams(path, params), body)
if err != nil {
return nil, fmt.Errorf("http.NewRequestWithContext(path): %w", err)
return 0, nil, fmt.Errorf("http.NewRequestWithContext(path): %w", err)
}

c.setHeaders(req)
Expand All @@ -51,25 +51,25 @@ func (c *Config) setHeaders(req *http.Request) {
}

// getBody makes an http request and returns the response body if there are no errors.
func (c *Config) getBody(req *http.Request) ([]byte, error) {
func (c *Config) getBody(req *http.Request) (int, []byte, error) {
resp, err := c.Client.Do(req)
if err != nil {
return nil, fmt.Errorf("httpClient.Do(req): %w", err)
return 0, nil, fmt.Errorf("httpClient.Do(req): %w", err)
}
defer resp.Body.Close()

b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("ioutil.ReadAll: %w", err)
return 0, nil, fmt.Errorf("ioutil.ReadAll: %w", err)
}

// fmt.Println(string(b))
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return b, fmt.Errorf("failed: %v (status: %s): %w: %s",
return resp.StatusCode, b, fmt.Errorf("failed: %v (status: %s): %w: %s",
resp.Request.RequestURI, resp.Status, ErrInvalidStatusCode, string(b))
}

return b, nil
return resp.StatusCode, b, nil
}

// setPathParams makes sure the path starts with /api and returns the full URL.
Expand Down
30 changes: 23 additions & 7 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,38 @@ var _ APIer = (*Config)(nil)

// Get makes a GET http request and returns the body.
func (c *Config) Get(path string, params url.Values) ([]byte, error) {
return c.req(path, http.MethodGet, params, nil)
code, data, err := c.req(path, http.MethodGet, params, nil)
c.Debugf("Sent (%s) to %s, Response (%s): %s (err: %v)",
http.MethodGet, c.setPathParams(path, params), http.StatusText(code), string(data), err)

return data, err
}

// Get makes a DELETE http request and returns the body.
func (c *Config) Delete(path string, params url.Values) ([]byte, error) {
code, data, err := c.req(path, http.MethodDelete, params, nil)
c.Debugf("Sent (%s) to %s, Response (%s): %s (err: %v)",
http.MethodDelete, c.setPathParams(path, params), http.StatusText(code), string(data), err)

return data, err
}

// Put makes a PUT http request and returns the body.
func (c *Config) Put(path string, params url.Values, body []byte) ([]byte, error) {
return c.req(path, http.MethodPut, params, bytes.NewBuffer(body))
code, data, err := c.req(path, http.MethodPut, params, bytes.NewBuffer(body))
c.Debugf("Sent (%s) %d bytes to %s: %s\n Response (%s): %s (err: %v)",
http.MethodPut, len(body), c.setPathParams(path, params), string(body), http.StatusText(code), string(data), err)

return data, err
}

// Post makes a POST http request and returns the body.
func (c *Config) Post(path string, params url.Values, body []byte) ([]byte, error) {
return c.req(path, http.MethodPost, params, bytes.NewBuffer(body))
}
code, data, err := c.req(path, http.MethodPost, params, bytes.NewBuffer(body))
c.Debugf("Sent (%s) %d bytes to %s: %s\n Response (%s): %s (err: %v)",
http.MethodPost, len(body), c.setPathParams(path, params), string(body), http.StatusText(code), string(data), err)

// Get makes a DELETE http request and returns the body.
func (c *Config) Delete(path string, params url.Values) ([]byte, error) {
return c.req(path, http.MethodDelete, params, nil)
return data, err
}

// GetInto performs an HTTP GET against an API path and unmarshals the payload into the provided pointer interface.
Expand Down
10 changes: 5 additions & 5 deletions lidarr/lidarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (l *Lidarr) UpdateQualityProfile(profile *QualityProfile) error {
return fmt.Errorf("json.Marshal(profile): %w", err)
}

_, err = l.Put("v1/qualityProfile/"+strconv.FormatInt(profile.ID, 10), nil, put)
_, err = l.Put("v1/qualityProfile/"+strconv.FormatInt(profile.ID, starr.Bits10), nil, put)
if err != nil {
return fmt.Errorf("api.Put(qualityProfile): %w", err)
}
Expand Down Expand Up @@ -187,7 +187,7 @@ func (l *Lidarr) GetArtist(mbID string) ([]*Artist, error) {
func (l *Lidarr) GetArtistByID(artistID int64) (*Artist, error) {
var artist Artist

err := l.GetInto("v1/artist/"+strconv.FormatInt(artistID, 10), nil, &artist)
err := l.GetInto("v1/artist/"+strconv.FormatInt(artistID, starr.Bits10), nil, &artist)
if err != nil {
return &artist, fmt.Errorf("api.Get(artist): %w", err)
}
Expand Down Expand Up @@ -227,7 +227,7 @@ func (l *Lidarr) UpdateArtist(artist *Artist) (*Artist, error) {

var output Artist

err = l.PutInto("v1/artist/"+strconv.FormatInt(artist.ID, 10), params, body, &output)
err = l.PutInto("v1/artist/"+strconv.FormatInt(artist.ID, starr.Bits10), params, body, &output)
if err != nil {
return nil, fmt.Errorf("api.Put(artist): %w", err)
}
Expand Down Expand Up @@ -258,7 +258,7 @@ func (l *Lidarr) GetAlbum(mbID string) ([]*Album, error) {
func (l *Lidarr) GetAlbumByID(albumID int64) (*Album, error) {
var album Album

err := l.GetInto("v1/album/"+strconv.FormatInt(albumID, 10), nil, &album)
err := l.GetInto("v1/album/"+strconv.FormatInt(albumID, starr.Bits10), nil, &album)
if err != nil {
return nil, fmt.Errorf("api.Get(album): %w", err)
}
Expand All @@ -278,7 +278,7 @@ func (l *Lidarr) UpdateAlbum(albumID int64, album *Album) (*Album, error) {

var output Album

err = l.PutInto("v1/album/"+strconv.FormatInt(albumID, 10), params, put, &output)
err = l.PutInto("v1/album/"+strconv.FormatInt(albumID, starr.Bits10), params, put, &output)
if err != nil {
return nil, fmt.Errorf("api.Put(album): %w", err)
}
Expand Down
4 changes: 4 additions & 0 deletions lidarr/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ func New(c *starr.Config) *Lidarr {
}
}

if c.Debugf == nil {
c.Debugf = func(string, ...interface{}) {}
}

return &Lidarr{APIer: c}
}

Expand Down
10 changes: 5 additions & 5 deletions radarr/radarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (r *Radarr) GetQueue(maxRecords, page int) (*Queue, error) {
// GetMovie grabs a movie from the queue, or all movies if tmdbId is 0.
func (r *Radarr) GetMovie(tmdbID int64) ([]*Movie, error) {
params := make(url.Values)
params.Set("tmdbId", strconv.FormatInt(tmdbID, 10))
params.Set("tmdbId", strconv.FormatInt(tmdbID, starr.Bits10))

var movie []*Movie

Expand All @@ -137,7 +137,7 @@ func (r *Radarr) GetMovie(tmdbID int64) ([]*Movie, error) {
func (r *Radarr) GetMovieByID(movieID int64) (*Movie, error) {
var movie Movie

err := r.GetInto("v3/movie/"+strconv.FormatInt(movieID, 10), nil, &movie)
err := r.GetInto("v3/movie/"+strconv.FormatInt(movieID, starr.Bits10), nil, &movie)
if err != nil {
return nil, fmt.Errorf("api.Get(movie): %w", err)
}
Expand Down Expand Up @@ -181,7 +181,7 @@ func (r *Radarr) UpdateQualityProfile(profile *QualityProfile) error {
return fmt.Errorf("json.Marshal(profile): %w", err)
}

_, err = r.Put("v3/qualityProfile/"+strconv.FormatInt(profile.ID, 10), nil, put)
_, err = r.Put("v3/qualityProfile/"+strconv.FormatInt(profile.ID, starr.Bits10), nil, put)
if err != nil {
return fmt.Errorf("api.Put(qualityProfile): %w", err)
}
Expand Down Expand Up @@ -211,7 +211,7 @@ func (r *Radarr) UpdateMovie(movieID int64, movie *Movie) error {
params := make(url.Values)
params.Add("moveFiles", "true")

_, err = r.Put("v3/movie/"+strconv.FormatInt(movieID, 10), params, put)
_, err = r.Put("v3/movie/"+strconv.FormatInt(movieID, starr.Bits10), params, put)
if err != nil {
return fmt.Errorf("api.Put(movie): %w", err)
}
Expand Down Expand Up @@ -254,7 +254,7 @@ func (r *Radarr) DeleteExclusions(ids []int64) error {
var errs string

for _, id := range ids {
_, err := r.Delete("v3/exclusions/"+strconv.FormatInt(id, 10), nil)
_, err := r.Delete("v3/exclusions/"+strconv.FormatInt(id, starr.Bits10), nil)
if err != nil {
errs += err.Error() + " "
}
Expand Down
4 changes: 4 additions & 0 deletions radarr/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ func New(c *starr.Config) *Radarr {
}
}

if c.Debugf == nil {
c.Debugf = func(string, ...interface{}) {}
}

return &Radarr{APIer: c}
}

Expand Down
10 changes: 5 additions & 5 deletions readarr/readarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (r *Readarr) UpdateQualityProfile(profile *QualityProfile) error {
return fmt.Errorf("json.Marshal(profile): %w", err)
}

_, err = r.Put("v1/qualityProfile/"+strconv.FormatInt(profile.ID, 10), nil, put)
_, err = r.Put("v1/qualityProfile/"+strconv.FormatInt(profile.ID, starr.Bits10), nil, put)
if err != nil {
return fmt.Errorf("api.Put(qualityProfile): %w", err)
}
Expand All @@ -156,7 +156,7 @@ func (r *Readarr) UpdateQualityProfile(profile *QualityProfile) error {
func (r *Readarr) GetAuthorByID(authorID int64) (*Author, error) {
var author Author

err := r.GetInto("v1/author/"+strconv.FormatInt(authorID, 10), nil, &author)
err := r.GetInto("v1/author/"+strconv.FormatInt(authorID, starr.Bits10), nil, &author)
if err != nil {
return nil, fmt.Errorf("api.Get(author): %w", err)
}
Expand All @@ -174,7 +174,7 @@ func (r *Readarr) UpdateAuthor(authorID int64, author *Author) error {
params := make(url.Values)
params.Add("moveFiles", "true")

b, err := r.Put("v1/author/"+strconv.FormatInt(authorID, 10), params, put)
b, err := r.Put("v1/author/"+strconv.FormatInt(authorID, starr.Bits10), params, put)
if err != nil {
return fmt.Errorf("api.Put(author): %w", err)
}
Expand Down Expand Up @@ -206,7 +206,7 @@ func (r *Readarr) GetBook(gridID string) ([]*Book, error) {
func (r *Readarr) GetBookByID(bookID int64) (*Book, error) {
var book Book

err := r.GetInto("v1/book/"+strconv.FormatInt(bookID, 10), nil, &book)
err := r.GetInto("v1/book/"+strconv.FormatInt(bookID, starr.Bits10), nil, &book)
if err != nil {
return nil, fmt.Errorf("api.Get(book): %w", err)
}
Expand All @@ -224,7 +224,7 @@ func (r *Readarr) UpdateBook(bookID int64, book *Book) error {
params := make(url.Values)
params.Add("moveFiles", "true")

b, err := r.Put("v1/book/"+strconv.FormatInt(bookID, 10), params, put)
b, err := r.Put("v1/book/"+strconv.FormatInt(bookID, starr.Bits10), params, put)
if err != nil {
return fmt.Errorf("api.Put(book): %w", err)
}
Expand Down
4 changes: 4 additions & 0 deletions readarr/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ func New(c *starr.Config) *Readarr {
}
}

if c.Debugf == nil {
c.Debugf = func(string, ...interface{}) {}
}

return &Readarr{APIer: c}
}

Expand Down
5 changes: 5 additions & 0 deletions shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ const (
Sonarr App = "Sonarr"
)

// Silly constants to not screw up integer->string conversions.
const (
Bits10 = 10
)

// String turns an App name into a string.
func (a App) String() string {
return string(a)
Expand Down
14 changes: 7 additions & 7 deletions sonarr/sonarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (s *Sonarr) UpdateQualityProfile(profile *QualityProfile) error {
return fmt.Errorf("json.Marshal(profile): %w", err)
}

_, err = s.Put("v3/qualityProfile/"+strconv.FormatInt(profile.ID, 10), nil, put)
_, err = s.Put("v3/qualityProfile/"+strconv.FormatInt(profile.ID, starr.Bits10), nil, put)
if err != nil {
return fmt.Errorf("api.Put(qualityProfile): %w", err)
}
Expand Down Expand Up @@ -183,7 +183,7 @@ func (s *Sonarr) UpdateReleaseProfile(profile *ReleaseProfile) error {
return fmt.Errorf("json.Marshal(profile): %w", err)
}

_, err = s.Put("v3/releaseProfile/"+strconv.FormatInt(profile.ID, 10), nil, put)
_, err = s.Put("v3/releaseProfile/"+strconv.FormatInt(profile.ID, starr.Bits10), nil, put)
if err != nil {
return fmt.Errorf("api.Put(releaseProfile): %w", err)
}
Expand All @@ -209,7 +209,7 @@ func (s *Sonarr) GetSeriesLookup(term string, tvdbID int64) ([]*SeriesLookup, er
params := make(url.Values)

if tvdbID > 0 {
params.Add("term", "tvdbid:"+strconv.FormatInt(tvdbID, 10))
params.Add("term", "tvdbid:"+strconv.FormatInt(tvdbID, starr.Bits10))
} else {
params.Add("term", term)
}
Expand All @@ -229,7 +229,7 @@ func (s *Sonarr) GetSeries(tvdbID int64) ([]*Series, error) {
params := make(url.Values)

if tvdbID != 0 {
params.Add("tvdbId", strconv.FormatInt(tvdbID, 10))
params.Add("tvdbId", strconv.FormatInt(tvdbID, starr.Bits10))
}

var series []*Series
Expand All @@ -246,7 +246,7 @@ func (s *Sonarr) GetSeries(tvdbID int64) ([]*Series, error) {
func (s *Sonarr) GetSeriesByID(seriesID int64) (*Series, error) {
var series Series

err := s.GetInto("v3/series/"+strconv.FormatInt(seriesID, 10), nil, &series)
err := s.GetInto("v3/series/"+strconv.FormatInt(seriesID, starr.Bits10), nil, &series)
if err != nil {
return nil, fmt.Errorf("api.Get(series): %w", err)
}
Expand All @@ -270,7 +270,7 @@ func (s *Sonarr) UpdateSeries(seriesID int64, series *Series) error {
params := make(url.Values)
params.Add("moveFiles", "true")

b, err := s.Put("v3/series/"+strconv.FormatInt(seriesID, 10), params, put)
b, err := s.Put("v3/series/"+strconv.FormatInt(seriesID, starr.Bits10), params, put)
if err != nil {
return fmt.Errorf("api.Put(series): %w", err)
}
Expand Down Expand Up @@ -336,7 +336,7 @@ func (s *Sonarr) GetSeriesEpisodes(seriesID int64) ([]*Episode, error) {
var output []*Episode

params := make(url.Values)
params.Add("seriesId", strconv.FormatInt(seriesID, 10))
params.Add("seriesId", strconv.FormatInt(seriesID, starr.Bits10))

err := s.GetInto("v3/episode?seriesId", params, &output)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions sonarr/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ func New(c *starr.Config) *Sonarr {
}
}

if c.Debugf == nil {
c.Debugf = func(string, ...interface{}) {}
}

return &Sonarr{APIer: c}
}

Expand Down
Loading

0 comments on commit e67a084

Please sign in to comment.