/
consumers_client.go
110 lines (85 loc) · 3.16 KB
/
consumers_client.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
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
package api
import (
"encoding/json"
"fmt"
"net/http"
"github.com/lensesio/lenses-go/v5/pkg"
)
// SingleTopicOffset represent the payload structure
// of the API for updating a single partition of a single topic.
type SingleTopicOffset struct {
Type string `json:"type" yaml:"type"`
Offset int `json:"offset,omitempty" yaml:"offset"`
}
// MultipleTopicOffsets represent the payload structure
// of the API for updating all partitions of multiple topics.
type MultipleTopicOffsets struct {
Type string `json:"type" yaml:"type"`
Target string `json:"target,omitempty" yaml:"type"`
Topics []string `json:"topics,omitempty" yaml:"topics"`
}
// UpdateSingleTopicOffset handles the API call to update
// a signle partition of a topic.
func (c *Client) UpdateSingleTopicOffset(groupID, topic, partitionID, offsetType string, offset int) error {
if offsetType == "" {
return errRequired("field `type` is missing")
}
path := fmt.Sprintf("%s/%s/offsets/topics/%s/partitions/%s", pkg.ConsumersGroupPath, groupID, topic, partitionID)
singleTopic := SingleTopicOffset{Type: offsetType, Offset: offset}
payload, err := json.Marshal(singleTopic)
resp, err := c.Do(http.MethodPut, path, contentTypeJSON, payload)
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}
// UpdateMultipleTopicsOffset handles the Lenses API call to update
// all partitions of multiple topics of a consumer group.
func (c *Client) UpdateMultipleTopicsOffset(groupID, offsetType, target string, topics []string) error {
path := fmt.Sprintf("%s/%s/offsets", pkg.ConsumersGroupPath, groupID)
multipleTopics := MultipleTopicOffsets{Type: offsetType, Target: target, Topics: topics}
payload, err := json.Marshal(multipleTopics)
_, err = c.Do(http.MethodPut, path, contentTypeJSON, payload)
if err != nil {
return err
}
return nil
}
// DeleteSingleTopicOffset handles the API call to delete consumer group offsets for
// a signle partition of a topic.
func (c *Client) DeleteSingleTopicOffset(groupID, topic, partitionID string) error {
path := fmt.Sprintf("%s/%s/topics/%s/partitions/%s/offsets", pkg.ConsumersGroupPath, groupID, topic, partitionID)
resp, err := c.Do(http.MethodDelete, path, contentTypeJSON, nil)
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}
// DeleteMultipleTopicsOffset handles the Lenses API call to delete
// all partitions of multiple topics of a consumer group.
func (c *Client) DeleteMultipleTopicsOffset(groupID string, topics []string) error {
path := fmt.Sprintf("%s/%s/offsets/delete", pkg.ConsumersGroupPath, groupID)
multipleTopics := MultipleTopicOffsets{Topics: topics}
payload, err := json.Marshal(multipleTopics)
if err != nil {
return err
}
resp, err := c.Do(http.MethodPost, path, contentTypeJSON, payload)
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}
// DeleteConsumerGroup handles the Lenses API call to delete a specific consumer group
func (c *Client) DeleteConsumerGroup(groupID string) error {
path := fmt.Sprintf("%s/%s", pkg.ConsumersGroupPath, groupID)
resp, err := c.Do(http.MethodDelete, path, contentTypeJSON, nil)
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}