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

     1  package lidarr
     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/v1/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  	AlbumIDs []int64  `json:"albumIds,omitempty"`
    21  	AlbumID  int64    `json:"albumId,omitempty"`
    22  	Folders  []string `json:"folders,omitempty"`
    23  	ArtistID int64    `json:"artistId,omitempty"`
    24  }
    25  
    26  // CommandResponse comes from the /api/v1/command endpoint.
    27  type CommandResponse struct {
    28  	ID                  int64                  `json:"id"`
    29  	Name                string                 `json:"name"`
    30  	CommandName         string                 `json:"commandName"`
    31  	Message             string                 `json:"message,omitempty"`
    32  	Priority            string                 `json:"priority"`
    33  	Status              string                 `json:"status"`
    34  	Queued              time.Time              `json:"queued"`
    35  	Started             time.Time              `json:"started,omitempty"`
    36  	Ended               time.Time              `json:"ended,omitempty"`
    37  	StateChangeTime     time.Time              `json:"stateChangeTime,omitempty"`
    38  	LastExecutionTime   time.Time              `json:"lastExecutionTime,omitempty"`
    39  	Duration            string                 `json:"duration,omitempty"`
    40  	Trigger             string                 `json:"trigger"`
    41  	SendUpdatesToClient bool                   `json:"sendUpdatesToClient"`
    42  	UpdateScheduledTask bool                   `json:"updateScheduledTask"`
    43  	Body                map[string]interface{} `json:"body"`
    44  }
    45  
    46  // GetCommands returns all available Lidarr commands.
    47  func (l *Lidarr) GetCommands() ([]*CommandResponse, error) {
    48  	return l.GetCommandsContext(context.Background())
    49  }
    50  
    51  // GetCommandsContext returns all available Lidarr commands.
    52  func (l *Lidarr) GetCommandsContext(ctx context.Context) ([]*CommandResponse, error) {
    53  	var output []*CommandResponse
    54  
    55  	req := starr.Request{URI: bpCommand}
    56  	if err := l.GetInto(ctx, req, &output); err != nil {
    57  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
    58  	}
    59  
    60  	return output, nil
    61  }
    62  
    63  // SendCommand sends a command to Lidarr.
    64  func (l *Lidarr) SendCommand(cmd *CommandRequest) (*CommandResponse, error) {
    65  	return l.SendCommandContext(context.Background(), cmd)
    66  }
    67  
    68  // SendCommandContext sends a command to Lidarr.
    69  func (l *Lidarr) SendCommandContext(ctx context.Context, cmd *CommandRequest) (*CommandResponse, error) {
    70  	var output CommandResponse
    71  
    72  	if cmd == nil || cmd.Name == "" {
    73  		return &output, nil
    74  	}
    75  
    76  	var body bytes.Buffer
    77  	if err := json.NewEncoder(&body).Encode(cmd); err != nil {
    78  		return nil, fmt.Errorf("json.Marshal(%s): %w", bpCommand, err)
    79  	}
    80  
    81  	req := starr.Request{URI: bpCommand, Body: &body}
    82  	if err := l.PostInto(ctx, req, &output); err != nil {
    83  		return nil, fmt.Errorf("api.Post(%s): %w", &req, err)
    84  	}
    85  
    86  	return &output, nil
    87  }
    88  
    89  // GetCommandStatus returns the status of an already started command.
    90  func (l *Lidarr) GetCommandStatus(commandID int64) (*CommandResponse, error) {
    91  	return l.GetCommandStatusContext(context.Background(), commandID)
    92  }
    93  
    94  // GetCommandStatusContext returns the status of an already started command.
    95  func (l *Lidarr) GetCommandStatusContext(ctx context.Context, commandID int64) (*CommandResponse, error) {
    96  	var output CommandResponse
    97  
    98  	if commandID == 0 {
    99  		return &output, nil
   100  	}
   101  
   102  	req := starr.Request{URI: path.Join(bpCommand, fmt.Sprint(commandID))}
   103  	if err := l.GetInto(ctx, req, &output); err != nil {
   104  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
   105  	}
   106  
   107  	return &output, nil
   108  }