| 1 |
// Copyright 2022-2025 The NATS Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package jetstream |
| 15 |
|
| 16 |
import ( |
| 17 |
"context" |
| 18 |
"encoding/json" |
| 19 |
"strings" |
| 20 |
) |
| 21 |
|
| 22 |
type ( |
| 23 |
apiResponse struct { |
| 24 |
Type string `json:"type"` |
| 25 |
Error *APIError `json:"error,omitempty"` |
| 26 |
} |
| 27 |
|
| 28 |
// apiPaged includes variables used to create paged responses from the JSON API |
| 29 |
apiPaged struct { |
| 30 |
Total int `json:"total"` |
| 31 |
Offset int `json:"offset"` |
| 32 |
Limit int `json:"limit"` |
| 33 |
} |
| 34 |
) |
| 35 |
|
| 36 |
// Request API subjects for JetStream. |
| 37 |
const ( |
| 38 |
// DefaultAPIPrefix is the default prefix for the JetStream API. |
| 39 |
DefaultAPIPrefix = "$JS.API." |
| 40 |
|
| 41 |
// jsDomainT is used to create JetStream API prefix by specifying only Domain |
| 42 |
jsDomainT = "$JS.%s.API." |
| 43 |
|
| 44 |
// jsExtDomainT is used to create a StreamSource External APIPrefix |
| 45 |
jsExtDomainT = "$JS.%s.API" |
| 46 |
|
| 47 |
// apiAccountInfo is for obtaining general information about JetStream. |
| 48 |
apiAccountInfo = "INFO" |
| 49 |
|
| 50 |
// apiConsumerCreateT is used to create consumers. |
| 51 |
apiConsumerCreateT = "CONSUMER.CREATE.%s.%s" |
| 52 |
|
| 53 |
// apiConsumerCreateWithFilterSubjectT is used to create consumers with a filter subject. |
| 54 |
// it accepts stream name, consumer name and filter subject |
| 55 |
apiConsumerCreateWithFilterSubjectT = "CONSUMER.CREATE.%s.%s.%s" |
| 56 |
|
| 57 |
// apiConsumerInfoT is used to retrieve consumer information. |
| 58 |
apiConsumerInfoT = "CONSUMER.INFO.%s.%s" |
| 59 |
|
| 60 |
// apiRequestNextT is the prefix for the request next message(s) for a consumer in worker/pull mode. |
| 61 |
apiRequestNextT = "CONSUMER.MSG.NEXT.%s.%s" |
| 62 |
|
| 63 |
// apiConsumerDeleteT is used to delete consumers. |
| 64 |
apiConsumerDeleteT = "CONSUMER.DELETE.%s.%s" |
| 65 |
|
| 66 |
// apiConsumerPauseT is used to pause a consumer. |
| 67 |
apiConsumerPauseT = "CONSUMER.PAUSE.%s.%s" |
| 68 |
|
| 69 |
// apiConsumerListT is used to return all detailed consumer information |
| 70 |
apiConsumerListT = "CONSUMER.LIST.%s" |
| 71 |
|
| 72 |
// apiConsumerNamesT is used to return a list with all consumer names for the stream. |
| 73 |
apiConsumerNamesT = "CONSUMER.NAMES.%s" |
| 74 |
|
| 75 |
// apiStreams can lookup a stream by subject. |
| 76 |
apiStreams = "STREAM.NAMES" |
| 77 |
|
| 78 |
// apiStreamCreateT is the endpoint to create new streams. |
| 79 |
apiStreamCreateT = "STREAM.CREATE.%s" |
| 80 |
|
| 81 |
// apiStreamInfoT is the endpoint to get information on a stream. |
| 82 |
apiStreamInfoT = "STREAM.INFO.%s" |
| 83 |
|
| 84 |
// apiStreamUpdateT is the endpoint to update existing streams. |
| 85 |
apiStreamUpdateT = "STREAM.UPDATE.%s" |
| 86 |
|
| 87 |
// apiStreamDeleteT is the endpoint to delete streams. |
| 88 |
apiStreamDeleteT = "STREAM.DELETE.%s" |
| 89 |
|
| 90 |
// apiStreamPurgeT is the endpoint to purge streams. |
| 91 |
apiStreamPurgeT = "STREAM.PURGE.%s" |
| 92 |
|
| 93 |
// apiStreamListT is the endpoint that will return all detailed stream information |
| 94 |
apiStreamListT = "STREAM.LIST" |
| 95 |
|
| 96 |
// apiMsgGetT is the endpoint to get a message. |
| 97 |
apiMsgGetT = "STREAM.MSG.GET.%s" |
| 98 |
|
| 99 |
// apiDirectMsgGetT is the endpoint to perform a direct get of a message. |
| 100 |
apiDirectMsgGetT = "DIRECT.GET.%s" |
| 101 |
|
| 102 |
// apiDirectMsgGetLastBySubjectT is the endpoint to perform a direct get of a message by subject. |
| 103 |
apiDirectMsgGetLastBySubjectT = "DIRECT.GET.%s.%s" |
| 104 |
|
| 105 |
// apiMsgDeleteT is the endpoint to remove a message. |
| 106 |
apiMsgDeleteT = "STREAM.MSG.DELETE.%s" |
| 107 |
|
| 108 |
// apiConsumerUnpinT is the endpoint to unpin a consumer. |
| 109 |
apiConsumerUnpinT = "CONSUMER.UNPIN.%s.%s" |
| 110 |
|
| 111 |
// apiConsumerResetT is the endpoint to reset a consumer's delivery state. |
| 112 |
apiConsumerResetT = "CONSUMER.RESET.%s.%s" |
| 113 |
) |
| 114 |
|
| 115 |
func (js *jetStream) apiRequestJSON(ctx context.Context, subject string, resp any, data ...[]byte) (*jetStreamMsg, error) { |
| 116 |
jsMsg, err := js.apiRequest(ctx, subject, data...) |
| 117 |
if err != nil { |
| 118 |
return nil, err |
| 119 |
} |
| 120 |
if err := json.Unmarshal(jsMsg.Data(), resp); err != nil { |
| 121 |
return nil, err |
| 122 |
} |
| 123 |
return jsMsg, nil |
| 124 |
} |
| 125 |
|
| 126 |
// a RequestWithContext with tracing via TraceCB |
| 127 |
func (js *jetStream) apiRequest(ctx context.Context, subj string, data ...[]byte) (*jetStreamMsg, error) { |
| 128 |
subj = js.apiSubject(subj) |
| 129 |
var req []byte |
| 130 |
if len(data) > 0 { |
| 131 |
req = data[0] |
| 132 |
} |
| 133 |
if js.opts.ClientTrace != nil { |
| 134 |
ctrace := js.opts.ClientTrace |
| 135 |
if ctrace.RequestSent != nil { |
| 136 |
ctrace.RequestSent(subj, req) |
| 137 |
} |
| 138 |
} |
| 139 |
resp, err := js.conn.RequestWithContext(ctx, subj, req) |
| 140 |
if err != nil { |
| 141 |
return nil, err |
| 142 |
} |
| 143 |
if js.opts.ClientTrace != nil { |
| 144 |
ctrace := js.opts.ClientTrace |
| 145 |
if ctrace.ResponseReceived != nil { |
| 146 |
ctrace.ResponseReceived(subj, resp.Data, resp.Header) |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
return js.toJSMsg(resp), nil |
| 151 |
} |
| 152 |
|
| 153 |
func (js *jetStream) apiSubject(subj string) string { |
| 154 |
if js.opts.apiPrefix == "" { |
| 155 |
return subj |
| 156 |
} |
| 157 |
var b strings.Builder |
| 158 |
b.WriteString(js.opts.apiPrefix) |
| 159 |
b.WriteString(subj) |
| 160 |
return b.String() |
| 161 |
} |