github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/clients/pkg/promtail/targets/cloudflare/client.go (about)

     1  package cloudflare
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"github.com/cloudflare/cloudflare-go"
     8  )
     9  
    10  // Client is a wrapper around the Cloudflare API that allow for testing and being zone/fields aware.
    11  type Client interface {
    12  	LogpullReceived(ctx context.Context, start, end time.Time) (cloudflare.LogpullReceivedIterator, error)
    13  }
    14  
    15  type wrappedClient struct {
    16  	client *cloudflare.API
    17  	zoneID string
    18  	fields []string
    19  }
    20  
    21  func (w *wrappedClient) LogpullReceived(ctx context.Context, start, end time.Time) (cloudflare.LogpullReceivedIterator, error) {
    22  	return w.client.LogpullReceived(ctx, w.zoneID, start, end, cloudflare.LogpullReceivedOption{
    23  		Fields: w.fields,
    24  	})
    25  }
    26  
    27  var getClient = func(apiKey, zoneID string, fields []string) (Client, error) {
    28  	c, err := cloudflare.NewWithAPIToken(apiKey)
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  	return &wrappedClient{
    33  		client: c,
    34  		zoneID: zoneID,
    35  		fields: fields,
    36  	}, nil
    37  }