-
Notifications
You must be signed in to change notification settings - Fork 204
/
destination.go
58 lines (44 loc) · 1.52 KB
/
destination.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
package wrapper
import (
"encoding/json"
"github.com/SpectoLabs/hoverfly/core/handlers/v2"
"github.com/SpectoLabs/hoverfly/hoverctl/configuration"
)
// GetDestination will go the destination endpoint in Hoverfly, parse the JSON response and return the destination of Hoverfly
func GetDestination(target configuration.Target) (string, error) {
response, err := doRequest(target, "GET", v2ApiDestination, "", nil)
if err != nil {
return "", err
}
defer response.Body.Close()
err = handleResponseError(response, "Could not retrieve destination")
if err != nil {
return "", err
}
var destinationView v2.DestinationView
err = UnmarshalToInterface(response, &destinationView)
if err != nil {
return "", err
}
return destinationView.Destination, nil
}
// SetDestination will go the destination endpoint in Hoverfly, sending JSON that will set the destination of Hoverfly
func SetDestination(target configuration.Target, destination string) (string, error) {
destinationReq := map[string]string{"destination": destination}
bytes, _ := json.Marshal(destinationReq) // JSON encode in case there are special chars
reqBody := string(bytes)
response, err := doRequest(target, "PUT", v2ApiDestination, reqBody, nil)
if err != nil {
return "", err
}
err = handleResponseError(response, "Could not set destination")
if err != nil {
return "", err
}
var destinationView v2.DestinationView
err = UnmarshalToInterface(response, &destinationView)
if err != nil {
return "", err
}
return destinationView.Destination, nil
}