Improve API

This commit is contained in:
Frédéric Guillot 2017-11-24 22:29:20 -08:00
parent 747da03e4c
commit 71bf7e4358
15 changed files with 40 additions and 63 deletions

View file

@ -1,5 +1,5 @@
// Code generated by go generate; DO NOT EDIT.
// 2017-11-24 16:04:49.318661623 -0800 PST m=+0.006828741
// 2017-11-24 21:40:33.354922728 -0800 PST m=+0.035838197
package locale
@ -148,12 +148,13 @@ var Translations = map[string]string{
"Refresh all feeds in the background": "Actualiser tous les abonnements en arrière-plan",
"Sign in with Google": "Se connecter avec Google",
"Unlink my Google account": "Dissocier mon compte Google",
"Link my Google account": "Associer mon compte Google"
"Link my Google account": "Associer mon compte Google",
"Category not found for this user.": "Cette catégorie n'existe pas pour cet utilisateur."
}
`,
}
var TranslationsChecksums = map[string]string{
"en_US": "6fe95384260941e8a5a3c695a655a932e0a8a6a572c1e45cb2b1ae8baa01b897",
"fr_FR": "f438ed9116ecc7b71412581255dd9b1332cacd9e2876615b03ec65e4b500bf02",
"fr_FR": "92b360ebbfae5c243897c05f7869e6bef0129a2c8275b6802fbaba0d05410e91",
}

View file

@ -132,5 +132,6 @@
"Refresh all feeds in the background": "Actualiser tous les abonnements en arrière-plan",
"Sign in with Google": "Se connecter avec Google",
"Unlink my Google account": "Dissocier mon compte Google",
"Link my Google account": "Associer mon compte Google"
"Link my Google account": "Associer mon compte Google",
"Category not found for this user.": "Cette catégorie n'existe pas pour cet utilisateur."
}

View file

@ -10,7 +10,7 @@ import (
"time"
)
// Feed represents a feed in the database
// Feed represents a feed in the database.
type Feed struct {
ID int64 `json:"id"`
UserID int64 `json:"user_id"`

View file

@ -18,11 +18,12 @@ import (
)
var (
errRequestFailed = "Unable to execute request: %v"
errServerFailure = "Unable to fetch feed (statusCode=%d)."
errDuplicate = "This feed already exists (%s)."
errNotFound = "Feed %d not found"
errEncoding = "Unable to normalize encoding: %v."
errRequestFailed = "Unable to execute request: %v"
errServerFailure = "Unable to fetch feed (statusCode=%d)."
errDuplicate = "This feed already exists (%s)."
errNotFound = "Feed %d not found"
errEncoding = "Unable to normalize encoding: %v."
errCategoryNotFound = "Category not found for this user."
)
// Handler contains all the logic to create and refresh feeds.
@ -34,6 +35,10 @@ type Handler struct {
func (h *Handler) CreateFeed(userID, categoryID int64, url string) (*model.Feed, error) {
defer helper.ExecutionTime(time.Now(), fmt.Sprintf("[Handler:CreateFeed] feedUrl=%s", url))
if !h.store.CategoryExists(userID, categoryID) {
return nil, errors.NewLocalizedError(errCategoryNotFound)
}
client := http.NewClient(url)
response, err := client.Get()
if err != nil {

View file

@ -33,7 +33,7 @@ func (h *Client) Get() (*Response, error) {
req := &http.Request{
URL: u,
Method: "GET",
Method: http.MethodGet,
Header: h.buildHeaders(),
}

View file

@ -6,6 +6,7 @@ package api
import (
"errors"
"github.com/miniflux/miniflux2/model"
"github.com/miniflux/miniflux2/server/api/payload"
"github.com/miniflux/miniflux2/server/core"
@ -99,23 +100,11 @@ func (c *Controller) GetFeedEntries(ctx *core.Context, request *core.Request, re
response.JSON().Standard(&payload.EntriesResponse{Total: count, Entries: entries})
}
// SetEntryStatus is the API handler to change the status of an entry.
// SetEntryStatus is the API handler to change the status of entries.
func (c *Controller) SetEntryStatus(ctx *core.Context, request *core.Request, response *core.Response) {
userID := ctx.UserID()
feedID, err := request.IntegerParam("feedID")
if err != nil {
response.JSON().BadRequest(err)
return
}
entryID, err := request.IntegerParam("entryID")
if err != nil {
response.JSON().BadRequest(err)
return
}
status, err := payload.DecodeEntryStatusPayload(request.Body())
entryIDs, status, err := payload.DecodeEntryStatusPayload(request.Body())
if err != nil {
response.JSON().BadRequest(errors.New("Invalid JSON payload"))
return
@ -126,31 +115,10 @@ func (c *Controller) SetEntryStatus(ctx *core.Context, request *core.Request, re
return
}
builder := c.store.GetEntryQueryBuilder(userID, ctx.UserTimezone())
builder.WithFeedID(feedID)
builder.WithEntryID(entryID)
entry, err := builder.GetEntry()
if err != nil {
response.JSON().ServerError(errors.New("Unable to fetch this entry from the database"))
if err := c.store.SetEntriesStatus(userID, entryIDs, status); err != nil {
response.JSON().ServerError(errors.New("Unable to change entries status"))
return
}
if entry == nil {
response.JSON().NotFound(errors.New("Entry not found"))
return
}
if err := c.store.SetEntriesStatus(userID, []int64{entry.ID}, status); err != nil {
response.JSON().ServerError(errors.New("Unable to change entry status"))
return
}
entry, err = builder.GetEntry()
if err != nil {
response.JSON().ServerError(errors.New("Unable to fetch this entry from the database"))
return
}
response.JSON().Standard(entry)
response.JSON().NoContent()
}

View file

@ -7,8 +7,9 @@ package payload
import (
"encoding/json"
"fmt"
"github.com/miniflux/miniflux2/model"
"io"
"github.com/miniflux/miniflux2/model"
)
type EntriesResponse struct {
@ -41,18 +42,19 @@ func DecodeURLPayload(data io.Reader) (string, error) {
return p.URL, nil
}
func DecodeEntryStatusPayload(data io.Reader) (string, error) {
func DecodeEntryStatusPayload(data io.Reader) ([]int64, string, error) {
type payload struct {
Status string `json:"status"`
EntryIDs []int64 `json:"entry_ids"`
Status string `json:"status"`
}
var p payload
decoder := json.NewDecoder(data)
if err := decoder.Decode(&p); err != nil {
return "", fmt.Errorf("invalid JSON payload: %v", err)
return nil, "", fmt.Errorf("invalid JSON payload: %v", err)
}
return p.Status, nil
return p.EntryIDs, p.Status, nil
}
func DecodeFeedCreationPayload(data io.Reader) (string, int64, error) {

View file

@ -62,7 +62,7 @@ func getRoutes(cfg *config.Config, store *storage.Storage, feedHandler *feed.Han
router.Handle("/v1/feeds/{feedID}/entries", apiHandler.Use(apiController.GetFeedEntries)).Methods("GET")
router.Handle("/v1/feeds/{feedID}/entries/{entryID}", apiHandler.Use(apiController.GetEntry)).Methods("GET")
router.Handle("/v1/feeds/{feedID}/entries/{entryID}", apiHandler.Use(apiController.SetEntryStatus)).Methods("PUT")
router.Handle("/v1/entries", apiHandler.Use(apiController.SetEntryStatus)).Methods("PUT")
router.Handle("/stylesheets/{name}.css", uiHandler.Use(uiController.Stylesheet)).Name("stylesheet").Methods("GET")
router.Handle("/js", uiHandler.Use(uiController.Javascript)).Name("javascript").Methods("GET")

View file

@ -1,5 +1,5 @@
// Code generated by go generate; DO NOT EDIT.
// 2017-11-24 16:04:49.314940117 -0800 PST m=+0.003107235
// 2017-11-24 21:40:33.326996526 -0800 PST m=+0.007911995
package static

View file

@ -1,5 +1,5 @@
// Code generated by go generate; DO NOT EDIT.
// 2017-11-24 16:04:49.315340301 -0800 PST m=+0.003507419
// 2017-11-24 21:40:33.330122316 -0800 PST m=+0.011037785
package static

View file

@ -1,5 +1,5 @@
// Code generated by go generate; DO NOT EDIT.
// 2017-11-24 16:04:49.316027642 -0800 PST m=+0.004194760
// 2017-11-24 21:40:33.333049571 -0800 PST m=+0.013965040
package static

View file

@ -1,5 +1,5 @@
// Code generated by go generate; DO NOT EDIT.
// 2017-11-24 16:04:49.318279667 -0800 PST m=+0.006446785
// 2017-11-24 21:40:33.353262943 -0800 PST m=+0.034178412
package template

View file

@ -1,5 +1,5 @@
// Code generated by go generate; DO NOT EDIT.
// 2017-11-24 16:04:49.316644027 -0800 PST m=+0.004811145
// 2017-11-24 21:40:33.335450873 -0800 PST m=+0.016366342
package template

View file

@ -1,5 +1,5 @@
// Code generated by go generate; DO NOT EDIT.
// 2017-11-24 16:04:49.314265268 -0800 PST m=+0.002432386
// 2017-11-24 21:40:33.323320501 -0800 PST m=+0.004235970
package sql

View file

@ -143,7 +143,7 @@ func (s *Storage) GetFeedById(userID, feedID int64) (*model.Feed, error) {
case err == sql.ErrNoRows:
return nil, nil
case err != nil:
return nil, fmt.Errorf("Unable to fetch feed: %v", err)
return nil, fmt.Errorf("unable to fetch feed: %v", err)
}
return &feed, nil
@ -170,7 +170,7 @@ func (s *Storage) CreateFeed(feed *model.Feed) error {
).Scan(&feed.ID)
if err != nil {
return fmt.Errorf("Unable to create feed: %v", err)
return fmt.Errorf("unable to create feed: %v", err)
}
for i := 0; i < len(feed.Entries); i++ {