mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-27 13:48:49 +08:00

* MF-1368 - Add internal http api package for query params reading Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix comments Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix comments Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix reviews Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Use internal/http and internalhttp alias Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Mv errors types to pkg Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Use httputil/query.go and remove aliases Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Add blank lines after error definitions Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Add ReadBoolValueQuery Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Mv readBoolValueQuery Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * User ErrNotFoundParam instead of pointer Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Revert ReadUintQuery to use default values Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Use default values for all query readers Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>
117 lines
2.6 KiB
Go
117 lines
2.6 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
|
|
kithttp "github.com/go-kit/kit/transport/http"
|
|
"github.com/go-zoo/bone"
|
|
"github.com/mainflux/mainflux"
|
|
"github.com/mainflux/mainflux/pkg/errors"
|
|
"github.com/mainflux/mainflux/provision"
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
)
|
|
|
|
const (
|
|
contentType = "application/json"
|
|
)
|
|
|
|
var (
|
|
errUnauthorized = errors.New("missing or invalid credentials provided")
|
|
errConflict = errors.New("entity already exists")
|
|
)
|
|
|
|
// MakeHandler returns a HTTP handler for API endpoints.
|
|
func MakeHandler(svc provision.Service) http.Handler {
|
|
|
|
opts := []kithttp.ServerOption{
|
|
kithttp.ServerErrorEncoder(encodeError),
|
|
}
|
|
|
|
r := bone.New()
|
|
|
|
r.Post("/mapping", kithttp.NewServer(
|
|
doProvision(svc),
|
|
decodeProvisionRequest,
|
|
encodeResponse,
|
|
opts...,
|
|
))
|
|
|
|
r.Get("/mapping", kithttp.NewServer(
|
|
getMapping(svc),
|
|
decodeMappingRequest,
|
|
encodeResponse,
|
|
opts...,
|
|
))
|
|
|
|
r.Handle("/metrics", promhttp.Handler())
|
|
r.GetFunc("/version", mainflux.Version("provision"))
|
|
|
|
return r
|
|
}
|
|
|
|
func encodeResponse(_ context.Context, w http.ResponseWriter, response interface{}) error {
|
|
w.Header().Set("Content-Type", contentType)
|
|
|
|
if ar, ok := response.(mainflux.Response); ok {
|
|
for k, v := range ar.Headers() {
|
|
w.Header().Set(k, v)
|
|
}
|
|
|
|
w.WriteHeader(ar.Code())
|
|
|
|
if ar.Empty() {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
return json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
func decodeProvisionRequest(_ context.Context, r *http.Request) (interface{}, error) {
|
|
if r.Header.Get("Content-Type") != contentType {
|
|
return nil, errors.ErrUnsupportedContentType
|
|
}
|
|
|
|
req := provisionReq{token: r.Header.Get("Authorization")}
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return req, nil
|
|
}
|
|
|
|
func decodeMappingRequest(_ context.Context, r *http.Request) (interface{}, error) {
|
|
if r.Header.Get("Content-Type") != contentType {
|
|
return nil, errors.ErrUnsupportedContentType
|
|
}
|
|
|
|
req := mappingReq{token: r.Header.Get("Authorization")}
|
|
|
|
return req, nil
|
|
}
|
|
|
|
func encodeError(_ context.Context, err error, w http.ResponseWriter) {
|
|
w.Header().Set("Content-Type", contentType)
|
|
|
|
switch err {
|
|
case errors.ErrUnsupportedContentType:
|
|
w.WriteHeader(http.StatusUnsupportedMediaType)
|
|
case io.EOF, errors.ErrMalformedEntity:
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
case errConflict:
|
|
w.WriteHeader(http.StatusConflict)
|
|
default:
|
|
switch err.(type) {
|
|
case *json.SyntaxError:
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
case *json.UnmarshalTypeError:
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
default:
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
}
|
|
}
|
|
}
|