File: client.go

package info (click to toggle)
golang-github-apex-log 1.1.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports, bullseye, experimental, forky, sid, trixie
  • size: 316 kB
  • sloc: makefile: 4
file content (128 lines) | stat: -rw-r--r-- 2,361 bytes parent folder | download
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package client

import (
	"fmt"
	"bytes"
	"encoding/json"
	"io"
	"net/http"
	"time"
)

// TODO: generate full client
// TODO: docs
// TODO: auth

// Error is an error returned by the client.
type Error struct {
	Method     string
	Status     string
	StatusCode int
	Type       string
	Message    string
}

// Error implementation.
func (e Error) Error() string {
	return fmt.Sprintf("%s: %s", e.Type, e.Message)
}

// Event represents a single log event.
type Event struct {
	// ID is the event id.
	ID string `json:"id"`

	// Level is the severity level.
	Level string `json:"level"`

	// Message is the log message.
	Message string `json:"message"`

	// Fields is the log fields.
	Fields map[string]interface{} `json:"fields"`

	// Timestamp is the creation timestamp.
	Timestamp time.Time `json:"timestamp"`
}

// AddEventsInput params.
type AddEventsInput struct {
	// ProjectID is the project id.
	ProjectID string `json:"project_id"`

	// Events is the batch of events.
	Events []Event `json:"events"`
}

// Client is the API client.
type Client struct {
	URL string
	AuthToken string
}

// AddEvents ingested a batch of events.
func (c *Client) AddEvents(in AddEventsInput) error {
	return c.call("", "add_events", nil, in)
}

// call invokes a method on the given service.
func (c *Client) call(service, name string, out interface{}, in ...interface{}) error {
	var body io.Reader

	// input params
	if len(in) > 0 {
		var buf bytes.Buffer
		err := json.NewEncoder(&buf).Encode(in)
		if err != nil {
			return err
		}
		body = &buf
	}

	// endpoint
	url := c.URL + "/" + service + "/" + name
	if service == "" {
		url = c.URL + "/" + name
	}

	// POST request
	req, err := http.NewRequest("POST", url, body)
	if err != nil {
		return err
	}

	req.Header.Set("Content-Type", "application/json")

	// auth token
	if c.AuthToken != "" {
		req.Header.Set("Authorization", "Bearer "+c.AuthToken)
	}

	// response
	res, err := http.DefaultClient.Do(req)
	if err != nil {
		return err
	}
	defer res.Body.Close()

	if res.StatusCode >= 300 {
		var e Error
		if err := json.NewDecoder(res.Body).Decode(&e); err != nil {
			return err
		}
		e.Status = res.Status
		e.StatusCode = res.StatusCode
		e.Method = name
		return e
	}

	// output params
	if out != nil {
		err = json.NewDecoder(res.Body).Decode(out)
		if err != nil {
			return err
		}
	}

	return nil
}