github.com/akamai/AkamaiOPEN-edgegrid-golang@v1.2.2/reportsgtm-v1/util.go (about) 1 package reportsgtm 2 3 import ( 4 "net/http" 5 6 "github.com/akamai/AkamaiOPEN-edgegrid-golang/client-v1" 7 "github.com/akamai/AkamaiOPEN-edgegrid-golang/configgtm-v1_3" 8 9 "fmt" 10 "time" 11 ) 12 13 // 14 // Support gtm reports thru Edgegrid 15 // Based on 1.0 Schema 16 // 17 18 type WindowResponse struct { 19 StartTime time.Time 20 EndTime time.Time 21 } 22 23 type APIWindowResponse struct { 24 Start string `json:"start"` 25 End string `json:"end"` 26 } 27 28 func setEncodedHeader(req *http.Request) { 29 30 if req.Method == "GET" { 31 req.Header.Set("Content-Type", "application/x-www-form-urlencoded") 32 } 33 34 return 35 36 } 37 38 // Utility method to convert an RFC3339 formated time string to a time.Time object 39 func convertRFC3339toDate(rfc3339Stamp string) (time.Time, error) { 40 41 t, err := time.Parse(time.RFC3339, rfc3339Stamp) 42 return t, err 43 44 } 45 46 func createTimeWindow(apiResponse *APIWindowResponse) (*WindowResponse, error) { 47 48 var err error 49 windowResponse := &WindowResponse{} 50 windowResponse.StartTime, err = convertRFC3339toDate(apiResponse.Start) 51 if err != nil { 52 return nil, err 53 } 54 windowResponse.EndTime, err = convertRFC3339toDate(apiResponse.End) 55 if err != nil { 56 return nil, err 57 } 58 return windowResponse, nil 59 60 } 61 62 // Core function to retrieve all Window API requests 63 func getWindowCore(hostURL string) (*WindowResponse, error) { 64 65 stat := &APIWindowResponse{} 66 67 req, err := client.NewRequest( 68 Config, 69 "GET", 70 hostURL, 71 nil, 72 ) 73 if err != nil { 74 return nil, err 75 } 76 77 printHttpRequest(req, true) 78 79 res, err := client.Do(Config, req) 80 if err != nil { 81 return nil, err 82 } 83 84 printHttpResponse(res, true) 85 86 if client.IsError(res) { 87 if res.StatusCode == 400 { 88 // Get the body. Could be bad dates. 89 var windRespErrBody map[string]interface{} 90 err = client.BodyJSON(res, windRespErrBody) 91 if err != nil { 92 return nil, err 93 } 94 // are available dates present? 95 if availEnd, ok := windRespErrBody["availableEndDate"]; ok { 96 stat.End = availEnd.(string) 97 } 98 if availStart, ok := windRespErrBody["availableStartDate"]; ok { 99 stat.Start = availStart.(string) 100 } 101 if stat.End == "" || stat.Start == "" { 102 cErr := configgtm.CommonError{} 103 cErr.SetItem("entityName", "Window") 104 cErr.SetItem("name", "Data Window") 105 cErr.SetItem("apiErrorMessage", "No available data window") 106 return nil, cErr 107 } 108 } else if res.StatusCode == 404 { 109 cErr := configgtm.CommonError{} 110 cErr.SetItem("entityName", "Window") 111 cErr.SetItem("name", "Data Window") 112 return nil, cErr 113 } else { 114 return nil, client.NewAPIError(res) 115 } 116 } else { 117 err = client.BodyJSON(res, stat) 118 if err != nil { 119 return nil, err 120 } 121 } 122 timeWindow, err := createTimeWindow(stat) 123 if err != nil { 124 return nil, err 125 } 126 return timeWindow, nil 127 } 128 129 // GetDemandWindow is a utility function that retrieves the data window for Demand category of Report APIs 130 func GetDemandWindow(domainName string, propertyName string) (*WindowResponse, error) { 131 132 hostURL := fmt.Sprintf("/gtm-api/v1/reports/demand/domains/%s/properties/%s/window", domainName, propertyName) 133 return getWindowCore(hostURL) 134 135 } 136 137 // GetLatencyDomainsWindow is a utility function that retrieves the data window for Latency category of Report APIs 138 func GetLatencyDomainsWindow(domainName string) (*WindowResponse, error) { 139 140 hostURL := fmt.Sprintf("/gtm-api/v1/reports/latency/domains/%s/window", domainName) 141 return getWindowCore(hostURL) 142 143 } 144 145 // GetLivenessTestsWindow is a utility function that retrieves the data window for Liveness category of Report APIs 146 func GetLivenessTestsWindow() (*WindowResponse, error) { 147 148 hostURL := fmt.Sprintf("/gtm-api/v1/reports/liveness-tests/window") 149 return getWindowCore(hostURL) 150 151 } 152 153 // GetDatacentersTrafficWindow is a utility function that retrieves the data window for Traffic category of Report APIs 154 func GetDatacentersTrafficWindow() (*WindowResponse, error) { 155 156 hostURL := fmt.Sprintf("/gtm-api/v1/reports/traffic/datacenters-window") 157 return getWindowCore(hostURL) 158 159 } 160 161 // GetPropertiesTrafficWindow is a utility function that retrieves the data window for Traffic category of Report API 162 func GetPropertiesTrafficWindow() (*WindowResponse, error) { 163 164 hostURL := fmt.Sprintf("/gtm-api/v1/reports/traffic/properties-window") 165 return getWindowCore(hostURL) 166 167 }