RequestJSON sends a JSON request-reply via NATS, marshaling the request and unmarshaling the reply. This eliminates the repeated marshal/request/unmarshal boilerplate across all NATS request-reply call sites.
(c MessagingClient, subject string, req Req, timeout time.Duration)
| 293 | // unmarshaling the reply. This eliminates the repeated marshal/request/unmarshal |
| 294 | // boilerplate across all NATS request-reply call sites. |
| 295 | func RequestJSON[Req, Reply any](c MessagingClient, subject string, req Req, timeout time.Duration) (*Reply, error) { |
| 296 | data, err := json.Marshal(req) |
| 297 | if err != nil { |
| 298 | return nil, fmt.Errorf("marshaling request: %w", err) |
| 299 | } |
| 300 | replyData, err := c.Request(subject, data, timeout) |
| 301 | if err != nil { |
| 302 | return nil, fmt.Errorf("NATS request to %s: %w", subject, err) |
| 303 | } |
| 304 | var reply Reply |
| 305 | if err := json.Unmarshal(replyData, &reply); err != nil { |
| 306 | return nil, fmt.Errorf("unmarshaling reply from %s: %w", subject, err) |
| 307 | } |
| 308 | return &reply, nil |
| 309 | } |
| 310 | |
| 311 | // Conn returns the underlying NATS connection for advanced usage. |
| 312 | // |
no test coverage detected