Skip to content

Commit

Permalink
feat: minor version change (#11)
Browse files Browse the repository at this point in the history
[create-pull-request] automated change

Co-authored-by: GamingEventapiBot <[email protected]>
  • Loading branch information
github-actions[bot] and GamingEventapiBot authored Jul 5, 2022
1 parent 50bb8a3 commit e6d16ef
Show file tree
Hide file tree
Showing 7 changed files with 292 additions and 1 deletion.
2 changes: 1 addition & 1 deletion configs.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"template_last_version": "0.5.21",
"document_last_version": "0.5.0"
"document_last_version": "0.6.0"
}
56 changes: 56 additions & 0 deletions src/channels/V0RustServersServerIdEventsStopped.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import ServerStopped from '../models/ServerStopped';
import * as Nats from 'nats';
import {
ErrorCode,
NatsTypescriptTemplateError
} from '../NatsTypescriptTemplateError';
/**
* Module which wraps functionality for the `v0/rust/servers/{server_id}/events/stopped` channel
* @module v0RustServersServerIdEventsStopped
*/
/**
* Internal functionality to setup subscription on the `v0/rust/servers/{server_id}/events/stopped` channel
*
* @param onDataCallback to call when messages are received
* @param nc to subscribe with
* @param codec used to convert messages
* @param server_id parameter to use in topic
* @param options to subscribe with, bindings from the AsyncAPI document overwrite these if specified
*/
export function subscribe(
onDataCallback: (
err ? : NatsTypescriptTemplateError,
msg ? : ServerStopped, server_id ? : string) => void,
nc: Nats.NatsConnection,
codec: Nats.Codec < any > , server_id: string,
options ? : Nats.SubscriptionOptions
): Promise < Nats.Subscription > {
return new Promise(async (resolve, reject) => {
let subscribeOptions: Nats.SubscriptionOptions = {
...options
};
try {
let subscription = nc.subscribe(`v0.rust.servers.${server_id}.events.stopped`, subscribeOptions);
(async () => {
for await (const msg of subscription) {
const unmodifiedChannel = `v0.rust.servers.{server_id}.events.stopped`;
let channel = msg.subject;
const serverIdSplit = unmodifiedChannel.split("{server_id}");
const splits = [
serverIdSplit[0],
serverIdSplit[1]
];
channel = channel.substring(splits[0].length);
const serverIdEnd = channel.indexOf(splits[1]);
const serverIdParam = "" + channel.substring(0, serverIdEnd);
let receivedData: any = codec.decode(msg.data);
onDataCallback(undefined, ServerStopped.unmarshal(receivedData), serverIdParam);
}
console.log("subscription closed");
})();
resolve(subscription);
} catch (e: any) {
reject(NatsTypescriptTemplateError.errorForCode(ErrorCode.INTERNAL_NATS_TS_ERROR, e));
}
})
}
46 changes: 46 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,26 @@ import {
} from './NatsTypescriptTemplateError';
import * as Nats from 'nats';
import * as v0RustServersServerIdEventsStartedChannel from "./channels/V0RustServersServerIdEventsStarted";
import * as v0RustServersServerIdEventsStoppedChannel from "./channels/V0RustServersServerIdEventsStopped";
import * as v0RustServersServerIdEventsPlayerSteamIdChattedChannel from "./channels/V0RustServersServerIdEventsPlayerSteamIdChatted";
import ServerStarted from "./models/ServerStarted";
import ServerStopped from "./models/ServerStopped";
import ChatMessage from "./models/ChatMessage";
export {
v0RustServersServerIdEventsStartedChannel
};
export {
v0RustServersServerIdEventsStoppedChannel
};
export {
v0RustServersServerIdEventsPlayerSteamIdChattedChannel
};
export {
ServerStarted
};
export {
ServerStopped
};
export {
ChatMessage
};
Expand Down Expand Up @@ -161,6 +169,44 @@ export class NatsAsyncApiClient {
}
});
}
/**
* Subscribe to the `v0/rust/servers/{server_id}/events/stopped`
*
* Channel for the API to process for when a server has stopped
*
* @param onDataCallback to call when messages are received
* @param server_id parameter to use in topic
* @param flush ensure client is force flushed after subscribing
* @param options to subscribe with, bindings from the AsyncAPI document overwrite these if specified
*/
public subscribeToV0RustServersServerIdEventsStopped(
onDataCallback: (
err ? : NatsTypescriptTemplateError,
msg ? : ServerStopped, server_id ? : string) => void, server_id: string,
flush ? : boolean,
options ? : Nats.SubscriptionOptions
): Promise < Nats.Subscription > {
return new Promise(async (resolve, reject) => {
if (!this.isClosed() && this.nc !== undefined && this.codec !== undefined) {
try {
const sub = await v0RustServersServerIdEventsStoppedChannel.subscribe(
onDataCallback,
this.nc,
this.codec, server_id,
options
);
if (flush) {
await this.nc.flush();
}
resolve(sub);
} catch (e: any) {
reject(e);
}
} else {
reject(NatsTypescriptTemplateError.errorForCode(ErrorCode.NOT_CONNECTED));
}
});
}
/**
* Subscribe to the `v0/rust/servers/{server_id}/events/player/{steam_id}/chatted`
*
Expand Down
55 changes: 55 additions & 0 deletions src/models/ServerStopped.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@


class ServerStopped {
private _timestamp: string;
private _additionalProperties?: Map<String, object | string | number | Array<unknown> | boolean | null>;

constructor(input: {
timestamp: string,
}) {
this._timestamp = input.timestamp;
}

get timestamp(): string { return this._timestamp; }
set timestamp(timestamp: string) { this._timestamp = timestamp; }

get additionalProperties(): Map<String, object | string | number | Array<unknown> | boolean | null> | undefined { return this._additionalProperties; }
set additionalProperties(additionalProperties: Map<String, object | string | number | Array<unknown> | boolean | null> | undefined) { this._additionalProperties = additionalProperties; }

public marshal() : string {
let json = '{'
if(this.timestamp !== undefined) {
json += `"timestamp": ${typeof this.timestamp === 'number' || typeof this.timestamp === 'boolean' ? this.timestamp : JSON.stringify(this.timestamp)},`;
}

if(this.additionalProperties !== undefined) {
for (const [key, value] of this.additionalProperties.entries()) {
//Only render additionalProperties which are not already a property
if(Object.keys(this).includes(String(key))) continue;
json += `"${key}": ${typeof value === 'number' || typeof value === 'boolean' ? value : JSON.stringify(value)},`;
}
}

//Remove potential last comma
return `${json.charAt(json.length-1) === ',' ? json.slice(0, json.length-1) : json}}`;
}

public static unmarshal(json: string | object): ServerStopped {
const obj = typeof json === "object" ? json : JSON.parse(json);
const instance = new ServerStopped({} as any);

if (obj["timestamp"] !== undefined) {
instance.timestamp = obj["timestamp"];
}

//Not part of core properties

if (instance.additionalProperties === undefined) {instance.additionalProperties = new Map();}
for (const [key, value] of Object.entries(obj).filter((([key,]) => {return !["timestamp"].includes(key);}))) {

instance.additionalProperties.set(key, value as any);
}
return instance;
}
}
export default ServerStopped;
31 changes: 31 additions & 0 deletions src/testclient/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,26 @@ import {
} from '../NatsTypescriptTemplateError';
import * as Nats from 'nats';
import * as v0RustServersServerIdEventsStartedChannel from "./testchannels/V0RustServersServerIdEventsStarted";
import * as v0RustServersServerIdEventsStoppedChannel from "./testchannels/V0RustServersServerIdEventsStopped";
import * as v0RustServersServerIdEventsPlayerSteamIdChattedChannel from "./testchannels/V0RustServersServerIdEventsPlayerSteamIdChatted";
import ServerStarted from "../models/ServerStarted";
import ServerStopped from "../models/ServerStopped";
import ChatMessage from "../models/ChatMessage";
export {
v0RustServersServerIdEventsStartedChannel
};
export {
v0RustServersServerIdEventsStoppedChannel
};
export {
v0RustServersServerIdEventsPlayerSteamIdChattedChannel
};
export {
ServerStarted
};
export {
ServerStopped
};
export {
ChatMessage
};
Expand Down Expand Up @@ -138,6 +146,29 @@ export class NatsAsyncApiTestClient {
return Promise.reject(NatsTypescriptTemplateError.errorForCode(ErrorCode.NOT_CONNECTED));
}
}
/**
* Publish to the `v0/rust/servers/{server_id}/events/stopped` channel
*
* Channel for the API to process for when a server has stopped
*
* @param message to publish
* @param server_id parameter to use in topic
*/
public publishToV0RustServersServerIdEventsStopped(
message: ServerStopped, server_id: string,
options ? : Nats.PublishOptions
): Promise < void > {
if (!this.isClosed() && this.nc !== undefined && this.codec !== undefined) {
return v0RustServersServerIdEventsStoppedChannel.publish(
message,
this.nc,
this.codec, server_id,
options
);
} else {
return Promise.reject(NatsTypescriptTemplateError.errorForCode(ErrorCode.NOT_CONNECTED));
}
}
/**
* Publish to the `v0/rust/servers/{server_id}/events/player/{steam_id}/chatted` channel
*
Expand Down
37 changes: 37 additions & 0 deletions src/testclient/testchannels/V0RustServersServerIdEventsStopped.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import ServerStopped from '../../models/ServerStopped';
import * as Nats from 'nats';
import {
ErrorCode,
NatsTypescriptTemplateError
} from '../../NatsTypescriptTemplateError';
/**
* Module which wraps functionality for the `v0/rust/servers/{server_id}/events/stopped` channel
* @module v0RustServersServerIdEventsStopped
*/
/**
* Internal functionality to publish message to channel
* v0/rust/servers/{server_id}/events/stopped
*
* @param message to publish
* @param nc to publish with
* @param codec used to convert messages
* @param server_id parameter to use in topic
* @param options to publish with
*/
export function publish(
message: ServerStopped,
nc: Nats.NatsConnection,
codec: Nats.Codec < any > , server_id: string,
options ? : Nats.PublishOptions
): Promise < void > {
return new Promise < void > (async (resolve, reject) => {
try {
let dataToSend: any = message.marshal();
dataToSend = codec.encode(dataToSend);
nc.publish(`v0.rust.servers.${server_id}.events.stopped`, dataToSend, options);
resolve();
} catch (e: any) {
reject(NatsTypescriptTemplateError.errorForCode(ErrorCode.INTERNAL_NATS_TS_ERROR, e));
}
});
};
66 changes: 66 additions & 0 deletions tests/integration/V0RustServersServerIdEventsStopped.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import {
describe,
it,
before
} from 'mocha';
import {
expect
} from 'chai';
import * as Client from '../../src'
import * as TestClient from '../../src/testclient'
import {
NatsTypescriptTemplateError
} from '../../src/NatsTypescriptTemplateError';
describe('v0/rust/servers/{server_id}/events/stopped can talk to itself', () => {
var client: Client.NatsAsyncApiClient;
var testClient: TestClient.NatsAsyncApiTestClient;
before(async () => {
client = new Client.NatsAsyncApiClient();
testClient = new TestClient.NatsAsyncApiTestClient();
const natsHost = process.env.NATS_HOST || "0.0.0.0"
const natsPort = process.env.NATS_PORT || "4222"
const natsUrl = `${natsHost}:${natsPort}`
await client.connectToHost(natsUrl);
await testClient.connectToHost(natsUrl);
});
it('can send message', async () => {
var receivedError: NatsTypescriptTemplateError | undefined = undefined;
var receivedMsg: Client.ServerStopped | undefined = undefined;
var receivedServerId: string | undefined = undefined
var publishMessage: TestClient.ServerStopped = TestClient.ServerStopped.unmarshal({
"timestamp": "2016-08-29T09:12:33.001Z"
});
var ServerIdToSend: string = "string"
const subscription = await client.subscribeToV0RustServersServerIdEventsStopped((err, msg, server_id) => {
receivedError = err;
receivedMsg = msg;
receivedServerId = server_id
}, ServerIdToSend,
true
);
const tryAndWaitForResponse = new Promise((resolve, reject) => {
let isReturned = false;
setTimeout(() => {
if (!isReturned) {
reject(new Error("Timeout"));
}
}, 3000)
setInterval(async () => {
if (subscription.getReceived() === 1) {
resolve(undefined);
isReturned = true
}
}, 100);
});
await testClient.publishToV0RustServersServerIdEventsStopped(publishMessage, ServerIdToSend);
await tryAndWaitForResponse;
expect(receivedError).to.be.undefined;
expect(receivedMsg).to.not.be.undefined;
expect(receivedMsg!.marshal()).to.equal(publishMessage.marshal());
expect(receivedServerId).to.be.equal(ServerIdToSend);
});
after(async () => {
await client.disconnect();
await testClient.disconnect();
});
});

0 comments on commit e6d16ef

Please sign in to comment.