golift.io/starr@v1.0.0/sonarr/notification.go (about) 1 package sonarr 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 OnSeriesDelete bool `json:"onSeriesDelete,omitempty"` 23 OnEpisodeFileDelete bool `json:"onEpisodeFileDelete,omitempty"` 24 OnEpisodeFileDeleteForUpgrade bool `json:"onEpisodeFileDeleteForUpgrade,omitempty"` 25 OnHealthIssue bool `json:"onHealthIssue,omitempty"` 26 OnApplicationUpdate bool `json:"onApplicationUpdate,omitempty"` 27 IncludeHealthWarnings bool `json:"includeHealthWarnings,omitempty"` 28 ID int64 `json:"id,omitempty"` 29 Name string `json:"name"` 30 Implementation string `json:"implementation"` 31 ConfigContract string `json:"configContract"` 32 Tags []int `json:"tags,omitempty"` 33 Fields []*starr.FieldInput `json:"fields"` 34 } 35 36 // NotificationOutput is the output from the notification methods. 37 type NotificationOutput struct { 38 OnGrab bool `json:"onGrab"` 39 OnDownload bool `json:"onDownload"` 40 OnUpgrade bool `json:"onUpgrade"` 41 OnRename bool `json:"onRename"` 42 OnSeriesDelete bool `json:"onSeriesDelete"` 43 OnEpisodeFileDelete bool `json:"onEpisodeFileDelete"` 44 OnEpisodeFileDeleteForUpgrade bool `json:"onEpisodeFileDeleteForUpgrade"` 45 OnHealthIssue bool `json:"onHealthIssue"` 46 OnApplicationUpdate bool `json:"onApplicationUpdate"` 47 SupportsOnGrab bool `json:"supportsOnGrab"` 48 SupportsOnDownload bool `json:"supportsOnDownload"` 49 SupportsOnUpgrade bool `json:"supportsOnUpgrade"` 50 SupportsOnRename bool `json:"supportsOnRename"` 51 SupportsOnSeriesDelete bool `json:"supportsOnSeriesDelete"` 52 SupportsOnEpisodeFileDelete bool `json:"supportsOnEpisodeFileDelete"` 53 SupportsOnEpisodeFileDeleteForUpgrade bool `json:"supportsOnEpisodeFileDeleteForUpgrade"` 54 SupportsOnHealthIssue bool `json:"supportsOnHealthIssue"` 55 SupportsOnApplicationUpdate bool `json:"supportsOnApplicationUpdate"` 56 IncludeHealthWarnings bool `json:"includeHealthWarnings"` 57 ID int64 `json:"id"` 58 Name string `json:"name"` 59 ImplementationName string `json:"implementationName"` 60 Implementation string `json:"implementation"` 61 ConfigContract string `json:"configContract"` 62 InfoLink string `json:"infoLink"` 63 Tags []int `json:"tags"` 64 Fields []*starr.FieldOutput `json:"fields"` 65 } 66 67 // GetNotifications returns all configured notifications. 68 func (s *Sonarr) GetNotifications() ([]*NotificationOutput, error) { 69 return s.GetNotificationsContext(context.Background()) 70 } 71 72 // GetNotificationsContext returns all configured notifications. 73 func (s *Sonarr) GetNotificationsContext(ctx context.Context) ([]*NotificationOutput, error) { 74 var output []*NotificationOutput 75 76 req := starr.Request{URI: bpNotification} 77 if err := s.GetInto(ctx, req, &output); err != nil { 78 return nil, fmt.Errorf("api.Get(%s): %w", &req, err) 79 } 80 81 return output, nil 82 } 83 84 // GetNotification returns a single notification. 85 func (s *Sonarr) GetNotification(notificationID int) (*NotificationOutput, error) { 86 return s.GetNotificationContext(context.Background(), notificationID) 87 } 88 89 // GetNotificationContext returns a single notification. 90 func (s *Sonarr) GetNotificationContext(ctx context.Context, notificationID int) (*NotificationOutput, error) { 91 var output NotificationOutput 92 93 req := starr.Request{URI: path.Join(bpNotification, fmt.Sprint(notificationID))} 94 if err := s.GetInto(ctx, req, &output); err != nil { 95 return nil, fmt.Errorf("api.Get(%s): %w", &req, err) 96 } 97 98 return &output, nil 99 } 100 101 // AddNotification creates a notification. 102 func (s *Sonarr) AddNotification(notification *NotificationInput) (*NotificationOutput, error) { 103 return s.AddNotificationContext(context.Background(), notification) 104 } 105 106 // AddNotificationContext creates a notification. 107 func (s *Sonarr) AddNotificationContext(ctx context.Context, client *NotificationInput) (*NotificationOutput, error) { 108 var output NotificationOutput 109 110 var body bytes.Buffer 111 if err := json.NewEncoder(&body).Encode(client); err != nil { 112 return nil, fmt.Errorf("json.Marshal(%s): %w", bpNotification, err) 113 } 114 115 req := starr.Request{URI: bpNotification, Body: &body} 116 if err := s.PostInto(ctx, req, &output); err != nil { 117 return nil, fmt.Errorf("api.Post(%s): %w", &req, err) 118 } 119 120 return &output, nil 121 } 122 123 // UpdateNotification updates the notification. 124 func (s *Sonarr) UpdateNotification(notification *NotificationInput) (*NotificationOutput, error) { 125 return s.UpdateNotificationContext(context.Background(), notification) 126 } 127 128 // UpdateNotificationContext updates the notification. 129 func (s *Sonarr) UpdateNotificationContext(ctx context.Context, 130 client *NotificationInput, 131 ) (*NotificationOutput, error) { 132 var output NotificationOutput 133 134 var body bytes.Buffer 135 if err := json.NewEncoder(&body).Encode(client); err != nil { 136 return nil, fmt.Errorf("json.Marshal(%s): %w", bpNotification, err) 137 } 138 139 req := starr.Request{URI: path.Join(bpNotification, fmt.Sprint(client.ID)), Body: &body} 140 if err := s.PutInto(ctx, req, &output); err != nil { 141 return nil, fmt.Errorf("api.Put(%s): %w", &req, err) 142 } 143 144 return &output, nil 145 } 146 147 // DeleteNotification removes a single notification. 148 func (s *Sonarr) DeleteNotification(notificationID int64) error { 149 return s.DeleteNotificationContext(context.Background(), notificationID) 150 } 151 152 func (s *Sonarr) DeleteNotificationContext(ctx context.Context, notificationID int64) error { 153 req := starr.Request{URI: path.Join(bpNotification, fmt.Sprint(notificationID))} 154 if err := s.DeleteAny(ctx, req); err != nil { 155 return fmt.Errorf("api.Delete(%s): %w", &req, err) 156 } 157 158 return nil 159 }