Skip to content

Commit d62094b

Browse files
octonatoefgpinto
andauthored
feat: KalixClient.post without body (#1247)
* feat: KalixClient.post without body * bodyless for DELETE, PUT and PATCH, * remove body param from javadoc Co-authored-by: Eduardo Pinto <[email protected]>
1 parent 567880b commit d62094b

File tree

5 files changed

+136
-15
lines changed

5 files changed

+136
-15
lines changed

sdk/spring-sdk/src/it/java/com/example/wiring/eventsourcedentities/counter/IncreaseAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public Effect<Integer> printIncrease(ValueIncreased event) {
4646
String entityId = this.actionContext().metadata().asCloudEvent().subject().get();
4747
if (event.value == 42) {
4848
CompletionStage<Integer> res =
49-
kalixClient.post("/counter/" + entityId + "/increase/1", "", Integer.class).execute();
49+
kalixClient.post("/counter/" + entityId + "/increase/1", Integer.class).execute();
5050
return effects().asyncReply(res);
5151
}
5252
return effects().reply(event.value);

sdk/spring-sdk/src/it/java/com/example/wiring/eventsourcedentities/counter/IncreaseActionWithIgnore.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public Effect<Integer> oneShallPass(ValueIncreased event) {
4242
String entityId = this.actionContext().metadata().asCloudEvent().subject().get();
4343
if (event.value == 1234) {
4444
CompletionStage<Integer> res =
45-
kalixClient.post("/counter/" + entityId + "/increase/1", "", Integer.class).execute();
45+
kalixClient.post("/counter/" + entityId + "/increase/1", Integer.class).execute();
4646
return effects().asyncReply(res);
4747
}
4848
return effects().reply(event.value);

sdk/spring-sdk/src/it/java/com/example/wiring/valueentities/user/ValidateUserAction.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public Action.Effect<String> createOrUpdateUser(@PathVariable String user, @Path
3838
if (email.isEmpty() || name.isEmpty())
3939
return effects().error("No field can be empty", Status.Code.INVALID_ARGUMENT);
4040

41-
var defCall = kalixClient.put("/user/" + user + "/" + email + "/" + name, "", String.class);
41+
var defCall = kalixClient.put("/user/" + user + "/" + email + "/" + name, String.class);
4242
return effects().forward(defCall);
4343
}
4444

@@ -47,13 +47,13 @@ public Action.Effect<String> updateEmail(@PathVariable String user, @PathVariabl
4747
if (email.isEmpty())
4848
return effects().error("No field can be empty", Status.Code.INVALID_ARGUMENT);
4949

50-
var defCall = kalixClient.patch("/user/" + user + "/email/" + email, "", String.class);
50+
var defCall = kalixClient.patch("/user/" + user + "/email/" + email, String.class);
5151
return effects().forward(defCall);
5252
}
5353

5454
@DeleteMapping
5555
public Action.Effect<String> delete(@PathVariable String user) {
56-
var defCall = kalixClient.delete("/user/" + user, "", String.class);
56+
var defCall = kalixClient.delete("/user/" + user, String.class);
5757
return effects().forward(defCall);
5858
}
5959
}

sdk/spring-sdk/src/main/scala/kalix/springsdk/KalixClient.scala

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,30 @@ trait KalixClient {
7676
*/
7777
def post[P, R](uri: String, body: P, returnType: Class[R]): DeferredCall[Any, R]
7878

79+
/**
80+
* Provides utility to do a POST HTTP request to a target endpoint belonging to a Kalix component. Such endpoint is
81+
* identified by a resource path (i.e. excluding authority and scheme).
82+
*
83+
* Example of use:
84+
* {{{
85+
* public Effect<Confirmation> createCounter() {
86+
* var serviceCall = kalixClient.post("/counter/create", Confirmation.class);
87+
* return effects().forward(serviceCall);
88+
* }
89+
* }}}
90+
*
91+
* @param uri
92+
* The resource path where the target endpoint will be reached at. Query parameters can be passed in as part of the
93+
* URI but should be encoded if containing special characters.
94+
* @param returnType
95+
* The type returned by the target endpoint
96+
* @tparam R
97+
* Type returned as response from the target endpoint
98+
* @return
99+
* a [[kalix.javasdk.DeferredCall]] to be used in forwards and timers or to be executed in place
100+
*/
101+
def post[R](uri: String, returnType: Class[R]): DeferredCall[Any, R]
102+
79103
/**
80104
* Provides utility to do a PUT HTTP request to a target endpoint belonging to a Kalix component. Such endpoint is
81105
* identified by a resource path (i.e. excluding authority and scheme).
@@ -104,6 +128,30 @@ trait KalixClient {
104128
*/
105129
def put[P, R](uri: String, body: P, returnType: Class[R]): DeferredCall[Any, R]
106130

131+
/**
132+
* Provides utility to do a PUT HTTP request to a target endpoint belonging to a Kalix component. Such endpoint is
133+
* identified by a resource path (i.e. excluding authority and scheme).
134+
*
135+
* Example of use:
136+
* {{{
137+
* public Effect<Number> nextNumber(Number number) {
138+
* var serviceCall = kalixClient.put("/fibonacci/next", number, Number.class);
139+
* return effects().forward(serviceCall);
140+
* }
141+
* }}}
142+
*
143+
* @param uri
144+
* The resource path where the target endpoint will be reached at. Query parameters can be passed in as part of the
145+
* URI but should be encoded if containing special characters.
146+
* @param returnType
147+
* The type returned by the target endpoint
148+
* @tparam R
149+
* Type returned as response from the target endpoint
150+
* @return
151+
* a [[kalix.javasdk.DeferredCall]] to be used in forwards and timers or to be executed in place
152+
*/
153+
def put[R](uri: String, returnType: Class[R]): DeferredCall[Any, R]
154+
107155
/**
108156
* Provides utility to do a PATCH HTTP request to a target endpoint belonging to a Kalix component. Such endpoint is
109157
* identified by a resource path (i.e. excluding authority and scheme).
@@ -132,31 +180,51 @@ trait KalixClient {
132180
*/
133181
def patch[P, R](uri: String, body: P, returnType: Class[R]): DeferredCall[Any, R]
134182

183+
/**
184+
* Provides utility to do a PATCH HTTP request to a target endpoint belonging to a Kalix component. Such endpoint is
185+
* identified by a resource path (i.e. excluding authority and scheme).
186+
*
187+
* Example of use:
188+
* {{{
189+
* public Effect<User> createUser(String email) {
190+
* var serviceCall = kalixClient.patch("/user/" + user.id + "/email", email, User.class);
191+
* return effects().forward(serviceCall);
192+
* }
193+
* }}}
194+
*
195+
* @param uri
196+
* The resource path where the target endpoint will be reached at. Query parameters can be passed in as part of the
197+
* URI but should be encoded if containing special characters.
198+
* @param returnType
199+
* The type returned by the target endpoint
200+
* @tparam R
201+
* Type returned as response from the target endpoint
202+
* @return
203+
* a [[kalix.javasdk.DeferredCall]] to be used in forwards and timers or to be executed in place
204+
*/
205+
def patch[R](uri: String, returnType: Class[R]): DeferredCall[Any, R]
206+
135207
/**
136208
* Provides utility to do a DELETE HTTP request to a target endpoint belonging to a Kalix component. Such endpoint is
137209
* identified by a resource path (i.e. excluding authority and scheme).
138210
*
139211
* Example of use:
140212
* {{{
141213
* public Effect<String> deleteUser(@RequestBody String userId) {
142-
* var serviceCall = kalixClient.delete("/user/"+userId, "", String.class);
214+
* var serviceCall = kalixClient.delete("/user/"+userId, String.class);
143215
* return effects().forward(serviceCall);
144216
* }
145217
* }}}
146218
*
147219
* @param uri
148220
* The resource path where the target endpoint will be reached at. Query parameters can be passed in as part of the
149221
* URI but should be encoded if containing special characters.
150-
* @param body
151-
* The HTTP body type expected by the target endpoint
152222
* @param returnType
153223
* The type returned by the target endpoint
154-
* @tparam P
155-
* Type used as a body for the request
156224
* @tparam R
157225
* Type returned as response from the target endpoint
158226
* @return
159227
* a [[kalix.javasdk.DeferredCall]] to be used in forwards and timers or to be executed in place
160228
*/
161-
def delete[P, R](uri: String, body: P, returnType: Class[R]): DeferredCall[Any, R]
229+
def delete[R](uri: String, returnType: Class[R]): DeferredCall[Any, R]
162230
}

sdk/spring-sdk/src/main/scala/kalix/springsdk/impl/RestKalixClientImpl.scala

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,24 @@ final class RestKalixClientImpl(messageCodec: SpringSdkMessageCodec) extends Kal
149149
}
150150
}
151151

