@@ -15,6 +15,10 @@ const (
1515 cfGraphQLEndpoint = "https://api.cloudflare.com/client/v4/graphql/"
1616)
1717
18+ var (
19+ graphqlClient * graphql.Client
20+ )
21+
1822type cloudflareResponse struct {
1923 Viewer struct {
2024 Zones []zoneResp `json:"zones"`
@@ -309,9 +313,9 @@ func fetchFirewallRules(zoneID string) map[string]string {
309313 }
310314 for _ , rulesetDesc := range listOfRulesets {
311315 if rulesetDesc .Phase == "http_request_firewall_managed" {
312- ruleset , err := cloudflareAPI .GetRuleset (ctx , cloudflare .ZoneIdentifier (zoneID ), rulesetDesc .ID )
313- if err != nil {
314- log .Fatalf ("Error fetching ruleset: %s" , err )
316+ ruleset , ruleGetErr := cloudflareAPI .GetRuleset (ctx , cloudflare .ZoneIdentifier (zoneID ), rulesetDesc .ID )
317+ if ruleGetErr != nil {
318+ log .Fatalf ("Error fetching ruleset: %s" , ruleGetErr )
315319 }
316320 for _ , rule := range ruleset .Rules {
317321 firewallRulesMap [rule .ID ] = rule .Description
@@ -447,7 +451,6 @@ query ($zoneIDs: [String!], $mintime: Time!, $maxtime: Time!, $limit: Int!) {
447451 request .Var ("zoneIDs" , zoneIDs )
448452
449453 ctx := context .Background ()
450- graphqlClient := graphql .NewClient (cfGraphQLEndpoint )
451454
452455 var resp cloudflareResponse
453456 if err := graphqlClient .Run (ctx , request , & resp ); err != nil {
@@ -503,7 +506,6 @@ func fetchColoTotals(zoneIDs []string) (*cloudflareResponseColo, error) {
503506 request .Var ("zoneIDs" , zoneIDs )
504507
505508 ctx := context .Background ()
506- graphqlClient := graphql .NewClient (cfGraphQLEndpoint )
507509 var resp cloudflareResponseColo
508510 if err := graphqlClient .Run (ctx , request , & resp ); err != nil {
509511 log .Error (err )
@@ -563,7 +565,6 @@ func fetchWorkerTotals(accountID string) (*cloudflareResponseAccts, error) {
563565 request .Var ("accountID" , accountID )
564566
565567 ctx := context .Background ()
566- graphqlClient := graphql .NewClient (cfGraphQLEndpoint )
567568 var resp cloudflareResponseAccts
568569 if err := graphqlClient .Run (ctx , request , & resp ); err != nil {
569570 log .Errorf ("Error fetching worker totals: %s" , err )
@@ -640,7 +641,6 @@ func fetchLoadBalancerTotals(zoneIDs []string) (*cloudflareResponseLb, error) {
640641 request .Var ("zoneIDs" , zoneIDs )
641642
642643 ctx := context .Background ()
643- graphqlClient := graphql .NewClient (cfGraphQLEndpoint )
644644 var resp cloudflareResponseLb
645645 if err := graphqlClient .Run (ctx , request , & resp ); err != nil {
646646 log .Errorf ("Error fetching load balancer totals: %s" , err )
@@ -692,7 +692,6 @@ func fetchLogpushAccount(accountID string) (*cloudflareResponseLogpushAccount, e
692692 request .Var ("mintime" , now1mAgo )
693693
694694 ctx := context .Background ()
695- graphqlClient := graphql .NewClient (cfGraphQLEndpoint )
696695 var resp cloudflareResponseLogpushAccount
697696 if err := graphqlClient .Run (ctx , request , & resp ); err != nil {
698697 log .Errorf ("Error fetching logpush account totals: %s" , err )
@@ -744,7 +743,6 @@ func fetchLogpushZone(zoneIDs []string) (*cloudflareResponseLogpushZone, error)
744743 request .Var ("mintime" , now1mAgo )
745744
746745 ctx := context .Background ()
747- graphqlClient := graphql .NewClient (cfGraphQLEndpoint )
748746 var resp cloudflareResponseLogpushZone
749747 if err := graphqlClient .Run (ctx , request , & resp ); err != nil {
750748 log .Errorf ("Error fetching logpush zone totals: %s" , err )
@@ -802,7 +800,6 @@ func fetchR2Account(accountID string) (*cloudflareResponseR2Account, error) {
802800 request .Var ("date" , now .Format ("2006-01-02" ))
803801
804802 ctx := context .Background ()
805- graphqlClient := graphql .NewClient (cfGraphQLEndpoint )
806803 var resp cloudflareResponseR2Account
807804 if err := graphqlClient .Run (ctx , request , & resp ); err != nil {
808805 log .Errorf ("Error fetching R2 account: %s" , err )
0 commit comments