1414public final class PostalCodeService {
1515 private static final Logger log = LoggerFactory .getLogger (PostalCodeService .class );
1616
17+ private static final String CEP_SERVICE_URL = "https://viacep.com.br/ws/%s/json/" ;
18+
1719 private final OkHttpClient client ;
1820
1921 public PostalCodeService () {
@@ -31,20 +33,22 @@ public Map<String, String> find(String postalCode) {
3133 log .debug ("Looking for Brazil PostalCode '{}'." , postalCode );
3234 // Massive use may block your access indefinitely.
3335 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 ))
3537 .get ()
3638 .build ();
3739
3840 Call call = client .newCall (request );
3941 try (Response response = call .execute ()) {
4042 assert response .body () != null ;
41- if (response .isSuccessful ()) {
43+ if (response .isSuccessful ()) {
4244 String responseJson = response .body ().string ();
43- Type type = new TypeToken <Map <String , String >>() { }.getType ();
45+ Type type = new TypeToken <Map <String , String >>() {
46+ }.getType ();
4447 Map <String , String > map = new Gson ().fromJson (responseJson , type );
4548 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" );
4852 return map ;
4953 }
5054 }
0 commit comments