client.go 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  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 logged 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 logged 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 try to find subscriptions from 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 gets 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 gets 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 gets feeds of 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 gets feeds of 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 creates OPML file.
  509. func (c *Client) Export() ([]byte, error) {
  510. ctx, cancel := withDefaultTimeout()
  511. defer cancel()
  512. return c.ExportContext(ctx)
  513. }
  514. // ExportContext creates OPML file.
  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. // MarkFeedAsRead marks all unread entries of the feed as read.
  599. func (c *Client) MarkFeedAsRead(feedID int64) error {
  600. ctx, cancel := withDefaultTimeout()
  601. defer cancel()
  602. return c.MarkFeedAsReadContext(ctx, feedID)
  603. }
  604. // MarkFeedAsReadContext marks all unread entries of the feed as read.
  605. func (c *Client) MarkFeedAsReadContext(ctx context.Context, feedID int64) error {
  606. _, err := c.request.Put(ctx, fmt.Sprintf("/v1/feeds/%d/mark-all-as-read", feedID), nil)
  607. return err
  608. }
  609. // RefreshAllFeeds refreshes all feeds.
  610. func (c *Client) RefreshAllFeeds() error {
  611. ctx, cancel := withDefaultTimeout()
  612. defer cancel()
  613. return c.RefreshAllFeedsContext(ctx)
  614. }
  615. // RefreshAllFeedsContext refreshes all feeds.
  616. func (c *Client) RefreshAllFeedsContext(ctx context.Context) error {
  617. _, err := c.request.Put(ctx, "/v1/feeds/refresh", nil)
  618. return err
  619. }
  620. // RefreshFeed refreshes a feed.
  621. func (c *Client) RefreshFeed(feedID int64) error {
  622. ctx, cancel := withDefaultTimeout()
  623. defer cancel()
  624. return c.RefreshFeedContext(ctx, feedID)
  625. }
  626. // RefreshFeedContext refreshes a feed.
  627. func (c *Client) RefreshFeedContext(ctx context.Context, feedID int64) error {
  628. _, err := c.request.Put(ctx, fmt.Sprintf("/v1/feeds/%d/refresh", feedID), nil)
  629. return err
  630. }
  631. // DeleteFeed removes a feed.
  632. func (c *Client) DeleteFeed(feedID int64) error {
  633. ctx, cancel := withDefaultTimeout()
  634. defer cancel()
  635. return c.DeleteFeedContext(ctx, feedID)
  636. }
  637. // DeleteFeedContext removes a feed.
  638. func (c *Client) DeleteFeedContext(ctx context.Context, feedID int64) error {
  639. return c.request.Delete(ctx, fmt.Sprintf("/v1/feeds/%d", feedID))
  640. }
  641. // FeedIcon gets a feed icon.
  642. func (c *Client) FeedIcon(feedID int64) (*FeedIcon, error) {
  643. ctx, cancel := withDefaultTimeout()
  644. defer cancel()
  645. return c.FeedIconContext(ctx, feedID)
  646. }
  647. // FeedIconContext gets a feed icon.
  648. func (c *Client) FeedIconContext(ctx context.Context, feedID int64) (*FeedIcon, error) {
  649. body, err := c.request.Get(ctx, fmt.Sprintf("/v1/feeds/%d/icon", feedID))
  650. if err != nil {
  651. return nil, err
  652. }
  653. defer body.Close()
  654. var feedIcon *FeedIcon
  655. if err := json.NewDecoder(body).Decode(&feedIcon); err != nil {
  656. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  657. }
  658. return feedIcon, nil
  659. }
  660. // FeedEntry gets a single feed entry.
  661. func (c *Client) FeedEntry(feedID, entryID int64) (*Entry, error) {
  662. ctx, cancel := withDefaultTimeout()
  663. defer cancel()
  664. return c.FeedEntryContext(ctx, feedID, entryID)
  665. }
  666. // FeedEntryContext gets a single feed entry.
  667. func (c *Client) FeedEntryContext(ctx context.Context, feedID, entryID int64) (*Entry, error) {
  668. body, err := c.request.Get(ctx, fmt.Sprintf("/v1/feeds/%d/entries/%d", feedID, entryID))
  669. if err != nil {
  670. return nil, err
  671. }
  672. defer body.Close()
  673. var entry *Entry
  674. if err := json.NewDecoder(body).Decode(&entry); err != nil {
  675. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  676. }
  677. return entry, nil
  678. }
  679. // CategoryEntry gets a single category entry.
  680. func (c *Client) CategoryEntry(categoryID, entryID int64) (*Entry, error) {
  681. ctx, cancel := withDefaultTimeout()
  682. defer cancel()
  683. return c.CategoryEntryContext(ctx, categoryID, entryID)
  684. }
  685. // CategoryEntryContext gets a single category entry.
  686. func (c *Client) CategoryEntryContext(ctx context.Context, categoryID, entryID int64) (*Entry, error) {
  687. body, err := c.request.Get(ctx, fmt.Sprintf("/v1/categories/%d/entries/%d", categoryID, entryID))
  688. if err != nil {
  689. return nil, err
  690. }
  691. defer body.Close()
  692. var entry *Entry
  693. if err := json.NewDecoder(body).Decode(&entry); err != nil {
  694. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  695. }
  696. return entry, nil
  697. }
  698. // Entry gets a single entry.
  699. func (c *Client) Entry(entryID int64) (*Entry, error) {
  700. ctx, cancel := withDefaultTimeout()
  701. defer cancel()
  702. return c.EntryContext(ctx, entryID)
  703. }
  704. // EntryContext gets a single entry.
  705. func (c *Client) EntryContext(ctx context.Context, entryID int64) (*Entry, error) {
  706. body, err := c.request.Get(ctx, fmt.Sprintf("/v1/entries/%d", entryID))
  707. if err != nil {
  708. return nil, err
  709. }
  710. defer body.Close()
  711. var entry *Entry
  712. if err := json.NewDecoder(body).Decode(&entry); err != nil {
  713. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  714. }
  715. return entry, nil
  716. }
  717. // Entries fetch entries.
  718. func (c *Client) Entries(filter *Filter) (*EntryResultSet, error) {
  719. ctx, cancel := withDefaultTimeout()
  720. defer cancel()
  721. return c.EntriesContext(ctx, filter)
  722. }
  723. // EntriesContext fetches entries.
  724. func (c *Client) EntriesContext(ctx context.Context, filter *Filter) (*EntryResultSet, error) {
  725. path := buildFilterQueryString("/v1/entries", filter)
  726. body, err := c.request.Get(ctx, path)
  727. if err != nil {
  728. return nil, err
  729. }
  730. defer body.Close()
  731. var result EntryResultSet
  732. if err := json.NewDecoder(body).Decode(&result); err != nil {
  733. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  734. }
  735. return &result, nil
  736. }
  737. // FeedEntries fetch feed entries.
  738. func (c *Client) FeedEntries(feedID int64, filter *Filter) (*EntryResultSet, error) {
  739. ctx, cancel := withDefaultTimeout()
  740. defer cancel()
  741. return c.FeedEntriesContext(ctx, feedID, filter)
  742. }
  743. // FeedEntriesContext fetches feed entries.
  744. func (c *Client) FeedEntriesContext(ctx context.Context, feedID int64, filter *Filter) (*EntryResultSet, error) {
  745. path := buildFilterQueryString(fmt.Sprintf("/v1/feeds/%d/entries", feedID), filter)
  746. body, err := c.request.Get(ctx, path)
  747. if err != nil {
  748. return nil, err
  749. }
  750. defer body.Close()
  751. var result EntryResultSet
  752. if err := json.NewDecoder(body).Decode(&result); err != nil {
  753. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  754. }
  755. return &result, nil
  756. }
  757. // CategoryEntries fetch entries of a category.
  758. func (c *Client) CategoryEntries(categoryID int64, filter *Filter) (*EntryResultSet, error) {
  759. ctx, cancel := withDefaultTimeout()
  760. defer cancel()
  761. return c.CategoryEntriesContext(ctx, categoryID, filter)
  762. }
  763. // CategoryEntriesContext fetches category entries.
  764. func (c *Client) CategoryEntriesContext(ctx context.Context, categoryID int64, filter *Filter) (*EntryResultSet, error) {
  765. path := buildFilterQueryString(fmt.Sprintf("/v1/categories/%d/entries", categoryID), filter)
  766. body, err := c.request.Get(ctx, path)
  767. if err != nil {
  768. return nil, err
  769. }
  770. defer body.Close()
  771. var result EntryResultSet
  772. if err := json.NewDecoder(body).Decode(&result); err != nil {
  773. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  774. }
  775. return &result, nil
  776. }
  777. // UpdateEntries updates the status of a list of entries.
  778. func (c *Client) UpdateEntries(entryIDs []int64, status string) error {
  779. ctx, cancel := withDefaultTimeout()
  780. defer cancel()
  781. return c.UpdateEntriesContext(ctx, entryIDs, status)
  782. }
  783. // UpdateEntriesContext updates the status of a list of entries.
  784. func (c *Client) UpdateEntriesContext(ctx context.Context, entryIDs []int64, status string) error {
  785. type payload struct {
  786. EntryIDs []int64 `json:"entry_ids"`
  787. Status string `json:"status"`
  788. }
  789. _, err := c.request.Put(ctx, "/v1/entries", &payload{EntryIDs: entryIDs, Status: status})
  790. return err
  791. }
  792. // UpdateEntry updates an entry.
  793. func (c *Client) UpdateEntry(entryID int64, entryChanges *EntryModificationRequest) (*Entry, error) {
  794. ctx, cancel := withDefaultTimeout()
  795. defer cancel()
  796. return c.UpdateEntryContext(ctx, entryID, entryChanges)
  797. }
  798. // UpdateEntryContext updates an entry.
  799. func (c *Client) UpdateEntryContext(ctx context.Context, entryID int64, entryChanges *EntryModificationRequest) (*Entry, error) {
  800. body, err := c.request.Put(ctx, fmt.Sprintf("/v1/entries/%d", entryID), entryChanges)
  801. if err != nil {
  802. return nil, err
  803. }
  804. defer body.Close()
  805. var entry *Entry
  806. if err := json.NewDecoder(body).Decode(&entry); err != nil {
  807. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  808. }
  809. return entry, nil
  810. }
  811. // ToggleStarred toggles entry starred value.
  812. func (c *Client) ToggleStarred(entryID int64) error {
  813. ctx, cancel := withDefaultTimeout()
  814. defer cancel()
  815. return c.ToggleStarredContext(ctx, entryID)
  816. }
  817. // ToggleStarredContext toggles entry starred value.
  818. func (c *Client) ToggleStarredContext(ctx context.Context, entryID int64) error {
  819. _, err := c.request.Put(ctx, fmt.Sprintf("/v1/entries/%d/star", entryID), nil)
  820. return err
  821. }
  822. // SaveEntry sends an entry to a third-party service.
  823. func (c *Client) SaveEntry(entryID int64) error {
  824. ctx, cancel := withDefaultTimeout()
  825. defer cancel()
  826. return c.SaveEntryContext(ctx, entryID)
  827. }
  828. // SaveEntryContext sends an entry to a third-party service.
  829. func (c *Client) SaveEntryContext(ctx context.Context, entryID int64) error {
  830. _, err := c.request.Post(ctx, fmt.Sprintf("/v1/entries/%d/save", entryID), nil)
  831. return err
  832. }
  833. // FetchEntryOriginalContent fetches the original content of an entry using the scraper.
  834. func (c *Client) FetchEntryOriginalContent(entryID int64) (string, error) {
  835. ctx, cancel := withDefaultTimeout()
  836. defer cancel()
  837. return c.FetchEntryOriginalContentContext(ctx, entryID)
  838. }
  839. // FetchEntryOriginalContentContext fetches the original content of an entry using the scraper.
  840. func (c *Client) FetchEntryOriginalContentContext(ctx context.Context, entryID int64) (string, error) {
  841. body, err := c.request.Get(ctx, fmt.Sprintf("/v1/entries/%d/fetch-content", entryID))
  842. if err != nil {
  843. return "", err
  844. }
  845. defer body.Close()
  846. var response struct {
  847. Content string `json:"content"`
  848. }
  849. if err := json.NewDecoder(body).Decode(&response); err != nil {
  850. return "", fmt.Errorf("miniflux: response error (%v)", err)
  851. }
  852. return response.Content, nil
  853. }
  854. // FetchCounters fetches feed counters.
  855. func (c *Client) FetchCounters() (*FeedCounters, error) {
  856. ctx, cancel := withDefaultTimeout()
  857. defer cancel()
  858. return c.FetchCountersContext(ctx)
  859. }
  860. // FetchCountersContext fetches feed counters.
  861. func (c *Client) FetchCountersContext(ctx context.Context) (*FeedCounters, error) {
  862. body, err := c.request.Get(ctx, "/v1/feeds/counters")
  863. if err != nil {
  864. return nil, err
  865. }
  866. defer body.Close()
  867. var result FeedCounters
  868. if err := json.NewDecoder(body).Decode(&result); err != nil {
  869. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  870. }
  871. return &result, nil
  872. }
  873. // FlushHistory changes all entries with the status "read" to "removed".
  874. func (c *Client) FlushHistory() error {
  875. ctx, cancel := withDefaultTimeout()
  876. defer cancel()
  877. return c.FlushHistoryContext(ctx)
  878. }
  879. // FlushHistoryContext changes all entries with the status "read" to "removed".
  880. func (c *Client) FlushHistoryContext(ctx context.Context) error {
  881. _, err := c.request.Put(ctx, "/v1/flush-history", nil)
  882. return err
  883. }
  884. // Icon fetches a feed icon.
  885. func (c *Client) Icon(iconID int64) (*FeedIcon, error) {
  886. ctx, cancel := withDefaultTimeout()
  887. defer cancel()
  888. return c.IconContext(ctx, iconID)
  889. }
  890. // IconContext fetches a feed icon.
  891. func (c *Client) IconContext(ctx context.Context, iconID int64) (*FeedIcon, error) {
  892. body, err := c.request.Get(ctx, fmt.Sprintf("/v1/icons/%d", iconID))
  893. if err != nil {
  894. return nil, err
  895. }
  896. defer body.Close()
  897. var feedIcon *FeedIcon
  898. if err := json.NewDecoder(body).Decode(&feedIcon); err != nil {
  899. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  900. }
  901. return feedIcon, nil
  902. }
  903. // Enclosure fetches a specific enclosure.
  904. func (c *Client) Enclosure(enclosureID int64) (*Enclosure, error) {
  905. ctx, cancel := withDefaultTimeout()
  906. defer cancel()
  907. return c.EnclosureContext(ctx, enclosureID)
  908. }
  909. // EnclosureContext fetches a specific enclosure.
  910. func (c *Client) EnclosureContext(ctx context.Context, enclosureID int64) (*Enclosure, error) {
  911. body, err := c.request.Get(ctx, fmt.Sprintf("/v1/enclosures/%d", enclosureID))
  912. if err != nil {
  913. return nil, err
  914. }
  915. defer body.Close()
  916. var enclosure *Enclosure
  917. if err := json.NewDecoder(body).Decode(&enclosure); err != nil {
  918. return nil, fmt.Errorf("miniflux: response error(%v)", err)
  919. }
  920. return enclosure, nil
  921. }
  922. // UpdateEnclosure updates an enclosure.
  923. func (c *Client) UpdateEnclosure(enclosureID int64, enclosureUpdate *EnclosureUpdateRequest) error {
  924. ctx, cancel := withDefaultTimeout()
  925. defer cancel()
  926. return c.UpdateEnclosureContext(ctx, enclosureID, enclosureUpdate)
  927. }
  928. // UpdateEnclosureContext updates an enclosure.
  929. func (c *Client) UpdateEnclosureContext(ctx context.Context, enclosureID int64, enclosureUpdate *EnclosureUpdateRequest) error {
  930. _, err := c.request.Put(ctx, fmt.Sprintf("/v1/enclosures/%d", enclosureID), enclosureUpdate)
  931. return err
  932. }
  933. func buildFilterQueryString(path string, filter *Filter) string {
  934. if filter != nil {
  935. values := url.Values{}
  936. if filter.Status != "" {
  937. values.Set("status", filter.Status)
  938. }
  939. if filter.Direction != "" {
  940. values.Set("direction", filter.Direction)
  941. }
  942. if filter.Order != "" {
  943. values.Set("order", filter.Order)
  944. }
  945. if filter.Limit >= 0 {
  946. values.Set("limit", strconv.Itoa(filter.Limit))
  947. }
  948. if filter.Offset >= 0 {
  949. values.Set("offset", strconv.Itoa(filter.Offset))
  950. }
  951. if filter.After > 0 {
  952. values.Set("after", strconv.FormatInt(filter.After, 10))
  953. }
  954. if filter.Before > 0 {
  955. values.Set("before", strconv.FormatInt(filter.Before, 10))
  956. }
  957. if filter.PublishedAfter > 0 {
  958. values.Set("published_after", strconv.FormatInt(filter.PublishedAfter, 10))
  959. }
  960. if filter.PublishedBefore > 0 {
  961. values.Set("published_before", strconv.FormatInt(filter.PublishedBefore, 10))
  962. }
  963. if filter.ChangedAfter > 0 {
  964. values.Set("changed_after", strconv.FormatInt(filter.ChangedAfter, 10))
  965. }
  966. if filter.ChangedBefore > 0 {
  967. values.Set("changed_before", strconv.FormatInt(filter.ChangedBefore, 10))
  968. }
  969. if filter.AfterEntryID > 0 {
  970. values.Set("after_entry_id", strconv.FormatInt(filter.AfterEntryID, 10))
  971. }
  972. if filter.BeforeEntryID > 0 {
  973. values.Set("before_entry_id", strconv.FormatInt(filter.BeforeEntryID, 10))
  974. }
  975. if filter.Starred != "" {
  976. values.Set("starred", filter.Starred)
  977. }
  978. if filter.Search != "" {
  979. values.Set("search", filter.Search)
  980. }
  981. if filter.CategoryID > 0 {
  982. values.Set("category_id", strconv.FormatInt(filter.CategoryID, 10))
  983. }
  984. if filter.FeedID > 0 {
  985. values.Set("feed_id", strconv.FormatInt(filter.FeedID, 10))
  986. }
  987. if filter.GloballyVisible {
  988. values.Set("globally_visible", "true")
  989. }
  990. for _, status := range filter.Statuses {
  991. values.Add("status", status)
  992. }
  993. path = fmt.Sprintf("%s?%s", path, values.Encode())
  994. }
  995. return path
  996. }