Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
MetObsAPI
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
MetObs
MetObsAPI
Commits
fb293ad0
Verified
Commit
fb293ad0
authored
1 year ago
by
David Hoese
Browse files
Options
Downloads
Patches
Plain Diff
Add error message specific to begin/end in the future
parent
20453e08
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
metobsapi/data_api.py
+8
-2
8 additions, 2 deletions
metobsapi/data_api.py
metobsapi/tests/test_data_api.py
+11
-1
11 additions, 1 deletion
metobsapi/tests/test_data_api.py
with
19 additions
and
3 deletions
metobsapi/data_api.py
+
8
−
2
View file @
fb293ad0
...
...
@@ -36,11 +36,16 @@ ROUNDING = {
def
handle_date
(
date
:
str
)
->
datetime
:
datetime_fmt
=
"
%Y-%m-%d
"
if
len
(
date
)
==
LENGTH_DATE_STR
else
"
%Y-%m-%dT%H:%M:%S
"
try
:
return
datetime
.
strptime
(
date
,
datetime_fmt
)
dt
=
datetime
.
strptime
(
date
,
datetime_fmt
)
except
ValueError
as
e
:
msg
=
f
"
Malformed date string
'
{
date
}
'"
LOG
.
warning
(
msg
)
raise
ValueError
(
msg
)
from
e
if
dt
>
(
datetime
.
utcnow
()
+
timedelta
(
minutes
=
1
)):
msg
=
f
"
Date/time is in the future:
{
date
}
"
LOG
.
warning
(
msg
)
raise
ValueError
(
msg
)
return
dt
def
handle_time_string
(
date_string
):
...
...
@@ -392,7 +397,8 @@ def _convert_begin_and_end(begin, end) -> tuple[datetime | timedelta, datetime |
begin
=
handle_time_string
(
begin
)
end
=
handle_time_string
(
end
)
except
(
TypeError
,
ValueError
)
as
e
:
raise
ValueError
(
"
malformed_timestamp
"
)
from
e
msg
=
f
"
malformed_timestamp:
{
str
(
e
)
}
"
raise
ValueError
(
msg
)
from
e
return
begin
,
end
...
...
This diff is collapsed.
Click to expand it.
metobsapi/tests/test_data_api.py
+
11
−
1
View file @
fb293ad0
...
...
@@ -198,13 +198,23 @@ def test_bad_format(client):
def
test_bad_begin_json
(
client
):
res
=
client
.
get
(
"
/api/data.json?symbols=air_temp&begin=blah
"
)
res
=
client
.
get
(
"
/api/data.json?symbols=
aoss.tower.
air_temp&begin=blah
"
)
res
=
json
.
loads
(
res
.
data
.
decode
())
assert
res
[
"
code
"
]
==
400
assert
res
[
"
status
"
]
==
"
error
"
assert
"
timestamp
"
in
res
[
"
message
"
]
def
test_future_begin_json
(
client
):
now
=
datetime
.
utcnow
()
future_begin
=
now
+
timedelta
(
days
=
1
)
res
=
client
.
get
(
f
"
/api/data.json?symbols=aoss.tower.air_temp&begin=
{
future_begin
:
%
Y
-%
m
-%
dT
%
H
:
%
M
:
%
S
}
"
)
res
=
json
.
loads
(
res
.
data
.
decode
())
assert
res
[
"
code
"
]
==
400
assert
res
[
"
status
"
]
==
"
error
"
assert
"
is in the future
"
in
res
[
"
message
"
]
def
test_bad_order
(
client
):
res
=
client
.
get
(
"
/api/data.json?order=blah&symbols=air_temp
"
)
res
=
json
.
loads
(
res
.
data
.
decode
())
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment