mirror of
https://github.com/empayre/fleet.git
synced 2024-11-06 08:55:24 +00:00
715d908613
Notable refactoring: - Use stdlib "context" in place of "golang.org/x/net/context" - Go-kit no longer wraps errors, so we remove the unwrap in transport_error.go - Use MakeHandler when setting up endpoint tests (fixes test bug caught during this refactoring) Closes #1411.
121 lines
3.0 KiB
Go
121 lines
3.0 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
func decodeEnableUserRequest(ctx context.Context, r *http.Request) (interface{}, error) {
|
|
id, err := idFromRequest(r, "id")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var req enableUserRequest
|
|
if err = json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
return nil, err
|
|
}
|
|
req.ID = id
|
|
return req, nil
|
|
}
|
|
|
|
func decodeAdminUserRequest(ctx context.Context, r *http.Request) (interface{}, error) {
|
|
id, err := idFromRequest(r, "id")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var req adminUserRequest
|
|
if err = json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
return nil, err
|
|
}
|
|
req.ID = id
|
|
return req, nil
|
|
}
|
|
|
|
func decodeCreateUserRequest(ctx context.Context, r *http.Request) (interface{}, error) {
|
|
var req createUserRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req.payload); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return req, nil
|
|
}
|
|
|
|
func decodeGetUserRequest(ctx context.Context, r *http.Request) (interface{}, error) {
|
|
id, err := idFromRequest(r, "id")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return getUserRequest{ID: id}, nil
|
|
}
|
|
|
|
func decodeListUsersRequest(ctx context.Context, r *http.Request) (interface{}, error) {
|
|
opt, err := listOptionsFromRequest(r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return listUsersRequest{ListOptions: opt}, nil
|
|
}
|
|
|
|
func decodeModifyUserRequest(ctx context.Context, r *http.Request) (interface{}, error) {
|
|
id, err := idFromRequest(r, "id")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var req modifyUserRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req.payload); err != nil {
|
|
return nil, err
|
|
}
|
|
req.ID = id
|
|
return req, nil
|
|
}
|
|
|
|
func decodeChangePasswordRequest(ctx context.Context, r *http.Request) (interface{}, error) {
|
|
var req changePasswordRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
return nil, err
|
|
}
|
|
return req, nil
|
|
}
|
|
|
|
func decodeRequirePasswordResetRequest(ctx context.Context, r *http.Request) (interface{}, error) {
|
|
id, err := idFromRequest(r, "id")
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "getting ID from request")
|
|
}
|
|
|
|
var req requirePasswordResetRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
return nil, errors.Wrap(err, "decoding JSON")
|
|
}
|
|
req.ID = id
|
|
|
|
return req, nil
|
|
}
|
|
|
|
func decodePerformRequiredPasswordResetRequest(ctx context.Context, r *http.Request) (interface{}, error) {
|
|
var req performRequiredPasswordResetRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
return nil, errors.Wrap(err, "decoding JSON")
|
|
}
|
|
return req, nil
|
|
}
|
|
|
|
func decodeForgotPasswordRequest(ctx context.Context, r *http.Request) (interface{}, error) {
|
|
var req forgotPasswordRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
return nil, err
|
|
}
|
|
return req, nil
|
|
}
|
|
|
|
func decodeResetPasswordRequest(ctx context.Context, r *http.Request) (interface{}, error) {
|
|
var req resetPasswordRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
return nil, err
|
|
}
|
|
return req, nil
|
|
}
|