Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type to service responses #786

Merged
merged 1 commit into from
Dec 30, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions async-nats/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ pub struct Stats {
/// Response for `STATS` requests.
#[derive(Serialize, Deserialize)]
pub struct StatsResponse {
#[serde(rename = "type")]
pub response_type: String,
pub name: String,
pub id: String,
pub version: String,
Expand All @@ -71,6 +73,8 @@ pub struct StatsResponse {
/// Right now, there is only one business endpoint, all other are internals.
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct EndpointStats {
#[serde(rename = "type")]
pub response_type: String,
pub name: String,
#[serde(rename = "num_requests")]
pub requests: usize,
Expand All @@ -84,6 +88,8 @@ pub struct EndpointStats {
/// Information about service instance.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Info {
#[serde(rename = "type")]
pub response_type: String,
pub name: String,
pub id: String,
pub description: Option<String>,
Expand Down Expand Up @@ -235,6 +241,7 @@ impl Service {
let id = nuid::next();
let started = time::OffsetDateTime::now_utc();
let info = Info {
response_type: "io.nats.micro.v1.info_response".to_string(),
name: config.name.clone(),
id: id.clone(),
description: config.description.clone(),
Expand Down Expand Up @@ -284,6 +291,7 @@ impl Service {
let client = client.clone();
let info_json = serde_json::to_vec(&info).map(Bytes::from)?;
let schema_json = serde_json::to_vec(&json!({
"type": "io.nats.micro.v1.schema_response",
"name": config.name.clone(),
"id": id.clone(),
"version": config.version.clone(),
Expand All @@ -295,6 +303,7 @@ impl Service {
tokio::select! {
Some(ping) = pings.next() => {
let pong = serde_json::to_vec(&json!({
"type": "io.nats.micro.v1.ping_response",
"name": info.name,
"id": info.id,
"version": info.version,
Expand All @@ -314,6 +323,7 @@ impl Service {
// FIXME: proper status handling
Some(stats_request) = stats.next() => {
let stats = serde_json::to_vec(&StatsResponse {
response_type: "io.nats.micro.v1.stats_response".to_string(),
name: info.name.clone(),
id: info.id.clone(),
version: info.version.clone(),
Expand Down