Skip to content

Commit

Permalink
Merge pull request #620 from nats-io/sinfo
Browse files Browse the repository at this point in the history
Add in StreamInfo
  • Loading branch information
derekcollison committed Dec 9, 2020
2 parents 8424c59 + 948f5f8 commit ec480fa
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
29 changes: 27 additions & 2 deletions js.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,14 @@ type JetStream interface {
QueueSubscribe(subj, queue string, cb MsgHandler, opts ...SubOpt) (*Subscription, error)

// Management
// TODO(dlc) - add more
// Create a stream.
AddStream(cfg *StreamConfig) (*StreamInfo, error)
// Create a consumer.
AddConsumer(stream string, cfg *ConsumerConfig) (*ConsumerInfo, error)
// Stream information.
StreamInfo(stream string) (*StreamInfo, error)

// TODO(dlc) - add more
}

// ApiError is included in all API responses if there was an error.
Expand Down Expand Up @@ -104,8 +109,10 @@ const (
JSApiConsumerInfoT = "CONSUMER.INFO.%s.%s"
// JSApiRequestNextT is the prefix for the request next message(s) for a consumer in worker/pull mode.
JSApiRequestNextT = "CONSUMER.MSG.NEXT.%s.%s"
// JSApiStreamCreate is the endpoint to create new streams.
// JSApiStreamCreateT is the endpoint to create new streams.
JSApiStreamCreateT = "STREAM.CREATE.%s"
// JSApiStreamInfoT is the endpoint to get information on a stream.
JSApiStreamInfoT = "STREAM.INFO.%s"
)

// JetStream returns a JetStream context for pub/sub interactions.
Expand Down Expand Up @@ -1074,6 +1081,24 @@ func (js *js) AddStream(cfg *StreamConfig) (*StreamInfo, error) {
return resp.StreamInfo, nil
}

type JSApiStreamInfoResponse = JSApiStreamCreateResponse

func (js *js) StreamInfo(stream string) (*StreamInfo, error) {
csSubj := js.apiSubj(fmt.Sprintf(JSApiStreamInfoT, stream))
r, err := js.nc.Request(csSubj, nil, js.wait)
if err != nil {
return nil, err
}
var resp JSApiStreamInfoResponse
if err := json.Unmarshal(r.Data, &resp); err != nil {
return nil, err
}
if resp.Error != nil {
return nil, errors.New(resp.Error.Description)
}
return resp.StreamInfo, nil
}

// StreamInfo shows config and current state for this stream.
type StreamInfo struct {
Config StreamConfig `json:"config"`
Expand Down
10 changes: 10 additions & 0 deletions test/js_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,16 @@ func TestJetStreamManagement(t *testing.T) {
if si == nil || si.Config.Name != "foo" {
t.Fatalf("StreamInfo is not correct %+v", si)
}

// Check info calls.
si, err = js.StreamInfo("foo")
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if si == nil || si.Config.Name != "foo" {
t.Fatalf("StreamInfo is not correct %+v", si)
}

// Create a consumer using our client API.
ci, err := js.AddConsumer("foo", &nats.ConsumerConfig{Durable: "dlc", AckPolicy: nats.AckExplicit})
if err != nil {
Expand Down

0 comments on commit ec480fa

Please sign in to comment.