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

     1  package radarr
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"encoding/json"
     7  	"fmt"
     8  	"path"
     9  
    10  	"golift.io/starr"
    11  )
    12  
    13  // Define Base Path for notification calls.
    14  const bpNotification = APIver + "/notification"
    15  
    16  // NotificationInput is the input for a new or updated notification.
    17  type NotificationInput struct {
    18  	OnGrab                      bool                `json:"onGrab,omitempty"`
    19  	OnDownload                  bool                `json:"onDownload,omitempty"`
    20  	OnUpgrade                   bool                `json:"onUpgrade,omitempty"`
    21  	OnRename                    bool                `json:"onRename,omitempty"`
    22  	OnMovieAdded                bool                `json:"onMovieAdded,omitempty"`
    23  	OnMovieDelete               bool                `json:"onMovieDelete,omitempty"`
    24  	OnMovieFileDelete           bool                `json:"onMovieFileDelete,omitempty"`
    25  	OnMovieFileDeleteForUpgrade bool                `json:"onMovieFileDeleteForUpgrade,omitempty"`
    26  	OnHealthIssue               bool                `json:"onHealthIssue,omitempty"`
    27  	OnApplicationUpdate         bool                `json:"onApplicationUpdate,omitempty"`
    28  	IncludeHealthWarnings       bool                `json:"includeHealthWarnings,omitempty"`
    29  	ID                          int64               `json:"id,omitempty"`
    30  	Name                        string              `json:"name"`
    31  	Implementation              string              `json:"implementation"`
    32  	ConfigContract              string              `json:"configContract"`
    33  	Tags                        []int               `json:"tags,omitempty"`
    34  	Fields                      []*starr.FieldInput `json:"fields"`
    35  }
    36  
    37  // NotificationOutput is the output from the notification methods.
    38  type NotificationOutput struct {
    39  	OnGrab                              bool                 `json:"onGrab,omitempty"`
    40  	OnDownload                          bool                 `json:"onDownload,omitempty"`
    41  	OnUpgrade                           bool                 `json:"onUpgrade,omitempty"`
    42  	OnRename                            bool                 `json:"onRename,omitempty"`
    43  	OnMovieAdded                        bool                 `json:"onMovieAdded,omitempty"`
    44  	OnMovieDelete                       bool                 `json:"onMovieDelete,omitempty"`
    45  	OnMovieFileDelete                   bool                 `json:"onMovieFileDelete,omitempty"`
    46  	OnMovieFileDeleteForUpgrade         bool                 `json:"onMovieFileDeleteForUpgrade,omitempty"`
    47  	OnHealthIssue                       bool                 `json:"onHealthIssue"`
    48  	OnApplicationUpdate                 bool                 `json:"onApplicationUpdate"`
    49  	SupportsOnGrab                      bool                 `json:"supportsOnGrab"`
    50  	SupportsOnDownload                  bool                 `json:"supportsOnDownload"`
    51  	SupportsOnUpgrade                   bool                 `json:"supportsOnUpgrade"`
    52  	SupportsOnRename                    bool                 `json:"supportsOnRename"`
    53  	SupportsOnMovieAdded                bool                 `json:"supportsOnMovieAdded"`
    54  	SupportsOnMovieDelete               bool                 `json:"SupportsOnMovieDelete"`
    55  	SupportsOnMovieFileDelete           bool                 `json:"supportsOnMovieFileDelete"`
    56  	SupportsOnMovieFileDeleteForUpgrade bool                 `json:"supportsOnMovieFileDeleteForUpgrade"`
    57  	SupportsOnHealthIssue               bool                 `json:"supportsOnHealthIssue"`
    58  	SupportsOnApplicationUpdate         bool                 `json:"supportsOnApplicationUpdate"`
    59  	IncludeHealthWarnings               bool                 `json:"includeHealthWarnings"`
    60  	ID                                  int64                `json:"id"`
    61  	Name                                string               `json:"name"`
    62  	ImplementationName                  string               `json:"implementationName"`
    63  	Implementation                      string               `json:"implementation"`
    64  	ConfigContract                      string               `json:"configContract"`
    65  	InfoLink                            string               `json:"infoLink"`
    66  	Tags                                []int                `json:"tags"`
    67  	Fields                              []*starr.FieldOutput `json:"fields"`
    68  }
    69  
    70  // GetNotifications returns all configured notifications.
    71  func (r *Radarr) GetNotifications() ([]*NotificationOutput, error) {
    72  	return r.GetNotificationsContext(context.Background())
    73  }
    74  
    75  // GetNotificationsContext returns all configured notifications.
    76  func (r *Radarr) GetNotificationsContext(ctx context.Context) ([]*NotificationOutput, error) {
    77  	var output []*NotificationOutput
    78  
    79  	req := starr.Request{URI: bpNotification}
    80  	if err := r.GetInto(ctx, req, &output); err != nil {
    81  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
    82  	}
    83  
    84  	return output, nil
    85  }
    86  
    87  // GetNotification returns a single notification.
    88  func (r *Radarr) GetNotification(notificationID int) (*NotificationOutput, error) {
    89  	return r.GetNotificationContext(context.Background(), notificationID)
    90  }
    91  
    92  // GetNotificationContext returns a single notification.
    93  func (r *Radarr) GetNotificationContext(ctx context.Context, notificationID int) (*NotificationOutput, error) {
    94  	var output NotificationOutput
    95  
    96  	req := starr.Request{URI: path.Join(bpNotification, fmt.Sprint(notificationID))}
    97  	if err := r.GetInto(ctx, req, &output); err != nil {
    98  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
    99  	}
   100  
   101  	return &output, nil
   102  }
   103  
   104  // AddNotification creates a notification.
   105  func (r *Radarr) AddNotification(notification *NotificationInput) (*NotificationOutput, error) {
   106  	return r.AddNotificationContext(context.Background(), notification)
   107  }
   108  
   109  // AddNotificationContext creates a notification.
   110  func (r *Radarr) AddNotificationContext(ctx context.Context, client *NotificationInput) (*NotificationOutput, error) {
   111  	var output NotificationOutput
   112  
   113  	var body bytes.Buffer
   114  	if err := json.NewEncoder(&body).Encode(client); err != nil {
   115  		return nil, fmt.Errorf("json.Marshal(%s): %w", bpNotification, err)
   116  	}
   117  
   118  	req := starr.Request{URI: bpNotification, Body: &body}
   119  	if err := r.PostInto(ctx, req, &output); err != nil {
   120  		return nil, fmt.Errorf("api.Post(%s): %w", &req, err)
   121  	}
   122  
   123  	return &output, nil
   124  }
   125  
   126  // UpdateNotification updates the notification.
   127  func (r *Radarr) UpdateNotification(notification *NotificationInput) (*NotificationOutput, error) {
   128  	return r.UpdateNotificationContext(context.Background(), notification)
   129  }
   130  
   131  // UpdateNotificationContext updates the notification.
   132  func (r *Radarr) UpdateNotificationContext(ctx context.Context,
   133  	client *NotificationInput,
   134  ) (*NotificationOutput, error) {
   135  	var output NotificationOutput
   136  
   137  	var body bytes.Buffer
   138  	if err := json.NewEncoder(&body).Encode(client); err != nil {
   139  		return nil, fmt.Errorf("json.Marshal(%s): %w", bpNotification, err)
   140  	}
   141  
   142  	req := starr.Request{URI: path.Join(bpNotification, fmt.Sprint(client.ID)), Body: &body}
   143  	if err := r.PutInto(ctx, req, &output); err != nil {
   144  		return nil, fmt.Errorf("api.Put(%s): %w", &req, err)
   145  	}
   146  
   147  	return &output, nil
   148  }
   149  
   150  // DeleteNotification removes a single notification.
   151  func (r *Radarr) DeleteNotification(notificationID int64) error {
   152  	return r.DeleteNotificationContext(context.Background(), notificationID)
   153  }
   154  
   155  func (r *Radarr) DeleteNotificationContext(ctx context.Context, notificationID int64) error {
   156  	req := starr.Request{URI: path.Join(bpNotification, fmt.Sprint(notificationID))}
   157  	if err := r.DeleteAny(ctx, req); err != nil {
   158  		return fmt.Errorf("api.Delete(%s): %w", &req, err)
   159  	}
   160  
   161  	return nil
   162  }