Skip to content

Commit

Permalink
Fixes #29: moved script into package, requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
RussellJimmies authored and Maxence Guindon committed Apr 25, 2024
1 parent 73807fb commit 044009e
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 66 deletions.
1 change: 1 addition & 0 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ NACHET_RESOURCE_GROUP=
NACHET_WORKSPACE=
NACHET_MODEL=
MICROSCOPE_URL=
METHODS=
8 changes: 8 additions & 0 deletions custom_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,11 @@ class ProcessInferenceResultError(Exception):

class ValidateEnvVariablesError(Exception):
pass


class ServerError(Exception):
pass


class MicroscopeQueryError(Exception):
pass
Empty file added microscope/__init__.py
Empty file.
61 changes: 61 additions & 0 deletions microscope/microscope_info.py
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
66 changes: 0 additions & 66 deletions microscope_info.py

This file was deleted.

0 comments on commit 044009e

Please sign in to comment.