Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create API consumer #19

Merged
merged 5 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ on:
- "main"
paths:
- "customer-api-provider/**"
- "customer-api-consumer/**"
- ".github/**"
pull_request:
branches:
- "main"
paths:
- "customer-api-provider/**"
- "customer-api-consumer/**"
- ".github/**"

jobs:
Expand All @@ -30,3 +32,6 @@ jobs:
- name: Build API Provider with Maven
working-directory: customer-api-provider
run: mvn -B package --file pom.xml
- name: Build API Consumer with Maven
working-directory: customer-api-consumer
run: mvn -B package --file pom.xml
45 changes: 45 additions & 0 deletions customer-api-consumer/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#Maven
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
release.properties
.flattened-pom.xml

# Eclipse
.project
.classpath
.settings/
bin/

# IntelliJ
.idea
*.ipr
*.iml
*.iws

# NetBeans
nb-configuration.xml

# Visual Studio Code
.vscode
.factorypath

# OSX
.DS_Store

# Vim
*.swp
*.swo

# patch
*.orig
*.rej

# Local environment
.env

# Plugin directory
/.quarkus/cli/plugins/
# TLS Certificates
.certs/
162 changes: 162 additions & 0 deletions customer-api-consumer/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.schulung.quarkus</groupId>
<artifactId>customer-api-consumer</artifactId>
<version>1.0.0-SNAPSHOT</version>

<properties>
<compiler-plugin.version>3.12.1</compiler-plugin.version>
<maven.compiler.release>17</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
<quarkus.platform.group-id>io.quarkus.platform</quarkus.platform.group-id>
<quarkus.platform.version>3.13.3</quarkus.platform.version>
<skipITs>true</skipITs>
<surefire-plugin.version>3.2.5</surefire-plugin.version>
<lombok.version>1.18.32</lombok.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.quarkus.platform</groupId>
<artifactId>quarkus-bom</artifactId>
<version>${quarkus.platform.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-jsonb</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client-jsonb</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-health</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-cache</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-mockito</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compiler-plugin.version}</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>${quarkus.platform.group-id}</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<version>${quarkus.platform.version}</version>
<extensions>true</extensions>
<executions>
<execution>
<goals>
<goal>build</goal>
<goal>generate-code</goal>
<goal>generate-code-tests</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<configuration>
<systemPropertyVariables>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
<configuration>
<systemPropertyVariables>
<native.image.path>${project.build.directory}/${project.build.finalName}-runner
</native.image.path>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>native</id>
<activation>
<property>
<name>native</name>
</property>
</activation>
<properties>
<skipITs>false</skipITs>
<quarkus.native.enabled>true</quarkus.native.enabled>
</properties>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package de.schulung.sample.consumer;

import io.quarkus.runtime.Quarkus;
import io.quarkus.runtime.annotations.QuarkusMain;

@QuarkusMain
public class CustomerConsumerApplication {

public static void main(String[] args) {
Quarkus.run(args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package de.schulung.sample.consumer;

import de.schulung.sample.consumer.client.CustomerApi;
import de.schulung.sample.consumer.client.CustomerDto;
import io.quarkus.cache.CacheInvalidateAll;
import io.quarkus.cache.CacheResult;
import io.smallrye.mutiny.Uni;
import jakarta.enterprise.context.ApplicationScoped;
import lombok.RequiredArgsConstructor;

import java.util.Collection;

@ApplicationScoped
@RequiredArgsConstructor
public class CustomerService {

private final CustomerApi customerApi;

// https://quarkus.io/guides/cache

@CacheResult(cacheName = "customers-api-cache")
public Uni<Collection<CustomerDto>> getAllCustomers() { // quick'n'dirty without mapping!
return customerApi.getAllCustomers();
}

@CacheInvalidateAll(cacheName = "customers-api-cache")
public void reset(){}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package de.schulung.sample.consumer;

import de.schulung.sample.consumer.client.CustomerDto;
import io.smallrye.mutiny.Uni;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import lombok.RequiredArgsConstructor;

import java.util.stream.Collectors;

@RequiredArgsConstructor
@Path("/customers")
public class CustomersResource {

private final CustomerService service;

@GET
@Produces(MediaType.TEXT_PLAIN)
public Uni<String> getCustomerNames() {
return this.service
.getAllCustomers()
.map(list -> list
.stream()
.map(CustomerDto::getName)
.collect(Collectors.joining("\n"))
);
}

@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/reset")
public String reset() {
this.service.reset();
return "Done.";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package de.schulung.sample.consumer.client;

import io.smallrye.mutiny.Uni;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

import java.util.Collection;

@RegisterRestClient(configKey = "customer-api")
@Path("/customers")
public interface CustomerApi {

// https://quarkus.io/guides/rest-client

@GET
@Produces(MediaType.APPLICATION_JSON)
Uni<Collection<CustomerDto>> getAllCustomers();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package de.schulung.sample.consumer.client;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Default;
import jakarta.enterprise.inject.Produces;
import lombok.Getter;
import org.eclipse.microprofile.rest.client.inject.RestClient;

@ApplicationScoped
public class CustomerApiProvider {

@RestClient
@Getter(onMethod_ = {
@Produces,
@Default
})
CustomerApi customerApi;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package de.schulung.sample.consumer.client;

import jakarta.json.bind.annotation.JsonbProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.time.LocalDate;
import java.util.UUID;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class CustomerDto {

private UUID uuid;
private String name;
@JsonbProperty("birthdate")
private LocalDate birthDate;
private String state;

}
10 changes: 10 additions & 0 deletions customer-api-consumer/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
%dev.quarkus.http.port=8081
%dev.quarkus.rest-client."customer-api".url=http://localhost:8080/api/v1
quarkus.rest-client."customer-api".connect-timeout=2000
%dev.quarkus.rest-client."customer-api".read-timeout=2000
quarkus.cache.caffeine."customers-api-cache".metrics-enabled=true
quarkus.cache.caffeine."customers-api-cache".expire-after-write=60S
quarkus.management.port=9091
quarkus.management.enabled=true
%dev.quarkus.management.host=localhost

Loading