Skip to content

Commit

Permalink
support for python > 3; also backward compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonySchneider-cnic committed Jul 21, 2016
1 parent c89c917 commit 2756429
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 47 deletions.
22 changes: 17 additions & 5 deletions connection.py
@@ -1,7 +1,14 @@
import ispapi.util
import urllib, urllib2
import urllib
try:
# For Python 3.0 and later
from urllib.request import urlopen
except ImportError:
# Fall back to Python 2's urllib2
from urllib2 import urlopen
from ispapi.response import Response


"""
ISPAPI Connection
Expand All @@ -14,6 +21,7 @@ def __init__(self, config):
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
"""
Expand All @@ -31,8 +39,14 @@ def call_raw_http(self, command, config = None):

post['s_command'] = ispapi.util.command_encode(command)

req = urllib2.Request(self._config['url'], urllib.urlencode(post))
response = urllib2.urlopen(req)
try:
# For Python 3.0 and later
post = urllib.parse.urlencode(post)
except:
# Fall back to Python 2's urllib2
post = urllib.urlencode(post)

response = urlopen(self._config['url'], post.encode('UTF-8'))
content = response.read()
return content

Expand All @@ -47,5 +61,3 @@ 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))


67 changes: 34 additions & 33 deletions response.py
Expand Up @@ -13,7 +13,8 @@ def __init__(self, response):
self._response_hash = None
self._response_list_hash = None

if type(response) == str:
if type(response) == bytes:
response = response.decode("utf-8")
self._response_string = response

if type(response) == dict:
Expand Down Expand Up @@ -66,13 +67,13 @@ def __getitem__(self, index):
if type(index) == str:
return self.as_hash()[index]
pass

def code(self):
"""
Returns the response code
"""
return self.as_list_hash()["CODE"]

def description(self):
"""
Returns the response description
Expand All @@ -90,13 +91,13 @@ def runtime(self):
Returns the response runtime
"""
return self.as_list_hash()["RUNTIME"]

def queuetime(self):
"""
Returns the response queuetime
"""
return self.as_list_hash()["QUEUETIME"]

def property(self, index = None):
"""
Returns the property for a given index
Expand All @@ -110,7 +111,7 @@ def property(self, index = None):
return None
else:
return properties

def is_success(self):
"""
Returns true if the results is a success
Expand All @@ -120,7 +121,7 @@ def is_success(self):
return True
else:
return False

def is_tmp_error(self):
"""
Returns true if the results is a tmp error
Expand All @@ -130,56 +131,56 @@ def is_tmp_error(self):
return True
else:
return False
def columns(self):

def columns(self):
"""
Returns the columns
"""
return self.as_list_hash()["COLUMNS"]
def first(self):

def first(self):
"""
Returns the index of the first element
"""
return self.as_list_hash()["FIRST"]
def last(self):

def last(self):
"""
Returns the index of the last element
"""
return self.as_list_hash()["LAST"]

def count(self):
"""
Returns the number of list elements returned (= last - first + 1)
"""
"""
return self.as_list_hash()["COUNT"]
def limit(self):

def limit(self):
"""
Returns the limit of the response
"""
return self.as_list_hash()["LIMIT"]

def total(self):
"""
Returns the total number of elements found (!= count)
"""
"""
return self.as_list_hash()["TOTAL"]
def pages(self):

def pages(self):
"""
Returns the number of pages
"""
return self.as_list_hash()["PAGES"]
def page(self):

def page(self):
"""
Returns the number of the current page (starts with 1)
"""
return self.as_list_hash()["PAGE"]
def prevpage(self):

def prevpage(self):
"""
Returns the number of the previous page
Expand All @@ -188,29 +189,29 @@ def prevpage(self):
return self.as_list_hash()["PREVPAGE"]
except:
return None
def prevpagefirst(self):

def prevpagefirst(self):
"""
Returns the first index for the previous page
"""
try:
return self.as_list_hash()["PREVPAGEFIRST"]
except:
return None
def nextpage(self):

def nextpage(self):
"""
Returns the number of the next page
"""
return self.as_list_hash()["NEXTPAGE"]

def nextpagefirst(self):
"""
Returns the first index for the next page
"""
"""
return self.as_list_hash()["NEXTPAGEFIRST"]
def lastpagefirst(self):

def lastpagefirst(self):
"""
Returns the first index for the last page
"""
Expand Down
14 changes: 5 additions & 9 deletions util.py
Expand Up @@ -16,10 +16,6 @@ def _command_encode(command):

if type(command) == int:
command = str(command)

if type(command) == unicode:
command = command.encode('utf-8')

if type(command) == str:
return ["=" + command]
elif type(command) == dict:
Expand All @@ -32,6 +28,7 @@ def _command_encode(command):
r.append(str(i) + v2)
else:
pass

return r


Expand Down Expand Up @@ -80,7 +77,7 @@ def response_to_list_hash(response):
'QUEUETIME': "",
'ITEMS': []
}

if 'CODE' in response: list_hash['CODE'] = response['CODE']
if 'DESCRIPTION' in response: list_hash['DESCRIPTION'] = response['DESCRIPTION']
if 'RUNTIME' in response: list_hash['RUNTIME'] = response['RUNTIME']
Expand Down Expand Up @@ -123,14 +120,14 @@ def response_to_list_hash(response):
list_hash['PREVPAGEFIRST'] = (list_hash['PREVPAGE'] - 1) * list_hash['LIMIT']
list_hash['NEXTPAGE'] = list_hash['PAGE'] + 1
list_hash['NEXTPAGEFIRST'] = (list_hash['NEXTPAGE'] - 1) * list_hash['LIMIT']

if 'TOTAL' in list_hash and 'LIMIT' in list_hash and list_hash['LIMIT'] > 0:
list_hash['PAGES'] = int(list_hash['TOTAL'] + list_hash['LIMIT'] - 1) / list_hash['LIMIT']
list_hash['LASTPAGEFIRST'] = (list_hash['PAGES'] - 1) * list_hash['LIMIT']
if 'NEXTPAGE' in list_hash and (list_hash['NEXTPAGE'] > list_hash['PAGES']):
list_hash['NEXTPAGE'] = None
list_hash['NEXTPAGEFIRST'] = None

return list_hash


Expand Down Expand Up @@ -174,5 +171,4 @@ def base64_decode(string):
"""
Decodes data encoded with MIME base64
"""
return base64.b64decode(string)

return base64.b64decode(string)

0 comments on commit 2756429

Please sign in to comment.