From bddca15b69692bd055c507f2469e68dca1e56098 Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Tue, 19 Jun 2018 22:58:29 -0700 Subject: Add new fields for feed username/password --- api/feed.go | 19 +++++++++++++------ api/payload.go | 45 ++++++++++++++++++++++++++------------------- api/subscription.go | 8 ++++++-- 3 files changed, 45 insertions(+), 27 deletions(-) (limited to 'api') diff --git a/api/feed.go b/api/feed.go index f6e3c16..351457f 100644 --- a/api/feed.go +++ b/api/feed.go @@ -15,18 +15,18 @@ import ( // CreateFeed is the API handler to create a new feed. func (c *Controller) CreateFeed(w http.ResponseWriter, r *http.Request) { - feedURL, categoryID, err := decodeFeedCreationPayload(r.Body) + feedInfo, err := decodeFeedCreationPayload(r.Body) if err != nil { json.BadRequest(w, err) return } - if feedURL == "" { + if feedInfo.FeedURL == "" { json.BadRequest(w, errors.New("The feed_url is required")) return } - if categoryID <= 0 { + if feedInfo.CategoryID <= 0 { json.BadRequest(w, errors.New("The category_id is required")) return } @@ -34,17 +34,24 @@ func (c *Controller) CreateFeed(w http.ResponseWriter, r *http.Request) { ctx := context.New(r) userID := ctx.UserID() - if c.store.FeedURLExists(userID, feedURL) { + if c.store.FeedURLExists(userID, feedInfo.FeedURL) { json.BadRequest(w, errors.New("This feed_url already exists")) return } - if !c.store.CategoryExists(userID, categoryID) { + if !c.store.CategoryExists(userID, feedInfo.CategoryID) { json.BadRequest(w, errors.New("This category_id doesn't exists or doesn't belongs to this user")) return } - feed, err := c.feedHandler.CreateFeed(userID, categoryID, feedURL, false) + feed, err := c.feedHandler.CreateFeed( + userID, + feedInfo.CategoryID, + feedInfo.FeedURL, + feedInfo.Crawler, + feedInfo.Username, + feedInfo.Password, + ) if err != nil { json.ServerError(w, errors.New("Unable to create this feed")) return diff --git a/api/payload.go b/api/payload.go index bf470eb..45cb826 100644 --- a/api/payload.go +++ b/api/payload.go @@ -23,6 +23,20 @@ type entriesResponse struct { Entries model.Entries `json:"entries"` } +type feedCreation struct { + FeedURL string `json:"feed_url"` + CategoryID int64 `json:"category_id"` + Username string `json:"username"` + Password string `json:"password"` + Crawler bool `json:"crawler"` +} + +type subscriptionDiscovery struct { + URL string `json:"url"` + Username string `json:"username"` + Password string `json:"password"` +} + func decodeUserPayload(r io.ReadCloser) (*model.User, error) { var user model.User @@ -35,19 +49,16 @@ func decodeUserPayload(r io.ReadCloser) (*model.User, error) { return &user, nil } -func decodeURLPayload(r io.ReadCloser) (string, error) { - type payload struct { - URL string `json:"url"` - } +func decodeURLPayload(r io.ReadCloser) (*subscriptionDiscovery, error) { + defer r.Close() - var p payload + var s subscriptionDiscovery decoder := json.NewDecoder(r) - defer r.Close() - if err := decoder.Decode(&p); err != nil { - return "", fmt.Errorf("invalid JSON payload: %v", err) + if err := decoder.Decode(&s); err != nil { + return nil, fmt.Errorf("invalid JSON payload: %v", err) } - return p.URL, nil + return &s, nil } func decodeEntryStatusPayload(r io.ReadCloser) ([]int64, string, error) { @@ -66,20 +77,16 @@ func decodeEntryStatusPayload(r io.ReadCloser) ([]int64, string, error) { return p.EntryIDs, p.Status, nil } -func decodeFeedCreationPayload(r io.ReadCloser) (string, int64, error) { - type payload struct { - FeedURL string `json:"feed_url"` - CategoryID int64 `json:"category_id"` - } +func decodeFeedCreationPayload(r io.ReadCloser) (*feedCreation, error) { + defer r.Close() - var p payload + var fc feedCreation decoder := json.NewDecoder(r) - defer r.Close() - if err := decoder.Decode(&p); err != nil { - return "", 0, fmt.Errorf("invalid JSON payload: %v", err) + if err := decoder.Decode(&fc); err != nil { + return nil, fmt.Errorf("invalid JSON payload: %v", err) } - return p.FeedURL, p.CategoryID, nil + return &fc, nil } func decodeFeedModificationPayload(r io.ReadCloser) (*model.Feed, error) { diff --git a/api/subscription.go b/api/subscription.go index c858f0e..19adc9e 100644 --- a/api/subscription.go +++ b/api/subscription.go @@ -15,13 +15,17 @@ import ( // GetSubscriptions is the API handler to find subscriptions. func (c *Controller) GetSubscriptions(w http.ResponseWriter, r *http.Request) { - websiteURL, err := decodeURLPayload(r.Body) + subscriptionInfo, err := decodeURLPayload(r.Body) if err != nil { json.BadRequest(w, err) return } - subscriptions, err := subscription.FindSubscriptions(websiteURL) + subscriptions, err := subscription.FindSubscriptions( + subscriptionInfo.URL, + subscriptionInfo.Username, + subscriptionInfo.Password, + ) if err != nil { json.ServerError(w, errors.New("Unable to discover subscriptions")) return -- cgit v1.2.3