This repository has been archived by the owner on Apr 3, 2018. It is now read-only.
/
client.go
81 lines (67 loc) · 1.72 KB
/
client.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
//
// Copyright (c) 2017
// Cavium
//
// SPDX-License-Identifier: Apache-2.0
//
package distro
import (
"encoding/json"
"net/http"
"strconv"
"github.com/edgexfoundry/export-go"
"go.uber.org/zap"
)
const (
clientPort int = 48071
)
func getRegistrationBaseURL(host string) string {
return "http://" + host + ":" + strconv.Itoa(clientPort) +
"/api/v1/registration"
}
func getRegistrations() []export.Registration {
url := getRegistrationBaseURL(cfg.ClientHost)
return getRegistrationsURL(url)
}
func getRegistrationsURL(url string) []export.Registration {
response, err := http.Get(url)
if err != nil {
logger.Warn("Error getting all registrations", zap.String("url", url))
return nil
}
defer response.Body.Close()
registrations := []export.Registration{}
if err := json.NewDecoder(response.Body).Decode(®istrations); err != nil {
logger.Warn("Could not parse json", zap.Error(err))
return nil
}
results := registrations[:0]
for _, reg := range registrations {
if reg.Validate() {
results = append(results, reg)
}
}
return results
}
func getRegistrationByName(name string) *export.Registration {
url := getRegistrationBaseURL(cfg.ClientHost) + "/name/" + name
return getRegistrationByNameURL(url)
}
func getRegistrationByNameURL(url string) *export.Registration {
response, err := http.Get(url)
if err != nil {
logger.Error("Error getting all registrations", zap.String("url", url))
return nil
}
defer response.Body.Close()
reg := export.Registration{}
if err := json.NewDecoder(response.Body).Decode(®); err != nil {
logger.Error("Could not parse json", zap.Error(err))
return nil
}
if !reg.Validate() {
logger.Error("Failed to validate registrations fields")
return nil
}
return ®
}