Skip to content

Commit

Permalink
feat(diskspace): Implement disk space endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
SkYNewZ committed Apr 8, 2020
1 parent a27c3b0 commit c9c50c4
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 20 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Here are the currently supported endpoints:

- [x] Calendar
- [ ] Command
- [ ] Diskspace
- [x] Diskspace
- [ ] History
- [ ] Movie
- [x] Returns all Movies in your collection
Expand All @@ -25,6 +25,7 @@ Here are the currently supported endpoints:
- [ ] Delete the movie with the given ID
- [ ] Movie Lookup
- [ ] Queue
- [ ] List Exclusions
- [x] System-Status

## Getting started
Expand Down
16 changes: 12 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package radarr

import (
"fmt"
"errors"
"net/http"
"net/url"
"time"
Expand All @@ -17,11 +17,10 @@ type HTTPClientInterface interface {
func New(radarrURL, apiKey string, client HTTPClientInterface) (*Service, error) {
valid, err := url.ParseRequestURI(radarrURL)
if err != nil {
return nil, fmt.Errorf("Please specify a valid URL")
return nil, errors.New("Please specify a valid URL")
}

// if client not specified, defaulting to default http client
// TODO: test it
if client == nil {
d := http.DefaultClient
d.Transport = newTransport()
Expand All @@ -32,6 +31,7 @@ func New(radarrURL, apiKey string, client HTTPClientInterface) (*Service, error)
s := &Service{client: client, url: valid.String(), apiKey: apiKey}
s.Movies = newMovieService(s)
s.SystemStatus = newSystemStatusService(s)
s.Diskspace = newDiskspaceService(s)

return s, nil
}
Expand All @@ -42,6 +42,14 @@ type Service struct {
url string // Radarr base URL
apiKey string

Movies *MovieService
// https://github.com/Radarr/Radarr/wiki/API:Calendar
// https://github.com/Radarr/Radarr/wiki/API:Movie
// https://github.com/Radarr/Radarr/wiki/API:Movie-Lookup
Movies *MovieService

// https://github.com/Radarr/Radarr/wiki/API:System-Status
SystemStatus *SystemStatusService

// https://github.com/Radarr/Radarr/wiki/API:Diskspace
Diskspace *DiskspaceService
}
7 changes: 4 additions & 3 deletions constants.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package radarr

const (
movieURI string = "/movie"
statusURI string = "/system/status"
upcomingURI string = "/calendar"
movieURI string = "/movie"
statusURI string = "/system/status"
upcomingURI string = "/calendar"
diskspaceURI string = "/diskspace"
)
51 changes: 51 additions & 0 deletions disk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package radarr

import (
"encoding/json"
"fmt"
)

// Diskspace disk space Radarr response
type Diskspace struct {
Path string `json:"path"`
Label string `json:"label"`
FreeSpace int64 `json:"freeSpace"`
TotalSpace int64 `json:"totalSpace"`
}

// Diskspaces describe disk space info on your Radarr instance
type Diskspaces []Diskspace

// DiskspaceService contains Radarr diskspace operations
type DiskspaceService struct {
s *Service
}

func newDiskspaceService(s *Service) *DiskspaceService {
return &DiskspaceService{
s: s,
}
}

// Get return Radarr disk space info
func (s *DiskspaceService) Get() (*Diskspaces, error) {
diskspaceURL := fmt.Sprintf("%s/api%s?apikey=%s", s.s.url, diskspaceURI, s.s.apiKey)
response, err := s.s.client.Get(diskspaceURL)
if err != nil {
return nil, err
}

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

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

_ = response.Body.Close()
return &diskspaces, nil
}
108 changes: 108 additions & 0 deletions disk_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package radarr

import (
"net/http"
"reflect"
"testing"

internal "github.com/SkYNewZ/radarr/internal/radarr"
)

func Test_newDiskspaceService(t *testing.T) {
type args struct {
s *Service
}

s := &Service{client: http.DefaultClient, apiKey: internal.DummyAPIKey, url: internal.DummyURL}
tests := []struct {
name string
args args
want *DiskspaceService
}{
struct {
name string
args args
want *DiskspaceService
}{
name: "Constructor",
args: args{s},
want: &DiskspaceService{s},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := newDiskspaceService(tt.args.s); !reflect.DeepEqual(got, tt.want) {
t.Errorf("newDiskspaceService() = %v, want %v", got, tt.want)
}
})
}
}

func TestDiskspaceService_Get(t *testing.T) {
type fields struct {
s *Service
}

var classicService *Service = &Service{client: internal.DummyHTTPClient, url: internal.DummyURL, apiKey: internal.DummyAPIKey}
var badAPIKeyService *Service = &Service{client: internal.DummyHTTPClient, url: internal.DummyURL, apiKey: "bad"}

var exepectedResponse *Diskspaces = &Diskspaces{
Diskspace{
Label: "/",
Path: "/",
FreeSpace: 11059175424,
TotalSpace: 15614754816,
},
Diskspace{
Label: "/home",
Path: "/home",
FreeSpace: 88775757824,
TotalSpace: 98325770240,
},
}

tests := []struct {
name string
fields fields
want *Diskspaces
wantErr bool
}{
struct {
name string
fields fields
want *Diskspaces
wantErr bool
}{
name: "Diskspace lengh should be 2",
fields: fields{classicService},
wantErr: false,
want: exepectedResponse,
},
struct {
name string
fields fields
want *Diskspaces
wantErr bool
}{
name: "Bad API Key",
wantErr: true,
fields: fields{badAPIKeyService},
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &DiskspaceService{
s: tt.fields.s,
}
got, err := s.Get()
if (err != nil) != tt.wantErr {
t.Errorf("DiskspaceService.Get() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("DiskspaceService.Get() = %v, want %v", got, tt.want)
}
})
}
}
1 change: 1 addition & 0 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func parseRadarrResponse(response *http.Response) error {
if err != nil {
return err
}

if body["error"] != "" {
e.Message = body["error"]
} else if body["message"] != "" {
Expand Down
46 changes: 35 additions & 11 deletions internal/radarr/dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
"net/url"
)

var dummyMovieResponse string = `
// DummyMovieResponse describe a dymmy movie
var DummyMovieResponse string = `
{
"title": "Frozen II",
"alternativeTitles": [
Expand Down Expand Up @@ -153,8 +154,23 @@ var dummySystemStatusResponse string = `
"runtimeName": "netCore"
}`

var dummyMoviesResponse string = fmt.Sprintf("[%s, %s]", dummyMovieResponse, dummyMovieResponse)
var dummyUpcomingWithBothFilterResponse = fmt.Sprintf("[%s]", dummyMovieResponse)
var dummyDiskspaceResponse string = `
[{
"path": "/",
"label": "/",
"freeSpace": 11059175424,
"totalSpace": 15614754816
},
{
"path": "/home",
"label": "/home",
"freeSpace": 88775757824,
"totalSpace": 98325770240
}
]`

var dummyMoviesResponse string = fmt.Sprintf("[%s, %s]", DummyMovieResponse, DummyMovieResponse)
var dummyUpcomingWithBothFilterResponse = fmt.Sprintf("[%s]", DummyMovieResponse)

// DummyUnauthorizedResponse describe Unauthorized Radarr response
var DummyUnauthorizedResponse string = `{"error": "Unauthorized"}`
Expand Down Expand Up @@ -218,13 +234,6 @@ func (*mockedTransport2) RoundTrip(req *http.Request) (*http.Response, error) {
return nil, errors.New("foo")
}

// TestCase describe a test case
type TestCase struct {
Title string
Expected interface{}
Got interface{}
}

// HTTPClient implements HTTPClientInterface
type HTTPClient struct{}

Expand Down Expand Up @@ -254,7 +263,7 @@ func (c *HTTPClient) Get(targetURL string) (resp *http.Response, err error) {
return &http.Response{
StatusCode: http.StatusOK,
Status: http.StatusText(http.StatusOK),
Body: ioutil.NopCloser(bytes.NewBufferString(dummyMovieResponse)),
Body: ioutil.NopCloser(bytes.NewBufferString(DummyMovieResponse)),
}, nil

case fmt.Sprintf("%s/api%s?apikey=%s", DummyURL, "/movie", DummyAPIKey):
Expand All @@ -281,6 +290,14 @@ func (c *HTTPClient) Get(targetURL string) (resp *http.Response, err error) {
Body: ioutil.NopCloser(bytes.NewBufferString(dummyMoviesResponse)),
}, nil

case fmt.Sprintf("%s/api%s?apikey=%s&end=%s", DummyURL, "/calendar", DummyAPIKey, dummyEndDate):
// Upcoming movies with end filter. Returns 0 movies
return &http.Response{
StatusCode: http.StatusOK,
Status: http.StatusText(http.StatusOK),
Body: ioutil.NopCloser(bytes.NewBufferString(dummyEmptyListResponse)),
}, nil

case fmt.Sprintf("%s/api%s?apikey=%s&start=%s&end=%s", DummyURL, "/calendar", DummyAPIKey, dummyStartDate, dummyEndDate):
// Upcoming movies with start filter and end filter. Return 1 movies
return &http.Response{
Expand All @@ -304,6 +321,13 @@ func (c *HTTPClient) Get(targetURL string) (resp *http.Response, err error) {
Body: ioutil.NopCloser(bytes.NewBufferString(dummySystemStatusResponse)),
}, nil

case fmt.Sprintf("%s/api%s?apikey=%s", DummyURL, "/diskspace", DummyAPIKey):
return &http.Response{
StatusCode: http.StatusOK,
Status: http.StatusText(http.StatusOK),
Body: ioutil.NopCloser(bytes.NewBufferString(dummyDiskspaceResponse)),
}, nil

default:
// Defaulting to 404
return &http.Response{
Expand Down
3 changes: 2 additions & 1 deletion movie.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package radarr

import (
"encoding/json"
"errors"
"fmt"
"net/url"
"time"
Expand Down Expand Up @@ -196,7 +197,7 @@ func (m *MovieService) Upcoming(opts ...*UpcomingOptions) (*Movies, error) {
// If both dates are filled, verify order
if opts[0].Start != nil && opts[0].End != nil {
if opts[0].End.Before(*opts[0].Start) || opts[0].Start.After(*opts[0].End) {
return nil, fmt.Errorf("Incorrect dates. Please ensure date are set properly")
return nil, errors.New("Incorrect dates. Please ensure date are set properly")
}
}

Expand Down

0 comments on commit c9c50c4

Please sign in to comment.