Skip to content

Commit

Permalink
feat: added java std template
Browse files Browse the repository at this point in the history
  • Loading branch information
sleipnir committed Aug 19, 2024
1 parent fbf84fa commit f7a0224
Showing 1 changed file with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
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() {
Expand All @@ -31,20 +33,22 @@ 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("https://viacep.com.br/ws/%s/json/", postalCode))
.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()) {
if (response.isSuccessful()) {
String responseJson = response.body().string();
Type type = new TypeToken<Map<String, String>>() { }.getType();
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");
if (!map.containsKey("erro")) {
if (!map.containsKey("pais"))
map.put("pais", "Brasil");
return map;
}
}
Expand Down

0 comments on commit f7a0224

Please sign in to comment.