forked from akamai/AkamaiOPEN-edgegrid-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
106 lines (86 loc) · 2.19 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
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
// An example Diagnostic Tools v1 API Client
package main
import (
"fmt"
"log"
"math/rand"
"net/url"
"time"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/client-v1"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/edgegrid"
)
func random(min int, max int) int {
rand.Seed(time.Now().UTC().UnixNano())
random := rand.Intn(max-min) + min
return random
}
type LocationsResponse struct {
Locations []string `json:"locations"`
}
type DigResponse struct {
Dig struct {
Hostname string `json:"hostname"`
QueryType string `json:"queryType"`
Result string `json:"result"`
ErrorString string `json:"errorString"`
} `json:"dig"`
}
func main() {
Example()
}
func Example() {
config, err := edgegrid.InitEdgeRc("~/.edgerc", "default")
config.Debug = false
if err == nil {
if err == nil {
fmt.Println("Requesting locations that support the diagnostic-tools API.")
req, err := client.NewRequest(
config,
"GET",
"/diagnostic-tools/v1/locations",
nil,
)
if err != nil {
log.Fatal(err.Error())
}
res, err := client.Do(config, req)
if err != nil {
log.Fatal(err.Error())
return
}
locationsResponse := LocationsResponse{}
client.BodyJSON(res, &locationsResponse)
if err != nil {
log.Fatal(err.Error())
}
fmt.Printf("There are %d locations that can run dig in the Akamai Network\n", len(locationsResponse.Locations))
if len(locationsResponse.Locations) == 0 {
log.Fatal("No locations found")
}
location := locationsResponse.Locations[random(0, len(locationsResponse.Locations))-1]
fmt.Println("We will make our call from " + location)
fmt.Println("Running dig from " + location)
client.Client.Timeout = 5 * time.Minute
req, err = client.NewRequest(
config,
"GET",
"/diagnostic-tools/v1/dig?hostname=developer.akamai.com&location="+url.QueryEscape(location)+"&queryType=A",
nil,
)
if err != nil {
log.Fatal(err.Error())
return
}
res, err = client.Do(config, req)
if err != nil {
log.Fatal(err.Error())
return
}
digResponse := DigResponse{}
client.BodyJSON(res, &digResponse)
fmt.Println(digResponse.Dig.Result)
} else {
log.Fatal(err.Error())
}
}
}