golift.io/starr@v1.0.0/lidarr/notification.go (about) 1 package lidarr 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 OnReleaseImport bool `json:"onReleaseImport,omitempty"` 20 OnUpgrade bool `json:"onUpgrade,omitempty"` 21 OnRename bool `json:"onRename,omitempty"` 22 OnTrackRetag bool `json:"onTrackRetag,omitempty"` 23 OnHealthIssue bool `json:"onHealthIssue,omitempty"` 24 OnDownloadFailure bool `json:"onDownloadFailure,omitempty"` 25 OnImportFailure bool `json:"onImportFailure,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,omitempty"` 39 OnReleaseImport bool `json:"onReleaseImport,omitempty"` 40 OnUpgrade bool `json:"onUpgrade,omitempty"` 41 OnRename bool `json:"onRename,omitempty"` 42 OnTrackRetag bool `json:"onTrackRetag,omitempty"` 43 OnHealthIssue bool `json:"onHealthIssue,omitempty"` 44 OnDownloadFailure bool `json:"onDownloadFailure,omitempty"` 45 OnImportFailure bool `json:"onImportFailure,omitempty"` 46 OnApplicationUpdate bool `json:"onApplicationUpdate,omitempty"` 47 SupportsOnGrab bool `json:"supportsOnGrab"` 48 SupportsOnReleaseImport bool `json:"supportsOnReleaseImport"` 49 SupportsOnUpgrade bool `json:"supportsOnUpgrade"` 50 SupportsOnRename bool `json:"supportsOnRename"` 51 SupportsOnApplicationUpdate bool `json:"supportsOnApplicationUpdate"` 52 SupportsOnDownloadFailure bool `json:"supportsOnDownloadFailure"` 53 SupportsOnImportFailure bool `json:"supportsOnImportFailure"` 54 SupportsOnTrackRetag bool `json:"supportsOnTrackRetag"` 55 SupportsOnHealthIssue bool `json:"supportsOnHealthIssue"` 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 (l *Lidarr) GetNotifications() ([]*NotificationOutput, error) { 69 return l.GetNotificationsContext(context.Background()) 70 } 71 72 // GetNotificationsContext returns all configured notifications. 73 func (l *Lidarr) GetNotificationsContext(ctx context.Context) ([]*NotificationOutput, error) { 74 var output []*NotificationOutput 75 76 req := starr.Request{URI: bpNotification} 77 if err := l.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 (l *Lidarr) GetNotification(notificationID int) (*NotificationOutput, error) { 86 return l.GetNotificationContext(context.Background(), notificationID) 87 } 88 89 // GetNotificationContext returns a single notification. 90 func (l *Lidarr) 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 := l.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 (l *Lidarr) AddNotification(notification *NotificationInput) (*NotificationOutput, error) { 103 return l.AddNotificationContext(context.Background(), notification) 104 } 105 106 // AddNotificationContext creates a notification. 107 func (l *Lidarr) 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 := l.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 (l *Lidarr) UpdateNotification(notification *NotificationInput) (*NotificationOutput, error) { 125 return l.UpdateNotificationContext(context.Background(), notification) 126 } 127 128 // UpdateNotificationContext updates the notification. 129 func (l *Lidarr) 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 := l.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 (l *Lidarr) DeleteNotification(notificationID int64) error { 149 return l.DeleteNotificationContext(context.Background(), notificationID) 150 } 151 152 func (l *Lidarr) DeleteNotificationContext(ctx context.Context, notificationID int64) error { 153 req := starr.Request{URI: path.Join(bpNotification, fmt.Sprint(notificationID))} 154 if err := l.DeleteAny(ctx, req); err != nil { 155 return fmt.Errorf("api.Delete(%s): %w", &req, err) 156 } 157 158 return nil 159 }