Skip to content

Commit

Permalink
[feat] Add new /received endpoint as option
Browse files Browse the repository at this point in the history
  • Loading branch information
elithrar committed Jul 19, 2017
1 parent e31d140 commit bb64692
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
24 changes: 15 additions & 9 deletions cmd/logshare-cli/main.go
Expand Up @@ -48,7 +48,7 @@ func run(conf *config) func(c *cli.Context) error {
conf.zoneID = id
}

client, err := logshare.New(conf.apiKey, conf.apiEmail, nil)
client, err := logshare.New(conf.apiKey, conf.apiEmail, &logshare.Options{ByReceived: conf.byReceived})
if err != nil {
return err
}
Expand Down Expand Up @@ -87,19 +87,21 @@ func parseFlags(conf *config, c *cli.Context) error {
conf.startTime = c.Int64("start-time")
conf.endTime = c.Int64("end-time")
conf.count = c.Int("count")
conf.byReceived = c.Bool("by-received")

return conf.Validate()
}

type config struct {
apiKey string
apiEmail string
rayID string
zoneID string
zoneName string
startTime int64
endTime int64
count int
apiKey string
apiEmail string
rayID string
zoneID string
zoneName string
startTime int64
endTime int64
count int
byReceived bool
}

func (conf *config) Validate() error {
Expand Down Expand Up @@ -150,4 +152,8 @@ var flags = []cli.Flag{
Value: 1,
Usage: "The number (count) of logs to retrieve. Pass '-1' to retrieve all logs for the given time period",
},
cli.BoolFlag{
Name: "by-received",
Usage: "Retrieve logs by the processing time on Cloudflare. This mode allows you to fetch all available logs vs. based on the log timestamps themselves.",
},
}
28 changes: 25 additions & 3 deletions logshare.go
Expand Up @@ -11,14 +11,19 @@ import (
"github.com/pkg/errors"
)

var apiURL = "https://api.cloudflare.com/client/v4"
const (
apiURL = "https://api.cloudflare.com/client/v4"
byRequest = "requests"
byReceived = "received"
)

// Client holds the current API credentials & HTTP client configuration. Client
// should not be modified concurrently.
type Client struct {
endpoint string
apiKey string
apiEmail string
byReceived bool
httpClient *http.Client
dest io.Writer
headers http.Header
Expand All @@ -32,6 +37,8 @@ type Options struct {
Headers http.Header
// Destination to stream logs to.
Dest io.Writer
// Fetch logs by the processing/recieved timestamp
ByReceived bool
}

// Meta contains data about the API response: the number of logs returned,
Expand All @@ -55,22 +62,37 @@ func New(apiKey string, apiEmail string, options *Options) (*Client, error) {
return nil, errors.New("apiEmail cannot be empty")
}

var byReceived bool
if options != nil {
byReceived = options.ByReceived
}

client := &Client{
apiKey: apiKey,
apiEmail: apiEmail,
endpoint: apiURL,
httpClient: http.DefaultClient,
dest: os.Stdout,
headers: make(http.Header),
byReceived: byReceived,
}

return client, nil
}

func (c *Client) buildURL(zoneID string) string {
endpoint := byRequest
if c.byReceived {
endpoint = byReceived
}

return fmt.Sprintf("%s/zones/%s/logs/%s", c.endpoint, zoneID, endpoint)
}

// GetFromRayID fetches logs for the given rayID, or starting at the given rayID
// if a non-zero end timestamp is provided.
func (c *Client) GetFromRayID(zoneID string, rayID string, end int64, count int) (*Meta, error) {
url := fmt.Sprintf("%s/zones/%s/logs/requests?start_id=%s", c.endpoint, zoneID, rayID)
url := fmt.Sprintf("%s?start_id=%s", c.buildURL(zoneID), rayID)

if end > 0 {
url += fmt.Sprintf("&end=%d", end)
Expand All @@ -86,7 +108,7 @@ func (c *Client) GetFromRayID(zoneID string, rayID string, end int64, count int)
// GetFromTimestamp fetches logs between the start and end timestamps provided,
// (up to 'count' logs).
func (c *Client) GetFromTimestamp(zoneID string, start int64, end int64, count int) (*Meta, error) {
url := fmt.Sprintf("%s/zones/%s/logs/requests?start=%d", c.endpoint, zoneID, start)
url := fmt.Sprintf("%s?start=%d", c.buildURL(zoneID), start)

if end > 0 {
url += fmt.Sprintf("&end=%d", end)
Expand Down

0 comments on commit bb64692

Please sign in to comment.