Skip to content

Commit 5556f36

Browse files
authored
Add parameters to action manifest and action request (#334)
* Add parameters to action manifest and action request * Dont require parameters in action request
1 parent e310eb2 commit 5556f36

File tree

10 files changed

+59
-8
lines changed

10 files changed

+59
-8
lines changed

adapter-docs/src/docs/asciidoc/overview.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ A request consists of the following fields.
167167
| Attachments
168168
| Any number of attachments. See table below for a list of possible request attachments.
169169

170+
| Parameters
171+
| A list of arbitrary attachments requested by the action
172+
170173
| Time budget
171174
| The app will use a time out if no response is received within x seconds after the user triggered an operation.
172175
Each action request will contain an estimated time budget the adapter should strive to respond within.

adapter-protobuf-java/src/main/proto/adapter.proto

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ message AdapterManifest {
7979

8080
reserved 5;
8181

82+
// A list of arbitrary parameters this action can accept as input
83+
repeated ParameterDefinition accepted_parameters = 6;
84+
8285
}
8386

8487

@@ -97,6 +100,20 @@ message AdapterManifest {
97100

98101
}
99102

103+
// An arbitrary parameter that this action can accept
104+
// Example
105+
// ---------
106+
// Name: colour
107+
// Description: A colour (red|blue|green|yellow|pink|cyan|white)
108+
message ParameterDefinition {
109+
110+
// The name of the parameter
111+
string name = 1;
112+
113+
// A helpful description of what the parameter is
114+
string description = 2;
115+
}
116+
100117
}
101118

102119

@@ -123,6 +140,9 @@ message ActionRequest {
123140
// error is better then not returning anything.
124141
int32 time_budget_millis = 4;
125142

143+
// Arbitrary map of parameters and their values as requested by the action manifest
144+
map<string, string> parameters = 6;
145+
126146
}
127147

128148
// Your adapter should return this as a response to requests submitted

adapter-sdk-kotlin/src/main/kotlin/com/github/oslokommune/oslonokkelen/adapter/action/ActionDescription.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ data class ActionDescription(
66
val id: ActionId,
77
val description: String = "Action: ${id.short}",
88
val requiredAttachmentTypes: Set<Adapter.AttachmentType> = emptySet(),
9-
val possibleOutputAttachmentTypes: Set<Adapter.AttachmentType> = emptySet()
9+
val possibleOutputAttachmentTypes: Set<Adapter.AttachmentType> = emptySet(),
10+
val parameters: List<Adapter.AdapterManifest.ParameterDefinition> = emptyList()
1011
)

adapter-sdk-kotlin/src/main/kotlin/com/github/oslokommune/oslonokkelen/adapter/action/AdapterActionRequest.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,17 @@ data class AdapterActionRequest(
88
val requestId: String = UUID.randomUUID().toString(),
99
val actionId: ActionId,
1010
val timeBudget: Duration,
11-
val attachments: List<AdapterAttachment>
11+
val attachments: List<AdapterAttachment>,
12+
val parameters: Map<String, String>
1213
) {
1314

1415
inline fun <reified A : AdapterAttachment> require(): A {
1516
return attachments.filterIsInstance<A>().firstOrNull()
1617
?: throw InvalidAdapterRequestException("Missing attachment: ${A::class.simpleName}")
1718
}
19+
20+
fun requireParameter(parameterName: String): String {
21+
return parameters[parameterName]
22+
?: throw InvalidAdapterRequestException("Missing parameter: $parameterName")
23+
}
1824
}

adapter-sdk-kotlin/src/main/kotlin/com/github/oslokommune/oslonokkelen/adapter/protobuf/ProtobufParser.kt

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ object ProtobufParser {
6767
null
6868
}
6969
})
70+
.putAllParameters(optionalMap(requestClaim, "parameters")?.mapNotNull {
71+
val key = it.key as? String
72+
val value = it.value as? String
73+
if(key != null && value != null) {
74+
key to value
75+
} else {
76+
null
77+
}
78+
}?.toMap() ?: emptyMap())
7079

7180
return requestBuilder.build()
7281
}
@@ -90,6 +99,10 @@ object ProtobufParser {
9099
return value ?: throw missingKey(key, requestClaim)
91100
}
92101

