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

Services are versioned through top-level package. The package itself contains an HTTP header that will be bound to the '/version' endpoint of each of the available services. Signed-off-by: Dejan Mijic <dejan@mainflux.com>
208 lines
3.2 KiB
Go
208 lines
3.2 KiB
Go
package api
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/mainflux/mainflux/manager"
|
|
)
|
|
|
|
const contentType = "application/json; charset=utf-8"
|
|
|
|
type apiResponse interface {
|
|
code() int
|
|
headers() map[string]string
|
|
empty() bool
|
|
}
|
|
|
|
type clientReq struct {
|
|
key string
|
|
id string
|
|
client manager.Client
|
|
}
|
|
|
|
type channelReq struct {
|
|
key string
|
|
id string
|
|
channel manager.Channel
|
|
}
|
|
|
|
type viewResourceReq struct {
|
|
key string
|
|
id string
|
|
}
|
|
|
|
type listResourcesReq struct {
|
|
key string
|
|
size int
|
|
offset int
|
|
}
|
|
|
|
type tokenRes struct {
|
|
Token string `json:"token,omitempty"`
|
|
}
|
|
|
|
func (res tokenRes) code() int {
|
|
return http.StatusCreated
|
|
}
|
|
|
|
func (res tokenRes) headers() map[string]string {
|
|
return map[string]string{}
|
|
}
|
|
|
|
func (res tokenRes) empty() bool {
|
|
return res.Token == ""
|
|
}
|
|
|
|
type removeRes struct{}
|
|
|
|
func (res removeRes) code() int {
|
|
return http.StatusNoContent
|
|
}
|
|
|
|
func (res removeRes) headers() map[string]string {
|
|
return map[string]string{}
|
|
}
|
|
|
|
func (res removeRes) empty() bool {
|
|
return true
|
|
}
|
|
|
|
type clientRes struct {
|
|
id string
|
|
created bool
|
|
}
|
|
|
|
func (res clientRes) code() int {
|
|
if res.created {
|
|
return http.StatusCreated
|
|
}
|
|
|
|
return http.StatusOK
|
|
}
|
|
|
|
func (res clientRes) headers() map[string]string {
|
|
if res.created {
|
|
return map[string]string{
|
|
"Location": fmt.Sprint("/clients/", res.id),
|
|
}
|
|
}
|
|
|
|
return map[string]string{}
|
|
}
|
|
|
|
func (res clientRes) empty() bool {
|
|
return true
|
|
}
|
|
|
|
type viewClientRes struct {
|
|
manager.Client
|
|
}
|
|
|
|
func (res viewClientRes) code() int {
|
|
return http.StatusOK
|
|
}
|
|
|
|
func (res viewClientRes) headers() map[string]string {
|
|
return map[string]string{}
|
|
}
|
|
|
|
func (res viewClientRes) empty() bool {
|
|
return false
|
|
}
|
|
|
|
type listClientsRes struct {
|
|
Clients []manager.Client `json:"clients"`
|
|
count int
|
|
}
|
|
|
|
func (res listClientsRes) code() int {
|
|
return http.StatusOK
|
|
}
|
|
|
|
func (res listClientsRes) headers() map[string]string {
|
|
return map[string]string{
|
|
"X-Count": fmt.Sprintf("%d", res.count),
|
|
}
|
|
}
|
|
|
|
func (res listClientsRes) empty() bool {
|
|
return false
|
|
}
|
|
|
|
type channelRes struct {
|
|
id string
|
|
created bool
|
|
}
|
|
|
|
func (res channelRes) code() int {
|
|
if res.created {
|
|
return http.StatusCreated
|
|
}
|
|
|
|
return http.StatusOK
|
|
}
|
|
|
|
func (res channelRes) headers() map[string]string {
|
|
if res.created {
|
|
return map[string]string{
|
|
"Location": fmt.Sprint("/channels/", res.id),
|
|
}
|
|
}
|
|
|
|
return map[string]string{}
|
|
}
|
|
|
|
func (res channelRes) empty() bool {
|
|
return true
|
|
}
|
|
|
|
type viewChannelRes struct {
|
|
manager.Channel
|
|
}
|
|
|
|
func (res viewChannelRes) code() int {
|
|
return http.StatusOK
|
|
}
|
|
|
|
func (res viewChannelRes) headers() map[string]string {
|
|
return map[string]string{}
|
|
}
|
|
|
|
func (res viewChannelRes) empty() bool {
|
|
return false
|
|
}
|
|
|
|
type listChannelsRes struct {
|
|
Channels []manager.Channel `json:"channels"`
|
|
count int
|
|
}
|
|
|
|
func (res listChannelsRes) code() int {
|
|
return http.StatusOK
|
|
}
|
|
|
|
func (res listChannelsRes) headers() map[string]string {
|
|
return map[string]string{
|
|
"X-Count": fmt.Sprintf("%d", res.count),
|
|
}
|
|
}
|
|
|
|
func (res listChannelsRes) empty() bool {
|
|
return false
|
|
}
|
|
|
|
type accessRes struct{}
|
|
|
|
func (res accessRes) code() int {
|
|
return http.StatusAccepted
|
|
}
|
|
|
|
func (res accessRes) headers() map[string]string {
|
|
return map[string]string{}
|
|
}
|
|
|
|
func (res accessRes) empty() bool {
|
|
return true
|
|
}
|