diff --git a/README.md b/README.md index 2922a35..4d303f5 100644 --- a/README.md +++ b/README.md @@ -21,8 +21,7 @@ Pkg.add("INMET") The INMET API requires a token, which can be requested by sending an e-mail to [cadastro.act@inmet.gov.br](mailto:cadastro.act@inmet.gov.br). -Save the token in an environment variable `INMET_TOKEN` to conclude -the installation. +Save the token in an environment variable `INMET_TOKEN` to use this package. ## Usage diff --git a/src/INMET.jl b/src/INMET.jl index 54aa8a4..a672086 100644 --- a/src/INMET.jl +++ b/src/INMET.jl @@ -11,18 +11,6 @@ import Unitful: °, m, °C, percent export Date, DateTime -function __init__() - if !haskey(ENV, "INMET_TOKEN") - @warn """ - The INMET API requires a token, which can be requested by sending an e-mail - to [cadastro.act@inmet.gov.br](mailto:cadastro.act@inmet.gov.br). - - Save the token in an environment variable `INMET_TOKEN` to conclude - the installation. - """ - end -end - # ----------- # PUBLIC API # ----------- @@ -58,7 +46,7 @@ function series(station, start, finish, freq=:day) kind = freq == :day ? "diaria" : "" from = string(start) to = string(finish) - token = ENV["INMET_TOKEN"] + token = inmettoken() url = "https://apitempo.inmet.gov.br/token/estacao/$kind/$from/$to/$station/$token" url |> download |> frame end @@ -74,7 +62,7 @@ hour information is retained (data in hourly frequency). function on(time) date = string(Date(time)) hour = time isa DateTime ? (@sprintf "%02d00" Dates.hour(time)) : "0000" - token = ENV["INMET_TOKEN"] + token = inmettoken() url = "https://apitempo.inmet.gov.br/token/estacao/dados/$date/$hour/$token" url |> download |> frame end @@ -83,6 +71,18 @@ end # HELPER FUNCTIONS # ----------------- +function inmettoken() + if !haskey(ENV, "INMET_TOKEN") + throw(ErrorException(""" + The INMET API requires a token, which can be requested by sending an e-mail + to [cadastro.act@inmet.gov.br](mailto:cadastro.act@inmet.gov.br). + + Save the token in an environment variable `INMET_TOKEN` to use this package. + """)) + end + ENV["INMET_TOKEN"] +end + function download(url) page = HTTP.get(url) str = String(page.body) diff --git a/test/runtests.jl b/test/runtests.jl index 3a0da8e..2b24a6e 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -13,16 +13,25 @@ using Test end @testset "series" begin - df = INMET.series(:A301, Date(2021,1,1), Date(2021,7,31)) - @test all(isequal("A301"), df.CD_ESTACAO) + if haskey(ENV, "INMET_TOKEN") + df = INMET.series(:A301, Date(2021,1,1), Date(2021,7,31)) + @test all(isequal("A301"), df.CD_ESTACAO) + else + @test_throws ErrorException INMET.series(:A301, Date(2021,1,1), Date(2021,7,31)) + end end @testset "on" begin - dfday = INMET.on(Date(2021,7,1)) - dfhour = INMET.on(DateTime(2021,7,1,1)) - @test size(dfday, 1) > size(dfhour, 1) - @test all(isequal("2021-07-01"), dfday.DT_MEDICAO) - @test all(isequal("2021-07-01"), dfhour.DT_MEDICAO) - @test all(isequal("0100"), dfhour.HR_MEDICAO) + if haskey(ENV, "INMET_TOKEN") + dfday = INMET.on(Date(2021,7,1)) + dfhour = INMET.on(DateTime(2021,7,1,1)) + @test size(dfday, 1) > size(dfhour, 1) + @test all(isequal("2021-07-01"), dfday.DT_MEDICAO) + @test all(isequal("2021-07-01"), dfhour.DT_MEDICAO) + @test all(isequal("0100"), dfhour.HR_MEDICAO) + else + @test_throws ErrorException INMET.on(Date(2021,7,1)) + @test_throws ErrorException INMET.on(Date(2021,7,1)) + end end end