152+
override def post[R](uriStr: String, returnType: Class[R]): DeferredCall[Any, R] = {
153+
matchMethodOrThrow(HttpMethods.POST, uriStr) { httpDef =>
154+
requestToRestDefCall(
155+
uriStr,
156+
None,
157+
httpDef,
158+
() =>
159+
webClient.flatMap {
160+
_.post()
161+
.uri(uriStr)
162+
.retrieve()
163+
.bodyToMono(returnType)
164+
.toFuture
165+
.asScala
166+
}.asJava)
167+
}
168+
}
169+
152170
override def put[P, R](uriStr: String, body: P, returnType: Class[R]): DeferredCall[Any, R] = {
153171
matchMethodOrThrow(HttpMethods.PUT, uriStr) { httpDef =>
154172
requestToRestDefCall(
@@ -168,6 +186,24 @@ final class RestKalixClientImpl(messageCodec: SpringSdkMessageCodec) extends Kal
168186
}
169187
}
170188

189+
override def put[R](uriStr: String, returnType: Class[R]): DeferredCall[Any, R] = {
190+
matchMethodOrThrow(HttpMethods.PUT, uriStr) { httpDef =>
191+
requestToRestDefCall(
192+
uriStr,
193+
None,
194+
httpDef,
195+
() =>
196+
webClient.flatMap {
197+
_.put()
198+
.uri(uriStr)
199+
.retrieve()
200+
.bodyToMono(returnType)
201+
.toFuture
202+
.asScala
203+
}.asJava)
204+
}
205+
}
206+
171207
override def patch[P, R](uriStr: String, body: P, returnType: Class[R]): DeferredCall[Any, R] = {
172208
matchMethodOrThrow(HttpMethods.PATCH, uriStr) { httpDef =>
173209
requestToRestDefCall(
@@ -187,17 +223,34 @@ final class RestKalixClientImpl(messageCodec: SpringSdkMessageCodec) extends Kal
187223
}
188224
}
189225

190-
override def delete[P, R](uriStr: String, body: P, returnType: Class[R]): DeferredCall[Any, R] = {
226+
override def patch[R](uriStr: String, returnType: Class[R]): DeferredCall[Any, R] = {
227+
matchMethodOrThrow(HttpMethods.PATCH, uriStr) { httpDef =>
228+
requestToRestDefCall(
229+
uriStr,
230+
None,
231+
httpDef,
232+
() =>
233+
webClient.flatMap {
234+
_.patch()
235+
.uri(uriStr)
236+
.retrieve()
237+
.bodyToMono(returnType)
238+
.toFuture
239+
.asScala
240+
}.asJava)
241+
}
242+
}
243+
244+
override def delete[R](uriStr: String, returnType: Class[R]): DeferredCall[Any, R] = {
191245
matchMethodOrThrow(HttpMethods.DELETE, uriStr) { httpDef =>
192246
requestToRestDefCall(
193247
uriStr,
194-
Some(body),
248+
None,
195249
httpDef,
196250
() =>
197251
webClient.flatMap {
198-
_.method(SpringHttpMethod.DELETE)
252+
_.delete()
199253
.uri(uriStr)
200-
.bodyValue(body)
201254
.retrieve()
202255
.bodyToMono(returnType)
203256
.toFuture

0 commit comments

Comments
 (0)