Skip to content

Commit

Permalink
fix several flake8 issues; replace print with logging
Browse files Browse the repository at this point in the history
modules shouldn't expect to have usable stdout ;)
  • Loading branch information
costela committed May 31, 2017
1 parent 381f8ff commit f4c1c6b
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 129 deletions.
38 changes: 12 additions & 26 deletions glpi/glpi.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@
# GLPI API Rest documentation:
# https://github.com/glpi-project/glpi/blob/9.1/bugfixes/apirest.md

from __future__ import print_function
import os
import json as json_import
import logging
import requests
from requests.structures import CaseInsensitiveDict
import os

from version import __version__

logger = logging.getLogger(__name__)


def load_from_vcap_services(service_name):
vcap_services = os.getenv("VCAP_SERVICES")
Expand Down Expand Up @@ -168,7 +172,7 @@ def set_session_token(self):
self.session = r.json()['session_token']
return True
except Exception as e:
raise Exception("Unable to init session in GLPI server: %s" % e)
raise GlpiException("Unable to init session in GLPI server: %s" % e)

return False

Expand Down Expand Up @@ -215,7 +219,7 @@ def request(self, method, url, accept_json=False, headers={},
self.set_session_token()
headers.update({'Session-Token': self.session})
except Exception as e:
raise Exception("Unable to get Session token. ERROR: %s" % e)
raise GlpiException("Unable to get Session token. ERROR: %s" % e)

if self.app_token is not None:
headers.update({'App-Token': self.app_token})
Expand All @@ -234,6 +238,7 @@ def request(self, method, url, accept_json=False, headers={},
headers=headers, params=params,
data=data, **kwargs)
except Exception:
logger.error("ERROR requesting uri(%s) payload(%s)" % (url, data))
raise

return response
Expand Down Expand Up @@ -266,12 +271,7 @@ def create(self, data_json=None):

payload = '{"input": { %s }}' % (self.get_payload(data_json))

try:
response = self.request('POST', self.uri, data=payload,
accept_json=True)
except Exception as e:
print "#>> ERROR requesting uri(%s) payload(%s)" % (uri, payload)
raise
response = self.request('POST', self.uri, data=payload, accept_json=True)

return response.json()

Expand Down Expand Up @@ -327,14 +327,7 @@ def update(self, data):
payload = '{"input": { %s }}' % (self.get_payload(data))
new_url = "%s/%d" % (self.uri, data['id'])

try:
response = self.request('PUT', self.uri, data=payload)
except Exception as e:
print {
"message_error": "ERROR requesting uri(%s) payload(%s)" % (
uri, payload)
}
raise
response = self.request('PUT', new_url, data=payload)

return response.json()

Expand All @@ -350,14 +343,7 @@ def delete(self, item_id, force_purge=False):
else:
payload = '{"input": { "id": %d }}' % (item_id)

try:
response = self.request('DELETE', self.uri, data=payload)
except Exception as e:
print {
"message_error": "ERROR requesting uri(%s) payload(%s)" % (
uri, payload)
}
raise
response = self.request('DELETE', self.uri, data=payload)
return response.json()


Expand Down Expand Up @@ -442,7 +428,7 @@ def init_item(self, item_name):
try:
self.init_api()
except:
print "message_error: Unable to InitSession in GLPI Server."
logger.error("Unable to InitSession in GLPI Server.")
return False

if update_api:
Expand Down
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
[metadata]
description-file = README.md

[flake8]
max-line-length = 120
Loading

0 comments on commit f4c1c6b

Please sign in to comment.