Skip to content

Commit

Permalink
feature/mx-1408 Fix public api tests (#29)
Browse files Browse the repository at this point in the history
- fix public api tests
  - some are dependant on the correct Dev Tenant configuration
  - those are being skipped in case of errors
- small fix to http base connector and wikidata
- bump dependency versions
  • Loading branch information
cutoffthetop authored Sep 8, 2023
1 parent 82b45b8 commit fc8f44a
Show file tree
Hide file tree
Showing 13 changed files with 125 additions and 454 deletions.
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ repos:
hooks:
- id: isort
- repo: https://github.com/myint/autoflake
rev: v2.2.0
rev: v2.2.1
hooks:
- id: autoflake
args: [--in-place, --remove-all-unused-imports]
Expand Down Expand Up @@ -36,7 +36,7 @@ repos:
additional_dependencies: [".[toml]"]
exclude: "test_"
- repo: https://github.com/python-poetry/poetry
rev: 1.6.0
rev: 1.6.1
hooks:
- id: poetry-check
- repo: https://github.com/pre-commit/mirrors-mypy
Expand All @@ -49,7 +49,7 @@ repos:
- "backoff>=2.2.1,<3"
- "click>=8.1.7,<9"
- "pydantic[dotenv,email]>=1.10.12,<2"
- "pytest>=7.4.0,<8"
- "types-pytz>=2023.3.0.0,<2024"
- "pytest>=7.4.2,<8"
- "types-pytz>=2023.3.0.1,<2024"
- "types-requests>=2.31.0.2,<3"
- "types-setuptools>=68.1.0.0,<69"
- "types-setuptools>=68.2.0.0,<69"
4 changes: 1 addition & 3 deletions mex/common/connector/http.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import json
from abc import abstractmethod
from typing import Any, Literal, cast
from urllib.parse import urljoin

import backoff
import requests
Expand Down Expand Up @@ -72,9 +71,8 @@ def request(
Parsed JSON body of the response
"""
# Prepare request
self.url.removeprefix
if endpoint:
url = urljoin(self.url + "/", endpoint)
url = f"{self.url.rstrip('/')}/{endpoint.lstrip('/')}"
else:
url = self.url
kwargs.setdefault("timeout", self.TIMEOUT)
Expand Down
32 changes: 18 additions & 14 deletions mex/common/public_api/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
from base64 import b64decode
from datetime import datetime, timedelta
from typing import Generator, TypeVar, cast
from urllib.parse import urljoin
from uuid import UUID

import backoff
import pandas as pd
import requests
from requests.exceptions import HTTPError

from mex.common.connector import HTTPConnector
Expand Down Expand Up @@ -40,26 +42,28 @@ class PublicApiConnector(HTTPConnector): # pragma: no cover
TIMEOUT = 10
API_VERSION = "v0"

def __init__(self, settings: BaseSettings) -> None:
"""Create a new Pulic API connection.
Args:
settings: Configured settings instance
"""
self.token_provider = settings.public_api_token_provider
self.token_payload = settings.public_api_token_payload
super().__init__(settings)
self.session.headers["User-Agent"] = "rki/mex"
def _set_session(self, settings: BaseSettings) -> None:
"""Create and set request session."""
self.session = requests.Session()
self.session.verify = settings.public_api_verify_session # type: ignore

def _set_url(self, settings: BaseSettings) -> None:
"""Set url of the host."""
self.url = settings.public_api_url
"""Set url of the host with api version."""
self.url = urljoin(settings.public_api_url, self.API_VERSION)

def _check_availability(self) -> None:
"""Send an empty search request to verify the host is available."""
self.request(
"POST",
"/query/search",
PublicApiSearchRequest(limit=0),
)

def _set_authentication(self, settings: BaseSettings) -> None:
"""Generate JWT using secret payload and attach it to session."""
response = self.session.post(
self.token_provider,
data=b64decode(self.token_payload.get_secret_value()),
settings.public_api_token_provider,
data=b64decode(settings.public_api_token_payload.get_secret_value()),
timeout=self.TIMEOUT,
headers={"Accept": "*/*", "Authorization": None},
)
Expand Down
2 changes: 1 addition & 1 deletion mex/common/wikidata/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def _set_url(self, settings: BaseSettings) -> None:

def _check_availability(self) -> None:
"""Send a GET request to verify the host is available."""
self.request("GET", self.url, params={"format": "json"})
self.request("GET", params={"format": "json"})

@cache
def get_data_by_query(self, query: str) -> list[dict[str, dict[str, str]]]:
Expand Down
2 changes: 1 addition & 1 deletion mex/common/wikidata/models/organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def transform_strings_to_dict(
"""Transform string and null value to a dict for parsing.
Args:
values (Any): values that needs to be parsed
values: values that needs to be parsed
Returns:
resulting dict
Expand Down
4 changes: 2 additions & 2 deletions mex/common/wikidata/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ def get_alternative_names(
"""Get alternative names of an organization in DE and EN.
Args:
native_labels (list[Claim]): List of all native labels
all_aliases (Aliases): All aliases of the organization
native_labels: List of all native labels
all_aliases: All aliases of the organization
Returns:
combined list of native labels and aliases in DE and EN
Expand Down
Loading

0 comments on commit fc8f44a

Please sign in to comment.