Skip to content

Commit

Permalink
Add GetFeedWithAuth() func
Browse files Browse the repository at this point in the history
  • Loading branch information
ybizeul committed Jan 15, 2024
1 parent eb46ee8 commit cb67c95
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 77 deletions.
16 changes: 16 additions & 0 deletions internal/feed/feedmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,19 @@ func (m *FeedManager) GetFeed(feedName string) (*Feed, error) {

return result, nil
}

func (m *FeedManager) GetFeedWithAuth(feedName string, secret string) (*Feed, error) {
result, err := m.GetFeed(feedName)

if err != nil {
return nil, err
}

err = result.IsSecretValid(secret)

if err != nil {
return nil, err
}

return result, nil
}
86 changes: 9 additions & 77 deletions internal/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,22 +226,12 @@ func (api *ApiHandler) feedWSHandler(w http.ResponseWriter, r *http.Request) {
utils.CloseWithCodeAndMessage(w, 500, "Unable to obtain feed name")
}

f, err := api.FeedManager.GetFeed(feedName)
_, err := api.FeedManager.GetFeedWithAuth(feedName, secret)

if err != nil {
switch {
case errors.Is(err, feed.FeedErrorNotFound):
utils.CloseWithCodeAndMessage(w, 404, fmt.Sprintf("feed '%s' not found", feedName))
default:
utils.CloseWithCodeAndMessage(w, 500, fmt.Sprintf("Error while getting feed: %s", err.Error()))
}
return
}

err = f.IsSecretValid(secret)

