mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 00:39:13 +00:00
refactor(http): api key cache handling (#1632)
This commit is contained in:
parent
0d53f7e5fc
commit
d13b421c42
6 changed files with 117 additions and 50 deletions
|
@ -17,7 +17,6 @@ import (
|
|||
type apikeyService interface {
|
||||
List(ctx context.Context) ([]domain.APIKey, error)
|
||||
Store(ctx context.Context, key *domain.APIKey) error
|
||||
Update(ctx context.Context, key *domain.APIKey) error
|
||||
Delete(ctx context.Context, key string) error
|
||||
ValidateAPIKey(ctx context.Context, token string) bool
|
||||
}
|
||||
|
@ -51,18 +50,14 @@ func (h apikeyHandler) list(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func (h apikeyHandler) store(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
var (
|
||||
ctx = r.Context()
|
||||
data domain.APIKey
|
||||
)
|
||||
var data domain.APIKey
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.service.Store(ctx, &data); err != nil {
|
||||
if err := h.service.Store(r.Context(), &data); err != nil {
|
||||
h.encoder.Error(w, err)
|
||||
return
|
||||
}
|
||||
|
|
|
@ -24,6 +24,7 @@ func (e encoder) StatusResponse(w http.ResponseWriter, status int, response inte
|
|||
if response != nil {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(status)
|
||||
|
||||
if err := json.NewEncoder(w).Encode(response); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
|
@ -37,6 +38,7 @@ func (e encoder) StatusResponseMessage(w http.ResponseWriter, status int, messag
|
|||
if message != "" {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(status)
|
||||
|
||||
if err := json.NewEncoder(w).Encode(statusResponse{Message: message}); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
|
@ -53,6 +55,7 @@ func (e encoder) StatusCreated(w http.ResponseWriter) {
|
|||
func (e encoder) StatusCreatedData(w http.ResponseWriter, data interface{}) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
|
||||
if err := json.NewEncoder(w).Encode(data); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
|
@ -74,7 +77,11 @@ func (e encoder) NotFoundErr(w http.ResponseWriter, err error) {
|
|||
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
json.NewEncoder(w).Encode(res)
|
||||
|
||||
if err := json.NewEncoder(w).Encode(res); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (e encoder) StatusInternalError(w http.ResponseWriter) {
|
||||
|
@ -88,7 +95,11 @@ func (e encoder) Error(w http.ResponseWriter, err error) {
|
|||
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
json.NewEncoder(w).Encode(res)
|
||||
|
||||
if err := json.NewEncoder(w).Encode(res); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (e encoder) StatusError(w http.ResponseWriter, status int, err error) {
|
||||
|
@ -98,6 +109,7 @@ func (e encoder) StatusError(w http.ResponseWriter, status int, err error) {
|
|||
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(status)
|
||||
|
||||
if err := json.NewEncoder(w).Encode(res); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue