Build a Sample Java API Client App
Follow the instructions below to generate a sample Java API client application that you can build with Apache Maven and include as a dependency in your own project.
Prerequisites
Before you start, make sure that you have installed the following:
Generate the Client App
To run all the commands in the procedure below, all you need is your favorite command-line tool like Linux console or Windows Command Prompt.
To generate the client app:
-
Use the commands below to create an empty folder:
mkdir /tmp/example-client
mkdir C:\tmp\example-client
and move to the new folder: -
Use the OLP CLI command below to get your access token:
olp api token get
An output similar to the one below is generated if your credentials have been configured properly:
Token: eyJhbGciOiJSUzUxMiIsImN0eSI6IkpX...qrMg5Nj1woORNHaigA
Token Expires at: 2019-08-01T14:03:48.100Z
-
Use the OLP CLI command below to list all the location services that you have access to:
olp service list
The output looks similar to the one below:
name version HRN
Location Service Demo 1.0.0 hrn:here:service::olp-here:location-service-demo-1
Rendering - Vector Tiles 2.0.0 hrn:here:service::olp-here:rendering-vector-tiles-2
Routing - Intermodal 1.0.0 hrn:here:service::olp-here:routing-intermodal-1
Routing 1.0.0 hrn:here:service::olp-here:routing-1
From the above list, you need the HRN of the HERE Location Service Demo service to get its OpenAPI specification. HERE provides this location service for demonstration purposes only.
-
Use the access token and the service HRN obtained from the previous steps to download the OpenAPI specification of the HERE Location Service Demo service:
curl https://registry.services.api.platform.here.com/v1/services/hrn:here:service::olp-here:location-service-demo-1/openApi \
-H "Authorization: Bearer <your access token>" \g
-o /tmp/example-client/openapi.yaml
curl https://registry.services.api.platform.here.com/v1/services/hrn:here:service::olp-here:location-service-demo-1/openApi ^
-H "Authorization: Bearer <your access token>" ^
-o C:\tmp\example-client\openapi.yaml
-
Use the OpenAPI generator command below to generate the client application based on the service's specification:
openapi-generator generate \
--generator-name java \
--output /tmp/example-client \
--input-spec /tmp/example-client/openapi.yaml \
--api-package com.here.ols.example.client.api \
--model-package com.here.ols.example.client.model \
--invoker-package com.here.ols.example.invoker \
--group-id com.here.ols.example \
--artifact-id example-client
openapi-generator generate ^
--generator-name java ^
--output C:\tmp\example-client ^
--input-spec C:\tmp\example-client\openapi.yaml ^
--api-package com.here.ols.example.client.api ^
--model-package com.here.ols.example.client.model ^
--invoker-package com.here.ols.example.invoker ^
--group-id com.here.ols.example ^
--artifact-id example-client
The resulting file structure looks like the one below:
ls
build.gradle git_push.sh* gradlew* pom.xml src/
build.sbt gradle/ gradlew.bat README.md
docs/ gradle.properties openapi.yaml settings.gradle
dir
build.gradle git_push.sh gradlew pom.xml src
build.sbt gradle gradlew.bat README.md
docs gradle.properties openapi.yaml settings.gradle
For a full description of all the parameters that you can pass to the generate
command, use the openapi-generator help generate
command or see the OpenAPI Generator documentation.
-
From your command-line tool, create a simple main class that calls the /v1/pedestrianroutes
endpoint and writes the result to the standard output:
cat >> /tmp/example-client/src/main/java/com/here/ols/example/Example.java << EOF
package com.here.ols.example;
import com.here.ols.example.client.api.ServiceApi;
import com.here.ols.example.invoker.ApiClient;
import com.here.ols.example.invoker.ApiException;
public class Example {
public static void main(String... args) throws ApiException {
ServiceApi serviceApi = new ServiceApi();
ApiClient apiClient = new ApiClient();
apiClient.setBasePath(args[0]);
apiClient.addDefaultHeader("Authorization", "Bearer " + args[1]);
serviceApi.setApiClient(apiClient);
System.out.println(serviceApi.getPedestrianRoutes());
}
}
EOF
copy con C:\tmp\example-client\src\main\java\com\here\ols\example\Example.java
package com.here.ols.example;
import com.here.ols.example.client.api.ServiceApi;
import com.here.ols.example.invoker.ApiClient;
import com.here.ols.example.invoker.ApiException;
public class Example {
public static void main(String... args) throws ApiException {
ServiceApi serviceApi = new ServiceApi();
ApiClient apiClient = new ApiClient();
apiClient.setBasePath(args[0]);
apiClient.addDefaultHeader("Authorization", "Bearer " + args[1]);
serviceApi.setApiClient(apiClient);
System.out.println(serviceApi.getPedestrianRoutes());
}
}
This main class consumes two parameters:
- The service base URL
- Your access token
-
Build your client application with Maven in your project:
mvn clean package
As a result, you end up with the sample client .jar
file.
-
Use the command below to run the example client app:
java -classpath "target/example-client-1.0.0.jar;target/lib/*" com.here.ols.example.Example \
https://location-service-demo.services.api.platform.here.com \
<your access token>
java -classpath "C:\tmp\example-client\target\example-client-1.0.0.jar;target\lib\*" com.here.ols.example.Example ^
https://location-service-demo.services.api.platform.here.com ^
<your access token>
The command displays the following output:
{"type":"FeatureCollection","features":[{"type":"Feature","properties":null,"geometry":{"type":"LineString","coordinates":[[30.50373315811157,50.4742767962409],[30.50529956817627,50.47310234931454],[30.503690242767334,50.47196884379929],[30.506608486175537,50.470248048543745],[30.50472021102905,50.468977897586086],[30.506522655487057,50.46829500662366],[30.506179332733154,50.4681037953868]]}}]}