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