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

refactor: use a default contentType of application/json in Forms #1176

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions packages/binding-coap/src/coap-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export default class CoapClient implements ProtocolClient {
debug(`CoapClient received Content-Format: ${res.headers["Content-Format"]}`);

// FIXME does not work with blockwise because of node-coap
const contentType = res.headers["Content-Format"] ?? form.contentType ?? ContentSerdes.DEFAULT;
const contentType = res.headers["Content-Format"] ?? form.contentType;

res.on("data", (data: Buffer) => {
next(new Content(`${contentType}`, Readable.from(res.payload)));
Expand Down Expand Up @@ -253,7 +253,7 @@ export default class CoapClient implements ProtocolClient {
const method = form["cov:method"] ?? defaultMethod;
debug(`CoapClient got Form "method" ${method}`);

const contentFormat = form["cov:contentFormat"] ?? form.contentType ?? "application/json";
const contentFormat = form["cov:contentFormat"] ?? form.contentType;
debug(`"CoapClient got Form 'contentType' ${contentFormat} `);

const accept = form["cov:accept"];
Expand Down
2 changes: 1 addition & 1 deletion packages/binding-coap/src/coap-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,7 @@ export default class CoapServer implements ProtocolServer {
);
}

return contentType ?? ContentSerdes.DEFAULT;
return contentType;
}

private checkContentTypeSupportForInput(method: string, contentType: string): boolean {
Expand Down
6 changes: 3 additions & 3 deletions packages/binding-coap/src/coaps-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import { Subscription } from "rxjs/Subscription";

import { ProtocolClient, Content, createLoggers, ContentSerdes } from "@node-wot/core";

Check failure on line 24 in packages/binding-coap/src/coaps-client.ts

View workflow job for this annotation

GitHub Actions / eslint

'ContentSerdes' is defined but never used
import { CoapForm, CoapMethodName, isValidCoapMethod, isSupportedCoapMethod } from "./coap";
import { CoapClient as coaps, CoapResponse, RequestMethod, SecurityParameters } from "node-coap-client";
import { Readable } from "stream";
Expand All @@ -47,7 +47,7 @@
debug(`CoapsClient received ${res.code} from ${form.href}`);

// FIXME: Add toString conversion for response Content-Format
const contentType = form.contentType ?? ContentSerdes.DEFAULT;
const contentType = form.contentType;
const body = Readable.from(res.payload ?? Buffer.alloc(0));

resolve(new Content(contentType, body));
Expand Down Expand Up @@ -79,7 +79,7 @@
debug(`CoapsClient received ${res.code} from ${form.href}`);

// FIXME: Add toString conversion for response Content-Format
const contentType = form.contentType ?? ContentSerdes.DEFAULT;
const contentType = form.contentType;
const body = Readable.from(res.payload ?? Buffer.alloc(0));

resolve(new Content(contentType, body));
Expand Down Expand Up @@ -118,7 +118,7 @@

const callback = (resp: CoapResponse) => {
if (resp.payload != null) {
next(new Content(form?.contentType ?? ContentSerdes.DEFAULT, Readable.from(resp.payload)));
next(new Content(form?.contentType, Readable.from(resp.payload)));
}
};

Expand Down
2 changes: 1 addition & 1 deletion packages/binding-http/src/subscription-protocols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export class SSESubscription implements InternalSubscription {
};
this.eventSource.onmessage = (event) => {
debug(`HttpClient received ${JSON.stringify(event)} from ${this.form.href}`);
const output = new Content(this.form.contentType ?? ContentSerdes.DEFAULT, Readable.from(event.data));
const output = new Content(this.form.contentType, Readable.from(event.data));
next(output);
};
this.eventSource.onerror = function (event) {
Expand Down
2 changes: 1 addition & 1 deletion packages/binding-modbus/src/modbus-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import ModbusRTU from "modbus-serial";
import { ReadCoilResult, ReadRegisterResult } from "modbus-serial/ModbusRTU";
import { ModbusEntity, ModbusFunction, ModbusForm } from "./modbus";
import { Content, ContentSerdes, createLoggers, Endianness } from "@node-wot/core";

Check failure on line 18 in packages/binding-modbus/src/modbus-connection.ts

View workflow job for this annotation

GitHub Actions / eslint

'ContentSerdes' is defined but never used
import { Readable } from "stream";
import { inspect } from "util";

Expand Down Expand Up @@ -451,7 +451,7 @@
this.quantity = form["modv:quantity"];
this.function = form["modv:function"] as ModbusFunction;
this.endianness = endianness;
this.contentType = form.contentType ?? ContentSerdes.DEFAULT;
this.contentType = form.contentType;
this.content = content;
this.transaction = null;
}
Expand Down
8 changes: 4 additions & 4 deletions packages/binding-mqtt/src/mqtt-broker-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ export default class MqttBrokerServer implements ProtocolServer {
* https://github.com/mqttjs/MQTT.js/pull/1103
* For further discussion see https://github.com/eclipse-thingweb/node-wot/pull/253
*/
const contentType = packet?.properties?.contentType ?? ContentSerdes.DEFAULT;
const contentType = packet?.properties?.contentType;

const options: InteractionOptions & { formIndex: number } = {
formIndex: ProtocolHelpers.findRequestMatchingFormIndex(
Expand All @@ -274,7 +274,7 @@ export default class MqttBrokerServer implements ProtocolServer {
),
};

const formContentType = action.forms[options.formIndex].contentType ?? ContentSerdes.DEFAULT;
const formContentType = action.forms[options.formIndex].contentType;
const inputContent = new Content(formContentType, Readable.from(payload));

thing
Expand Down Expand Up @@ -306,7 +306,7 @@ export default class MqttBrokerServer implements ProtocolServer {
) {
const readOnly = property.readOnly ?? false;
if (!readOnly) {
const contentType = packet?.properties?.contentType ?? ContentSerdes.DEFAULT;
const contentType = packet?.properties?.contentType;

const options: InteractionOptions & { formIndex: number } = {
formIndex: ProtocolHelpers.findRequestMatchingFormIndex(
Expand All @@ -317,7 +317,7 @@ export default class MqttBrokerServer implements ProtocolServer {
),
};

const formContentType = property.forms[options.formIndex].contentType ?? ContentSerdes.DEFAULT;
const formContentType = property.forms[options.formIndex].contentType;
const inputContent = new Content(formContentType, Readable.from(payload));

try {
Expand Down
2 changes: 1 addition & 1 deletion packages/binding-mqtt/src/mqtt-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default class MqttClient implements ProtocolClient {
error?: (error: Error) => void,
complete?: () => void
): Promise<Subscription> {
const contentType = form.contentType ?? ContentSerdes.DEFAULT;
const contentType = form.contentType;
const requestUri = new url.URL(form.href);
const brokerUri: string = `${this.scheme}://` + requestUri.host;
// Keeping the path as the topic for compatibility reasons.
Expand Down
6 changes: 2 additions & 4 deletions packages/binding-opcua/src/opcua-protocol-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ export class OPCUAProtocolClient implements ProtocolClient {

///
private async _dataValueToContent(form: OPCUAForm, dataValue: DataValue): Promise<Content> {
const contentType = form.contentType ?? "application/json";
const contentType = form.contentType;

// QUESTION: how can we extend the default contentSerDes.valueToContent for application/json,
const contentSerDes = ContentSerdes.get();
Expand Down Expand Up @@ -615,13 +615,11 @@ export class OPCUAProtocolClient implements ProtocolClient {
): Promise<Content> {
const outputArguments = (argumentDefinition.outputArguments ?? []) as unknown as Argument[];

const contentType = form.contentType ?? "application/json";

const body: Record<string, unknown> = {};
for (let index = 0; index < outputArguments.length; index++) {
const argument = outputArguments[index];
const { name } = argument;
const element = _variantToJSON(outputVariants[index], contentType);
const element = _variantToJSON(outputVariants[index], form.contentType);
body[name ?? "null"] = element;
}

Expand Down
6 changes: 1 addition & 5 deletions packages/core/src/exposed-thing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,11 +409,7 @@ export default class ExposedThing extends TD.Thing implements WoT.ExposedThing {
const form = this.properties[propertyName]?.forms[options.formIndex] ?? {
contentType: "application/json",
};
return ContentManager.valueToContent(
result,
this.properties[propertyName],
form?.contentType ?? "application/json"
);
return ContentManager.valueToContent(result, this.properties[propertyName], form.contentType);
} else {
throw new Error(`ExposedThing '${this.title}' has no readHandler for Property '${propertyName}'`);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/td-tools/src/thing-description.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export type ThingInteraction = TDT.PropertyElement | TDT.ActionElement | TDT.Eve
export class Form implements TDT.FormElementBase {
op?: string | string[];
href: TDT.AnyUri;
contentType?: string;
contentType: string;
contentCoding?: string;
subprotocol?: TDT.Subprotocol;
security?: TDT.Security;
Expand All @@ -67,7 +67,7 @@ export class Form implements TDT.FormElementBase {

constructor(href: string, contentType?: string) {
this.href = href;
if (contentType != null) this.contentType = contentType;
this.contentType = contentType ?? "application/json";
}
}
export interface ExpectedResponse {
Expand Down
Loading