Skip to content

Commit

Permalink
fixup! refactor(coap-server): refactor handleRequest method
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Jul 21, 2023
1 parent d30b339 commit b71bb54
Showing 1 changed file with 44 additions and 31 deletions.
75 changes: 44 additions & 31 deletions packages/binding-coap/src/coap-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ export default class CoapServer implements ProtocolServer {

switch (method) {
case "GET":
if (req.headers.Observe === undefined) {
if (req.headers.Observe == null) {
this.handleReadProperty(property, req, contentType, thing, res, affordanceKey);
} else {
this.handleObserveProperty(property, req, contentType, thing, res, affordanceKey);
Expand Down Expand Up @@ -489,19 +489,14 @@ export default class CoapServer implements ProtocolServer {
affordanceKey: string
) {
try {
const options: AugmentedInteractionOptions = {
formIndex: ProtocolHelpers.findRequestMatchingFormIndex(
property.forms,
this.scheme,
req.url,
contentType
),
};
const uriVariables = Helpers.parseUrlParameters(req.url, thing.uriVariables, property.uriVariables);
if (!this.isEmpty(uriVariables)) {
options.uriVariables = uriVariables;
}
const content = await thing.handleReadProperty(affordanceKey, options);
const interactionOptions = this.createInteractionOptions(
property.forms,
property.uriVariables,
thing,
req,
contentType
);
const content = await thing.handleReadProperty(affordanceKey, interactionOptions);
this.streamContentResponse(res, content);
} catch (err) {
error(`CoapServer on port ${this.getPort()} got internal error on read '${req.url}': ${err.message}`);
Expand Down Expand Up @@ -550,18 +545,17 @@ export default class CoapServer implements ProtocolServer {
affordanceKey: string
) {
try {
const options: AugmentedInteractionOptions = {
formIndex: ProtocolHelpers.findRequestMatchingFormIndex(
property.forms,
this.scheme,
req.url,
contentType
),
};
const interactionOptions = this.createInteractionOptions(
property.forms,
property.uriVariables,
thing,
req,
contentType
);
await thing.handleWriteProperty(
affordanceKey,
new Content(contentType, Readable.from(req.payload)),
options
interactionOptions
);
this.sendChangedResponse(res, "Changed");
} catch (err) {
Expand Down Expand Up @@ -589,18 +583,18 @@ export default class CoapServer implements ProtocolServer {
return;
}

const options: AugmentedInteractionOptions = {
formIndex: ProtocolHelpers.findRequestMatchingFormIndex(action.forms, this.scheme, req.url, contentType),
};
const uriVariables = Helpers.parseUrlParameters(req.url, thing.uriVariables, action.uriVariables);
if (!this.isEmpty(uriVariables)) {
options.uriVariables = uriVariables;
}
const interactionOptions = this.createInteractionOptions(
action.forms,
action.uriVariables,
thing,
req,
contentType
);
try {
const output = await thing.handleInvokeAction(
affordanceKey,
new Content(contentType, Readable.from(req.payload)),
options
interactionOptions
);
if (output) {
this.streamContentResponse(res, output, { end: true });
Expand All @@ -613,6 +607,25 @@ export default class CoapServer implements ProtocolServer {
}
}

// TODO: Add types
private createInteractionOptions(
forms: any[],

Check warning on line 612 in packages/binding-coap/src/coap-server.ts

View workflow job for this annotation

GitHub Actions / eslint

Unexpected any. Specify a different type
affordanceUriVariables: any,

Check warning on line 613 in packages/binding-coap/src/coap-server.ts

View workflow job for this annotation

GitHub Actions / eslint

Unexpected any. Specify a different type
thing: ExposedThing,
req: IncomingMessage,
contentType: string
) {
const options: AugmentedInteractionOptions = {
formIndex: ProtocolHelpers.findRequestMatchingFormIndex(forms, this.scheme, req.url, contentType),
};
const uriVariables = Helpers.parseUrlParameters(req.url, thing.uriVariables, affordanceUriVariables);
if (!this.isEmpty(uriVariables)) {
options.uriVariables = uriVariables;
}

return options;
}

private async handleEventRequest(
thing: ExposedThing,
affordanceKey: string,
Expand Down

0 comments on commit b71bb54

Please sign in to comment.