golift.io/starr@v1.0.0/radarr/command.go (about)

     1  package radarr
     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/v3/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  	MovieIDs []int64 `json:"movieIds,omitempty"`
    20  }
    21  
    22  // CommandResponse comes from the /api/v3/command endpoint.
    23  type CommandResponse struct {
    24  	ID                  int64                  `json:"id"`
    25  	Name                string                 `json:"name"`
    26  	CommandName         string                 `json:"commandName"`
    27  	Message             string                 `json:"message,omitempty"`
    28  	Priority            string                 `json:"priority"`
    29  	Status              string                 `json:"status"`
    30  	Queued              time.Time              `json:"queued"`
    31  	Started             time.Time              `json:"started,omitempty"`
    32  	Ended               time.Time              `json:"ended,omitempty"`
    33  	StateChangeTime     time.Time              `json:"stateChangeTime,omitempty"`
    34  	LastExecutionTime   time.Time              `json:"lastExecutionTime,omitempty"`
    35  	Duration            string                 `json:"duration,omitempty"`
    36  	Trigger             string                 `json:"trigger"`
    37  	SendUpdatesToClient bool                   `json:"sendUpdatesToClient"`
    38  	UpdateScheduledTask bool                   `json:"updateScheduledTask"`
    39  	Body                map[string]interface{} `json:"body"`
    40  }
    41  
    42  // GetCommands returns all available Radarr commands.
    43  func (r *Radarr) GetCommands() ([]*CommandResponse, error) {
    44  	return r.GetCommandsContext(context.Background())
    45  }
    46  
    47  // GetCommandsContext returns all available Radarr commands.
    48  func (r *Radarr) GetCommandsContext(ctx context.Context) ([]*CommandResponse, error) {
    49  	var output []*CommandResponse
    50  
    51  	req := starr.Request{URI: bpCommand}
    52  	if err := r.GetInto(ctx, req, &output); err != nil {
    53  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
    54  	}
    55  
    56  	return output, nil
    57  }
    58  
    59  // SendCommand sends a command to Radarr.
    60  func (r *Radarr) SendCommand(cmd *CommandRequest) (*CommandResponse, error) {
    61  	return r.SendCommandContext(context.Background(), cmd)
    62  }
    63  
    64  // SendCommandContext sends a command to Radarr.
    65  func (r *Radarr) SendCommandContext(ctx context.Context, cmd *CommandRequest) (*CommandResponse, error) {
    66  	var output CommandResponse
    67  
    68  	if cmd == nil || cmd.Name == "" {
    69  		return &output, nil
    70  	}
    71  
    72  	var body bytes.Buffer
    73  	if err := json.NewEncoder(&body).Encode(cmd); err != nil {
    74  		return nil, fmt.Errorf("json.Marshal(%s): %w", bpCommand, err)
    75  	}
    76  
    77  	req := starr.Request{URI: bpCommand, Body: &body}
    78  	if err := r.PostInto(ctx, req, &output); err != nil {
    79  		return nil, fmt.Errorf("api.Post(%s): %w", &req, err)
    80  	}
    81  
    82  	return &output, nil
    83  }