Skip to content

Commit

Permalink
Make the token warning an error in functions (#6)
Browse files Browse the repository at this point in the history
* Make the token warning an error in functions

* Update tests
  • Loading branch information
eliascarv authored Oct 17, 2024
1 parent 1fde1e2 commit b3101b2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 24 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
28 changes: 14 additions & 14 deletions src/INMET.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
# -----------
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down
25 changes: 17 additions & 8 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit b3101b2

Please sign in to comment.