Skip to content

Commit

Permalink
feat(history): Implement history endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
SkYNewZ committed Apr 8, 2020
1 parent 2980635 commit 986d5ad
Show file tree
Hide file tree
Showing 13 changed files with 472 additions and 17 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Here are the currently supported endpoints:
- [x] Calendar
- [ ] Command
- [x] Diskspace
- [ ] History
- [x] History
- [ ] Movie
- [x] Returns all Movies in your collection
- [x] Returns the movie with the matching ID or 404 if no matching movie is found
Expand All @@ -40,7 +40,7 @@ import (
"github.com/SkYNewZ/radarr"
)

// Instanciate a standard client
// Instantiate a standard client
func main() {
client, err := radarr.New("https://my.radarr-instance.fr", "radarr-api-key", nil)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
// HTTPClientInterface interface for the http.Client
type HTTPClientInterface interface {
Get(url string) (resp *http.Response, err error)
Do(req *http.Request) (*http.Response, error)
}

// New Create a Radarr client
Expand All @@ -33,6 +34,7 @@ func New(radarrURL, apiKey string, client HTTPClientInterface) (*Service, error)
s.SystemStatus = newSystemStatusService(s)
s.Diskspace = newDiskspaceService(s)
s.Command = newCommandService(s)
s.History = newHistoryService(s)

return s, nil
}
Expand All @@ -56,4 +58,7 @@ type Service struct {

// https://github.com/Radarr/Radarr/wiki/API:Command
Command *CommandService

// https://github.com/Radarr/Radarr/wiki/API:History
History *HistoryService
}
4 changes: 2 additions & 2 deletions client_logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,11 @@ func Test_isDebugLevel(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
os.Setenv(envLog, tt.name)
_ = os.Setenv(envLog, tt.name)
if got := isDebugLevel(); got != tt.want {
t.Errorf("isDebugLevel() = %v, want %v", got, tt.want)
}
})
}
os.Unsetenv(envLog)
_ = os.Unsetenv(envLog)
}
2 changes: 2 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestNew(t *testing.T) {
serviceWithCustomHTTPClient.Diskspace = newDiskspaceService(serviceWithCustomHTTPClient)
serviceWithCustomHTTPClient.SystemStatus = newSystemStatusService(serviceWithCustomHTTPClient)
serviceWithCustomHTTPClient.Command = newCommandService(serviceWithCustomHTTPClient)
serviceWithCustomHTTPClient.History = newHistoryService(serviceWithCustomHTTPClient)

client := http.Client{}
client.Timeout = time.Second * 10
Expand All @@ -30,6 +31,7 @@ func TestNew(t *testing.T) {
serviceWithDefaultHTTPClient.Diskspace = newDiskspaceService(serviceWithDefaultHTTPClient)
serviceWithDefaultHTTPClient.SystemStatus = newSystemStatusService(serviceWithDefaultHTTPClient)
serviceWithDefaultHTTPClient.Command = newCommandService(serviceWithDefaultHTTPClient)
serviceWithDefaultHTTPClient.History = newHistoryService(serviceWithDefaultHTTPClient)

tests := []struct {
name string
Expand Down
5 changes: 3 additions & 2 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ type Command struct {
ID int `json:"id"`
}

type Tasks []Command
// Commands is a set of commands
type Commands []Command

// CommandService not usable for now
// contains Radarr commands operations
Expand All @@ -45,7 +46,7 @@ func (c *CommandService) Status(commandID string) *Command {
}

// StatusAll Queries the status of all currently started commands.
func (c *CommandService) StatusAll() *Tasks {
func (c *CommandService) StatusAll() *Command {
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestCommandService_StatusAll(t *testing.T) {
tests := []struct {
name string
fields fields
want *Tasks
want *Command
}{
// TODO: Add test cases.
}
Expand Down
3 changes: 2 additions & 1 deletion command_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ const (
Copy
)

// DownloadedMoviesScanOptions available options when using DownloadedMoviesScanCommand
type DownloadedMoviesScanOptions struct {
Path string `json:"path"`
DownloadClientID string `json:"downloadClientId"`
IportMode ImportMode `json:"importMode"`
ImportMode ImportMode `json:"importMode"`
}

var availableImportMode [2]string = [2]string{"Move", "Copy"}
Expand Down
1 change: 1 addition & 0 deletions constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ const (
statusURI string = "/system/status"
upcomingURI string = "/calendar"
diskspaceURI string = "/diskspace"
historyURI string = "/history"
)
4 changes: 2 additions & 2 deletions example_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/SkYNewZ/radarr"
)

// Instanciate a standard client
// Instantiate a standard client
func ExampleNew_basic() {
client, err := radarr.New("https://my.radarr-instance.fr", "radarr-api-key", nil)
if err != nil {
Expand All @@ -23,7 +23,7 @@ func ExampleNew_basic() {
fmt.Printf("%s", movie.Title)
}

// Instanciate a client with a custom HTTP client
// Instantiate a client with a custom HTTP client
func ExampleNew_advanced() {
client, err := radarr.New("https://my.radarr-instance.fr", "radarr-api-key", &http.Client{
Timeout: time.Second * 10,
Expand Down
144 changes: 144 additions & 0 deletions history.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package radarr

import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"time"
)

// History return your Radarr history
type History struct {
Page int `json:"page"`
PageSize int `json:"pageSize"`
SortKey string `json:"sortKey"`
SortDirection string `json:"sortDirection"`
TotalRecords int `json:"totalRecords"`
Records []Record `json:"records"`
}

// Revision movie revisions
type Revision struct {
Version int `json:"version"`
Real int `json:"real"`
IsRepack bool `json:"isRepack"`
}

// Images movie images
type Images struct {
CoverType string `json:"coverType"`
URL string `json:"url"`
}

// Ratings your movies ratings
type Ratings struct {
Votes int `json:"votes"`
Value float64 `json:"value"`
}

// Data contains arbitrary data
type Data struct {
Reason string `json:"reason,omitempty"`
DroppedPath string `json:"droppedPath,omitempty"`
ImportedPath string `json:"importedPath,omitempty"`
Indexer string `json:"indexer,omitempty"`
NzbInfoURL string `json:"nzbInfoUrl,omitempty"`
ReleaseGroup string `json:"releaseGroup,omitempty"`
Age string `json:"age,omitempty"`
AgeHours string `json:"ageHours,omitempty"`
AgeMinutes string `json:"ageMinutes,omitempty"`
PublishedDate time.Time `json:"publishedDate,omitempty"`
DownloadClient string `json:"downloadClient,omitempty"`
Size string `json:"size,omitempty"`
DownloadURL string `json:"downloadUrl,omitempty"`
GUID string `json:"guid,omitempty"`
TvdbID string `json:"tvdbId,omitempty"`
TvRageID string `json:"tvRageId,omitempty"`
Protocol string `json:"protocol,omitempty"`
IndexerFlags string `json:"indexerFlags,omitempty"`
IndexerID string `json:"indexerId,omitempty"`
TorrentInfoHash string `json:"torrentInfoHash,omitempty"`
}

// Record history item
type Record struct {
MovieID int `json:"movieId"`
SourceTitle string `json:"sourceTitle"`
Quality Quality `json:"quality"`
QualityCutoffNotMet bool `json:"qualityCutoffNotMet"`
Date time.Time `json:"date"`
EventType string `json:"eventType"`
Movie Movie `json:"movie"`
ID int `json:"id"`
DownloadID string `json:"downloadId,omitempty"`
Data Data `json:"data,omitempty"`
}

// Records is a set of Record
type Records = []Record

// HistoryService perform actions on your Radarr history
type HistoryService struct {
s *Service
}

func newHistoryService(s *Service) *HistoryService {
return &HistoryService{s: s}
}

// Get return all history
func (s *HistoryService) Get() (*Records, error) {

// First call
var page int = 1
history, err := s.paginate(page)
if err != nil {
return nil, err
}

for {
if len(history.Records) == history.TotalRecords {
break
}

page++
s, err := s.paginate(page)
if err != nil {
break
}

history.Records = append(history.Records, s.Records...)
}

return &history.Records, nil
}

func (s *HistoryService) paginate(page int) (*History, error) {
request, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/api%s", s.s.url, historyURI), nil)
if err != nil {
return nil, err
}

q := request.URL.Query()
q.Add("page", strconv.Itoa(page))
q.Add("pageSize", "50")
q.Add("apikey", s.s.apiKey)

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

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

0 comments on commit 986d5ad

Please sign in to comment.