if err != nil {
switch {
case errors.Is(err, feed.FeedErrorInvalidSecret):
utils.CloseWithCodeAndMessage(w, 401, "Unauthorized")
default:
Expand Down Expand Up @@ -323,22 +313,13 @@ func (api *ApiHandler) feedPatchHandlerFunc(w http.ResponseWriter, r *http.Reque
if feedName == "" {
utils.CloseWithCodeAndMessage(w, 500, "Unable to obtain feed name")
}
f, err := api.FeedManager.GetFeed(feedName)

f, err := api.FeedManager.GetFeedWithAuth(feedName, secret)

if err != nil {
switch {
case errors.Is(err, feed.FeedErrorNotFound):
utils.CloseWithCodeAndMessage(w, 404, fmt.Sprintf("feed '%s' not found", feedName))
default:
utils.CloseWithCodeAndMessage(w, 500, fmt.Sprintf("Error while getting feed: %s", err.Error()))
}
return
}

err = f.IsSecretValid(secret)

if err != nil {
switch {
case errors.Is(err, feed.FeedErrorInvalidSecret):
utils.CloseWithCodeAndMessage(w, 401, "Unauthorized")
default:
Expand Down Expand Up @@ -376,22 +357,12 @@ func (api *ApiHandler) feedItemHandlerFunc(w http.ResponseWriter, r *http.Reques
return
}

f, err := api.FeedManager.GetFeed(feedName)
f, err := api.FeedManager.GetFeedWithAuth(feedName, secret)

if err != nil {
switch {
case errors.Is(err, feed.FeedErrorNotFound):
utils.CloseWithCodeAndMessage(w, 404, fmt.Sprintf("feed '%s' not found", feedName))
default:
utils.CloseWithCodeAndMessage(w, 500, fmt.Sprintf("Error while getting feed: %s", err.Error()))
}
return
}

err = f.IsSecretValid(secret)

if err != nil {
switch {
case errors.Is(err, feed.FeedErrorInvalidSecret):
utils.CloseWithCodeAndMessage(w, 401, "Unauthorized")
default:
Expand Down Expand Up @@ -431,22 +402,12 @@ func (api *ApiHandler) feedPostHandlerFunc(w http.ResponseWriter, r *http.Reques
utils.CloseWithCodeAndMessage(w, 500, "Unable to obtain feed name")
}

f, err := api.FeedManager.GetFeed(feedName)
f, err := api.FeedManager.GetFeedWithAuth(feedName, secret)

if err != nil {
switch {
case errors.Is(err, feed.FeedErrorNotFound):
utils.CloseWithCodeAndMessage(w, 404, fmt.Sprintf("feed '%s' not found", feedName))
default:
utils.CloseWithCodeAndMessage(w, 500, fmt.Sprintf("Error while getting feed: %s", err.Error()))
}
return
}

err = f.IsSecretValid(secret)

if err != nil {
switch {
case errors.Is(err, feed.FeedErrorInvalidSecret):
utils.CloseWithCodeAndMessage(w, 401, "Unauthorized")
default:
Expand Down Expand Up @@ -486,22 +447,12 @@ func (api *ApiHandler) feedItemDeleteHandlerFunc(w http.ResponseWriter, r *http.
utils.CloseWithCodeAndMessage(w, 500, "Unable to obtain feed name")
}

f, err := api.FeedManager.GetFeed(feedName)
f, err := api.FeedManager.GetFeedWithAuth(feedName, secret)

if err != nil {
switch {
case errors.Is(err, feed.FeedErrorNotFound):
utils.CloseWithCodeAndMessage(w, 404, fmt.Sprintf("feed '%s' not found", feedName))
default:
utils.CloseWithCodeAndMessage(w, 500, fmt.Sprintf("Error while getting feed: %s", err.Error()))
}
return
}

err = f.IsSecretValid(secret)

if err != nil {
switch {
case errors.Is(err, feed.FeedErrorInvalidSecret):
utils.CloseWithCodeAndMessage(w, 401, "Unauthorized")
default:
Expand Down Expand Up @@ -542,22 +493,12 @@ func (api *ApiHandler) feedSubscriptionHandlerFunc(w http.ResponseWriter, r *htt
utils.CloseWithCodeAndMessage(w, 500, "Unable to obtain feed name")
}

f, err := api.FeedManager.GetFeed(feedName)
f, err := api.FeedManager.GetFeedWithAuth(feedName, secret)

if err != nil {
switch {
case errors.Is(err, feed.FeedErrorNotFound):
utils.CloseWithCodeAndMessage(w, 404, fmt.Sprintf("feed '%s' not found", feedName))
default:
utils.CloseWithCodeAndMessage(w, 500, fmt.Sprintf("Error while getting feed: %s", err.Error()))
}
return
}

err = f.IsSecretValid(secret)

if err != nil {
switch {
case errors.Is(err, feed.FeedErrorInvalidSecret):
utils.CloseWithCodeAndMessage(w, 401, "Unauthorized")
default:
Expand Down Expand Up @@ -603,29 +544,20 @@ func (api *ApiHandler) feedUnsubscribeHandlerFunc(w http.ResponseWriter, r *http
utils.CloseWithCodeAndMessage(w, 500, "Unable to obtain feed name")
}

f, err := api.FeedManager.GetFeed(feedName)
f, err := api.FeedManager.GetFeedWithAuth(feedName, secret)

if err != nil {
switch {
case errors.Is(err, feed.FeedErrorNotFound):
utils.CloseWithCodeAndMessage(w, 404, fmt.Sprintf("feed '%s' not found", feedName))
default:
utils.CloseWithCodeAndMessage(w, 500, fmt.Sprintf("Error while getting feed: %s", err.Error()))
}
return
}

err = f.IsSecretValid(secret)

if err != nil {
switch {
case errors.Is(err, feed.FeedErrorInvalidSecret):
utils.CloseWithCodeAndMessage(w, 401, "Unauthorized")
default:
utils.CloseWithCodeAndMessage(w, 500, fmt.Sprintf("Error while getting feed: %s", err.Error()))
}
return
}

body, err := io.ReadAll(r.Body)

defer r.Body.Close()
Expand Down

0 comments on commit cb67c95

Please sign in to comment.