client.go 40 KB

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