Skip to content

Commit

Permalink
added pep8 scripts and code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
KaiSchwarz-cnic committed Jul 9, 2018
1 parent 01fe1e0 commit 95e0199
Show file tree
Hide file tree
Showing 7 changed files with 464 additions and 440 deletions.
38 changes: 19 additions & 19 deletions hexonet/apiconnector/__init__.py
Expand Up @@ -4,24 +4,24 @@
__version__ = '1.0.0'
name = "apiconnector"

def connect(login=None, password=None, url=None, entity=None, user=None, role=None, config=None):
"""
Returns an instance of apiconnector.Connection
"""

if config == None:
config = {}
if login != None:
config['login'] = login
if password != None:
config['password'] = password
if url != None:
config['url'] = url
if entity != None:
config['entity'] = entity
if user != None:
config['user'] = user
if role != None:
config['role'] = role
return Connection(config)
def connect(login=None, password=None, url=None, entity=None, user=None, role=None, config=None):
"""
Returns an instance of apiconnector.Connection
"""

if config is None:
config = {}
if login is not None:
config['login'] = login
if password is not None:
config['password'] = password
if url is not None:
config['url'] = url
if entity is not None:
config['entity'] = entity
if user is not None:
config['user'] = user
if role is not None:
config['role'] = role
return Connection(config)
89 changes: 45 additions & 44 deletions hexonet/apiconnector/connection.py
Expand Up @@ -8,55 +8,56 @@
# Fall back to Python 2's urllib2
from urllib2 import urlopen
try:
# For Python 3.0 and later
from urllib import parse as urlparse
# For Python 3.0 and later
from urllib import parse as urlparse
except ImportError:
# Fall back to Python 2
from urlparse import urlparse
# Fall back to Python 2
from urlparse import urlparse


"""
APICONNECTOR Connection
"""


class Connection:
def __init__(self, config):
"""
Constructor
"""
self._config = config

def call_raw_http(self, command, config = None):

"""
Make a curl API call over HTTP(S) and returns the response as a string
"""
post = {}
if ('login' in self._config):
post['s_login'] = self._config['login']
if ('password' in self._config):
post['s_pw'] = self._config['password']
if ('entity' in self._config):
post['s_entity'] = self._config['entity']
if ('user' in self._config):
post['s_user'] = self._config['user']
if ('role' in self._config):
post['s_login'] = self._config['login'] + "!" + self._config['role']

post['s_command'] = apiconnector.util.command_encode(command)
post = urlparse.urlencode(post)
response = urlopen(self._config['url'], post.encode('UTF-8'))
content = response.read()
return content

def call_raw(self, command, config = None):
"""
Make a curl API call and returns the response as a string
"""
return self.call_raw_http(command, config)

def call(self, command, config = None):
"""
Make a curl API call and returns the response as a response object
"""
return Response(self.call_raw(command, config))
def __init__(self, config):
"""
Constructor
"""
self._config = config

def call_raw_http(self, command, config=None):
"""
Make a curl API call over HTTP(S) and returns the response as a string
"""
post = {}
if ('login' in self._config):
post['s_login'] = self._config['login']
if ('password' in self._config):
post['s_pw'] = self._config['password']
if ('entity' in self._config):
post['s_entity'] = self._config['entity']
if ('user' in self._config):
post['s_user'] = self._config['user']
if ('role' in self._config):
post['s_login'] = self._config['login'] + "!" + self._config['role']

post['s_command'] = apiconnector.util.command_encode(command)
post = urlparse.urlencode(post)
response = urlopen(self._config['url'], post.encode('UTF-8'))
content = response.read()
return content

def call_raw(self, command, config=None):
"""
Make a curl API call and returns the response as a string
"""
return self.call_raw_http(command, config)

def call(self, command, config=None):
"""
Make a curl API call and returns the response as a response object
"""
return Response(self.call_raw(command, config))

0 comments on commit 95e0199

Please sign in to comment.