From 278af035ef562be1b3c4bfbfde79c42ae4fc38e5 Mon Sep 17 00:00:00 2001 From: Adriano Santos Date: Wed, 21 Aug 2024 16:10:55 -0300 Subject: [PATCH] refactor: extract interface --- .../src/main/java/io/eigr/spawn/java/App.java | 4 +- .../spawn/java/actors/PostalCodeActor.java | 12 ++-- .../eigr/spawn/java/actors/RouterActor.java | 8 +-- .../spawn/java/service/PostalCodeService.java | 60 +----------------- .../spawn/java/service/ViaCepService.java | 62 +++++++++++++++++++ 5 files changed, 77 insertions(+), 69 deletions(-) create mode 100644 java-std/{{cookiecutter.app_name}}/src/main/java/io/eigr/spawn/java/service/ViaCepService.java diff --git a/java-std/{{cookiecutter.app_name}}/src/main/java/io/eigr/spawn/java/App.java b/java-std/{{cookiecutter.app_name}}/src/main/java/io/eigr/spawn/java/App.java index e9611d4..478d77c 100644 --- a/java-std/{{cookiecutter.app_name}}/src/main/java/io/eigr/spawn/java/App.java +++ b/java-std/{{cookiecutter.app_name}}/src/main/java/io/eigr/spawn/java/App.java @@ -9,6 +9,7 @@ import io.eigr.spawn.java.actors.PostalCodeActor; import io.eigr.spawn.java.actors.RouterActor; import io.eigr.spawn.java.service.PostalCodeService; +import io.eigr.spawn.java.service.ViaCepService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -22,7 +23,7 @@ public static void main(String[] args) throws SpawnException { Config cfg = Config.createDefaultConfig(); DependencyInjector dependencyInjector = SimpleDependencyInjector.createInjector(); - dependencyInjector.bind(PostalCodeService.class, new PostalCodeService()); + dependencyInjector.bind(PostalCodeService.class, new ViaCepService()); Spawn spawnSystem = new Spawn.SpawnSystem() .create(cfg.spawnSystemName, dependencyInjector) @@ -82,3 +83,4 @@ public String toString() { } } } + diff --git a/java-std/{{cookiecutter.app_name}}/src/main/java/io/eigr/spawn/java/actors/PostalCodeActor.java b/java-std/{{cookiecutter.app_name}}/src/main/java/io/eigr/spawn/java/actors/PostalCodeActor.java index e48cee7..030f724 100644 --- a/java-std/{{cookiecutter.app_name}}/src/main/java/io/eigr/spawn/java/actors/PostalCodeActor.java +++ b/java-std/{{cookiecutter.app_name}}/src/main/java/io/eigr/spawn/java/actors/PostalCodeActor.java @@ -20,7 +20,7 @@ import static io.eigr.spawn.api.actors.behaviors.ActorBehavior.action; import static io.eigr.spawn.api.actors.behaviors.ActorBehavior.name; -public class PostalCodeActor implements StatefulActor { +public final class PostalCodeActor implements StatefulActor { private static final Logger log = LoggerFactory.getLogger(PostalCodeActor.class); private PostalCodeService postalCodeService; @@ -30,8 +30,7 @@ public ActorBehavior configure(BehaviorCtx behaviorCtx) { this.postalCodeService = behaviorCtx.getInjector().getInstance(PostalCodeService.class); return new UnNamedActorBehavior( name("PostalCode"), - action("GetPostalCodeData", ActionBindings.of(DomainProto.GetRequest.class, this::getPostalCodeData)) - ); + action("GetPostalCodeData", ActionBindings.of(DomainProto.GetRequest.class, this::getPostalCodeData))); } private Value getPostalCodeData(ActorContext context, DomainProto.GetRequest msg) { @@ -49,7 +48,8 @@ private Value getPostalCodeData(ActorContext contex // If postal code data found, build state and response if (!postalCodeData.isEmpty()) { - DomainProto.PostalCodeState newState = buildPostalCodeState(msg.getCode(), postalCodeData, DomainProto.PostalCodeStatus.FOUND); + DomainProto.PostalCodeState newState = buildPostalCodeState(msg.getCode(), postalCodeData, + DomainProto.PostalCodeStatus.FOUND); return createValueWithStateAndResponse(newState); } @@ -67,7 +67,8 @@ private Value createValueWithStateAndResponse(DomainProto.PostalCodeState state) return Value.at().state(state).response(response).reply(); } - private DomainProto.PostalCodeState buildPostalCodeState(String code, Map data, DomainProto.PostalCodeStatus status) { + private DomainProto.PostalCodeState buildPostalCodeState(String code, Map data, + DomainProto.PostalCodeStatus status) { return DomainProto.PostalCodeState.newBuilder() .setCode(code) .setCity(data.get("localidade")) @@ -79,4 +80,3 @@ private DomainProto.PostalCodeState buildPostalCodeState(String code, Map find(String postalCode) { - log.debug("Looking for Brazil PostalCode '{}'.", postalCode); - // Massive use may block your access indefinitely. - Request request = new Request.Builder() - .url(String.format(CEP_SERVICE_URL, postalCode)) - .get() - .build(); - - Call call = client.newCall(request); - try (Response response = call.execute()) { - assert response.body() != null; - if (response.isSuccessful()) { - String responseJson = response.body().string(); - Type type = new TypeToken>() { - }.getType(); - Map map = new Gson().fromJson(responseJson, type); - log.debug("PostalCode response '{}'.", map); - if (!map.containsKey("erro")) { - if (!map.containsKey("pais")) - map.put("pais", "Brasil"); - return map; - } - } - } catch (Exception err) { - log.error("Error while load postalcode.", err); - } - log.warn("postalcode '{}' not found.", postalCode); - return new HashMap<>(); - } +public interface PostalCodeService { + Map find(String postalCode); } diff --git a/java-std/{{cookiecutter.app_name}}/src/main/java/io/eigr/spawn/java/service/ViaCepService.java b/java-std/{{cookiecutter.app_name}}/src/main/java/io/eigr/spawn/java/service/ViaCepService.java new file mode 100644 index 0000000..97f351f --- /dev/null +++ b/java-std/{{cookiecutter.app_name}}/src/main/java/io/eigr/spawn/java/service/ViaCepService.java @@ -0,0 +1,62 @@ +package io.eigr.spawn.java.service; + +import com.google.common.reflect.TypeToken; +import com.google.gson.Gson; +import okhttp3.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.lang.reflect.Type; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +public final class ViaCepService implements PostalCodeService { + private static final Logger log = LoggerFactory.getLogger(ViaCepService.class); + + private static final String CEP_SERVICE_URL = "https://viacep.com.br/ws/%s/json/"; + + private final OkHttpClient client; + + public ViaCepService() { + this.client = new OkHttpClient.Builder() + .connectTimeout(120, TimeUnit.SECONDS) + .readTimeout(120, TimeUnit.SECONDS) + .writeTimeout(120, TimeUnit.SECONDS) + .callTimeout(400, TimeUnit.SECONDS) + .retryOnConnectionFailure(true) + .connectionPool(new ConnectionPool(256, 100, TimeUnit.SECONDS)) + .build(); + } + + @Override + public Map find(String postalCode) { + log.debug("Looking for Brazil PostalCode '{}'.", postalCode); + // Massive use may block your access indefinitely. + Request request = new Request.Builder() + .url(String.format(CEP_SERVICE_URL, postalCode)) + .get() + .build(); + + Call call = client.newCall(request); + try (Response response = call.execute()) { + assert response.body() != null; + if (response.isSuccessful()) { + String responseJson = response.body().string(); + Type type = new TypeToken>() { + }.getType(); + Map map = new Gson().fromJson(responseJson, type); + log.debug("PostalCode response '{}'.", map); + if (!map.containsKey("erro")) { + if (!map.containsKey("pais")) + map.put("pais", "Brasil"); + return map; + } + } + } catch (Exception err) { + log.error("Error while load postalcode.", err); + } + log.warn("postalcode '{}' not found.", postalCode); + return new HashMap<>(); + } +}