-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #29: moved script into package, requirements
- Loading branch information
1 parent
73807fb
commit 044009e
Showing
5 changed files
with
70 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,4 @@ NACHET_RESOURCE_GROUP= | |
NACHET_WORKSPACE= | ||
NACHET_MODEL= | ||
MICROSCOPE_URL= | ||
METHODS= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import os | ||
import json | ||
import uuid | ||
import logging | ||
import requests | ||
from custom_exceptions import MicroscopeQueryError | ||
from dotenv import load_dotenv | ||
|
||
load_dotenv() | ||
|
||
METHODS = os.getenv("METHODS") | ||
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"OpenApiError: {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: | ||
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 | ||
|
||
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 |
This file was deleted.
Oops, something went wrong.