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

* feat(api): aligns things api documentation This commit addresses the following changes: - Removes bulk provision of channels endpoint `/channels/bulk` to the OpenAPI specification file `api/openapi/things.yml`. - Adds connect and disconnect API documentation for things Note: No implementation changes have been made in this commit. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * Remove policies Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * feat(api): update group members endpoint This commit updates the group members endpoint in the users API. Instead of having a separate endpoint for /{groupID}/members, we can now use /{groupID}/users. This change simplifies the API structure and improves consistency. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * feat(api): add OpenAPI specification for Auth Service This commit adds the OpenAPI 3.0 specification for the Auth Service. The `auth.yml` file contains the definition of the HTTP API for managing platform users. This specification will help in improving the API by allowing changes to be made to the definition. Changes: - Added `auth.yml` file with OpenAPI 3.0 specification - Defined the title and description of the Auth Service in the `info` section Note: This commit only includes the addition of the OpenAPI specification file and does not include any implementation changes. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * feat(auth): add new endpoint for deleting policies - Updated the `MakeHandler` function in `auth/api/http/policies/transport.go` to add a new endpoint for deleting policies. - Replaced the `mux.Put` method with `mux.Post` to match the HTTP verb for deleting policies. - The new endpoint is now `/policies/delete`. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> --------- Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> Co-authored-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com>
107 lines
2.8 KiB
Go
107 lines
2.8 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package policies
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"strings"
|
|
|
|
kithttp "github.com/go-kit/kit/transport/http"
|
|
"github.com/go-zoo/bone"
|
|
"github.com/mainflux/mainflux"
|
|
"github.com/mainflux/mainflux/auth"
|
|
"github.com/mainflux/mainflux/internal/apiutil"
|
|
"github.com/mainflux/mainflux/logger"
|
|
"github.com/mainflux/mainflux/pkg/errors"
|
|
)
|
|
|
|
const contentType = "application/json"
|
|
|
|
// MakeHandler returns a HTTP handler for API endpoints.
|
|
func MakeHandler(svc auth.Service, mux *bone.Mux, logger logger.Logger) *bone.Mux {
|
|
opts := []kithttp.ServerOption{
|
|
kithttp.ServerErrorEncoder(apiutil.LoggingErrorEncoder(logger, encodeError)),
|
|
}
|
|
|
|
mux.Post("/policies", kithttp.NewServer(
|
|
(createPolicyEndpoint(svc)),
|
|
decodePoliciesRequest,
|
|
encodeResponse,
|
|
opts...,
|
|
))
|
|
|
|
mux.Post("/policies/delete", kithttp.NewServer(
|
|
(deletePoliciesEndpoint(svc)),
|
|
decodePoliciesRequest,
|
|
encodeResponse,
|
|
opts...,
|
|
))
|
|
|
|
return mux
|
|
}
|
|
|
|
func decodePoliciesRequest(_ context.Context, r *http.Request) (interface{}, error) {
|
|
if !strings.Contains(r.Header.Get("Content-Type"), contentType) {
|
|
return nil, errors.ErrUnsupportedContentType
|
|
}
|
|
|
|
req := policiesReq{token: apiutil.ExtractBearerToken(r)}
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
return nil, errors.Wrap(errors.ErrMalformedEntity, err)
|
|
}
|
|
|
|
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) {
|
|
switch {
|
|
case errors.Contains(err, errors.ErrMalformedEntity),
|
|
err == apiutil.ErrEmptyList,
|
|
err == apiutil.ErrMissingPolicyObj,
|
|
err == apiutil.ErrMissingPolicySub,
|
|
err == apiutil.ErrMalformedPolicy:
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
case errors.Contains(err, errors.ErrAuthentication),
|
|
err == apiutil.ErrBearerToken:
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
case errors.Contains(err, errors.ErrNotFound):
|
|
w.WriteHeader(http.StatusNotFound)
|
|
case errors.Contains(err, errors.ErrConflict):
|
|
w.WriteHeader(http.StatusConflict)
|
|
case errors.Contains(err, errors.ErrAuthorization):
|
|
w.WriteHeader(http.StatusForbidden)
|
|
case errors.Contains(err, errors.ErrUnsupportedContentType):
|
|
w.WriteHeader(http.StatusUnsupportedMediaType)
|
|
default:
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
}
|
|
|
|
if errorVal, ok := err.(errors.Error); ok {
|
|
w.Header().Set("Content-Type", contentType)
|
|
if err := json.NewEncoder(w).Encode(apiutil.ErrorRes{Err: errorVal.Msg()}); err != nil {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
}
|
|
}
|
|
}
|