-
Notifications
You must be signed in to change notification settings - Fork 726
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add API route /v1/enclosures/{enclosureId}
The GET method exposes individual enclosures by their ID. The PUT method allows external clients to update media_progression, similiar to the internal API used by the UI. The media proxy code was refactored to a method on the Enclosure and EnclosureList structures to avoid duplicate code and risking diverging behavior between googlereader and the API.
- Loading branch information
1 parent
bcbf9f4
commit 5c4df28
Showing
6 changed files
with
151 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package api // import "miniflux.app/v2/internal/api" | ||
|
||
import ( | ||
json_parser "encoding/json" | ||
"net/http" | ||
|
||
"miniflux.app/v2/internal/http/request" | ||
"miniflux.app/v2/internal/http/response/json" | ||
"miniflux.app/v2/internal/model" | ||
"miniflux.app/v2/internal/validator" | ||
) | ||
|
||
func (h *handler) getEnclosureById(w http.ResponseWriter, r *http.Request) { | ||
enclosureID := request.RouteInt64Param(r, "enclosureID") | ||
|
||
enclosure, err := h.store.GetEnclosure(enclosureID) | ||
|
||
if err != nil { | ||
json.ServerError(w, r, err) | ||
return | ||
} | ||
|
||
if enclosure == nil { | ||
json.NotFound(w, r) | ||
return | ||
} | ||
|
||
userID := request.UserID(r) | ||
|
||
if enclosure.UserID != userID { | ||
json.NotFound(w, r) | ||
return | ||
} | ||
|
||
enclosure.ProxifyEnclosureURL(h.router) | ||
|
||
json.OK(w, r, enclosure) | ||
} | ||
|
||
func (h *handler) updateEnclosureById(w http.ResponseWriter, r *http.Request) { | ||
enclosureID := request.RouteInt64Param(r, "enclosureID") | ||
|
||
var enclosureUpdateRequest model.EnclosureUpdateRequest | ||
|
||
if err := json_parser.NewDecoder(r.Body).Decode(&enclosureUpdateRequest); err != nil { | ||
json.BadRequest(w, r, err) | ||
return | ||
} | ||
|
||
if err := validator.ValidateEnclosureUpdateRequest(&enclosureUpdateRequest); err != nil { | ||
json.BadRequest(w, r, err) | ||
return | ||
} | ||
|
||
enclosure, err := h.store.GetEnclosure(enclosureID) | ||
|
||
if err != nil { | ||
json.BadRequest(w, r, err) | ||
return | ||
} | ||
|
||
if enclosure == nil { | ||
json.NotFound(w, r) | ||
return | ||
} | ||
|
||
userID := request.UserID(r) | ||
|
||
if enclosure.UserID != userID { | ||
json.NotFound(w, r) | ||
return | ||
} | ||
|
||
enclosure.MediaProgression = enclosureUpdateRequest.MediaProgression | ||
|
||
if err := h.store.UpdateEnclosure(enclosure); err != nil { | ||
json.ServerError(w, r, err) | ||
return | ||
} | ||
|
||
json.NoContent(w, r) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package validator | ||
|
||
import ( | ||
"fmt" | ||
|
||
"miniflux.app/v2/internal/model" | ||
) | ||
|
||
func ValidateEnclosureUpdateRequest(request *model.EnclosureUpdateRequest) error { | ||
if request.MediaProgression < 0 { | ||
return fmt.Errorf(`media progression must an positive integer`) | ||
} | ||
|
||
return nil | ||
} |