Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Structural changes, added extractor

  • Loading branch information...
commit de122585c77e61219be9e13ce5321ac40bb87783 1 parent 1b517e9
@georges-gouriten authored
View
25 apiblender/authentication.py
@@ -1,25 +0,0 @@
-class AuthenticationManager:
- """Ensure authentication."""
-
- def __init__(self):
- self.servers = {}
-
-
- def add_server(self, server):
- if server.name not in self.servers:
- parameters = server.authentication.request_parameters(
- server.host,
- server.port)
- if parameters:
- self.servers.update({ server.name: {
- "parameters": parameters }})
- return True
- else:
- print("ERROR, authentication failed for server: %s." %\
- server.name)
- print("> Please check your authentication \
- configuration. Server not created.")
- return None
-
- def exists_server(self, server):
- return (server.name in self.servers)
View
73 apiblender/main.py
@@ -6,65 +6,51 @@
import serverclasses
import policy
-import authentication
+import auth
GENERAL_CONFIG = os.path.dirname(__file__) + "/config/general.json"
class Blender:
"""You have to create one."""
-
def __init__(self):
+ self.load_config_file()
+ self.policy_manager = policy.PolicyManager()
+ self.auth_manager = auth.AuthManager()
+ self.server = None
+ self.interaction = None
- # Load the configuration file
+ def load_config_file(self):
if not os.path.exists(GENERAL_CONFIG):
sys.exit('ERROR: File %s was not found!' % GENERAL_CONFIG)
with open(GENERAL_CONFIG, 'r') as config_file:
general_config = json.load(config_file)
-
self.user_agent = general_config["user-agent"]
self.apis_path = os.path.dirname(__file__) + general_config["apis_path"]
-
- self.policy_manager = policy.PolicyManager()
- self.authentication_manager = authentication.AuthenticationManager()
-
- self.server = None
- self.interaction = None
-
-
+
def list_servers(self):
"""List available servers."""
-
for filename in os.listdir(self.apis_path):
print filename.split('.json')[0]
-
def load_server(self, server_name):
-
server_config_path = self.apis_path + server_name + '.json'
-
if not os.path.exists(server_config_path):
print('ERROR: File %s was not found.' % server_config_path)
else:
with open(server_config_path, 'r') as server_config_file:
server_config = json.load(server_config_file)
-
self.server = serverclasses.Server(server_config)
-
if self.server.policy:
self.policy_manager.add_server(self.server)
-
- if self.server.authentication:
- if not self.authentication_manager.add_server(self.server):
+ if self.server.auth:
+ if not self.auth_manager.add_server(self.server):
self.server = None
-
def load_interaction(self, interaction_name):
-
if not self.server:
print('ERROR loading %s: no server loaded.' % interaction_name)
return None
-
if interaction_name in [interaction.name for interaction in
self.server.interactions]:
self.interaction = interaction
@@ -73,33 +59,34 @@ def load_interaction(self, interaction_name):
% (interaction_name, self.server.name))
return None
-
def blend(self, request_parameters):
-
if not self.interaction:
print('ERROR blending: no interaction loaded.')
return None
-
if not self.policy_manager.get_request_permission(self.server):
sleeping_time = self.policy_manager.get_sleeping_time()
print "sleeping for: %s seconds" % (sleeping_time)
time.sleep(sleeping_time)
+ response = self.make_request(request_parameters)
+ ready_response = self.prepare_response(response)
+ print str(ready_response)[0:100]
+ return ready_response
+ def make_request(self, request_parameters):
#TODO: request_parameters verification
-
total_parameters = {}
total_parameters.update(
- self.interaction.request.path_constant_parameters)
+ self.interaction.request.path_constant_parameters)
total_parameters.update(request_parameters)
- if self.authentication_manager.exists_server(self.server):
+ if self.auth_manager.exists_server(self.server):
total_parameters.update(
- authentication_manager.servers["server.name"])
+ auth_manager.servers["server.name"])
c = httplib.HTTPSConnection(self.server.host, self.server.port,
timeout = 10)
else:
c = httplib.HTTPConnection(self.server.host, self.server.port,
- timeout = 10)
+ timeout = 10)
total_path = "%s?%s" % (self.interaction.request.root_path,
urllib.urlencode(total_parameters))
@@ -107,9 +94,21 @@ def blend(self, request_parameters):
print "> Request: %s%s" % (self.server.host, total_path)
c.request(self.interaction.request.method, total_path)
r = c.getresponse()
- http_response = r.read()
+ response = r.read()
c.close()
-
- print http_response[0:100]
-
-
+ return response
+
+ def prepare_response(self, response):
+ try:
+ #TODO json or xml
+ response = json.loads(response)
+ except Exception:
+ #TODO manage exception
+ print 'wrong results'
+ return False
+ if self.interaction.response.extractor:
+ ready_response = \
+ self.interaction.response.extractor.extract(response)
+ else:
+ ready_response = response
+ return ready_response
View
43 apiblender/serverclasses.py
@@ -15,9 +15,9 @@ def __init__(self, server_config):
if server_config["authentication"]:
if server_config["authentication"]["type"] == "simple":
- self.authentication = AuthSimple(server_config["authentication"])
+ self.auth = AuthSimple(server_config["authentication"])
else:
- self.authentication = None
+ self.auth = None
if server_config["policy"]:
self.policy = Policy(server_config["policy"])
@@ -32,20 +32,20 @@ def __init__(self, server_config):
class Authentication:
- def __init__(self, authentication_config):
- self.type_ = authentication_config["type"]
+ def __init__(self, auth_config):
+ self.type_ = auth_config["type"]
class AuthSimple(Authentication):
- def __init__(self, authentication_config):
+ def __init__(self, auth_config):
#
# TODO: validation
#
- self.path = authentication_config["path"]
- self.parameters = authentication_config["parameters"]
+ self.path = auth_config["path"]
+ self.parameters = auth_config["parameters"]
def request_parameters(self, host, port):
@@ -65,7 +65,7 @@ def request_parameters(self, host, port):
class AuthOauth2(Authentication):
- def __init__(self, authentication_config):
+ def __init__(self, auth_config):
pass
@@ -122,16 +122,13 @@ def __init__(self, request_config):
class Response:
def __init__(self, response_config):
-
#
# TODO: validation
#
-
if "expected_status_code" in response_config.keys():
self.expected_status_code = response_config["expected_status_code"]
else:
self.expected_status_code = 200
-
if response_config["serialization_format"] in ["JSON", "XML"]:
self.serialization_format = response_config["serialization_format"]
else:
@@ -139,3 +136,27 @@ def __init__(self, response_config):
# TODO: manage exception
#
raise Exception
+ if "integration" in response_config.keys():
+ self.extractor = Extractor(response_config["integration"])
+ else:
+ self.extractor = None
+
+
+class Extractor:
+
+ def __init__(self, extractor):
+ self.extractor = extractor
+
+ def extract(response):
+ return response
+ #TODO
+# for k, v in self.extractor.iteritems():
+# if v:
+# try:
+# value = utils.getFromDict(result,v)
+# standardContent.update({k: value})
+# except Exception as e:
+# continue
+
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.