Skip to content

Commit

Permalink
Added most of the methods from helloworld and profile endpoint - this…
Browse files Browse the repository at this point in the history
… time for real, without tests so far, #48
  • Loading branch information
MarcoHuebner committed Aug 28, 2022
1 parent 3ec97d3 commit f058d7b
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ repos:
hooks:
- id: isort
name: isort (python)
args: ["--profile", "black"]
args: ["--profile", "black", "--filter-files"]
- repo: https://github.com/python-jsonschema/check-jsonschema
rev: 0.14.2
hooks:
Expand Down
50 changes: 50 additions & 0 deletions src/pygenesis/helloworld.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""Module provides wrapper for HelloWorld GENESIS REST-API functions."""

import requests

from pygenesis.config import load_config
from pygenesis.http_helper import _check_invalid_status_code

config = load_config()


# TODO: Write tests
def whoami() -> str:
"""
Wrapper method which constructs an URL for testing the Destatis API
whoami method, which returns host name and IP address.
Returns:
str: text test response from Destatis
"""
url = f"{config['GENESIS API']['base_url']}" + "helloworld/whoami"

response = requests.get(url)

_check_invalid_status_code(response.status_code)

return response.text


def logincheck():
"""
Wrapper method which constructs an URL for testing the Destatis API
logincheck method, which tests the login credentials (from the config.ini).
Returns:
str: text logincheck response from Destatis
"""
url = f"{config['GENESIS API']['base_url']}" + "helloworld/logincheck"

params = {
"username": config["GENESIS API"]["username"],
"password": config["GENESIS API"]["password"],
}

response = requests.get(url, params=params)

# NOTE: Cannot use get_response_from_endpoint due to colliding
# and misleading usage of "Status" key in response
_check_invalid_status_code(response.status_code)

return response.text
1 change: 0 additions & 1 deletion src/pygenesis/http_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ def _check_destatis_status(destatis_status: dict) -> None:
Raises:
DestatisStatusError: If the status code or type displays an error (caused by the user inputs)
"""
print(destatis_status)
# -1 status code for unexpected errors and if no status code is given (faulty response)
destatis_status_code = destatis_status.get("Code", -1)
destatis_status_type = destatis_status.get("Type", "Information")
Expand Down
48 changes: 48 additions & 0 deletions src/pygenesis/profile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""Module provides wrapper for Profile GENESIS REST-API functions."""

from pygenesis.config import (
_write_config,
get_config_path_from_settings,
load_config,
)
from pygenesis.http_helper import get_response_from_endpoint


# TODO: write tests
def password(new_password: str):
"""
Changes Genesis REST-API password and updates local config.
Args:
new_password (str): New password for the Genesis REST-API
Returns:
str: text response from Destatis
"""
params = {
"new": new_password,
"repeat": new_password,
}

# load config.ini beforehand, to ensure passwords are changed at the same time
config = load_config()
try:
config["GENESIS API"]["password"]
except KeyError as e:
raise KeyError(
e,
"Password not found in config! Please make sure \
init_config() was run properly & your user data is set correctly!",
)

# change remote password
response = get_response_from_endpoint("profile", "password", params)
# change local password
config["GENESIS API"]["password"] = new_password
_write_config(config, get_config_path_from_settings())

return response.text


def remove_result():
pass

0 comments on commit f058d7b

Please sign in to comment.