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