golift.io/starr@v1.0.0/lidarr/blocklist.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 // Define Base Path for Block List queries. 15 const bpBlocklist = APIver + "/blocklist" 16 17 // BlockList represents the /api/v1/blocklist endpoint. 18 type BlockList struct { 19 Page int `json:"page"` 20 PageSize int `json:"pageSize"` 21 SortKey string `json:"sortKey"` 22 SortDirection string `json:"sortDirection"` 23 TotalRecords int `json:"totalRecords"` 24 Records []*BlockListRecord `json:"records"` 25 } 26 27 // BlockListRecord represents a single block list item. 28 type BlockListRecord struct { 29 Artist *Artist `json:"artist"` 30 Quality *starr.Quality `json:"quality"` 31 CustomFormats []interface{} `json:"customFormats"` 32 AlbumIDs []int64 `json:"albumIds"` 33 ID int64 `json:"id"` 34 ArtistID int64 `json:"artistId"` 35 Date time.Time `json:"date"` 36 SourceTitle string `json:"sourceTitle"` 37 Protocol string `json:"protocol"` 38 Indexer string `json:"indexer"` 39 Message string `json:"message"` 40 } 41 42 // GetBlockList returns the count of block list items requested. 43 // If you need control over the page, use GetBlockListPage(). 44 func (l *Lidarr) GetBlockList(count int) (*BlockList, error) { 45 return l.GetBlockListContext(context.Background(), count) 46 } 47 48 // GetBlockListContext returns block list items. 49 func (l *Lidarr) GetBlockListContext(ctx context.Context, records int) (*BlockList, error) { 50 list := &BlockList{Records: []*BlockListRecord{}} 51 perPage := starr.SetPerPage(records, 0) 52 53 for page := 1; ; page++ { 54 curr, err := l.GetBlockListPageContext(ctx, &starr.PageReq{PageSize: perPage, Page: page}) 55 if err != nil { 56 return nil, err 57 } 58 59 list.Records = append(list.Records, curr.Records...) 60 if len(list.Records) >= curr.TotalRecords || 61 (len(list.Records) >= records && records != 0) || 62 len(curr.Records) == 0 { 63 list.PageSize = curr.TotalRecords 64 list.TotalRecords = curr.TotalRecords 65 list.SortDirection = curr.SortDirection 66 list.SortKey = curr.SortKey 67 68 break 69 } 70 71 perPage = starr.AdjustPerPage(records, curr.TotalRecords, len(list.Records), perPage) 72 } 73 74 return list, nil 75 } 76 77 // GetBlockListPage returns block list items based on filters. 78 func (l *Lidarr) GetBlockListPage(params *starr.PageReq) (*BlockList, error) { 79 return l.GetBlockListPageContext(context.Background(), params) 80 } 81 82 // GetBlockListPageContext returns block list items based on filters. 83 func (l *Lidarr) GetBlockListPageContext(ctx context.Context, params *starr.PageReq) (*BlockList, error) { 84 var output BlockList 85 86 params.CheckSet("sortKey", "date") 87 88 req := starr.Request{URI: bpBlocklist, Query: params.Params()} 89 if err := l.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 // DeleteBlockList removes a single block list item. 97 func (l *Lidarr) DeleteBlockList(listID int64) error { 98 return l.DeleteBlockListContext(context.Background(), listID) 99 } 100 101 // DeleteBlockListContext removes a single block list item with a context. 102 func (l *Lidarr) DeleteBlockListContext(ctx context.Context, listID int64) error { 103 req := starr.Request{URI: path.Join(bpBlocklist, fmt.Sprint(listID))} 104 if err := l.DeleteAny(ctx, req); err != nil { 105 return fmt.Errorf("api.Delete(%s): %w", &req, err) 106 } 107 108 return nil 109 } 110 111 // DeleteBlockLists removes multiple block list items. 112 func (l *Lidarr) DeleteBlockLists(ids []int64) error { 113 return l.DeleteBlockListsContext(context.Background(), ids) 114 } 115 116 // DeleteBlockListsContext removes multiple block list items with a context. 117 func (l *Lidarr) DeleteBlockListsContext(ctx context.Context, ids []int64) error { 118 input := struct { 119 IDs []int64 `json:"ids"` 120 }{IDs: ids} 121 122 var body bytes.Buffer 123 if err := json.NewEncoder(&body).Encode(input); err != nil { 124 return fmt.Errorf("json.Marshal(%s): %w", bpBlocklist, err) 125 } 126 127 req := starr.Request{URI: path.Join(bpBlocklist, "bulk"), Body: &body} 128 if err := l.DeleteAny(ctx, req); err != nil { 129 return fmt.Errorf("api.Delete(%s): %w", &req, err) 130 } 131 132 return nil 133 }