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

Issue #29: Module that retrieves microscope config from Tagarno Open API #44

Closed
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
2 changes: 2 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ NACHET_SUBSCRIPTION_ID=
NACHET_RESOURCE_GROUP=
NACHET_WORKSPACE=
NACHET_MODEL=
MICROSCOPE_URL=
METHODS=
4 changes: 4 additions & 0 deletions custom_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,7 @@ class ValidateEnvVariablesError(Exception):

class ServerError(Exception):
pass


class MicroscopeQueryError(Exception):
pass
Empty file added microscope/__init__.py
Empty file.
67 changes: 67 additions & 0 deletions microscope/microscope_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import os
import json
import uuid
import logging
import requests
from dotenv import load_dotenv
from custom_exceptions import MicroscopeQueryError

load_dotenv()

methods_str = os.getenv("METHODS", "[]")
METHODS = json.loads(methods_str)
MICROSCOPE_URL = os.getenv("MICROSCOPE_URL")
params = {"id": int(uuid.uuid4())}
HEADERS = {'Content-Type': 'application/json'}

def post_request(MICROSCOPE_URL, method, params, headers=HEADERS):
url = f"{MICROSCOPE_URL}?jsonrpc=2.0&method={method}&id={params['id']}"

data = json.dumps({
"jsonrpc": "2.0",
"method": method,
"id": params['id'],
})

try:
resp = requests.post(url, data=data, headers=headers)
resp.raise_for_status()
return resp.json()
except requests.RequestException as e:
logging.error(f"Request Error: {e}")
raise MicroscopeQueryError(f"MicroscopeQueryError: {e}") from e

def is_hex(s):
try:
int(s, 16)
return True
except ValueError:
return False

def get_microscope_configuration(METHODS):
config = {}
for method in METHODS:
try:
resp = post_request(MICROSCOPE_URL, method, params, HEADERS)
result = resp["result"]

# Check if the response is in hexadecimal and convert it
if isinstance(result, str) and is_hex(result):
result = int(result, 16)

config[method] = result

except MicroscopeQueryError as mqe:
config[method] = None
logging.error(f"MicroscopeQueryError: {mqe}")

return config


if __name__ == "__main__":
try:
config = get_microscope_configuration(METHODS)
if config:
print(config)
except requests.RequestException as e:
raise MicroscopeQueryError(f"OpenApiError: {e}") from e
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ quart
quart-cors
python-dotenv
hypercorn
requests
Empty file added tests/test_microscope_info.py
Empty file.
Loading