Skip to content

Commit

Permalink
add logging debug on request/response
Browse files Browse the repository at this point in the history
  • Loading branch information
Zepmanbc committed May 19, 2020
1 parent 81020df commit 7c5eeea
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions creopyson/connection.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
"""Connection module."""
import requests
import json
import logging
from pathlib import Path
from .exceptions import MissingKey, ErrorJsonDecode

lg = logging.getLogger(__name__)


class Client(object):
"""Creates Client object."""

def __init__(self, ip_adress="localhost", port=9056):
"""Create Client objet. Define server and sessionID vars."""
self.server = "http://{}:{}/creoson".format(ip_adress, port)
self.sessionId = ''
self.sessionId = ""

def connect(self):
"""Connect to CREOSON.
Expand Down Expand Up @@ -43,8 +46,9 @@ def _creoson_post(self, command, function, data=None, key_data=None):
"sessionId": self.sessionId,
"command": command,
"function": function,
"data": data
"data": data,
}
lg.debug("request: %s", str(request))
try:
r = requests.post(self.server, data=json.dumps(request))
except requests.exceptions.RequestException as e:
Expand All @@ -55,9 +59,9 @@ def _creoson_post(self, command, function, data=None, key_data=None):

try:
json_result = r.json()
lg.debug("response: %s", str(json_result))
except TypeError:
raise ErrorJsonDecode(
"Cannot decode JSON, creoson result invalid.")
raise ErrorJsonDecode("Cannot decode JSON, creoson result invalid.")

if "status" not in json_result.keys():
raise MissingKey("Missing `status` in creoson result.")
Expand All @@ -70,8 +74,7 @@ def _creoson_post(self, command, function, data=None, key_data=None):
error_msg = json_result["status"]["message"]
raise RuntimeError(error_msg)

if request["command"] == "connection" and\
request["function"] == "connect":
if request["command"] == "connection" and request["function"] == "connect":
if "sessionId" not in json_result.keys():
raise MissingKey("Missing `sessionId` in creoson result.")
else:
Expand All @@ -81,8 +84,7 @@ def _creoson_post(self, command, function, data=None, key_data=None):
if "data" not in json_result.keys():
raise MissingKey("Missing `data` in creoson return")
if key_data not in json_result["data"].keys():
raise MissingKey(
"Missing `{}` in creoson result".format(key_data))
raise MissingKey("Missing `{}` in creoson result".format(key_data))
return json_result["data"][key_data]

return json_result.get("data", None)
Expand All @@ -93,7 +95,7 @@ def disconnect(self):
Empty sessionId.
"""
self._creoson_post("connection", "disconnect")
self.sessionId = ''
self.sessionId = ""

def is_creo_running(self):
"""Check whether Creo is running.
Expand All @@ -111,11 +113,7 @@ def is_creo_running(self):
"""
# return self._creoson_post("connection", "is_creo_running")["running"]
return self._creoson_post(
"connection",
"is_creo_running",
key_data="running"
)
return self._creoson_post("connection", "is_creo_running", key_data="running")

def kill_creo(self):
"""Kill primary Creo processes.
Expand Down Expand Up @@ -176,7 +174,7 @@ def start_creo(self, path, retries=0, use_desktop=False):
"start_dir": start_dir,
"start_command": start_command,
"retries": retries,
"use_desktop": use_desktop
"use_desktop": use_desktop,
}
return self._creoson_post("connection", "start_creo", data)

Expand Down

0 comments on commit 7c5eeea

Please sign in to comment.