Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Input validation to handle 502 Errors #87

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion imap_data_access/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
"l1cb",
"l1d",
"l2",
"l2pre",
"l3",
"l3a",
"l3b",
Expand Down
40 changes: 40 additions & 0 deletions imap_data_access/file_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,46 @@ def is_valid_date(input_date: str) -> bool:
except ValueError:
return False

@staticmethod
def is_valid_version(input_version: str) -> bool:
"""Check input version string is in valid format 'vXXX' or 'latest'.

Parameters
----------
input_version : str
Version to be checked.

Returns
-------
bool
Whether input version is valid or not.
"""
if input_version != "latest" and (
input_version[0] != "v" and len(input_version) != 4
):
return False
else:
return True
daralynnrhode marked this conversation as resolved.
Show resolved Hide resolved

@staticmethod
def is_valid_repointing(input_repointing: str) -> bool:
"""Check input version string is in valid format 'vXXX' or 'latest'.
daralynnrhode marked this conversation as resolved.
Show resolved Hide resolved

Parameters
----------
input_repointing : str
Repointing to be checked.

Returns
-------
bool
Whether input repointing is valid or not.
"""
if not re.fullmatch(r"repoint\d{5}", str(input_repointing)):
return False
else:
return True
daralynnrhode marked this conversation as resolved.
Show resolved Hide resolved

def construct_path(self) -> Path:
"""Construct valid path from class variables and data_dir.

Expand Down
58 changes: 12 additions & 46 deletions imap_data_access/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
import contextlib
import json
import logging
import re
import urllib.request
from datetime import datetime
from pathlib import Path
from typing import Optional, Union
from urllib.error import HTTPError, URLError
from urllib.parse import urlencode

import imap_data_access
from imap_data_access import file_validation

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -165,81 +164,48 @@ def query(
)
# Check instrument name
if instrument is not None:
if instrument not in (
"codice",
"glows",
"hi",
"hit",
"idex",
"lo",
"mag",
"swapi",
"swe",
"ultra",
):
if instrument not in imap_data_access.VALID_INSTRUMENTS:
raise ValueError(
"Not a valid instrument, please choose from "
"('codice', 'glows', 'hi', 'hit', 'idex', "
"'lo', 'mag', 'swapi', 'swe', 'ultra')"
+ ", ".join(imap_data_access.VALID_INSTRUMENTS)
)

# Check data-level
# do an if statement that checks that data_level was passed in,
# then check it against all options, l0, l1a, l1b, l2, l3 etc.
if data_level is not None:
if data_level not in (
"l0",
"l1",
"l1a",
"l1b",
"l1c",
"l1ca",
"l1cb",
"l1d",
"l2",
"l2pre",
"l3",
"l3a",
"l3b",
"l3c",
"l3d",
):
if data_level not in imap_data_access.VALID_DATALEVELS:
raise ValueError(
"Not a valid data level, choose from "
"('l0','l1','l1a','l1b','l1c','l1ca','l1cb',"
"'l1d','l2','l2pre','l3','l3a','l3b','l3c','l3d')."
+ ", ".join(imap_data_access.VALID_DATALEVELS)
)

# Check start-date
if start_date is not None:
daralynnrhode marked this conversation as resolved.
Show resolved Hide resolved
try:
datetime.strptime(start_date, "%Y%m%d")
except ValueError as err:
raise ValueError("Not a valid start date, use format 'YYYYMMDD'.") from err
if not file_validation.ScienceFilePath.is_valid_date(start_date):
raise ValueError("Not a valid start date, use format 'YYYYMMDD'.")

# Check end-date
if end_date is not None:
try:
datetime.strptime(end_date, "%Y%m%d")
except ValueError as err:
raise ValueError("Not a valid end date, use format 'YYYYMMDD'.") from err
if not file_validation.ScienceFilePath.is_valid_date(end_date):
raise ValueError("Not a valid end date, use format 'YYYYMMDD'.")

# Check version make sure to include 'latest'
if version is not None:
daralynnrhode marked this conversation as resolved.
Show resolved Hide resolved
if version != "latest" and (version[0] != "v" and len(version) != 4):
if not file_validation.ScienceFilePath.is_valid_version(version):
raise ValueError("Not a valid version, use format 'vXXX'.")

# check repointing follows 'repoint00000' format
if repointing is not None:
daralynnrhode marked this conversation as resolved.
Show resolved Hide resolved
if not re.fullmatch(r"repoint\d{5}", str(repointing)):
if not file_validation.ScienceFilePath.is_valid_repointing(repointing):
raise ValueError(
"Not a valid repointing, use format repointing<num>,"
" where <num> is a 5 digit integer."
)

# check extension
if extension is not None:
daralynnrhode marked this conversation as resolved.
Show resolved Hide resolved
if extension not in ("pkts", "cdf"):
if extension not in imap_data_access.VALID_FILE_EXTENSION:
raise ValueError("Not a valid extension, choose from ('pkts', 'cdf').")

url = f"{imap_data_access.config['DATA_ACCESS_URL']}"
Expand Down
14 changes: 4 additions & 10 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,20 +226,14 @@ def test_query_bad_params(mock_urlopen: unittest.mock.MagicMock):
(
"instrument",
"badInput",
re.escape(
"Not a valid instrument, please choose from "
"('codice', 'glows', 'hi', 'hit', 'idex', 'lo',"
" 'mag', 'swapi', 'swe', 'ultra')"
),
"Not a valid instrument, please choose from "
+ ", ".join(imap_data_access.VALID_INSTRUMENTS),
daralynnrhode marked this conversation as resolved.
Show resolved Hide resolved
),
(
"data_level",
"badInput",
re.escape(
"Not a valid data level, choose from "
"('l0','l1','l1a','l1b','l1c','l1ca','l1cb','l1d',"
"'l2','l2pre','l3','l3a','l3b','l3c','l3d')."
),
"Not a valid data level, choose from "
+ ", ".join(imap_data_access.VALID_DATALEVELS),
),
("start_date", "badInput", "Not a valid start date, use format 'YYYYMMDD'."),
("end_date", "badInput", "Not a valid end date, use format 'YYYYMMDD'."),
Expand Down
Loading