Renamed api_client.check_version() to api_client.is_server_compatible().

Started adding unit tests: pypo-api-validator.py.
This commit is contained in:
paul.baranowski 2010-12-14 19:09:13 -05:00
parent 5e04c806bd
commit 4684837f2b
6 changed files with 165 additions and 45 deletions

View File

@ -37,7 +37,7 @@ class ApiClientInterface:
#
# Should exit the program if this version of pypo is not compatible with
# 3rd party software.
def check_version(self):
def is_server_compatible(self, verbose = True):
pass
# Implementation: Required
@ -108,7 +108,7 @@ class CampcasterApiClient(ApiClientInterface):
def __init__(self, config):
self.config = config
def __get_campcaster_version(self):
def __get_campcaster_version(self, verbose = True):
logger = logging.getLogger()
url = self.config["base_url"] + self.config["api_base"] + self.config["version_url"]
url = url.replace("%%api_key%%", self.config["api_key"])
@ -124,26 +124,28 @@ class CampcasterApiClient(ApiClientInterface):
except Exception, e:
try:
if e[1] == 401:
print '#####################################'
print '# YOUR API KEY SEEMS TO BE INVALID:'
print '# ' + self.config["api_key"]
print '#####################################'
sys.exit()
if (verbose):
print '#####################################'
print '# YOUR API KEY SEEMS TO BE INVALID:'
print '# ' + self.config["api_key"]
print '#####################################'
return False
except Exception, e:
pass
try:
if e[1] == 404:
print '#####################################'
print '# Unable to contact the Campcaster-API'
print '# ' + url
print '#####################################'
sys.exit()
if (verbose):
print '#####################################'
print '# Unable to contact the Campcaster-API'
print '# ' + url
print '#####################################'
return False
except Exception, e:
pass
version = 0
logger.error("Unable to detect Campcaster Version - %s", e)
logger.error("Unable to detect Campcaster Version - %s, Response: %s", e, response)
return version
@ -165,21 +167,25 @@ class CampcasterApiClient(ApiClientInterface):
self.get_media(item["uri"], filename)
def check_version(self):
version = self.__get_campcaster_version()
if (version == 0):
print 'Unable to get Campcaster version number.'
print
sys.exit()
def is_server_compatible(self, verbose = True):
version = self.__get_campcaster_version(verbose)
if (version == 0 or version == False):
if (verbose):
print 'Unable to get Campcaster version number.'
print
return False
elif (version[0:4] != "1.6."):
print 'Campcaster version: ' + str(version)
print 'pypo not compatible with this version of Campcaster.'
print
sys.exit()
if (verbose):
print 'Campcaster version: ' + str(version)
print 'pypo not compatible with this version of Campcaster.'
print
return False
else:
print 'Campcaster version: ' + str(version)
print 'pypo is compatible with this version of Campcaster.'
print
if (verbose):
print 'Campcaster version: ' + str(version)
print 'pypo is compatible with this version of Campcaster.'
print
return True
def get_schedule(self, start=None, end=None):
@ -361,27 +367,31 @@ class ObpApiClient():
self.config = config
self.api_auth = urllib.urlencode({'api_key': self.config["api_key"]})
def check_version(self):
def is_server_compatible(self, verbose = True):
obp_version = self.get_obp_version()
if obp_version == 0:
print '#################################################'
print 'Unable to get OBP version. Is OBP up and running?'
print '#################################################'
print
sys.exit()
if (verbose):
print '#################################################'
print 'Unable to get OBP version. Is OBP up and running?'
print '#################################################'
print
return False
elif obp_version < OBP_MIN_VERSION:
print 'OBP version: ' + str(obp_version)
print 'OBP min-version: ' + str(OBP_MIN_VERSION)
print 'pypo not compatible with this version of OBP'
print
sys.exit()
if (verbose):
print 'OBP version: ' + str(obp_version)
print 'OBP min-version: ' + str(OBP_MIN_VERSION)
print 'pypo not compatible with this version of OBP'
print
return False
else:
print 'OBP API: ' + str(API_BASE)
print 'OBP version: ' + str(obp_version)
print 'OBP min-version: ' + str(OBP_MIN_VERSION)
print 'pypo is compatible with this version of OBP'
print
if (verbose):
print 'OBP API: ' + str(API_BASE)
print 'OBP version: ' + str(obp_version)
print 'OBP min-version: ' + str(OBP_MIN_VERSION)
print 'pypo is compatible with this version of OBP'
print
return True
def get_obp_version(self):

View File

@ -0,0 +1,59 @@
[loggers]
keys=root
[handlers]
keys=consoleHandler,fileHandlerERROR,fileHandlerDEBUG,nullHandler
[formatters]
keys=simpleFormatter
[logger_root]
level=DEBUG
handlers=consoleHandler,fileHandlerERROR,fileHandlerDEBUG
[logger_libs]
handlers=nullHandler
level=DEBUG
qualname="process"
propagate=0
[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout,)
[handler_fileHandlerERROR]
class=FileHandler
level=WARNING
formatter=simpleFormatter
args=("./error.log",)
[handler_fileHandlerDEBUG]
class=FileHandler
level=DEBUG
formatter=simpleFormatter
args=("./debug.log",)
[handler_nullHandler]
class=FileHandler
level=DEBUG
formatter=simpleFormatter
args=("./log_null.log",)
[formatter_simpleFormatter]
format=%(asctime)s %(levelname)s - [%(filename)s : %(funcName)s() : line %(lineno)d] - %(message)s
datefmt=
## multitail color sheme
## pyml / python
# colorscheme:pyml:www.obp.net
# cs_re:blue:\[[^ ]*\]
# cs_re:red:CRITICAL:*
# cs_re:red,black,blink:ERROR:*
# cs_re:blue:NOTICE:*
# cs_re:cyan:INFO:*
# cs_re:green:DEBUG:*

View File

@ -0,0 +1,50 @@
import time
import os
import traceback
from optparse import *
import sys
import time
import datetime
import logging
import logging.config
import shutil
import urllib
import urllib2
import pickle
import telnetlib
import random
import string
import operator
import inspect
# additional modules (should be checked)
from configobj import ConfigObj
# custom imports
from util import *
from api_clients import *
import random
import unittest
# configure logging
logging.config.fileConfig("logging-api-validator.cfg")
try:
config = ConfigObj('config.cfg')
except Exception, e:
print 'Error loading config file: ', e
sys.exit()
class TestApiFunctions(unittest.TestCase):
def setUp(self):
self.api_client = api_client.api_client_factory(config)
def test_is_server_compatible(self):
self.assertTrue(self.api_client.is_server_compatible(False))
if __name__ == '__main__':
unittest.main()

View File

@ -105,7 +105,8 @@ class Global:
def selfcheck(self):
self.api_client = api_client.api_client_factory(config)
self.api_client.check_version()
if (not self.api_client.is_server_compatible()):
sys.exit()
"""

View File

@ -76,10 +76,10 @@ class Global:
print
def selfcheck(self):
self.api_auth = urllib.urlencode({'api_key': API_KEY})
self.api_client = api_client.api_client_factory(config)
self.api_client.check_version()
if (not self.api_client.is_server_compatible()):
sys.exit()
class Notify:
def __init__(self):