File: util.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 (166 lines) | stat: -rw-r--r-- 4,230 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package reportsgtm

import (
	"github.com/akamai/AkamaiOPEN-edgegrid-golang/client-v1"
	"github.com/akamai/AkamaiOPEN-edgegrid-golang/configgtm-v1_3"
	"net/http"

	"fmt"
	"time"
)

//
// Support gtm reports thru Edgegrid
// Based on 1.0 Schema
//

type WindowResponse struct {
	StartTime time.Time
	EndTime   time.Time
}

type APIWindowResponse struct {
	Start string `json:"start"`
	End   string `json:"end"`
}

func setEncodedHeader(req *http.Request) {

	if req.Method == "GET" {
		req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	}

	return

}

// Utility method to convert an RFC3339 formated time string to a time.Time object
func convertRFC3339toDate(rfc3339Stamp string) (time.Time, error) {

	t, err := time.Parse(time.RFC3339, rfc3339Stamp)
	return t, err

}

func createTimeWindow(apiResponse *APIWindowResponse) (*WindowResponse, error) {

	var err error
	windowResponse := &WindowResponse{}
	windowResponse.StartTime, err = convertRFC3339toDate(apiResponse.Start)
	if err != nil {
		return nil, err
	}
	windowResponse.EndTime, err = convertRFC3339toDate(apiResponse.End)
	if err != nil {
		return nil, err
	}
	return windowResponse, nil

}

// Core function to retrieve all Window API requests
func getWindowCore(hostURL string) (*WindowResponse, error) {

	stat := &APIWindowResponse{}

	req, err := client.NewRequest(
		Config,
		"GET",
		hostURL,
		nil,
	)
	if err != nil {
		return nil, err
	}

	printHttpRequest(req, true)

	res, err := client.Do(Config, req)
	if err != nil {
		return nil, err
	}

	printHttpResponse(res, true)

	if client.IsError(res) {
		if res.StatusCode == 400 {
			// Get the body. Could be bad dates.
			var windRespErrBody map[string]interface{}
			err = client.BodyJSON(res, windRespErrBody)
			if err != nil {
				return nil, err
			}
			// are available dates present?
			if availEnd, ok := windRespErrBody["availableEndDate"]; ok {
				stat.End = availEnd.(string)
			}
			if availStart, ok := windRespErrBody["availableStartDate"]; ok {
				stat.Start = availStart.(string)
			}
			if stat.End == "" || stat.Start == "" {
				cErr := configgtm.CommonError{}
				cErr.SetItem("entityName", "Window")
				cErr.SetItem("name", "Data Window")
				cErr.SetItem("apiErrorMessage", "No available data window")
				return nil, cErr
			}
		} else if res.StatusCode == 404 {
			cErr := configgtm.CommonError{}
			cErr.SetItem("entityName", "Window")
			cErr.SetItem("name", "Data Window")
			return nil, cErr
		} else {
			return nil, client.NewAPIError(res)
		}
	} else {
		err = client.BodyJSON(res, stat)
		if err != nil {
			return nil, err
		}
	}
	timeWindow, err := createTimeWindow(stat)
	if err != nil {
		return nil, err
	}
	return timeWindow, nil
}

// GetDemandWindow is a utility function that retrieves the data window for Demand category of Report APIs
func GetDemandWindow(domainName string, propertyName string) (*WindowResponse, error) {

	hostURL := fmt.Sprintf("/gtm-api/v1/reports/demand/domains/%s/properties/%s/window", domainName, propertyName)
	return getWindowCore(hostURL)

}

// GetLatencyDomainsWindow is a utility function that retrieves the data window for Latency category of Report APIs
func GetLatencyDomainsWindow(domainName string) (*WindowResponse, error) {

	hostURL := fmt.Sprintf("/gtm-api/v1/reports/latency/domains/%s/window", domainName)
	return getWindowCore(hostURL)

}

// GetLivenessTestsWindow is a utility function that retrieves the data window for Liveness category of Report APIs
func GetLivenessTestsWindow() (*WindowResponse, error) {

	hostURL := fmt.Sprintf("/gtm-api/v1/reports/liveness-tests/window")
	return getWindowCore(hostURL)

}

// GetDatacentersTrafficWindow is a utility function that retrieves the data window for Traffic category of Report APIs
func GetDatacentersTrafficWindow() (*WindowResponse, error) {

	hostURL := fmt.Sprintf("/gtm-api/v1/reports/traffic/datacenters-window")
	return getWindowCore(hostURL)

}

// GetPropertiesTrafficWindow is a utility function that retrieves the data window for Traffic category of Report API
func GetPropertiesTrafficWindow() (*WindowResponse, error) {

	hostURL := fmt.Sprintf("/gtm-api/v1/reports/traffic/properties-window")
	return getWindowCore(hostURL)

}