Lagom 1.2: how to invoke service calls with headers

798 views
Skip to first unread message

sallad...@gmail.com

unread,
Nov 23, 2016, 9:15:27 AM11/23/16
to Lagom Framework Users
Hello,

I am currently using the Lagom Message Broker API and I want to do some treatments (=> emit service calls). My problem is that the service I want to call is a HeaderServiceCall, which needs to be invoked with headers. Is there a pattern, a simple method to do this? Here is my implementation:

organizationService.organizationsTopic().subscribe().atLeastOnce(Flow.fromFunction[OrganizationMessage, Done] {
case x: OrganizationCreatedMessage =>
x.operatorId.fold[Done] {
throw new RuntimeException("Operator doesn't exist")
} { realOperatorId => {
val addOrganizationCall = addOrganization(realOperatorId).invoke(
AddOrganizationRequest(
organizationId = x.entityId
))
val addPermissionsCall = addPermissions(realOperatorId).invoke(
AddPermissionsRequest(
permissions = Set(Permission(x.entityId, EntityType.ORGANIZATION, Action.values))
++ Set(Permission(x.entityId, EntityType.USER, Action.values)
)
))
for {
_ <- addOrganizationCall
_ <- addPermissionsCall
} yield Done
Done
}
}
})

Tim Moore

unread,
Nov 23, 2016, 8:35:34 PM11/23/16
to sallad...@gmail.com, Lagom Framework Users
Hi,

You can call handleRequestHeader on the ServiceCall before calling invoke. It takes a function that transforms the request, and you can add headers, etc. in that function.

Here is the Javadoc with more details:


This method is useful when the content of the header depend on the context of the request.

However, for services that always require a particular header to be added, it can be better to use a HeaderFilter, which can be declared on the service descriptor and automatically applied on both the client and server side. This is good for things such as authentication, where the headers can be derived from the Principal already available in the request.


You can see an example of this in use in https://github.com/lagom/online-auction-java

This filter adds the current user ID to a header on the client request and extracts it on the server:

And these service descriptors apply the filter:



It looks like we haven't covered this in the docs yet, so I added an issue to follow that up: https://github.com/lagom/lagom/issues/286

Cheers,
Tim Moore


--
You received this message because you are subscribed to the Google Groups "Lagom Framework Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lagom-framework+unsubscribe@googlegroups.com.
To post to this group, send email to lagom-framework@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lagom-framework/980e87ef-987a-4279-9393-6cf652fe0744%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Tim Moore
Senior Engineer, Lagom, Lightbend, Inc.


Reply all
Reply to author
Forward
0 new messages