Skip to content

Commit

Permalink
fix: add pattern to href in case of uriVariables
Browse files Browse the repository at this point in the history
e.g., decrement{?step}
  • Loading branch information
danielpeintner committed Jan 30, 2019
1 parent a2a64d4 commit df9ecdd
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions packages/binding-http/src/http-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,25 @@ export default class HttpServer implements ProtocolServer {
return -1;
}
}


private updateInteractionNameWithUriVariablePattern(interactionName: string, uriVariables: {[key: string]: WoT.DataSchema;}) : string {
if(uriVariables && Object.keys(uriVariables).length > 0) {
let pattern = "{?"
let index = 0;
for(let key in uriVariables) {
if(index != 0) {
pattern += ",";
}
pattern += encodeURIComponent(key);
index++;
}
pattern += "}";
return encodeURIComponent(interactionName) + pattern;
} else {
return encodeURIComponent(interactionName);
}
}

public expose(thing: ExposedThing): Promise<void> {

Expand Down Expand Up @@ -176,7 +195,8 @@ export default class HttpServer implements ProtocolServer {
}

for (let propertyName in thing.properties) {
let href = base + "/" + this.PROPERTY_DIR + "/" + encodeURIComponent(propertyName);
let propertyNamePattern = this.updateInteractionNameWithUriVariablePattern(propertyName, thing.properties[propertyName].uriVariables);
let href = base + "/" + this.PROPERTY_DIR + "/" + propertyNamePattern;
let form = new TD.Form(href, type);
if (thing.properties[propertyName].readOnly) {
form.op = ["readproperty"];
Expand All @@ -201,15 +221,17 @@ export default class HttpServer implements ProtocolServer {
}

for (let actionName in thing.actions) {
let href = base + "/" + this.ACTION_DIR + "/" + encodeURIComponent(actionName);
let actionNamePattern = this.updateInteractionNameWithUriVariablePattern(actionName, thing.actions[actionName].uriVariables);
let href = base + "/" + this.ACTION_DIR + "/" + actionNamePattern;
let form = new TD.Form(href, type);
form.op = ["invokeaction"];
thing.actions[actionName].forms.push(form);
console.log(`HttpServer on port ${this.getPort()} assigns '${href}' to Action '${actionName}'`);
}

for (let eventName in thing.events) {
let href = base + "/" + this.EVENT_DIR + "/" + encodeURIComponent(eventName);
let eventNamePattern = this.updateInteractionNameWithUriVariablePattern(eventName, thing.events[eventName].uriVariables);
let href = base + "/" + this.EVENT_DIR + "/" + eventNamePattern;
let form = new TD.Form(href, type);
form.subprotocol = "longpoll";
form.op = ["subscribeevent"];
Expand Down

0 comments on commit df9ecdd

Please sign in to comment.