102+
private fun optionalMap(requestClaim: Map<String, Any>, key: String): Map<*, *>? {
103+
return requestClaim[key] as? Map<*, *>
104+
}
105+
93106
private fun missingKey(key: String, requestClaim: Map<*, *>): IllegalStateException {
94107
return IllegalStateException("Missing key $key (found: ${requestClaim.keys.joinToString(", ")})")
95108
}
@@ -101,7 +114,8 @@ object ProtobufParser {
101114
timeBudget = Duration.ofMillis(serializedRequest.timeBudgetMillis.toLong()),
102115
attachments = serializedRequest.attachmentsList.mapNotNull { attachment ->
103116
parseAttachment(attachment)
104-
}
117+
},
118+
parameters = serializedRequest.parametersMap
105119
)
106120
}
107121

@@ -224,7 +238,8 @@ object ProtobufParser {
224238
id = thing.id.createActionId(serializedAction.id),
225239
description = serializedAction.description,
226240
requiredAttachmentTypes = serializedAction.requiredInputAttachmentTypesList.toSet(),
227-
possibleOutputAttachmentTypes = serializedAction.possibleOutputAttachmentTypesList.toSet()
241+
possibleOutputAttachmentTypes = serializedAction.possibleOutputAttachmentTypesList.toSet(),
242+
parameters = serializedAction.acceptedParametersList
228243
)
229244

230245
thingActions = thingActions.put(action.id, action)

adapter-sdk-kotlin/src/main/kotlin/com/github/oslokommune/oslonokkelen/adapter/protobuf/ProtobufSerializer.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ object ProtobufSerializer {
2424
.setRequestId(request.requestId)
2525
.setThingId(request.actionId.thingId.value)
2626
.setActionId(request.actionId.value)
27+
.putAllParameters(request.parameters)
2728
.build()
2829
}
2930

@@ -318,6 +319,7 @@ object ProtobufSerializer {
318319
.setDescription(action.description)
319320
.addAllRequiredInputAttachmentTypes(action.requiredAttachmentTypes.toList())
320321
.addAllPossibleOutputAttachmentTypes(action.possibleOutputAttachmentTypes.toList())
322+
.addAllAcceptedParameters(action.parameters)
321323
.build()
322324
} ?: emptyList()
323325

adapter-sdk-kotlin/src/main/kotlin/com/github/oslokommune/oslonokkelen/adapter/tokens/generator/BackendTokenGenerator.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ class BackendTokenGenerator(
6363
is AdapterAttachment.ErrorDescription,
6464
is AdapterAttachment.ErrorCategory -> null
6565
}
66-
}
66+
},
67+
"parameters" to request.parameters
6768
)
6869

6970
return buildToken {

adapter-sdk-kotlin/src/test/kotlin/com/github/oslokommune/oslonokkelen/adapter/protobuf/ProtobufParserTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ internal class ProtobufParserTest {
2121
timeBudget = Duration.ofMillis(2400),
2222
attachments = listOf(
2323
AdapterAttachment.NorwegianFodselsnummer("12345678912")
24-
)
24+
),
25+
parameters = mapOf("test" to "testValue")
2526
)
2627

2728
val token = BackendTokenGeneratorTest.testGenerator.createActionRequestToken(

adapter-sdk-kotlin/src/test/kotlin/com/github/oslokommune/oslonokkelen/adapter/protobuf/ProtobufSerializerTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ internal class ProtobufSerializerTest {
3535
timeBudget = Duration.ofSeconds(3),
3636
attachments = listOf(
3737
AdapterAttachment.NorwegianFodselsnummer("30098602247")
38-
)
38+
),
39+
parameters = mapOf("test" to "testValue")
3940
)
4041

4142
val serializedRequest = ProtobufSerializer.serialize(request)

adapter-sdk-kotlin/src/test/kotlin/com/github/oslokommune/oslonokkelen/adapter/tokens/generator/BackendTokenGeneratorTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ class BackendTokenGeneratorTest {
3434
timeBudget = Duration.ofSeconds(2),
3535
attachments = listOf(
3636
AdapterAttachment.NorwegianFodselsnummer("30098602247")
37-
)
37+
),
38+
parameters = mapOf("test" to "testValue")
3839
)
3940
)
4041

0 commit comments

Comments
 (0)