-
Notifications
You must be signed in to change notification settings - Fork 0
/
http-requests.go
100 lines (88 loc) · 2.75 KB
/
http-requests.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
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
)
// Client side http requests
func addrToNode(addr string) (Node, error) {
resp, err := http.Get(fmt.Sprintf("http://%s/node", addr))
if err == nil {
var node Node
json.NewDecoder(resp.Body).Decode(&node)
return node, nil
}
return Node{}, err
}
func updateRouting(addr string, routingInfo RoutingInfo) (Node, error){
url := fmt.Sprintf("http://%s/update-routing", addr)
jsonData, _ := json.Marshal(routingInfo)
req, err := http.NewRequest(http.MethodPut, url, bytes.NewBuffer(jsonData))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
var node Node
json.NewDecoder(resp.Body).Decode(&node)
return node, nil
return Node{}, err
}
func readDataFromSpecificNode(node Node, key string) (KeyValuePair, error) {
url := fmt.Sprintf("http://%s/server-data", node.Address)
jsonData, _ := json.Marshal(KeyValuePair{key, ""})
req, err := http.NewRequest(http.MethodGet, url, bytes.NewBuffer(jsonData))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
if resp.StatusCode == http.StatusNotFound {
return KeyValuePair{}, errors.New(resp.Status)
}
var keyValuePair KeyValuePair
json.NewDecoder(resp.Body).Decode(&keyValuePair)
return keyValuePair, nil
}
func writeDataToSpecificNode(node Node, keyValuePair KeyValuePair) (ResponseMessage, error) {
url := fmt.Sprintf("http://%s/server-data", node.Address)
jsonData, _ := json.Marshal(keyValuePair)
req, err := http.NewRequest(http.MethodPut, url, bytes.NewBuffer(jsonData))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
json.NewDecoder(resp.Body).Decode(&keyValuePair)
return ResponseMessage{true, "Written"}, nil
}
func deleteDataFromSpecificNode(node Node, key string) (ResponseMessage, error) {
url := fmt.Sprintf("http://%s/server-data", node.Address)
jsonData, _ := json.Marshal(KeyValuePair{key, ""})
req, err := http.NewRequest(http.MethodDelete, url, bytes.NewBuffer(jsonData))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
if resp.StatusCode == http.StatusOK {
return ResponseMessage{true, "Successfully deleted"}, nil
}
return ResponseMessage{false, "Delete failure"}, errors.New("delete failure")
}
func getAllDataFromNode(node Node) []KeyValuePair {
resp, err := http.Get(fmt.Sprintf("http://%s/data", node.Address))
if err == nil {
var allData []KeyValuePair
json.NewDecoder(resp.Body).Decode(&allData)
return allData
}
return []KeyValuePair{}
}