-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(diskspace): Implement disk space endpoint
- Loading branch information
Showing
8 changed files
with
215 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters