-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrest.go
101 lines (87 loc) · 2.42 KB
/
rest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// December 14, 2016
// Craig Hesling <craig@hesling.com>
// Package rest provides the data structures and primitive mechanisms
// for representing and communicating framework constructs with the RESTful
// server.
package rest
import (
"encoding/json"
"fmt"
"net/http"
)
const (
rootAPISubPath = "/apiv1"
authAPISubPath = "/authv1"
deviceSubPath = "/device"
servicesSubPath = "/service"
serviceDevicesSubPath = "/things"
serviceTokenSubPath = "/token"
locationSubPath = "/location"
userSubPath = "/user"
groupSubPath = "/group"
healthCheckSubPath = "/check"
)
const (
httpStatusCodeOK = 200
)
const jsonPrettyIndent = " "
func DecodeOCError(resp *http.Response) error {
if resp == nil {
return fmt.Errorf("Filed to decode response. Check err returned by http request.")
}
if resp.StatusCode == httpStatusCodeOK {
return nil
}
var ocerror struct {
Error struct {
Message string `json:"message"`
} `json:"error"`
}
if err := json.NewDecoder(resp.Body).Decode(&ocerror); err != nil {
// failed to decode a message error
// return server error message instead
return fmt.Errorf(resp.Status)
}
// Insert blank error message
if ocerror.Error.Message == "" {
ocerror.Error.Message = "<Blank-Error-Message-Received>"
}
return fmt.Errorf(ocerror.Error.Message)
}
// Host represents the RESTful HTTP server that hosts the framework
type Host struct {
uri string
// This is where we add APIKeys and username/password for user
user string
pass string
client http.Client
}
// NewHost returns an object referencing the framework server
func NewHost(uri string) Host {
// no need to decompose uri using net/url package
return Host{uri: uri, client: http.Client{}}
}
func (host *Host) Login(username, password string) error {
host.user = username
host.pass = password
// TODO: Check login credentials -- return error if no good
return nil
}
// PubSub describes a node's pubsub endpoint
type PubSub struct {
Protocol string `json:"protocol"`
Topic string `json:"endpoint"`
}
// Owner describes the owning user's details
type Owner struct {
Id string `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
}
// NodeDescriptor provides the common fields that Device and Service nodes share
type NodeDescriptor struct {
Name string `json:"name"`
ID string `json:"id"`
Pubsub PubSub `json:"pubsub"`
Owner Owner `json:"owner"`
}