diff options
Diffstat (limited to 'lib/cepis.ml')
| -rw-r--r-- | lib/cepis.ml | 57 |
1 files changed, 51 insertions, 6 deletions
diff --git a/lib/cepis.ml b/lib/cepis.ml index a168d37..be69f2e 100644 --- a/lib/cepis.ml +++ b/lib/cepis.ml @@ -1,4 +1,4 @@ -let version = "0.1.0" +let version = "0.2.0" let default_url = "https://cep.is" let max_batch = 500 @@ -39,21 +39,38 @@ type health = { let is_ok h = String.equal h.status "ok" +type faixa = { + localidade : string; + uf : string; + estado : string; + regiao : string; + ibge : string; + cep_ini : string; + cep_fim : string; + ceps : int; +} + type error = | Invalid_cep of string - | Cep_not_found of string + | Cep_not_found of { message : string; faixa : faixa option } | Rate_limited of { message : string; retry_after : float option } | Server_error of { message : string; status : int } | Network_error of string | Invalid_response of string let error_message = function - | Invalid_cep m | Cep_not_found m | Network_error m | Invalid_response m -> m - | Rate_limited { message; _ } | Server_error { message; _ } -> message + | Invalid_cep m | Network_error m | Invalid_response m -> m + | Cep_not_found { message; _ } + | Rate_limited { message; _ } + | Server_error { message; _ } -> + message let string_of_error = function | Invalid_cep m -> "invalid CEP: " ^ m - | Cep_not_found m -> "CEP not found: " ^ m + | Cep_not_found { message; faixa = None } -> "CEP not found: " ^ message + | Cep_not_found { message; faixa = Some f } -> + Printf.sprintf "CEP not found: %s (that number falls in %s/%s)" message + f.localidade f.uf | Rate_limited { message; retry_after = Some s } -> Printf.sprintf "rate limited (retry after %gs): %s" s message | Rate_limited { message; retry_after = None } -> "rate limited: " ^ message @@ -125,6 +142,24 @@ let geo_of_json = function | _ -> None) | _ -> None +let faixa_of_json : Yojson.Safe.t -> faixa option = function + | `Assoc fields -> ( + match string_field fields "localidade" with + | "" -> None + | localidade -> + Some + { + localidade; + uf = string_field fields "uf"; + estado = string_field fields "estado"; + regiao = string_field fields "regiao"; + ibge = string_field fields "ibge"; + cep_ini = string_field fields "cep_ini"; + cep_fim = string_field fields "cep_fim"; + ceps = int_field fields "ceps"; + }) + | _ -> None + let address_of_fields fields = { cep = string_field fields "cep"; @@ -190,6 +225,12 @@ let api_message body fallback = | _ -> fallback | exception _ -> fallback +let faixa_of_body body = + match Yojson.Safe.from_string body with + | `Assoc fields -> faixa_of_json (field fields "faixa") + | _ -> None + | exception _ -> None + let months = [| "jan"; @@ -481,7 +522,11 @@ module Client = struct if code = 404 then Error (Cep_not_found - (Printf.sprintf "CEP %s does not exist in the DNE database" number)) + { + message = + Printf.sprintf "CEP %s does not exist in the DNE database" number; + faixa = faixa_of_body body; + }) else if code = 400 then Error (Invalid_cep |