File: client.go

package info (click to toggle)
golang-github-akamai-akamaiopen-edgegrid-golang 1.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,408 kB
  • sloc: sh: 532; makefile: 3
file content (113 lines) | stat: -rw-r--r-- 2,470 bytes parent folder | download
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
//An example Diagnostic Tools v1 API Client
package main

import (
	"fmt"
	"log"
	"math/rand"
	"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
}

//Location ghost location type
type Location struct {
	ID    string `json:"id"`
	Value string `json:"value"`
}

//LocationsResponse response type for ghost locations
type LocationsResponse struct {
	Locations []Location `json:"locations"`
}

//DigResponse response type for dig API
type DigResponse struct {
	Dig struct {
		Hostname    string `json:"hostname"`
		QueryType   string `json:"queryType"`
		Result      string `json:"result"`
		ErrorString string `json:"errorString"`
	} `json:"digInfo"`
}

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/v2/ghost-locations/available",
				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.Value)

			fmt.Println("Running dig from " + location.Value)

			client.Client.Timeout = 5 * time.Minute
			req, err = client.NewRequest(
				config,
				"GET",
				"/diagnostic-tools/v2/ghost-locations/"+location.ID+"/dig-info?hostName=developer.akamai.com&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())
		}
	}
}