golift.io/starr@v1.0.0/sonarr/command.go (about) 1 package sonarr 2 3 import ( 4 "bytes" 5 "context" 6 "encoding/json" 7 "fmt" 8 "path" 9 "time" 10 11 "golift.io/starr" 12 ) 13 14 const bpCommand = APIver + "/command" 15 16 // CommandRequest goes into the /api/v3/command endpoint. 17 // This was created from the search command and may not support other commands yet. 18 type CommandRequest struct { 19 Name string `json:"name"` 20 Files []int64 `json:"files,omitempty"` // RenameFiles only 21 SeriesIDs []int64 `json:"seriesIds,omitempty"` 22 SeriesID int64 `json:"seriesId,omitempty"` 23 EpisodeIDs []int64 `json:"episodeIds,omitempty"` 24 EpisodeID int64 `json:"episodeId,omitempty"` 25 } 26 27 // CommandResponse comes from the /api/v3/command endpoint. 28 type CommandResponse struct { 29 ID int64 `json:"id"` 30 Name string `json:"name"` 31 CommandName string `json:"commandName"` 32 Message string `json:"message,omitempty"` 33 Priority string `json:"priority"` 34 Status string `json:"status"` 35 Queued time.Time `json:"queued"` 36 Started time.Time `json:"started,omitempty"` 37 Ended time.Time `json:"ended,omitempty"` 38 StateChangeTime time.Time `json:"stateChangeTime,omitempty"` 39 LastExecutionTime time.Time `json:"lastExecutionTime,omitempty"` 40 Duration string `json:"duration,omitempty"` 41 Trigger string `json:"trigger"` 42 SendUpdatesToClient bool `json:"sendUpdatesToClient"` 43 UpdateScheduledTask bool `json:"updateScheduledTask"` 44 Body map[string]interface{} `json:"body"` 45 } 46 47 // GetCommands returns all available Sonarr commands. 48 // These can be used with SendCommand. 49 func (s *Sonarr) GetCommands() ([]*CommandResponse, error) { 50 return s.GetCommandsContext(context.Background()) 51 } 52 53 // GetCommands returns all available Sonarr commands. 54 // These can be used with SendCommand. 55 func (s *Sonarr) GetCommandsContext(ctx context.Context) ([]*CommandResponse, error) { 56 var output []*CommandResponse 57 58 req := starr.Request{URI: bpCommand} 59 if err := s.GetInto(ctx, req, &output); err != nil { 60 return nil, fmt.Errorf("api.Get(%s): %w", &req, err) 61 } 62 63 return output, nil 64 } 65 66 // SendCommand sends a command to Sonarr. 67 func (s *Sonarr) SendCommand(cmd *CommandRequest) (*CommandResponse, error) { 68 return s.SendCommandContext(context.Background(), cmd) 69 } 70 71 // SendCommandContext sends a command to Sonarr. 72 func (s *Sonarr) SendCommandContext(ctx context.Context, cmd *CommandRequest) (*CommandResponse, error) { 73 var output CommandResponse 74 75 if cmd == nil || cmd.Name == "" { 76 return &output, nil 77 } 78 79 var body bytes.Buffer 80 if err := json.NewEncoder(&body).Encode(cmd); err != nil { 81 return nil, fmt.Errorf("json.Marshal(%s): %w", bpCommand, err) 82 } 83 84 req := starr.Request{URI: bpCommand, Body: &body} 85 if err := s.PostInto(ctx, req, &output); err != nil { 86 return nil, fmt.Errorf("api.Post(%s): %w", &req, err) 87 } 88 89 return &output, nil 90 } 91 92 // GetCommandStatus returns the status of an already started command. 93 func (s *Sonarr) GetCommandStatus(commandID int64) (*CommandResponse, error) { 94 return s.GetCommandStatusContext(context.Background(), commandID) 95 } 96 97 // GetCommandStatusContext returns the status of an already started command. 98 func (s *Sonarr) GetCommandStatusContext(ctx context.Context, commandID int64) (*CommandResponse, error) { 99 var output CommandResponse 100 101 if commandID == 0 { 102 return &output, nil 103 } 104 105 req := starr.Request{URI: path.Join(bpCommand, fmt.Sprint(commandID))} 106 if err := s.GetInto(ctx, req, &output); err != nil { 107 return nil, fmt.Errorf("api.Get(%s): %w", &req, err) 108 } 109 110 return &output, nil 111 }