Skip to content

Commit

Permalink
fix: Use more defer function
Browse files Browse the repository at this point in the history
  • Loading branch information
SkYNewZ committed Apr 26, 2020
1 parent d7a421c commit b9a834c
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 28 deletions.
8 changes: 4 additions & 4 deletions disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,22 @@ func newDiskspaceService(s *Service) *DiskspaceService {
// Get return Radarr disk space info
func (s *DiskspaceService) Get() (*Diskspaces, error) {
diskspaceURL := fmt.Sprintf("%s/api%s", s.s.url, diskspaceURI)
response, err := s.s.client.Get(diskspaceURL)
resp, err := s.s.client.Get(diskspaceURL)
if err != nil {
return nil, err
}
defer resp.Body.Close()

err = parseRadarrResponse(response)
err = parseRadarrResponse(resp)
if err != nil {
return nil, err
}

var diskspaces Diskspaces
err = json.NewDecoder(response.Body).Decode(&diskspaces)
err = json.NewDecoder(resp.Body).Decode(&diskspaces)
if err != nil {
return nil, err
}

_ = response.Body.Close()
return &diskspaces, nil
}
1 change: 1 addition & 0 deletions dummy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func helperLoadBytes(name string) []byte {
if err != nil {
panic(err)
}

return bytes
}

Expand Down
9 changes: 6 additions & 3 deletions history.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,19 +125,22 @@ func (s *HistoryService) paginate(page int) (*History, error) {
q.Add("pageSize", "50")

request.URL.RawQuery = q.Encode()
response, err := s.s.client.Do(request)
resp, err := s.s.client.Do(request)
if err != nil {
return nil, err
}
err = parseRadarrResponse(response)
defer resp.Body.Close()

err = parseRadarrResponse(resp)
if err != nil {
return nil, err
}

var history History
err = json.NewDecoder(response.Body).Decode(&history)
err = json.NewDecoder(resp.Body).Decode(&history)
if err != nil {
return nil, err
}

return &history, nil
}
6 changes: 3 additions & 3 deletions lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func (l *LookupService) Plain(term string) (Movies, error) {
if err != nil {
return nil, err
}
defer resp.Body.Close()

err = parseRadarrResponse(resp)
if err != nil {
Expand All @@ -51,7 +52,6 @@ func (l *LookupService) Plain(term string) (Movies, error) {
return nil, err
}

_ = resp.Body.Close()
return movies, nil
}

Expand All @@ -72,6 +72,7 @@ func (l *LookupService) Tmdb(TMDBID int) (*Movie, error) {
if err != nil {
return nil, err
}
defer resp.Body.Close()

err = parseRadarrResponse(resp)
if err != nil {
Expand All @@ -84,7 +85,6 @@ func (l *LookupService) Tmdb(TMDBID int) (*Movie, error) {
return nil, err
}

_ = resp.Body.Close()
return &movie, nil
}

Expand All @@ -110,6 +110,7 @@ func (l *LookupService) Imdb(IMDBID string) (*Movie, error) {
if err != nil {
return nil, err
}
defer resp.Body.Close()

err = parseRadarrResponse(resp)
if err != nil {
Expand All @@ -122,6 +123,5 @@ func (l *LookupService) Imdb(IMDBID string) (*Movie, error) {
return nil, err
}

_ = resp.Body.Close()
return &movie, nil
}
28 changes: 14 additions & 14 deletions movie.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,47 +167,47 @@ type UpcomingOptions struct {
// https://github.com/Radarr/Radarr/wiki/API:Movie#getid
func (m *MovieService) Get(movieID int) (*Movie, error) {
movieURL := fmt.Sprintf("%s/api%s/%d", m.s.url, movieURI, movieID)
response, err := m.s.client.Get(movieURL)
resp, err := m.s.client.Get(movieURL)
if err != nil {
return nil, err
}
defer resp.Body.Close()

err = parseRadarrResponse(response)
err = parseRadarrResponse(resp)
if err != nil {
return nil, err
}

var movie Movie
err = json.NewDecoder(response.Body).Decode(&movie)
err = json.NewDecoder(resp.Body).Decode(&movie)
if err != nil {
return nil, err
}

_ = response.Body.Close()
return &movie, nil
}

// List Returns the movie with the matching ID or eerror if no matching movie is found
// https://github.com/Radarr/Radarr/wiki/API:Movie#get
func (m *MovieService) List() (Movies, error) {
moviesURL := fmt.Sprintf("%s/api%s", m.s.url, movieURI)
response, err := m.s.client.Get(moviesURL)
resp, err := m.s.client.Get(moviesURL)
if err != nil {
return nil, err
}
defer resp.Body.Close()

err = parseRadarrResponse(response)
err = parseRadarrResponse(resp)
if err != nil {
return nil, err
}

var movies Movies
err = json.NewDecoder(response.Body).Decode(&movies)
err = json.NewDecoder(resp.Body).Decode(&movies)
if err != nil {
return nil, err
}

_ = response.Body.Close()
return movies, nil
}

Expand Down Expand Up @@ -245,23 +245,23 @@ func (m *MovieService) Upcoming(opts ...*UpcomingOptions) (Movies, error) {
}
}

response, err := m.s.client.Do(req)
resp, err := m.s.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

err = parseRadarrResponse(response)
err = parseRadarrResponse(resp)
if err != nil {
return nil, err
}

var movies Movies
err = json.NewDecoder(response.Body).Decode(&movies)
err = json.NewDecoder(resp.Body).Decode(&movies)
if err != nil {
return nil, err
}

_ = response.Body.Close()
return movies, nil
}

Expand All @@ -286,9 +286,9 @@ func (m *MovieService) Delete(movie *Movie, opts ...*DeleteMovieOptions) error {
if err != nil {
return err
}
defer resp.Body.Close()

err = parseRadarrResponse(resp)
_ = resp.Body.Close()
return err
}

Expand All @@ -299,6 +299,7 @@ func (m *MovieService) Excluded() (ExcludedMovies, error) {
if err != nil {
return nil, err
}
defer resp.Body.Close()

err = parseRadarrResponse(resp)
if err != nil {
Expand All @@ -311,6 +312,5 @@ func (m *MovieService) Excluded() (ExcludedMovies, error) {
return nil, err
}

_ = resp.Body.Close()
return movies, nil
}
8 changes: 4 additions & 4 deletions status.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,22 @@ func newSystemStatusService(s *Service) *SystemStatusService {
// Get https://github.com/Radarr/Radarr/wiki/API:System-Status#get
func (s *SystemStatusService) Get() (*SystemStatus, error) {
statusURL := fmt.Sprintf("%s/api%s", s.s.url, statusURI)
response, err := s.s.client.Get(statusURL)
resp, err := s.s.client.Get(statusURL)
if err != nil {
return nil, err
}
defer resp.Body.Close()

err = parseRadarrResponse(response)
err = parseRadarrResponse(resp)
if err != nil {
return nil, err
}

var status SystemStatus
err = json.NewDecoder(response.Body).Decode(&status)
err = json.NewDecoder(resp.Body).Decode(&status)
if err != nil {
return nil, err
}

_ = response.Body.Close()
return &status, nil
}

0 comments on commit b9a834c

Please sign in to comment.