client.go 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package client // import "miniflux.app/v2/client"
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "io"
  9. "net/http"
  10. "net/url"
  11. "strconv"
  12. "strings"
  13. )
  14. // Client holds API procedure calls.
  15. type Client struct {
  16. request *request
  17. }
  18. // New returns a new Miniflux client.
  19. //
  20. // Deprecated: use NewClient instead.
  21. func New(endpoint string, credentials ...string) *Client {
  22. return NewClient(endpoint, credentials...)
  23. }
  24. // NewClient returns a new Miniflux client.
  25. func NewClient(endpoint string, credentials ...string) *Client {
  26. switch len(credentials) {
  27. case 2:
  28. return NewClientWithOptions(endpoint, WithCredentials(credentials[0], credentials[1]))
  29. case 1:
  30. return NewClientWithOptions(endpoint, WithAPIKey(credentials[0]))
  31. default:
  32. return NewClientWithOptions(endpoint)
  33. }
  34. }
  35. // NewClientWithOptions returns a new Miniflux client with options.
  36. func NewClientWithOptions(endpoint string, options ...Option) *Client {
  37. // Trim trailing slashes and /v1 from the endpoint.
  38. endpoint = strings.TrimSuffix(endpoint, "/")
  39. endpoint = strings.TrimSuffix(endpoint, "/v1")
  40. request := &request{endpoint: endpoint, client: http.DefaultClient}
  41. for _, option := range options {
  42. option(request)
  43. }
  44. return &Client{request: request}
  45. }
  46. func withDefaultTimeout() (context.Context, func()) {
  47. ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
  48. return ctx, cancel
  49. }
  50. // Healthcheck checks if the application is up and running.
  51. func (c *Client) Healthcheck() error {
  52. ctx, cancel := withDefaultTimeout()
  53. defer cancel()
  54. return c.HealthcheckContext(ctx)
  55. }
  56. // HealthcheckContext checks if the application is up and running.
  57. func (c *Client) HealthcheckContext(ctx context.Context) error {
  58. body, err := c.request.Get(ctx, "/healthcheck")
  59. if err != nil {
  60. return fmt.Errorf("miniflux: unable to perform healthcheck: %w", err)
  61. }
  62. defer body.Close()
  63. responseBodyContent, err := io.ReadAll(body)
  64. if err != nil {
  65. return fmt.Errorf("miniflux: unable to read healthcheck response: %w", err)
  66. }
  67. if string(responseBodyContent) != "OK" {
  68. return fmt.Errorf("miniflux: invalid healthcheck response: %q", responseBodyContent)
  69. }
  70. return nil
  71. }
  72. // Version returns the version of the Miniflux instance.
  73. func (c *Client) Version() (*VersionResponse, error) {
  74. ctx, cancel := withDefaultTimeout()
  75. defer cancel()
  76. return c.VersionContext(ctx)
  77. }
  78. // VersionContext returns the version of the Miniflux instance.
  79. func (c *Client) VersionContext(ctx context.Context) (*VersionResponse, error) {
  80. body, err := c.request.Get(ctx, "/v1/version")
  81. if err != nil {
  82. return nil, err
  83. }
  84. defer body.Close()
  85. var versionResponse *VersionResponse
  86. if err := json.NewDecoder(body).Decode(&versionResponse); err != nil {
  87. return nil, fmt.Errorf("miniflux: json error (%v)", err)
  88. }
  89. return versionResponse, nil
  90. }
  91. // Me returns the logged user information.
  92. func (c *Client) Me() (*User, error) {
  93. ctx, cancel := withDefaultTimeout()
  94. defer cancel()
  95. return c.MeContext(ctx)
  96. }
  97. // MeContext returns the logged user information.
  98. func (c *Client) MeContext(ctx context.Context) (*User, error) {
  99. body, err := c.request.Get(ctx, "/v1/me")
  100. if err != nil {
  101. return nil, err
  102. }
  103. defer body.Close()
  104. var user *User
  105. if err := json.NewDecoder(body).Decode(&user); err != nil {
  106. return nil, fmt.Errorf("miniflux: json error (%v)", err)
  107. }
  108. return user, nil
  109. }
  110. // Users returns all users.
  111. func (c *Client) Users() (Users, error) {
  112. ctx, cancel := withDefaultTimeout()
  113. defer cancel()
  114. return c.UsersContext(ctx)
  115. }
  116. // UsersContext returns all users.
  117. func (c *Client) UsersContext(ctx context.Context) (Users, error) {
  118. body, err := c.request.Get(ctx, "/v1/users")
  119. if err != nil {
  120. return nil, err
  121. }
  122. defer body.Close()
  123. var users Users
  124. if err := json.NewDecoder(body).Decode(&users); err != nil {
  125. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  126. }
  127. return users, nil
  128. }
  129. // UserByID returns a single user.
  130. func (c *Client) UserByID(userID int64) (*User, error) {
  131. ctx, cancel := withDefaultTimeout()
  132. defer cancel()
  133. return c.UserByIDContext(ctx, userID)
  134. }
  135. // UserByIDContext returns a single user.
  136. func (c *Client) UserByIDContext(ctx context.Context, userID int64) (*User, error) {
  137. body, err := c.request.Get(ctx, fmt.Sprintf("/v1/users/%d", userID))
  138. if err != nil {
  139. return nil, err
  140. }
  141. defer body.Close()
  142. var user User
  143. if err := json.NewDecoder(body).Decode(&user); err != nil {
  144. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  145. }
  146. return &user, nil
  147. }
  148. // UserByUsername returns a single user.
  149. func (c *Client) UserByUsername(username string) (*User, error) {
  150. ctx, cancel := withDefaultTimeout()
  151. defer cancel()
  152. return c.UserByUsernameContext(ctx, username)
  153. }
  154. // UserByUsernameContext returns a single user.
  155. func (c *Client) UserByUsernameContext(ctx context.Context, username string) (*User, error) {
  156. body, err := c.request.Get(ctx, "/v1/users/"+username)
  157. if err != nil {
  158. return nil, err
  159. }
  160. defer body.Close()
  161. var user User
  162. if err := json.NewDecoder(body).Decode(&user); err != nil {
  163. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  164. }
  165. return &user, nil
  166. }
  167. // CreateUser creates a new user in the system.
  168. func (c *Client) CreateUser(username, password string, isAdmin bool) (*User, error) {
  169. ctx, cancel := withDefaultTimeout()
  170. defer cancel()
  171. return c.CreateUserContext(ctx, username, password, isAdmin)
  172. }
  173. // CreateUserContext creates a new user in the system.
  174. func (c *Client) CreateUserContext(ctx context.Context, username, password string, isAdmin bool) (*User, error) {
  175. body, err := c.request.Post(ctx, "/v1/users", &UserCreationRequest{
  176. Username: username,
  177. Password: password,
  178. IsAdmin: isAdmin,
  179. })
  180. if err != nil {
  181. return nil, err
  182. }
  183. defer body.Close()
  184. var user *User
  185. if err := json.NewDecoder(body).Decode(&user); err != nil {
  186. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  187. }
  188. return user, nil
  189. }
  190. // UpdateUser updates a user in the system.
  191. func (c *Client) UpdateUser(userID int64, userChanges *UserModificationRequest) (*User, error) {
  192. ctx, cancel := withDefaultTimeout()
  193. defer cancel()
  194. return c.UpdateUserContext(ctx, userID, userChanges)
  195. }
  196. // UpdateUserContext updates a user in the system.
  197. func (c *Client) UpdateUserContext(ctx context.Context, userID int64, userChanges *UserModificationRequest) (*User, error) {
  198. body, err := c.request.Put(ctx, fmt.Sprintf("/v1/users/%d", userID), userChanges)
  199. if err != nil {
  200. return nil, err
  201. }
  202. defer body.Close()
  203. var u *User
  204. if err := json.NewDecoder(body).Decode(&u); err != nil {
  205. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  206. }
  207. return u, nil
  208. }
  209. // DeleteUser removes a user from the system.
  210. func (c *Client) DeleteUser(userID int64) error {
  211. ctx, cancel := withDefaultTimeout()
  212. defer cancel()
  213. return c.DeleteUserContext(ctx, userID)
  214. }
  215. // DeleteUserContext removes a user from the system.
  216. func (c *Client) DeleteUserContext(ctx context.Context, userID int64) error {
  217. return c.request.Delete(ctx, fmt.Sprintf("/v1/users/%d", userID))
  218. }
  219. // APIKeys returns all API keys for the authenticated user.
  220. func (c *Client) APIKeys() (APIKeys, error) {
  221. ctx, cancel := withDefaultTimeout()
  222. defer cancel()
  223. return c.APIKeysContext(ctx)
  224. }
  225. // APIKeysContext returns all API keys for the authenticated user.
  226. func (c *Client) APIKeysContext(ctx context.Context) (APIKeys, error) {
  227. body, err := c.request.Get(ctx, "/v1/api-keys")
  228. if err != nil {
  229. return nil, err
  230. }
  231. defer body.Close()
  232. var apiKeys APIKeys
  233. if err := json.NewDecoder(body).Decode(&apiKeys); err != nil {
  234. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  235. }
  236. return apiKeys, nil
  237. }
  238. // CreateAPIKey creates a new API key for the authenticated user.
  239. func (c *Client) CreateAPIKey(description string) (*APIKey, error) {
  240. ctx, cancel := withDefaultTimeout()
  241. defer cancel()
  242. return c.CreateAPIKeyContext(ctx, description)
  243. }
  244. // CreateAPIKeyContext creates a new API key for the authenticated user.
  245. func (c *Client) CreateAPIKeyContext(ctx context.Context, description string) (*APIKey, error) {
  246. body, err := c.request.Post(ctx, "/v1/api-keys", &APIKeyCreationRequest{
  247. Description: description,
  248. })
  249. if err != nil {
  250. return nil, err
  251. }
  252. defer body.Close()
  253. var apiKey *APIKey
  254. if err := json.NewDecoder(body).Decode(&apiKey); err != nil {
  255. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  256. }
  257. return apiKey, nil
  258. }
  259. // DeleteAPIKey removes an API key for the authenticated user.
  260. func (c *Client) DeleteAPIKey(apiKeyID int64) error {
  261. ctx, cancel := withDefaultTimeout()
  262. defer cancel()
  263. return c.DeleteAPIKeyContext(ctx, apiKeyID)
  264. }
  265. // DeleteAPIKeyContext removes an API key for the authenticated user.
  266. func (c *Client) DeleteAPIKeyContext(ctx context.Context, apiKeyID int64) error {
  267. return c.request.Delete(ctx, fmt.Sprintf("/v1/api-keys/%d", apiKeyID))
  268. }
  269. // MarkAllAsRead marks all unread entries as read for a given user.
  270. func (c *Client) MarkAllAsRead(userID int64) error {
  271. ctx, cancel := withDefaultTimeout()
  272. defer cancel()
  273. return c.MarkAllAsReadContext(ctx, userID)
  274. }
  275. // MarkAllAsReadContext marks all unread entries as read for a given user.
  276. func (c *Client) MarkAllAsReadContext(ctx context.Context, userID int64) error {
  277. _, err := c.request.Put(ctx, fmt.Sprintf("/v1/users/%d/mark-all-as-read", userID), nil)
  278. return err
  279. }
  280. // IntegrationsStatus fetches the integrations status for the signed-in user.
  281. func (c *Client) IntegrationsStatus() (bool, error) {
  282. ctx, cancel := withDefaultTimeout()
  283. defer cancel()
  284. return c.IntegrationsStatusContext(ctx)
  285. }
  286. // IntegrationsStatusContext fetches the integrations status for the signed-in user.
  287. func (c *Client) IntegrationsStatusContext(ctx context.Context) (bool, error) {
  288. body, err := c.request.Get(ctx, "/v1/integrations/status")
  289. if err != nil {
  290. return false, err
  291. }
  292. defer body.Close()
  293. var response struct {
  294. HasIntegrations bool `json:"has_integrations"`
  295. }
  296. if err := json.NewDecoder(body).Decode(&response); err != nil {
  297. return false, fmt.Errorf("miniflux: response error (%v)", err)
  298. }
  299. return response.HasIntegrations, nil
  300. }
  301. // Discover tries to find subscriptions on a website.
  302. func (c *Client) Discover(url string) (Subscriptions, error) {
  303. ctx, cancel := withDefaultTimeout()
  304. defer cancel()
  305. return c.DiscoverContext(ctx, url)
  306. }
  307. // DiscoverContext tries to find subscriptions from a website.
  308. func (c *Client) DiscoverContext(ctx context.Context, url string) (Subscriptions, error) {
  309. body, err := c.request.Post(ctx, "/v1/discover", map[string]string{"url": url})
  310. if err != nil {
  311. return nil, err
  312. }
  313. defer body.Close()
  314. var subscriptions Subscriptions
  315. if err := json.NewDecoder(body).Decode(&subscriptions); err != nil {
  316. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  317. }
  318. return subscriptions, nil
  319. }
  320. // Categories retrieves the list of categories.
  321. func (c *Client) Categories() (Categories, error) {
  322. ctx, cancel := withDefaultTimeout()
  323. defer cancel()
  324. return c.CategoriesContext(ctx)
  325. }
  326. // CategoriesContext retrieves the list of categories.
  327. func (c *Client) CategoriesContext(ctx context.Context) (Categories, error) {
  328. body, err := c.request.Get(ctx, "/v1/categories")
  329. if err != nil {
  330. return nil, err
  331. }
  332. defer body.Close()
  333. var categories Categories
  334. if err := json.NewDecoder(body).Decode(&categories); err != nil {
  335. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  336. }
  337. return categories, nil
  338. }
  339. // CategoriesWithCounters fetches the categories with their respective feed and unread counts.
  340. func (c *Client) CategoriesWithCounters() (Categories, error) {
  341. ctx, cancel := withDefaultTimeout()
  342. defer cancel()
  343. return c.CategoriesWithCountersContext(ctx)
  344. }
  345. // CategoriesWithCountersContext fetches the categories with their respective feed and unread counts.
  346. func (c *Client) CategoriesWithCountersContext(ctx context.Context) (Categories, error) {
  347. body, err := c.request.Get(ctx, "/v1/categories?counts=true")
  348. if err != nil {
  349. return nil, err
  350. }
  351. defer body.Close()
  352. var categories Categories
  353. if err := json.NewDecoder(body).Decode(&categories); err != nil {
  354. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  355. }
  356. return categories, nil
  357. }
  358. // CreateCategory creates a new category.
  359. func (c *Client) CreateCategory(title string) (*Category, error) {
  360. ctx, cancel := withDefaultTimeout()
  361. defer cancel()
  362. return c.CreateCategoryContext(ctx, title)
  363. }
  364. // CreateCategoryContext creates a new category.
  365. func (c *Client) CreateCategoryContext(ctx context.Context, title string) (*Category, error) {
  366. body, err := c.request.Post(ctx, "/v1/categories", &CategoryCreationRequest{
  367. Title: title,
  368. })
  369. if err != nil {
  370. return nil, err
  371. }
  372. defer body.Close()
  373. var category *Category
  374. if err := json.NewDecoder(body).Decode(&category); err != nil {
  375. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  376. }
  377. return category, nil
  378. }
  379. // CreateCategoryWithOptions creates a new category with options.
  380. func (c *Client) CreateCategoryWithOptions(createRequest *CategoryCreationRequest) (*Category, error) {
  381. ctx, cancel := withDefaultTimeout()
  382. defer cancel()
  383. return c.CreateCategoryWithOptionsContext(ctx, createRequest)
  384. }
  385. // CreateCategoryWithOptionsContext creates a new category with options.
  386. func (c *Client) CreateCategoryWithOptionsContext(ctx context.Context, createRequest *CategoryCreationRequest) (*Category, error) {
  387. body, err := c.request.Post(ctx, "/v1/categories", createRequest)
  388. if err != nil {
  389. return nil, err
  390. }
  391. defer body.Close()
  392. var category *Category
  393. if err := json.NewDecoder(body).Decode(&category); err != nil {
  394. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  395. }
  396. return category, nil
  397. }
  398. // UpdateCategory updates a category.
  399. func (c *Client) UpdateCategory(categoryID int64, title string) (*Category, error) {
  400. ctx, cancel := withDefaultTimeout()
  401. defer cancel()
  402. return c.UpdateCategoryContext(ctx, categoryID, title)
  403. }
  404. // UpdateCategoryContext updates a category.
  405. func (c *Client) UpdateCategoryContext(ctx context.Context, categoryID int64, title string) (*Category, error) {
  406. body, err := c.request.Put(ctx, fmt.Sprintf("/v1/categories/%d", categoryID), &CategoryModificationRequest{
  407. Title: SetOptionalField(title),
  408. })
  409. if err != nil {
  410. return nil, err
  411. }
  412. defer body.Close()
  413. var category *Category
  414. if err := json.NewDecoder(body).Decode(&category); err != nil {
  415. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  416. }
  417. return category, nil
  418. }
  419. // UpdateCategoryWithOptions updates a category with options.
  420. func (c *Client) UpdateCategoryWithOptions(categoryID int64, categoryChanges *CategoryModificationRequest) (*Category, error) {
  421. ctx, cancel := withDefaultTimeout()
  422. defer cancel()
  423. return c.UpdateCategoryWithOptionsContext(ctx, categoryID, categoryChanges)
  424. }
  425. // UpdateCategoryWithOptionsContext updates a category with options.
  426. func (c *Client) UpdateCategoryWithOptionsContext(ctx context.Context, categoryID int64, categoryChanges *CategoryModificationRequest) (*Category, error) {
  427. body, err := c.request.Put(ctx, fmt.Sprintf("/v1/categories/%d", categoryID), categoryChanges)
  428. if err != nil {
  429. return nil, err
  430. }
  431. defer body.Close()
  432. var category *Category
  433. if err := json.NewDecoder(body).Decode(&category); err != nil {
  434. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  435. }
  436. return category, nil
  437. }
  438. // MarkCategoryAsRead marks all unread entries in a category as read.
  439. func (c *Client) MarkCategoryAsRead(categoryID int64) error {
  440. ctx, cancel := withDefaultTimeout()
  441. defer cancel()
  442. return c.MarkCategoryAsReadContext(ctx, categoryID)
  443. }
  444. // MarkCategoryAsReadContext marks all unread entries in a category as read.
  445. func (c *Client) MarkCategoryAsReadContext(ctx context.Context, categoryID int64) error {
  446. _, err := c.request.Put(ctx, fmt.Sprintf("/v1/categories/%d/mark-all-as-read", categoryID), nil)
  447. return err
  448. }
  449. // CategoryFeeds returns all feeds for a category.
  450. func (c *Client) CategoryFeeds(categoryID int64) (Feeds, error) {
  451. ctx, cancel := withDefaultTimeout()
  452. defer cancel()
  453. return c.CategoryFeedsContext(ctx, categoryID)
  454. }
  455. // CategoryFeedsContext returns all feeds for a category.
  456. func (c *Client) CategoryFeedsContext(ctx context.Context, categoryID int64) (Feeds, error) {
  457. body, err := c.request.Get(ctx, fmt.Sprintf("/v1/categories/%d/feeds", categoryID))
  458. if err != nil {
  459. return nil, err
  460. }
  461. defer body.Close()
  462. var feeds Feeds
  463. if err := json.NewDecoder(body).Decode(&feeds); err != nil {
  464. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  465. }
  466. return feeds, nil
  467. }
  468. // DeleteCategory removes a category.
  469. func (c *Client) DeleteCategory(categoryID int64) error {
  470. ctx, cancel := withDefaultTimeout()
  471. defer cancel()
  472. return c.DeleteCategoryContext(ctx, categoryID)
  473. }
  474. // DeleteCategoryContext removes a category.
  475. func (c *Client) DeleteCategoryContext(ctx context.Context, categoryID int64) error {
  476. return c.request.Delete(ctx, fmt.Sprintf("/v1/categories/%d", categoryID))
  477. }
  478. // RefreshCategory refreshes a category.
  479. func (c *Client) RefreshCategory(categoryID int64) error {
  480. ctx, cancel := withDefaultTimeout()
  481. defer cancel()
  482. return c.RefreshCategoryContext(ctx, categoryID)
  483. }
  484. // RefreshCategoryContext refreshes a category.
  485. func (c *Client) RefreshCategoryContext(ctx context.Context, categoryID int64) error {
  486. _, err := c.request.Put(ctx, fmt.Sprintf("/v1/categories/%d/refresh", categoryID), nil)
  487. return err
  488. }
  489. // Feeds gets all feeds.
  490. func (c *Client) Feeds() (Feeds, error) {
  491. ctx, cancel := withDefaultTimeout()
  492. defer cancel()
  493. return c.FeedsContext(ctx)
  494. }
  495. // FeedsContext gets all feeds.
  496. func (c *Client) FeedsContext(ctx context.Context) (Feeds, error) {
  497. body, err := c.request.Get(ctx, "/v1/feeds")
  498. if err != nil {
  499. return nil, err
  500. }
  501. defer body.Close()
  502. var feeds Feeds
  503. if err := json.NewDecoder(body).Decode(&feeds); err != nil {
  504. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  505. }
  506. return feeds, nil
  507. }
  508. // Export exports subscriptions as an OPML document.
  509. func (c *Client) Export() ([]byte, error) {
  510. ctx, cancel := withDefaultTimeout()
  511. defer cancel()
  512. return c.ExportContext(ctx)
  513. }
  514. // ExportContext exports subscriptions as an OPML document.
  515. func (c *Client) ExportContext(ctx context.Context) ([]byte, error) {
  516. body, err := c.request.Get(ctx, "/v1/export")
  517. if err != nil {
  518. return nil, err
  519. }
  520. defer body.Close()
  521. opml, err := io.ReadAll(body)
  522. if err != nil {
  523. return nil, err
  524. }
  525. return opml, nil
  526. }
  527. // Import imports an OPML file.
  528. func (c *Client) Import(f io.ReadCloser) error {
  529. ctx, cancel := withDefaultTimeout()
  530. defer cancel()
  531. return c.ImportContext(ctx, f)
  532. }
  533. // ImportContext imports an OPML file.
  534. func (c *Client) ImportContext(ctx context.Context, f io.ReadCloser) error {
  535. _, err := c.request.PostFile(ctx, "/v1/import", f)
  536. return err
  537. }
  538. // Feed gets a feed.
  539. func (c *Client) Feed(feedID int64) (*Feed, error) {
  540. ctx, cancel := withDefaultTimeout()
  541. defer cancel()
  542. return c.FeedContext(ctx, feedID)
  543. }
  544. // FeedContext gets a feed.
  545. func (c *Client) FeedContext(ctx context.Context, feedID int64) (*Feed, error) {
  546. body, err := c.request.Get(ctx, fmt.Sprintf("/v1/feeds/%d", feedID))
  547. if err != nil {
  548. return nil, err
  549. }
  550. defer body.Close()
  551. var feed *Feed
  552. if err := json.NewDecoder(body).Decode(&feed); err != nil {
  553. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  554. }
  555. return feed, nil
  556. }
  557. // CreateFeed creates a new feed.
  558. func (c *Client) CreateFeed(feedCreationRequest *FeedCreationRequest) (int64, error) {
  559. ctx, cancel := withDefaultTimeout()
  560. defer cancel()
  561. return c.CreateFeedContext(ctx, feedCreationRequest)
  562. }
  563. // CreateFeedContext creates a new feed.
  564. func (c *Client) CreateFeedContext(ctx context.Context, feedCreationRequest *FeedCreationRequest) (int64, error) {
  565. body, err := c.request.Post(ctx, "/v1/feeds", feedCreationRequest)
  566. if err != nil {
  567. return 0, err
  568. }
  569. defer body.Close()
  570. type result struct {
  571. FeedID int64 `json:"feed_id"`
  572. }
  573. var r result
  574. if err := json.NewDecoder(body).Decode(&r); err != nil {
  575. return 0, fmt.Errorf("miniflux: response error (%v)", err)
  576. }
  577. return r.FeedID, nil
  578. }
  579. // UpdateFeed updates a feed.
  580. func (c *Client) UpdateFeed(feedID int64, feedChanges *FeedModificationRequest) (*Feed, error) {
  581. ctx, cancel := withDefaultTimeout()
  582. defer cancel()
  583. return c.UpdateFeedContext(ctx, feedID, feedChanges)
  584. }
  585. // UpdateFeedContext updates a feed.
  586. func (c *Client) UpdateFeedContext(ctx context.Context, feedID int64, feedChanges *FeedModificationRequest) (*Feed, error) {
  587. body, err := c.request.Put(ctx, fmt.Sprintf("/v1/feeds/%d", feedID), feedChanges)
  588. if err != nil {
  589. return nil, err
  590. }
  591. defer body.Close()
  592. var f *Feed
  593. if err := json.NewDecoder(body).Decode(&f); err != nil {
  594. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  595. }
  596. return f, nil
  597. }
  598. // ImportFeedEntry imports a single entry into a feed.
  599. func (c *Client) ImportFeedEntry(feedID int64, payload any) (int64, error) {
  600. ctx, cancel := withDefaultTimeout()
  601. defer cancel()
  602. body, err := c.request.Post(
  603. ctx,
  604. fmt.Sprintf("/v1/feeds/%d/entries/import", feedID),
  605. payload,
  606. )
  607. if err != nil {
  608. return 0, err
  609. }
  610. defer body.Close()
  611. var response struct {
  612. ID int64 `json:"id"`
  613. }
  614. if err := json.NewDecoder(body).Decode(&response); err != nil {
  615. return 0, fmt.Errorf("miniflux: json error (%v)", err)
  616. }
  617. return response.ID, nil
  618. }
  619. // MarkFeedAsRead marks all unread entries of the feed as read.
  620. func (c *Client) MarkFeedAsRead(feedID int64) error {
  621. ctx, cancel := withDefaultTimeout()
  622. defer cancel()
  623. return c.MarkFeedAsReadContext(ctx, feedID)
  624. }
  625. // MarkFeedAsReadContext marks all unread entries of the feed as read.
  626. func (c *Client) MarkFeedAsReadContext(ctx context.Context, feedID int64) error {
  627. _, err := c.request.Put(ctx, fmt.Sprintf("/v1/feeds/%d/mark-all-as-read", feedID), nil)
  628. return err
  629. }
  630. // RefreshAllFeeds refreshes all feeds.
  631. func (c *Client) RefreshAllFeeds() error {
  632. ctx, cancel := withDefaultTimeout()
  633. defer cancel()
  634. return c.RefreshAllFeedsContext(ctx)
  635. }
  636. // RefreshAllFeedsContext refreshes all feeds.
  637. func (c *Client) RefreshAllFeedsContext(ctx context.Context) error {
  638. _, err := c.request.Put(ctx, "/v1/feeds/refresh", nil)
  639. return err
  640. }
  641. // RefreshFeed refreshes a feed.
  642. func (c *Client) RefreshFeed(feedID int64) error {
  643. ctx, cancel := withDefaultTimeout()
  644. defer cancel()
  645. return c.RefreshFeedContext(ctx, feedID)
  646. }
  647. // RefreshFeedContext refreshes a feed.
  648. func (c *Client) RefreshFeedContext(ctx context.Context, feedID int64) error {
  649. _, err := c.request.Put(ctx, fmt.Sprintf("/v1/feeds/%d/refresh", feedID), nil)
  650. return err
  651. }
  652. // DeleteFeed removes a feed.
  653. func (c *Client) DeleteFeed(feedID int64) error {
  654. ctx, cancel := withDefaultTimeout()
  655. defer cancel()
  656. return c.DeleteFeedContext(ctx, feedID)
  657. }
  658. // DeleteFeedContext removes a feed.
  659. func (c *Client) DeleteFeedContext(ctx context.Context, feedID int64) error {
  660. return c.request.Delete(ctx, fmt.Sprintf("/v1/feeds/%d", feedID))
  661. }
  662. // FeedIcon gets a feed icon.
  663. func (c *Client) FeedIcon(feedID int64) (*FeedIcon, error) {
  664. ctx, cancel := withDefaultTimeout()
  665. defer cancel()
  666. return c.FeedIconContext(ctx, feedID)
  667. }
  668. // FeedIconContext gets a feed icon.
  669. func (c *Client) FeedIconContext(ctx context.Context, feedID int64) (*FeedIcon, error) {
  670. body, err := c.request.Get(ctx, fmt.Sprintf("/v1/feeds/%d/icon", feedID))
  671. if err != nil {
  672. return nil, err
  673. }
  674. defer body.Close()
  675. var feedIcon *FeedIcon
  676. if err := json.NewDecoder(body).Decode(&feedIcon); err != nil {
  677. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  678. }
  679. return feedIcon, nil
  680. }
  681. // FeedEntry gets a single feed entry.
  682. func (c *Client) FeedEntry(feedID, entryID int64) (*Entry, error) {
  683. ctx, cancel := withDefaultTimeout()
  684. defer cancel()
  685. return c.FeedEntryContext(ctx, feedID, entryID)
  686. }
  687. // FeedEntryContext gets a single feed entry.
  688. func (c *Client) FeedEntryContext(ctx context.Context, feedID, entryID int64) (*Entry, error) {
  689. body, err := c.request.Get(ctx, fmt.Sprintf("/v1/feeds/%d/entries/%d", feedID, entryID))
  690. if err != nil {
  691. return nil, err
  692. }
  693. defer body.Close()
  694. var entry *Entry
  695. if err := json.NewDecoder(body).Decode(&entry); err != nil {
  696. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  697. }
  698. return entry, nil
  699. }
  700. // CategoryEntry gets a single category entry.
  701. func (c *Client) CategoryEntry(categoryID, entryID int64) (*Entry, error) {
  702. ctx, cancel := withDefaultTimeout()
  703. defer cancel()
  704. return c.CategoryEntryContext(ctx, categoryID, entryID)
  705. }
  706. // CategoryEntryContext gets a single category entry.
  707. func (c *Client) CategoryEntryContext(ctx context.Context, categoryID, entryID int64) (*Entry, error) {
  708. body, err := c.request.Get(ctx, fmt.Sprintf("/v1/categories/%d/entries/%d", categoryID, entryID))
  709. if err != nil {
  710. return nil, err
  711. }
  712. defer body.Close()
  713. var entry *Entry
  714. if err := json.NewDecoder(body).Decode(&entry); err != nil {
  715. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  716. }
  717. return entry, nil
  718. }
  719. // Entry gets a single entry.
  720. func (c *Client) Entry(entryID int64) (*Entry, error) {
  721. ctx, cancel := withDefaultTimeout()
  722. defer cancel()
  723. return c.EntryContext(ctx, entryID)
  724. }
  725. // EntryContext gets a single entry.
  726. func (c *Client) EntryContext(ctx context.Context, entryID int64) (*Entry, error) {
  727. body, err := c.request.Get(ctx, fmt.Sprintf("/v1/entries/%d", entryID))
  728. if err != nil {
  729. return nil, err
  730. }
  731. defer body.Close()
  732. var entry *Entry
  733. if err := json.NewDecoder(body).Decode(&entry); err != nil {
  734. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  735. }
  736. return entry, nil
  737. }
  738. // Entries fetches entries using the given filter.
  739. func (c *Client) Entries(filter *Filter) (*EntryResultSet, error) {
  740. ctx, cancel := withDefaultTimeout()
  741. defer cancel()
  742. return c.EntriesContext(ctx, filter)
  743. }
  744. // EntriesContext fetches entries.
  745. func (c *Client) EntriesContext(ctx context.Context, filter *Filter) (*EntryResultSet, error) {
  746. path := buildFilterQueryString("/v1/entries", filter)
  747. body, err := c.request.Get(ctx, path)
  748. if err != nil {
  749. return nil, err
  750. }
  751. defer body.Close()
  752. var result EntryResultSet
  753. if err := json.NewDecoder(body).Decode(&result); err != nil {
  754. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  755. }
  756. return &result, nil
  757. }
  758. // FeedEntries fetches entries for a feed using the given filter.
  759. func (c *Client) FeedEntries(feedID int64, filter *Filter) (*EntryResultSet, error) {
  760. ctx, cancel := withDefaultTimeout()
  761. defer cancel()
  762. return c.FeedEntriesContext(ctx, feedID, filter)
  763. }
  764. // FeedEntriesContext fetches feed entries.
  765. func (c *Client) FeedEntriesContext(ctx context.Context, feedID int64, filter *Filter) (*EntryResultSet, error) {
  766. path := buildFilterQueryString(fmt.Sprintf("/v1/feeds/%d/entries", feedID), filter)
  767. body, err := c.request.Get(ctx, path)
  768. if err != nil {
  769. return nil, err
  770. }
  771. defer body.Close()
  772. var result EntryResultSet
  773. if err := json.NewDecoder(body).Decode(&result); err != nil {
  774. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  775. }
  776. return &result, nil
  777. }
  778. // CategoryEntries fetches entries for a category using the given filter.
  779. func (c *Client) CategoryEntries(categoryID int64, filter *Filter) (*EntryResultSet, error) {
  780. ctx, cancel := withDefaultTimeout()
  781. defer cancel()
  782. return c.CategoryEntriesContext(ctx, categoryID, filter)
  783. }
  784. // CategoryEntriesContext fetches category entries.
  785. func (c *Client) CategoryEntriesContext(ctx context.Context, categoryID int64, filter *Filter) (*EntryResultSet, error) {
  786. path := buildFilterQueryString(fmt.Sprintf("/v1/categories/%d/entries", categoryID), filter)
  787. body, err := c.request.Get(ctx, path)
  788. if err != nil {
  789. return nil, err
  790. }
  791. defer body.Close()
  792. var result EntryResultSet
  793. if err := json.NewDecoder(body).Decode(&result); err != nil {
  794. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  795. }
  796. return &result, nil
  797. }
  798. // UpdateEntries updates the status of a list of entries.
  799. func (c *Client) UpdateEntries(entryIDs []int64, status string) error {
  800. ctx, cancel := withDefaultTimeout()
  801. defer cancel()
  802. return c.UpdateEntriesContext(ctx, entryIDs, status)
  803. }
  804. // UpdateEntriesContext updates the status of a list of entries.
  805. func (c *Client) UpdateEntriesContext(ctx context.Context, entryIDs []int64, status string) error {
  806. type payload struct {
  807. EntryIDs []int64 `json:"entry_ids"`
  808. Status string `json:"status"`
  809. }
  810. _, err := c.request.Put(ctx, "/v1/entries", &payload{EntryIDs: entryIDs, Status: status})
  811. return err
  812. }
  813. // UpdateEntry updates an entry.
  814. func (c *Client) UpdateEntry(entryID int64, entryChanges *EntryModificationRequest) (*Entry, error) {
  815. ctx, cancel := withDefaultTimeout()
  816. defer cancel()
  817. return c.UpdateEntryContext(ctx, entryID, entryChanges)
  818. }
  819. // UpdateEntryContext updates an entry.
  820. func (c *Client) UpdateEntryContext(ctx context.Context, entryID int64, entryChanges *EntryModificationRequest) (*Entry, error) {
  821. body, err := c.request.Put(ctx, fmt.Sprintf("/v1/entries/%d", entryID), entryChanges)
  822. if err != nil {
  823. return nil, err
  824. }
  825. defer body.Close()
  826. var entry *Entry
  827. if err := json.NewDecoder(body).Decode(&entry); err != nil {
  828. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  829. }
  830. return entry, nil
  831. }
  832. // ToggleStarred toggles the starred flag of an entry.
  833. func (c *Client) ToggleStarred(entryID int64) error {
  834. ctx, cancel := withDefaultTimeout()
  835. defer cancel()
  836. return c.ToggleStarredContext(ctx, entryID)
  837. }
  838. // ToggleStarredContext toggles entry starred value.
  839. func (c *Client) ToggleStarredContext(ctx context.Context, entryID int64) error {
  840. _, err := c.request.Put(ctx, fmt.Sprintf("/v1/entries/%d/star", entryID), nil)
  841. return err
  842. }
  843. // SaveEntry sends an entry to a third-party service.
  844. func (c *Client) SaveEntry(entryID int64) error {
  845. ctx, cancel := withDefaultTimeout()
  846. defer cancel()
  847. return c.SaveEntryContext(ctx, entryID)
  848. }
  849. // SaveEntryContext sends an entry to a third-party service.
  850. func (c *Client) SaveEntryContext(ctx context.Context, entryID int64) error {
  851. _, err := c.request.Post(ctx, fmt.Sprintf("/v1/entries/%d/save", entryID), nil)
  852. return err
  853. }
  854. // FetchEntryOriginalContent fetches the original content of an entry using the scraper.
  855. func (c *Client) FetchEntryOriginalContent(entryID int64) (string, error) {
  856. ctx, cancel := withDefaultTimeout()
  857. defer cancel()
  858. return c.FetchEntryOriginalContentContext(ctx, entryID)
  859. }
  860. // FetchEntryOriginalContentContext fetches the original content of an entry using the scraper.
  861. func (c *Client) FetchEntryOriginalContentContext(ctx context.Context, entryID int64) (string, error) {
  862. body, err := c.request.Get(ctx, fmt.Sprintf("/v1/entries/%d/fetch-content", entryID))
  863. if err != nil {
  864. return "", err
  865. }
  866. defer body.Close()
  867. var response struct {
  868. Content string `json:"content"`
  869. }
  870. if err := json.NewDecoder(body).Decode(&response); err != nil {
  871. return "", fmt.Errorf("miniflux: response error (%v)", err)
  872. }
  873. return response.Content, nil
  874. }
  875. // FetchCounters fetches feed counters.
  876. func (c *Client) FetchCounters() (*FeedCounters, error) {
  877. ctx, cancel := withDefaultTimeout()
  878. defer cancel()
  879. return c.FetchCountersContext(ctx)
  880. }
  881. // FetchCountersContext fetches feed counters.
  882. func (c *Client) FetchCountersContext(ctx context.Context) (*FeedCounters, error) {
  883. body, err := c.request.Get(ctx, "/v1/feeds/counters")
  884. if err != nil {
  885. return nil, err
  886. }
  887. defer body.Close()
  888. var result FeedCounters
  889. if err := json.NewDecoder(body).Decode(&result); err != nil {
  890. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  891. }
  892. return &result, nil
  893. }
  894. // FlushHistory changes all entries with the status "read" to "removed".
  895. func (c *Client) FlushHistory() error {
  896. ctx, cancel := withDefaultTimeout()
  897. defer cancel()
  898. return c.FlushHistoryContext(ctx)
  899. }
  900. // FlushHistoryContext changes all entries with the status "read" to "removed".
  901. func (c *Client) FlushHistoryContext(ctx context.Context) error {
  902. _, err := c.request.Put(ctx, "/v1/flush-history", nil)
  903. return err
  904. }
  905. // Icon fetches a feed icon.
  906. func (c *Client) Icon(iconID int64) (*FeedIcon, error) {
  907. ctx, cancel := withDefaultTimeout()
  908. defer cancel()
  909. return c.IconContext(ctx, iconID)
  910. }
  911. // IconContext fetches a feed icon.
  912. func (c *Client) IconContext(ctx context.Context, iconID int64) (*FeedIcon, error) {
  913. body, err := c.request.Get(ctx, fmt.Sprintf("/v1/icons/%d", iconID))
  914. if err != nil {
  915. return nil, err
  916. }
  917. defer body.Close()
  918. var feedIcon *FeedIcon
  919. if err := json.NewDecoder(body).Decode(&feedIcon); err != nil {
  920. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  921. }
  922. return feedIcon, nil
  923. }
  924. // Enclosure fetches a specific enclosure.
  925. func (c *Client) Enclosure(enclosureID int64) (*Enclosure, error) {
  926. ctx, cancel := withDefaultTimeout()
  927. defer cancel()
  928. return c.EnclosureContext(ctx, enclosureID)
  929. }
  930. // EnclosureContext fetches a specific enclosure.
  931. func (c *Client) EnclosureContext(ctx context.Context, enclosureID int64) (*Enclosure, error) {
  932. body, err := c.request.Get(ctx, fmt.Sprintf("/v1/enclosures/%d", enclosureID))
  933. if err != nil {
  934. return nil, err
  935. }
  936. defer body.Close()
  937. var enclosure *Enclosure
  938. if err := json.NewDecoder(body).Decode(&enclosure); err != nil {
  939. return nil, fmt.Errorf("miniflux: response error(%v)", err)
  940. }
  941. return enclosure, nil
  942. }
  943. // UpdateEnclosure updates an enclosure.
  944. func (c *Client) UpdateEnclosure(enclosureID int64, enclosureUpdate *EnclosureUpdateRequest) error {
  945. ctx, cancel := withDefaultTimeout()
  946. defer cancel()
  947. return c.UpdateEnclosureContext(ctx, enclosureID, enclosureUpdate)
  948. }
  949. // UpdateEnclosureContext updates an enclosure.
  950. func (c *Client) UpdateEnclosureContext(ctx context.Context, enclosureID int64, enclosureUpdate *EnclosureUpdateRequest) error {
  951. _, err := c.request.Put(ctx, fmt.Sprintf("/v1/enclosures/%d", enclosureID), enclosureUpdate)
  952. return err
  953. }
  954. func buildFilterQueryString(path string, filter *Filter) string {
  955. if filter != nil {
  956. values := url.Values{}
  957. if filter.Status != "" {
  958. values.Set("status", filter.Status)
  959. }
  960. if filter.Direction != "" {
  961. values.Set("direction", filter.Direction)
  962. }
  963. if filter.Order != "" {
  964. values.Set("order", filter.Order)
  965. }
  966. if filter.Limit >= 0 {
  967. values.Set("limit", strconv.Itoa(filter.Limit))
  968. }
  969. if filter.Offset >= 0 {
  970. values.Set("offset", strconv.Itoa(filter.Offset))
  971. }
  972. if filter.After > 0 {
  973. values.Set("after", strconv.FormatInt(filter.After, 10))
  974. }
  975. if filter.Before > 0 {
  976. values.Set("before", strconv.FormatInt(filter.Before, 10))
  977. }
  978. if filter.PublishedAfter > 0 {
  979. values.Set("published_after", strconv.FormatInt(filter.PublishedAfter, 10))
  980. }
  981. if filter.PublishedBefore > 0 {
  982. values.Set("published_before", strconv.FormatInt(filter.PublishedBefore, 10))
  983. }
  984. if filter.ChangedAfter > 0 {
  985. values.Set("changed_after", strconv.FormatInt(filter.ChangedAfter, 10))
  986. }
  987. if filter.ChangedBefore > 0 {
  988. values.Set("changed_before", strconv.FormatInt(filter.ChangedBefore, 10))
  989. }
  990. if filter.AfterEntryID > 0 {
  991. values.Set("after_entry_id", strconv.FormatInt(filter.AfterEntryID, 10))
  992. }
  993. if filter.BeforeEntryID > 0 {
  994. values.Set("before_entry_id", strconv.FormatInt(filter.BeforeEntryID, 10))
  995. }
  996. if filter.Starred != "" {
  997. values.Set("starred", filter.Starred)
  998. }
  999. if filter.Search != "" {
  1000. values.Set("search", filter.Search)
  1001. }
  1002. if filter.CategoryID > 0 {
  1003. values.Set("category_id", strconv.FormatInt(filter.CategoryID, 10))
  1004. }
  1005. if filter.FeedID > 0 {
  1006. values.Set("feed_id", strconv.FormatInt(filter.FeedID, 10))
  1007. }
  1008. if filter.GloballyVisible {
  1009. values.Set("globally_visible", "true")
  1010. }
  1011. for _, status := range filter.Statuses {
  1012. values.Add("status", status)
  1013. }
  1014. path = fmt.Sprintf("%s?%s", path, values.Encode())
  1015. }
  1016. return path
  1017. }