-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make the token warning an error in functions (#6)
* Make the token warning an error in functions * Update tests
- Loading branch information
Showing
3 changed files
with
32 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,8 +21,7 @@ Pkg.add("INMET") | |
The INMET API requires a token, which can be requested by sending an e-mail | ||
to [[email protected]](mailto:[email protected]). | ||
|
||
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 [[email protected]](mailto:[email protected]). | ||
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 [[email protected]](mailto:[email protected]). | ||
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters