-
Notifications
You must be signed in to change notification settings - Fork 31
/
http.go
79 lines (65 loc) · 1.87 KB
/
http.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
package http
import (
"context"
"fmt"
"time"
msg "github.com/aziontech/azion-cli/messages/logs/http"
"github.com/aziontech/azion-cli/pkg/cmdutil"
"github.com/aziontech/azion-cli/pkg/logger"
"github.com/machinebox/graphql"
"go.uber.org/zap"
)
type HTTPEvent struct {
Host string `json:"host"`
GeolocCountry string `json:"geolocCountryName"`
GeolocRegion string `json:"geolocRegionName"`
HTTPUserAgent string `json:"httpUserAgent"`
RequestURI string `json:"requestUri"`
Status int `json:"status"`
Ts time.Time `json:"ts"`
UpstreamBytesSent int `json:"upstreamBytesSent"`
RequestTime string `json:"requestTime"`
RequestMethod string `json:"requestMethod"`
}
type HTTPEventsResponse struct {
HTTPEvents []HTTPEvent `json:"httpEvents"`
}
const query string = `
query HttpEventsLogs {
httpEvents(
%s
filter: {
tsGt: "%s"
}
orderBy: [ts_ASC]
)
{
host
httpUserAgent
geolocRegionName
requestUri
status
ts
upstreamBytesSent
requestTime
requestMethod
}
}
`
func HttpEvents(f *cmdutil.Factory, currentTime time.Time, limitFlag string) (HTTPEventsResponse, error) {
graphqlClient := graphql.NewClient("https://api.azionapi.net/events/graphql")
formattedTime := currentTime.Format("2006-01-02T15:04:05")
limit := "limit: " + limitFlag
//prepare query
formattedQuery := fmt.Sprintf(query, limit, formattedTime)
graphqlRequest := graphql.NewRequest(formattedQuery)
tokenvalue := f.Config.GetString("token")
token := "Token " + tokenvalue
graphqlRequest.Header.Set("Authorization", token)
var response HTTPEventsResponse
if err := graphqlClient.Run(context.Background(), graphqlRequest, &response); err != nil {
logger.Debug("", zap.Any("Error", err.Error()))
return HTTPEventsResponse{}, msg.ErrorRequest
}
return response, nil
}