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

Consumo da API 'xml_validation' e Inserção em 'add_sps_data #433

Merged
merged 4 commits into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
49 changes: 49 additions & 0 deletions core/utils/requester.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,52 @@ def post_data(
raise

return response.content if not json else response.json()


@retry(
retry=retry_if_exception_type(RetryableError),
wait=wait_exponential(multiplier=1, min=1, max=5),
stop=stop_after_attempt(5),
)
def fetch_data(url, params=None, headers=None, json=False, timeout=2, verify=True):
"""
Get the resource with HTTP
Retry: Wait 2^x * 1 second between each retry starting with 4 seconds,
then up to 10 seconds, then 10 seconds afterwards
Args:
url: URL address
headers: HTTP headers
json: True|False
verify: Verify the SSL.
Returns:
Return a requests.response object.
Except:
Raise a RetryableError to retry.
"""

try:
logger.info("Fetching the URL: %s" % url)
response = requests.get(url, params=params, headers=headers, timeout=timeout, verify=verify)
except (requests.exceptions.ConnectionError, requests.exceptions.Timeout) as exc:
logger.error("Erro fetching the content: %s, retry..., erro: %s" % (url, exc))
raise RetryableError(exc) from exc
except (
requests.exceptions.InvalidSchema,
requests.exceptions.MissingSchema,
requests.exceptions.InvalidURL,
) as exc:
raise NonRetryableError(exc) from exc
try:
response.raise_for_status()
except requests.HTTPError as exc:
if 400 <= exc.response.status_code < 500:
raise NonRetryableError(exc) from exc
elif 500 <= exc.response.status_code < 600:
logger.error(
"Erro fetching the content: %s, retry..., erro: %s" % (url, exc)
)
raise RetryableError(exc) from exc
else:
raise

return response.content if not json else response.json()
29 changes: 15 additions & 14 deletions upload/xml_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from packtools.sps.validation.preprint import PreprintValidation
from packtools.sps.validation.related_articles import RelatedArticlesValidation

from core.utils.requester import fetch_data
from upload import choices
from upload.models import ValidationResult
from tracker.models import UnexpectedEvent
Expand Down Expand Up @@ -68,22 +69,22 @@ def add_journal_data(data, journal, issue):
data["expected_license_code"] = journal.license_code


def add_sps_data(data, sps_data):
def add_sps_data(data, version, sps_data):
"""
results: [
{
"key": "value",
"value": {"key": "value"}
}
]
"""
# TODO
# depende do SPS / JATS / Critérios
data["dtd_versions"] = []
data["sps_versions"] = []
data["article_types"] = []
data["expected_article_type_vs_subject_similarity"] = 0
data["data_availability_specific_uses"] = []

data["credit_taxonomy"] = []

data["article_type_correspondences"] = []

data["future_date"] = ""
data["events_order"] = []
data["required_events"] = []
url = "https://core.scielo.org/api/v1/xml_validation/"
content = fetch_data(url, params={'version': version}, json=True, timeout=1)
results = content.get("results")
for c in results:
data[c.get("key")] = c.get("value")


def validate_xml_content(sps_pkg_name, xmltree, data):
Expand Down