-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonapi.go
More file actions
117 lines (100 loc) · 2.63 KB
/
jsonapi.go
File metadata and controls
117 lines (100 loc) · 2.63 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"net/http"
"os"
"strings"
)
type Input struct {
MPAN string
}
func main() {
// Get MPAS MPID from user
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter MPAN number: ")
input, err := reader.ReadString('\n')
// Remove newline from input and assign value to mpas var
mpan := strings.TrimSuffix(input, "\n")
url := "https://www.ecoes.co.uk/WebServices/Service/ECOESApi.svc/RESTful/JSON/GetTechnicalDetailsByMpan"
// fmt.Println("URL:>", url)
tmpl := template.Must(template.New("tmpl").Parse(
`{
"Authentication":{
"Key":os.Getenv("ECOES_API_KEY")"
},
"ParameterSets":[{
"Parameters":[{
"Key":"MPAN",
"Value":"{{.MPAN}}"
}]
}]
}`))
data := &Input{mpan}
var tpl bytes.Buffer
err = tmpl.Execute(&tpl, data)
if err != nil {
panic(err)
}
tmpl_result := tpl.String()
var jsonStr = []byte(tmpl_result)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
// req.Header.Set("X-Custom-Header", "myvalue")
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
// fmt.Println("response Status:", resp.Status)
// fmt.Println("response Headers:", resp.Header)
body, _ := ioutil.ReadAll(resp.Body)
// fmt.Println("response Body:", string(body))
type AutoGenerated struct {
Header struct {
RequestDate string `json:"RequestDate"`
RequestID int64 `json:"RequestId"`
ResponseTime string `json:"ResponseTime"`
VersionNumber string `json:"VersionNumber"`
} `json:"Header"`
Results []struct {
Errors []struct {
Code string `json:"Code"`
Description string `json:"Description"`
} `json:"Errors"`
ParameterSet struct {
Parameters []struct {
Key string `json:"Key"`
Value string `json:"Value"`
} `json:"Parameters"`
} `json:"ParameterSet"`
UtilityMatches []struct {
UtilityDetails []struct {
Key string `json:"Key"`
Value string `json:"Value"`
} `json:"UtilityDetails"`
UtilityKey string `json:"UtilityKey"`
UtilityType string `json:"UtilityType"`
Meters []struct {
MeterDetails []struct {
Key string `json:"Key"`
Value string `json:"Value"`
} `json:"MeterDetails"`
} `json:"Meters"`
} `json:"UtilityMatches"`
} `json:"Results"`
}
var t AutoGenerated
err = json.Unmarshal([]byte(body), &t)
if err != nil {
panic(err)
}
for i := range t.Results[0].UtilityMatches[0].UtilityDetails {
fmt.Println(t.Results[0].UtilityMatches[0].UtilityDetails[i].Value)
}
}