mirror of
https://github.com/mxpv/podsync.git
synced 2024-05-11 05:55:04 +00:00
Implement catch all handler for feeds, reword error responses
This commit is contained in:
@@ -2,6 +2,12 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrNotFound = errors.New("not found")
|
||||||
)
|
)
|
||||||
|
|
||||||
type Provider string
|
type Provider string
|
||||||
|
@@ -125,7 +125,7 @@ func (yt *YouTubeBuilder) queryFeed(feed *api.Feed) (*itunes.Podcast, string, er
|
|||||||
if feed.LinkType == api.Channel || feed.LinkType == api.User {
|
if feed.LinkType == api.Channel || feed.LinkType == api.User {
|
||||||
channel, err := yt.listChannels(feed.LinkType, feed.ItemId)
|
channel, err := yt.listChannels(feed.LinkType, feed.ItemId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, "", errors.Wrap(err, "failed to query channel")
|
return nil, "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
itemId := channel.ContentDetails.RelatedPlaylists.Uploads
|
itemId := channel.ContentDetails.RelatedPlaylists.Uploads
|
||||||
|
@@ -79,12 +79,7 @@ func (s *service) GetFeed(hashId string) (*itunes.Podcast, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *service) GetMetadata(hashId string) (*api.Feed, error) {
|
func (s *service) GetMetadata(hashId string) (*api.Feed, error) {
|
||||||
feed, err := s.storage.GetFeed(hashId)
|
return s.storage.GetFeed(hashId)
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Wrapf(err, "failed to query feed: %s", hashId)
|
|
||||||
}
|
|
||||||
|
|
||||||
return feed, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type feedOption func(*service)
|
type feedOption func(*service)
|
||||||
|
@@ -14,7 +14,6 @@ import (
|
|||||||
itunes "github.com/mxpv/podcast"
|
itunes "github.com/mxpv/podcast"
|
||||||
"github.com/mxpv/podsync/pkg/api"
|
"github.com/mxpv/podsync/pkg/api"
|
||||||
"github.com/mxpv/podsync/pkg/config"
|
"github.com/mxpv/podsync/pkg/config"
|
||||||
"github.com/pkg/errors"
|
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -198,16 +197,21 @@ func MakeHandlers(feed feed, cfg *config.AppConfig) http.Handler {
|
|||||||
c.JSON(http.StatusOK, gin.H{"id": hashId})
|
c.JSON(http.StatusOK, gin.H{"id": hashId})
|
||||||
})
|
})
|
||||||
|
|
||||||
r.GET("/api/feed/:hashId", func(c *gin.Context) {
|
r.NoRoute(func(c *gin.Context) {
|
||||||
hashId := c.Param("hashId")
|
hashId := c.Request.URL.Path[1:]
|
||||||
if hashId == "" || len(hashId) > 12 {
|
if hashId == "" || len(hashId) > 12 {
|
||||||
c.JSON(badRequest(errors.New("invalid feed id")))
|
c.String(http.StatusBadRequest, "invalid feed id")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
podcast, err := feed.GetFeed(hashId)
|
podcast, err := feed.GetFeed(hashId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(internalError(err))
|
code := http.StatusInternalServerError
|
||||||
|
if err == api.ErrNotFound {
|
||||||
|
code = http.StatusNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
c.String(code, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -217,13 +221,13 @@ func MakeHandlers(feed feed, cfg *config.AppConfig) http.Handler {
|
|||||||
r.GET("/api/metadata/:hashId", func(c *gin.Context) {
|
r.GET("/api/metadata/:hashId", func(c *gin.Context) {
|
||||||
hashId := c.Param("hashId")
|
hashId := c.Param("hashId")
|
||||||
if hashId == "" || len(hashId) > 12 {
|
if hashId == "" || len(hashId) > 12 {
|
||||||
c.JSON(badRequest(errors.New("invalid feed id")))
|
c.String(http.StatusBadRequest, "invalid feed id")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
feed, err := feed.GetMetadata(hashId)
|
feed, err := feed.GetMetadata(hashId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(internalError(err))
|
c.String(http.StatusInternalServerError, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -61,6 +61,10 @@ func (r *RedisStorage) GetFeed(hashId string) (*api.Feed, error) {
|
|||||||
return nil, errors.Wrapf(err, "failed to query feed with id %s", hashId)
|
return nil, errors.Wrapf(err, "failed to query feed with id %s", hashId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(result) == 0 {
|
||||||
|
return nil, api.ErrNotFound
|
||||||
|
}
|
||||||
|
|
||||||
// Expire after 3 month if no use
|
// Expire after 3 month if no use
|
||||||
if err := r.client.Expire(hashId, expiration).Err(); err != nil {
|
if err := r.client.Expire(hashId, expiration).Err(); err != nil {
|
||||||
return nil, errors.Wrap(err, "failed query update feed")
|
return nil, errors.Wrap(err, "failed query update feed")
|
||||||
|
Reference in New Issue
Block a user