mirror of
https://github.com/mainflux/mainflux.git
synced 2025-05-08 19:29:17 +08:00

* Add SKD tests for creating channel Signed-off-by: Ivan Milošević <iva@blokovi.com> * Add SDK tests for Channel function Signed-off-by: Ivan Milošević <iva@blokovi.com> * Add list channels over SDK tests Signed-off-by: Ivan Milošević <iva@blokovi.com> * Add SDK tests for updating channel Signed-off-by: Ivan Milošević <iva@blokovi.com> * Add SDK tests for deleting channel Signed-off-by: Ivan Milošević <iva@blokovi.com> * Add users service SDK tests Signed-off-by: Ivan Milošević <iva@blokovi.com> * SDK things tests Signed-off-by: Ivan Milošević <iva@blokovi.com> * Add SDK test for connecting and disconnecting things from channel Signed-off-by: Ivan Milošević <iva@blokovi.com> * testing SDK sending messages Signed-off-by: Ivan Milošević <iva@blokovi.com> * add tests for SDK func SetContentType Signed-off-by: Ivan Milošević <iva@blokovi.com> * add all test cases for sending messages Signed-off-by: Ivan Milošević <iva@blokovi.com> * Add handling StatusBadRequest when deleting thing over SDK Signed-off-by: Ivan Milošević <iva@blokovi.com> * Update error responses when deleting channel and thing Signed-off-by: Ivan Milošević <iva@blokovi.com> * Removed unused Unauthorized response when creating user Signed-off-by: Ivan Milošević <iva@blokovi.com> * update testing CreateChannel, tests if response is some string Signed-off-by: Ivan Milošević <iva@blokovi.com> * Add bad request case in testing CreateToken Signed-off-by: Ivan Milošević <iva@blokovi.com> * Remove response error conflict from things service Signed-off-by: Ivan Milošević <iva@blokovi.com> * Fix definition of sdk.ErrFailedDisconnect and return error in test cases for disconnecting things Signed-off-by: Ivan Milošević <iva@blokovi.com> * Add handling errors and formatting code Signed-off-by: Ivan Milošević <iva@blokovi.com> * Defined new ErrFailedPublish error in SDK Signed-off-by: Ivan Milošević <iva@blokovi.com> * Add SDK test for version Signed-off-by: Ivan Milošević <iva@blokovi.com> * Delete unused http response status in sdk.DeleteChannel Signed-off-by: Ivan Milošević <iva@blokovi.com>
292 lines
6.1 KiB
Go
292 lines
6.1 KiB
Go
//
|
|
// Copyright (c) 2018
|
|
// Mainflux
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
|
|
package http
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"strconv"
|
|
|
|
kithttp "github.com/go-kit/kit/transport/http"
|
|
"github.com/go-zoo/bone"
|
|
"github.com/mainflux/mainflux"
|
|
"github.com/mainflux/mainflux/things"
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
)
|
|
|
|
const contentType = "application/json"
|
|
|
|
var (
|
|
errUnsupportedContentType = errors.New("unsupported content type")
|
|
errInvalidQueryParams = errors.New("invalid query params")
|
|
)
|
|
|
|
// MakeHandler returns a HTTP handler for API endpoints.
|
|
func MakeHandler(svc things.Service) http.Handler {
|
|
opts := []kithttp.ServerOption{
|
|
kithttp.ServerErrorEncoder(encodeError),
|
|
}
|
|
|
|
r := bone.New()
|
|
|
|
r.Post("/things", kithttp.NewServer(
|
|
addThingEndpoint(svc),
|
|
decodeThingCreation,
|
|
encodeResponse,
|
|
opts...,
|
|
))
|
|
|
|
r.Put("/things/:id", kithttp.NewServer(
|
|
updateThingEndpoint(svc),
|
|
decodeThingUpdate,
|
|
encodeResponse,
|
|
opts...,
|
|
))
|
|
|
|
r.Delete("/things/:id", kithttp.NewServer(
|
|
removeThingEndpoint(svc),
|
|
decodeView,
|
|
encodeResponse,
|
|
opts...,
|
|
))
|
|
|
|
r.Get("/things/:id", kithttp.NewServer(
|
|
viewThingEndpoint(svc),
|
|
decodeView,
|
|
encodeResponse,
|
|
opts...,
|
|
))
|
|
|
|
r.Get("/things", kithttp.NewServer(
|
|
listThingsEndpoint(svc),
|
|
decodeList,
|
|
encodeResponse,
|
|
opts...,
|
|
))
|
|
|
|
r.Post("/channels", kithttp.NewServer(
|
|
createChannelEndpoint(svc),
|
|
decodeChannelCreation,
|
|
encodeResponse,
|
|
opts...,
|
|
))
|
|
|
|
r.Put("/channels/:id", kithttp.NewServer(
|
|
updateChannelEndpoint(svc),
|
|
decodeChannelUpdate,
|
|
encodeResponse,
|
|
opts...,
|
|
))
|
|
|
|
r.Delete("/channels/:id", kithttp.NewServer(
|
|
removeChannelEndpoint(svc),
|
|
decodeView,
|
|
encodeResponse,
|
|
opts...,
|
|
))
|
|
|
|
r.Get("/channels/:id", kithttp.NewServer(
|
|
viewChannelEndpoint(svc),
|
|
decodeView,
|
|
encodeResponse,
|
|
opts...,
|
|
))
|
|
|
|
r.Get("/channels", kithttp.NewServer(
|
|
listChannelsEndpoint(svc),
|
|
decodeList,
|
|
encodeResponse,
|
|
opts...,
|
|
))
|
|
|
|
r.Put("/channels/:chanId/things/:thingId", kithttp.NewServer(
|
|
connectEndpoint(svc),
|
|
decodeConnection,
|
|
encodeResponse,
|
|
opts...,
|
|
))
|
|
|
|
r.Delete("/channels/:chanId/things/:thingId", kithttp.NewServer(
|
|
disconnectEndpoint(svc),
|
|
decodeConnection,
|
|
encodeResponse,
|
|
opts...,
|
|
))
|
|
|
|
r.GetFunc("/version", mainflux.Version("things"))
|
|
r.Handle("/metrics", promhttp.Handler())
|
|
|
|
return r
|
|
}
|
|
|
|
func decodeThingCreation(_ context.Context, r *http.Request) (interface{}, error) {
|
|
if r.Header.Get("Content-Type") != contentType {
|
|
return nil, errUnsupportedContentType
|
|
}
|
|
|
|
req := addThingReq{key: r.Header.Get("Authorization")}
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return req, nil
|
|
}
|
|
|
|
func decodeThingUpdate(_ context.Context, r *http.Request) (interface{}, error) {
|
|
if r.Header.Get("Content-Type") != contentType {
|
|
return nil, errUnsupportedContentType
|
|
}
|
|
|
|
req := updateThingReq{
|
|
key: r.Header.Get("Authorization"),
|
|
id: bone.GetValue(r, "id"),
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return req, nil
|
|
}
|
|
|
|
func decodeChannelCreation(_ context.Context, r *http.Request) (interface{}, error) {
|
|
if r.Header.Get("Content-Type") != contentType {
|
|
return nil, errUnsupportedContentType
|
|
}
|
|
|
|
req := createChannelReq{key: r.Header.Get("Authorization")}
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return req, nil
|
|
}
|
|
|
|
func decodeChannelUpdate(_ context.Context, r *http.Request) (interface{}, error) {
|
|
if r.Header.Get("Content-Type") != contentType {
|
|
return nil, errUnsupportedContentType
|
|
}
|
|
|
|
req := updateChannelReq{
|
|
key: r.Header.Get("Authorization"),
|
|
id: bone.GetValue(r, "id"),
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return req, nil
|
|
}
|
|
|
|
func decodeView(_ context.Context, r *http.Request) (interface{}, error) {
|
|
req := viewResourceReq{
|
|
key: r.Header.Get("Authorization"),
|
|
id: bone.GetValue(r, "id"),
|
|
}
|
|
|
|
return req, nil
|
|
}
|
|
|
|
func decodeList(_ context.Context, r *http.Request) (interface{}, error) {
|
|
q, err := url.ParseQuery(r.URL.RawQuery)
|
|
if err != nil {
|
|
return nil, errInvalidQueryParams
|
|
}
|
|
offset := uint64(0)
|
|
limit := uint64(10)
|
|
|
|
off, lmt := q["offset"], q["limit"]
|
|
|
|
if len(off) > 1 || len(lmt) > 1 {
|
|
return nil, errInvalidQueryParams
|
|
}
|
|
|
|
if len(off) == 1 {
|
|
offset, err = strconv.ParseUint(off[0], 10, 64)
|
|
if err != nil {
|
|
return nil, errInvalidQueryParams
|
|
}
|
|
}
|
|
|
|
if len(lmt) == 1 {
|
|
limit, err = strconv.ParseUint(lmt[0], 10, 64)
|
|
if err != nil {
|
|
return nil, errInvalidQueryParams
|
|
}
|
|
}
|
|
|
|
req := listResourcesReq{
|
|
key: r.Header.Get("Authorization"),
|
|
offset: offset,
|
|
limit: limit,
|
|
}
|
|
|
|
return req, nil
|
|
}
|
|
|
|
func decodeConnection(_ context.Context, r *http.Request) (interface{}, error) {
|
|
req := connectionReq{
|
|
key: r.Header.Get("Authorization"),
|
|
chanID: bone.GetValue(r, "chanId"),
|
|
thingID: bone.GetValue(r, "thingId"),
|
|
}
|
|
|
|
return req, nil
|
|
}
|
|
|
|
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 encodeError(_ context.Context, err error, w http.ResponseWriter) {
|
|
w.Header().Set("Content-Type", contentType)
|
|
|
|
switch err {
|
|
case things.ErrMalformedEntity:
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
case things.ErrUnauthorizedAccess:
|
|
w.WriteHeader(http.StatusForbidden)
|
|
case things.ErrNotFound:
|
|
w.WriteHeader(http.StatusNotFound)
|
|
case errUnsupportedContentType:
|
|
w.WriteHeader(http.StatusUnsupportedMediaType)
|
|
case errInvalidQueryParams:
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
case io.ErrUnexpectedEOF:
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
case io.EOF:
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
default:
|
|
switch err.(type) {
|
|
case *json.SyntaxError:
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
case *json.UnmarshalTypeError:
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
default:
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
}
|
|
}
|
|
}
|