Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. // Copyright 2018 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package client // import "miniflux.app/client"
  5. import (
  6. "encoding/json"
  7. "fmt"
  8. "io"
  9. "io/ioutil"
  10. "net/url"
  11. "strconv"
  12. )
  13. // Client holds API procedure calls.
  14. type Client struct {
  15. request *request
  16. }
  17. // Me returns the logged user information.
  18. func (c *Client) Me() (*User, error) {
  19. body, err := c.request.Get("/v1/me")
  20. if err != nil {
  21. return nil, err
  22. }
  23. defer body.Close()
  24. var user *User
  25. decoder := json.NewDecoder(body)
  26. if err := decoder.Decode(&user); err != nil {
  27. return nil, fmt.Errorf("miniflux: json error (%v)", err)
  28. }
  29. return user, nil
  30. }
  31. // Users returns all users.
  32. func (c *Client) Users() (Users, error) {
  33. body, err := c.request.Get("/v1/users")
  34. if err != nil {
  35. return nil, err
  36. }
  37. defer body.Close()
  38. var users Users
  39. decoder := json.NewDecoder(body)
  40. if err := decoder.Decode(&users); err != nil {
  41. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  42. }
  43. return users, nil
  44. }
  45. // UserByID returns a single user.
  46. func (c *Client) UserByID(userID int64) (*User, error) {
  47. body, err := c.request.Get(fmt.Sprintf("/v1/users/%d", userID))
  48. if err != nil {
  49. return nil, err
  50. }
  51. defer body.Close()
  52. var user User
  53. decoder := json.NewDecoder(body)
  54. if err := decoder.Decode(&user); err != nil {
  55. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  56. }
  57. return &user, nil
  58. }
  59. // UserByUsername returns a single user.
  60. func (c *Client) UserByUsername(username string) (*User, error) {
  61. body, err := c.request.Get(fmt.Sprintf("/v1/users/%s", username))
  62. if err != nil {
  63. return nil, err
  64. }
  65. defer body.Close()
  66. var user User
  67. decoder := json.NewDecoder(body)
  68. if err := decoder.Decode(&user); err != nil {
  69. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  70. }
  71. return &user, nil
  72. }
  73. // CreateUser creates a new user in the system.
  74. func (c *Client) CreateUser(username, password string, isAdmin bool) (*User, error) {
  75. body, err := c.request.Post("/v1/users", &User{Username: username, Password: password, IsAdmin: isAdmin})
  76. if err != nil {
  77. return nil, err
  78. }
  79. defer body.Close()
  80. var user *User
  81. decoder := json.NewDecoder(body)
  82. if err := decoder.Decode(&user); err != nil {
  83. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  84. }
  85. return user, nil
  86. }
  87. // UpdateUser updates a user in the system.
  88. func (c *Client) UpdateUser(userID int64, userChanges *UserModification) (*User, error) {
  89. body, err := c.request.Put(fmt.Sprintf("/v1/users/%d", userID), userChanges)
  90. if err != nil {
  91. return nil, err
  92. }
  93. defer body.Close()
  94. var u *User
  95. decoder := json.NewDecoder(body)
  96. if err := decoder.Decode(&u); err != nil {
  97. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  98. }
  99. return u, nil
  100. }
  101. // DeleteUser removes a user from the system.
  102. func (c *Client) DeleteUser(userID int64) error {
  103. body, err := c.request.Delete(fmt.Sprintf("/v1/users/%d", userID))
  104. if err != nil {
  105. return err
  106. }
  107. body.Close()
  108. return nil
  109. }
  110. // Discover try to find subscriptions from a website.
  111. func (c *Client) Discover(url string) (Subscriptions, error) {
  112. body, err := c.request.Post("/v1/discover", map[string]string{"url": url})
  113. if err != nil {
  114. return nil, err
  115. }
  116. defer body.Close()
  117. var subscriptions Subscriptions
  118. decoder := json.NewDecoder(body)
  119. if err := decoder.Decode(&subscriptions); err != nil {
  120. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  121. }
  122. return subscriptions, nil
  123. }
  124. // Categories gets the list of categories.
  125. func (c *Client) Categories() (Categories, error) {
  126. body, err := c.request.Get("/v1/categories")
  127. if err != nil {
  128. return nil, err
  129. }
  130. defer body.Close()
  131. var categories Categories
  132. decoder := json.NewDecoder(body)
  133. if err := decoder.Decode(&categories); err != nil {
  134. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  135. }
  136. return categories, nil
  137. }
  138. // CreateCategory creates a new category.
  139. func (c *Client) CreateCategory(title string) (*Category, error) {
  140. body, err := c.request.Post("/v1/categories", map[string]interface{}{
  141. "title": title,
  142. })
  143. if err != nil {
  144. return nil, err
  145. }
  146. defer body.Close()
  147. var category *Category
  148. decoder := json.NewDecoder(body)
  149. if err := decoder.Decode(&category); err != nil {
  150. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  151. }
  152. return category, nil
  153. }
  154. // UpdateCategory updates a category.
  155. func (c *Client) UpdateCategory(categoryID int64, title string) (*Category, error) {
  156. body, err := c.request.Put(fmt.Sprintf("/v1/categories/%d", categoryID), map[string]interface{}{
  157. "title": title,
  158. })
  159. if err != nil {
  160. return nil, err
  161. }
  162. defer body.Close()
  163. var category *Category
  164. decoder := json.NewDecoder(body)
  165. if err := decoder.Decode(&category); err != nil {
  166. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  167. }
  168. return category, nil
  169. }
  170. // DeleteCategory removes a category.
  171. func (c *Client) DeleteCategory(categoryID int64) error {
  172. body, err := c.request.Delete(fmt.Sprintf("/v1/categories/%d", categoryID))
  173. if err != nil {
  174. return err
  175. }
  176. defer body.Close()
  177. return nil
  178. }
  179. // Feeds gets all feeds.
  180. func (c *Client) Feeds() (Feeds, error) {
  181. body, err := c.request.Get("/v1/feeds")
  182. if err != nil {
  183. return nil, err
  184. }
  185. defer body.Close()
  186. var feeds Feeds
  187. decoder := json.NewDecoder(body)
  188. if err := decoder.Decode(&feeds); err != nil {
  189. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  190. }
  191. return feeds, nil
  192. }
  193. // Export creates OPML file.
  194. func (c *Client) Export() ([]byte, error) {
  195. body, err := c.request.Get("/v1/export")
  196. if err != nil {
  197. return nil, err
  198. }
  199. defer body.Close()
  200. opml, err := ioutil.ReadAll(body)
  201. if err != nil {
  202. return nil, err
  203. }
  204. return opml, nil
  205. }
  206. // Import imports an OPML file.
  207. func (c *Client) Import(f io.ReadCloser) error {
  208. _, err := c.request.PostFile("/v1/import", f)
  209. return err
  210. }
  211. // Feed gets a feed.
  212. func (c *Client) Feed(feedID int64) (*Feed, error) {
  213. body, err := c.request.Get(fmt.Sprintf("/v1/feeds/%d", feedID))
  214. if err != nil {
  215. return nil, err
  216. }
  217. defer body.Close()
  218. var feed *Feed
  219. decoder := json.NewDecoder(body)
  220. if err := decoder.Decode(&feed); err != nil {
  221. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  222. }
  223. return feed, nil
  224. }
  225. // CreateFeed creates a new feed.
  226. func (c *Client) CreateFeed(url string, categoryID int64) (int64, error) {
  227. body, err := c.request.Post("/v1/feeds", map[string]interface{}{
  228. "feed_url": url,
  229. "category_id": categoryID,
  230. })
  231. if err != nil {
  232. return 0, err
  233. }
  234. defer body.Close()
  235. type result struct {
  236. FeedID int64 `json:"feed_id"`
  237. }
  238. var r result
  239. decoder := json.NewDecoder(body)
  240. if err := decoder.Decode(&r); err != nil {
  241. return 0, fmt.Errorf("miniflux: response error (%v)", err)
  242. }
  243. return r.FeedID, nil
  244. }
  245. // UpdateFeed updates a feed.
  246. func (c *Client) UpdateFeed(feedID int64, feedChanges *FeedModification) (*Feed, error) {
  247. body, err := c.request.Put(fmt.Sprintf("/v1/feeds/%d", feedID), feedChanges)
  248. if err != nil {
  249. return nil, err
  250. }
  251. defer body.Close()
  252. var f *Feed
  253. decoder := json.NewDecoder(body)
  254. if err := decoder.Decode(&f); err != nil {
  255. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  256. }
  257. return f, nil
  258. }
  259. // RefreshFeed refresh a feed.
  260. func (c *Client) RefreshFeed(feedID int64) error {
  261. body, err := c.request.Put(fmt.Sprintf("/v1/feeds/%d/refresh", feedID), nil)
  262. if err != nil {
  263. return err
  264. }
  265. body.Close()
  266. return nil
  267. }
  268. // DeleteFeed removes a feed.
  269. func (c *Client) DeleteFeed(feedID int64) error {
  270. body, err := c.request.Delete(fmt.Sprintf("/v1/feeds/%d", feedID))
  271. if err != nil {
  272. return err
  273. }
  274. body.Close()
  275. return nil
  276. }
  277. // FeedIcon gets a feed icon.
  278. func (c *Client) FeedIcon(feedID int64) (*FeedIcon, error) {
  279. body, err := c.request.Get(fmt.Sprintf("/v1/feeds/%d/icon", feedID))
  280. if err != nil {
  281. return nil, err
  282. }
  283. defer body.Close()
  284. var feedIcon *FeedIcon
  285. decoder := json.NewDecoder(body)
  286. if err := decoder.Decode(&feedIcon); err != nil {
  287. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  288. }
  289. return feedIcon, nil
  290. }
  291. // FeedEntry gets a single feed entry.
  292. func (c *Client) FeedEntry(feedID, entryID int64) (*Entry, error) {
  293. body, err := c.request.Get(fmt.Sprintf("/v1/feeds/%d/entries/%d", feedID, entryID))
  294. if err != nil {
  295. return nil, err
  296. }
  297. defer body.Close()
  298. var entry *Entry
  299. decoder := json.NewDecoder(body)
  300. if err := decoder.Decode(&entry); err != nil {
  301. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  302. }
  303. return entry, nil
  304. }
  305. // Entry gets a single entry.
  306. func (c *Client) Entry(entryID int64) (*Entry, error) {
  307. body, err := c.request.Get(fmt.Sprintf("/v1/entries/%d", entryID))
  308. if err != nil {
  309. return nil, err
  310. }
  311. defer body.Close()
  312. var entry *Entry
  313. decoder := json.NewDecoder(body)
  314. if err := decoder.Decode(&entry); err != nil {
  315. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  316. }
  317. return entry, nil
  318. }
  319. // Entries fetch entries.
  320. func (c *Client) Entries(filter *Filter) (*EntryResultSet, error) {
  321. path := buildFilterQueryString("/v1/entries", filter)
  322. body, err := c.request.Get(path)
  323. if err != nil {
  324. return nil, err
  325. }
  326. defer body.Close()
  327. var result EntryResultSet
  328. decoder := json.NewDecoder(body)
  329. if err := decoder.Decode(&result); err != nil {
  330. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  331. }
  332. return &result, nil
  333. }
  334. // FeedEntries fetch feed entries.
  335. func (c *Client) FeedEntries(feedID int64, filter *Filter) (*EntryResultSet, error) {
  336. path := buildFilterQueryString(fmt.Sprintf("/v1/feeds/%d/entries", feedID), filter)
  337. body, err := c.request.Get(path)
  338. if err != nil {
  339. return nil, err
  340. }
  341. defer body.Close()
  342. var result EntryResultSet
  343. decoder := json.NewDecoder(body)
  344. if err := decoder.Decode(&result); err != nil {
  345. return nil, fmt.Errorf("miniflux: response error (%v)", err)
  346. }
  347. return &result, nil
  348. }
  349. // UpdateEntries updates the status of a list of entries.
  350. func (c *Client) UpdateEntries(entryIDs []int64, status string) error {
  351. type payload struct {
  352. EntryIDs []int64 `json:"entry_ids"`
  353. Status string `json:"status"`
  354. }
  355. body, err := c.request.Put("/v1/entries", &payload{EntryIDs: entryIDs, Status: status})
  356. if err != nil {
  357. return err
  358. }
  359. body.Close()
  360. return nil
  361. }
  362. // ToggleBookmark toggles entry bookmark value.
  363. func (c *Client) ToggleBookmark(entryID int64) error {
  364. body, err := c.request.Put(fmt.Sprintf("/v1/entries/%d/bookmark", entryID), nil)
  365. if err != nil {
  366. return err
  367. }
  368. body.Close()
  369. return nil
  370. }
  371. // New returns a new Miniflux client.
  372. func New(endpoint, username, password string) *Client {
  373. return &Client{request: &request{endpoint: endpoint, username: username, password: password}}
  374. }
  375. func buildFilterQueryString(path string, filter *Filter) string {
  376. if filter != nil {
  377. values := url.Values{}
  378. if filter.Status != "" {
  379. values.Set("status", filter.Status)
  380. }
  381. if filter.Direction != "" {
  382. values.Set("direction", filter.Direction)
  383. }
  384. if filter.Order != "" {
  385. values.Set("order", filter.Order)
  386. }
  387. if filter.Limit >= 0 {
  388. values.Set("limit", strconv.Itoa(filter.Limit))
  389. }
  390. if filter.Offset >= 0 {
  391. values.Set("offset", strconv.Itoa(filter.Offset))
  392. }
  393. if filter.After > 0 {
  394. values.Set("after", strconv.FormatInt(filter.After, 10))
  395. }
  396. if filter.AfterEntryID > 0 {
  397. values.Set("after_entry_id", strconv.FormatInt(filter.AfterEntryID, 10))
  398. }
  399. if filter.Before > 0 {
  400. values.Set("before", strconv.FormatInt(filter.Before, 10))
  401. }
  402. if filter.BeforeEntryID > 0 {
  403. values.Set("before_entry_id", strconv.FormatInt(filter.BeforeEntryID, 10))
  404. }
  405. if filter.Starred {
  406. values.Set("starred", "1")
  407. }
  408. if filter.Search != "" {
  409. values.Set("search", filter.Search)
  410. }
  411. path = fmt.Sprintf("%s?%s", path, values.Encode())
  412. }
  413. return path
  414. }