What is the simplest way to consume an external REST service in Lagom?
Asked Answered
N

3

7

According to Lagom documentation, we can define external service URI (like below) and can get it from ServiceLocator.

lagomUnmanagedServices in ThisBuild := Map("weather" -> "http://localhost:3333")

http://www.lagomframework.com/documentation/1.0.x/ServiceLocator.html#Communicating-with-external-services

What is the simplest way to call the external REST API in Lagom?

I considered using WsClient in Lagom, but I didn't choose it. Lagom includes only WsClient for Scala, therefore it provides result values as not java.util.concurrent.CompletionStage but scala.concurrent.Future. It makes the pain to combine with other Lagom APIs like CompletionStage#doWithService.

Nathan answered 29/5, 2016 at 9:33 Comment(2)
That's a really good question. +1Teresaterese
Here's a sample java project (not a lagom project) that uses Lagom's libraries (not the the whole framework) to consume third party API's: github.com/ignasi35/lagom-client-demoThayne
T
2

A way to consume 3rd party REST services from lagom is by writing the 3rd party's REST spec using a Lagom Descriptor.

Imagine your code wanted to interact with Slack's API, you would create a slack-api project in your app and create the Slack descriptor there (you wouldn't need to create a slack-implof course).

Then, on your fancy-impl code you would depend on slack-api and in your FancyServiceImpl implementation you would inject SlackService in the constructor.

PS: The gist is scala code, but same idea applies to Lagom's Java DSL.

Thayne answered 19/1, 2017 at 22:53 Comment(1)
Thank you for your answer! I'll try it.Nathan
D
1

As none of the previous answers contain full information on how to implement it in Java, here's how I did it:

  1. Create Lagom API module, e.g. external-service-api and put request/response DTOs and Lagom service in it, e.g.:

    public interface ExternalService extends Service {
    
        ServiceCall<ExternalServiceRequest, ExternalServicePostResponse> somePostMethod();
    
        ServiceCall<NotUsed, ExternalServiceGetResponse> someGetMethod();
    
        @Override
        default Descriptor descriptor(){
            return Service.named("external-service").withCalls(
                Service.pathCall("/post-endpoint", this::somePostMethod),
                Service.pathCall("/get-endpoint", this::someGetMethod)
            ).withAutoAcl(true);
        }
    }
    
  2. Add dependency to your external-service-api in impl module where you want to use it.

  3. Register your service in *Module.java. Use: bindClient(ExternalService.class);

  4. Now the tricky part, I found some tutorials/repositories in which unmanaged service was defined in pom.xml in implementation module. It didn't work for me, and I don't know why (Lagom v. 1.4.5, Scala binaries 2.12). What I had to do, was to put unmanagedServices definitions in projects root pom.xml. Take notice that unmanagedService child element should be named same as you defined in descriptor.

        <plugin>
            <groupId>com.lightbend.lagom</groupId>
            <artifactId>lagom-maven-plugin</artifactId>
            <version>${lagom.version}</version>
            <configuration>
                <unmanagedServices>
                    <external-service>http://api.url.com/</external-service>
                </unmanagedServices>
            </configuration>
        </plugin>
    
  5. Inject your service:

    @Inject
    public SomeConstructor(ExternalService externalService){
        ...
    }
    
  6. To use it call e.g.: externalService.somePostMethod().invoke(new ExternalServiceRequest(...))
Dachau answered 16/1, 2019 at 1:47 Comment(0)
C
0

In Lagom, any 3rd party application/service can be accessed as an unmanaged service. You would need to add that in your application.conf like this

#external-services lagom.services { 3rd-party-service-identifier = "http://3rd-party-service-url" ... } and would also need to add the 3rd party service url as an unmanagedService in the pom.xml like this

<plugin>
    <groupId>com.lightbend.lagom</groupId>
    <artifactId>lagom-maven-plugin</artifactId>
    <version>${lagom.version}</version>
    <configuration>
      <kafkaEnabled>false</kafkaEnabled>
      <cassandraEnabled>false</cassandraEnabled>
      <unmanagedServices>
        <3rd-party-service-identifier>${3rd-party-service-URL}</3rd-party-service-identifier>
      </unmanagedServices>
    </configuration>
  </plugin>

this is how you let Lagom know about the 3rd party service, but to make lagom to work with the service please go to the link below. https://www.lagomframework.com/documentation/1.4.x/java/IntegratingNonLagom.html

Coniine answered 6/6, 2018 at 12:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.