Skip to content

Commit

Permalink
Fix WebAPI client example for Java
Browse files Browse the repository at this point in the history
Signed-off-by: Yasumasa Suenaga <[email protected]>
  • Loading branch information
YaSuenag committed Feb 24, 2024
1 parent b9b261c commit 0f4f83d
Show file tree
Hide file tree
Showing 129 changed files with 39 additions and 23,337 deletions.
53 changes: 12 additions & 41 deletions samples/java-client/README.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,19 @@
# Java Client Example

This folder contains an example for WebAPI client in Java. Client library would
be generated dynamically via
[openapi-generator-maven-plugin](https://github.com/OpenAPITools/openapi-generator/tree/master/modules/openapi-generator-maven-plugin),
and call WebAPI endpoints without HTTP code.
This folder contains an example for WebAPI client in Java. Client library would be pulled from [GitHub Packages](https://github.com/orgs/Green-Software-Foundation/packages?repo_name=carbon-aware-sdk).

Javadoc is [here](apidocs).

openapi-generator-maven-plugin generates Maven/Gradle project when it kicks,
however this example uses generated codes directly. So you don't need to
run/modify project files in it.
Javadoc is [here](https://carbon-aware-sdk.greensoftware.foundation/client-apidocs/1.0.0/java).

## Requirements

- OpenAPI spec file
- Both online and offline file are available.
- See [WebAPI document](../../docs/carbon-aware-webapi.md#autogenerate-webapi)
for details.
- WebAPI instance
- See the [Overview](../../docs/overview.md#publish-webapi-with-container)
if you'd like to start it on container.
- See the [Overview](../../docs/overview.md#publish-webapi-with-container) if you'd like to start it on container.
- Java 8 or later
- Maven

## Client code

[WebApiClient.java](src/main/java/foundation/greensoftware/carbonawaresdk/samples/java/WebApiClient.java)
is an example program to call WebAPI endpoint. It calls all of endpoints, and
shows the result.
[WebApiClient.java](src/main/java/example/foundation/greensoftware/carbonawaresdk/WebApiClient.java) is an example program to call WebAPI endpoint. It calls all of endpoints, and shows the result.

Following methods are available:

Expand All @@ -53,19 +39,14 @@ Following methods are available:
- Call /emissions/average-carbon-intensity/batch
- Shows average data for westus yesterday.

[OffsetDateTime](https://docs.oracle.com/javase/8/docs/api/java/time/OffsetDateTime.html)
is used for parameters in each APIs. However the error occurs if nano sec is set
to it in case of WattTime. So it is highly recommended that clears nanosec field
like `withNano(0)`.
[OffsetDateTime](https://docs.oracle.com/javase/8/docs/api/java/time/OffsetDateTime.html) is used for parameters in each APIs. However the error occurs if nano sec is set to it in case of WattTime. So it is highly recommended that clears nanosec field like `withNano(0)`.

## How it works

### 1. Set up for [POM](pom.xml)

You need to change following properties:

- `openapi.spec`
- OpenAPI spec file
- `webapi.endpoint`
- WebAPI base URL

Expand All @@ -83,27 +64,23 @@ $ mvn exec:java

### Running in container

This example also can run in container. You can use
[Maven official image](https://hub.docker.com/_/maven).
This example also can run in container. You can use [Maven official image](https://hub.docker.com/_/maven).

If you want to run both WebAPI and build process in container, you need to join
2 containers to same network.
If you want to run both WebAPI and build process in container, you need to join 2 containers to same network.

Following instructions are for Podman.

#### 1. Create pod

This pod publishes port 80 in the pod to 8080 on the host, then you can access
WebAPI in the pod. The pod is named to `carbon-aware-sdk`.
This pod publishes port 80 in the pod to 8080 on the host, then you can access WebAPI in the pod. The pod is named to `carbon-aware-sdk`.

```sh
podman pod create -p 8080:80 --name carbon-aware-sdk
```

#### 2. Start WebAPI container

Start WebAPI container in `carbon-aware-sdk` pod. It is specified at `--pod`
option.
Start WebAPI container in `carbon-aware-sdk` pod. It is specified at `--pod` option.

See [Overview](../../docs/overview.md) document to build container image.

Expand All @@ -117,13 +94,9 @@ $ podman run -it --rm --pod carbon-aware-sdk \

#### 3. Run Maven in the container

Run `mvn` command in Maven container in `catbon-aware-sdk` pod. You need to
mount Carbon Aware SDK source directory to the container. It mounts to `/src` in
the container in following case.
Run `mvn` command in Maven container in `catbon-aware-sdk` pod. You need to mount Carbon Aware SDK source directory to the container. It mounts to `/src` in the container in following case.

In following command, you can rebuild java-client, and can run the artifact. You
can get artifacts from `samples/java-client/target` on the container host of
course.
In following command, you can rebuild java-client, and can run the artifact. You can get artifacts from `samples/java-client/target` on the container host of course.

```sh
$ podman run -it --rm --pod carbon-aware-sdk \
Expand All @@ -132,6 +105,4 @@ $ podman run -it --rm --pod carbon-aware-sdk \
mvn -f /src/samples/java-client/pom.xml clean package exec:java
```

Maven will download many dependencies in each `mvn` call. You can avoid it when
you mount `.m2` like `-v $HOME/.m2:/root/.m2` because it shares Maven cache
between the host and the container.
Maven will download many dependencies in each `mvn` call. You can avoid it when you mount `.m2` like `-v $HOME/.m2:/root/.m2` because it shares Maven cache between the host and the container.
Loading

0 comments on commit 0f4f83d

Please sign in to comment.