-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Adriano Santos
committed
Aug 21, 2024
1 parent
8e8172d
commit 278af03
Showing
5 changed files
with
77 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 2 additions & 58 deletions
60
...{{cookiecutter.app_name}}/src/main/java/io/eigr/spawn/java/service/PostalCodeService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,5 @@ | ||
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 PostalCodeService { | ||
private static final Logger log = LoggerFactory.getLogger(PostalCodeService.class); | ||
|
||
private static final String CEP_SERVICE_URL = "https://viacep.com.br/ws/%s/json/"; | ||
|
||
private final OkHttpClient client; | ||
|
||
public PostalCodeService() { | ||
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(); | ||
} | ||
|
||
public Map<String, String> 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<Map<String, String>>() { | ||
}.getType(); | ||
Map<String, String> 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<String, String> find(String postalCode); | ||
} |
62 changes: 62 additions & 0 deletions
62
...std/{{cookiecutter.app_name}}/src/main/java/io/eigr/spawn/java/service/ViaCepService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, String> 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<Map<String, String>>() { | ||
}.getType(); | ||
Map<String, String> 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<>(); | ||
} | ||
} |