14
14
public final class PostalCodeService {
15
15
private static final Logger log = LoggerFactory .getLogger (PostalCodeService .class );
16
16
17
+ private static final String CEP_SERVICE_URL = "https://viacep.com.br/ws/%s/json/" ;
18
+
17
19
private final OkHttpClient client ;
18
20
19
21
public PostalCodeService () {
@@ -31,20 +33,22 @@ public Map<String, String> find(String postalCode) {
31
33
log .debug ("Looking for Brazil PostalCode '{}'." , postalCode );
32
34
// Massive use may block your access indefinitely.
33
35
Request request = new Request .Builder ()
34
- .url (String .format ("https://viacep.com.br/ws/%s/json/" , postalCode ))
36
+ .url (String .format (CEP_SERVICE_URL , postalCode ))
35
37
.get ()
36
38
.build ();
37
39
38
40
Call call = client .newCall (request );
39
41
try (Response response = call .execute ()) {
40
42
assert response .body () != null ;
41
- if (response .isSuccessful ()) {
43
+ if (response .isSuccessful ()) {
42
44
String responseJson = response .body ().string ();
43
- Type type = new TypeToken <Map <String , String >>() { }.getType ();
45
+ Type type = new TypeToken <Map <String , String >>() {
46
+ }.getType ();
44
47
Map <String , String > map = new Gson ().fromJson (responseJson , type );
45
48
log .debug ("PostalCode response '{}'." , map );
46
- if (!map .containsKey ("erro" )) {
47
- if (!map .containsKey ("pais" )) map .put ("pais" , "Brasil" );
49
+ if (!map .containsKey ("erro" )) {
50
+ if (!map .containsKey ("pais" ))
51
+ map .put ("pais" , "Brasil" );
48
52
return map ;
49
53
}
50
54
}
0 commit comments