RAPP Platform API
 All Classes Namespaces Files Functions Variables Typedefs
RappPlatformService.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 
4 # Copyright 2016 RAPP
5 
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 
10  #http://www.apache.org/licenses/LICENSE-2.0
11 
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 
18 # Authors: Konstantinos Panayiotou
19 # contact: klpanagi@gmail.com
20 
21 
22 ## @file RappCloud/Service/RappPlatformService.py
23 #
24 # @copyright Rapp Projecty EU 2016
25 # @author Konstantinos Panayiotou, [klpanagi@gmail.com]
26 #
27 
28 import yaml
29 from os import path
30 from RappCloud.ServiceController import ServiceControllerSync
31 from RappCloud.ServiceController import ServiceControllerAsync
32 from RappCloud.ServiceController.AsyncHandler import AsyncHandler
33 
34 
35 class RappPlatformService(object):
36  """ Service class """
37 
38  def __init__(self, msg=None, persistent=True, timeout=None,
39  address=None, port=None,
40  protocol=None, **kwargs):
41  """!
42  Constructor
43 
44  @param **kwargs - Keyword arguments. Apply values to the request attributes.
45  """
46 
47  # Load Default RAPP PLatform parameters from configuration file
48  # ~/.congig/rapp_platform_python_api/config
49  self.__load_platform_cfg()
50 
51  if address is not None:
52  self.__platformParams['address'] = address
53  if port is not None:
54  self.__platformParams['port'] = port
55  if protocol is not None:
56  self.__platformParams['protocol'] = protocol
57 
58  ## Cloud Object passed to the RappPlatformService.
59  self.__cloudObj = msg
60  ## Persistent connection value (Boolean)
61  self.__persistent = persistent
62  # Client timeout value (Number)
63  self.__timeout = timeout
64 
65  # Create service controller object. Pass the service instance
66  # for the service controller to hold.
67  # A Cloud Service holds a ServiceController instance.
68  ## Service Controller
69  self.__controller = ServiceControllerSync(self)
70  self.__controllerAsync = ServiceControllerAsync(self)
71 
72 
73  @property
74  def persistent(self):
75  """! Service persistent connection value getter """
76  return self.__persistent
77 
78  @persistent.setter
79  def persistent(self, val):
80  """! Service persistent connection value setter """
81  self.__persistent = val
82 
83 
84  @property
85  def timeout(self):
86  """! Service timeout value getter """
87  return self.__timeout
88 
89 
90  @timeout.setter
91  def timeout(self, val):
92  """! Service timeout value setter """
93  self.__timeout = val
94 
95 
96  @property
97  def req(self):
98  """! Service request object getter """
99  return self.__cloudObj.req if self.__cloudObj is not None else None
100 
101 
102  @property
103  def resp(self):
104  """! Service response object getter """
105  return self.__cloudObj.resp if self.__cloudObj is not None else None
106 
107 
108  @resp.setter
109  def resp(self, val):
110  """! Service response object setter """
111  self.__cloudObj.resp = val
112 
113 
114  def _make_url(self, svcUrlName):
115  """! Craft patform service full url path.
116 
117  @param svcUrlName string - The service urlname, i.e 'face_detection'
118 
119  @returns string - The full service url path.
120  """
121  return ''.join((self.__platformParams['protocol'], '://',
122  self.__platformParams['address'], ':',
123  self.__platformParams['port'], '/hop/',
124  svcUrlName))
125 
126 
127  def call(self, msg=None):
128  """! Call the RAPP Platform Service """
129  if msg is not None:
130  self.__cloudObj = msg
131  elif self.__cloudObj is None:
132  raise AttributeError('Missing Cloud Message object!')
133  else:
134  msg = self.__cloudObj
135 
136  urlpath = self._make_url(msg.svcname)
137 
138  cloudResponseDic = self.__controller.run_job(msg, urlpath)
139  for key, val in cloudResponseDic.iteritems():
140  msg.resp.set(key, val)
141  return msg.resp
142 
143 
144  def call_async(self, msg, clb=None):
145  """! Call the RAPP Platform Service """
146  _url = self._make_url(msg.svcname)
147  # TODO: Copy self.__cloudObject and pass to controller
148  # Use copy.deepcopy()
149  _future = self.__controllerAsync.run_job(msg, _url, clb=clb)
150  return AsyncHandler(_future)
151 
152 
154  _filepath = path.expanduser(
155  '~/.config/rapp_platform/python_platform_api/config')
156 
157  try:
158  f = open(_filepath, 'r')
159  _params = yaml.load(f)
160  except (IOError, yaml.YAMLError) as exc:
161  print str(exc)
162  print "Using default parameters:"
163  print " * address: 'localhost'"
164  print " * port: '9001'"
165  print " * protocol: 'http'"
166  # If config file does not exist or an exception is raised,
167  # fallback to use default parameters.
168  _params = {
169  'address': 'localhost',
170  'port': '9001',
171  'protocol': 'http'
172  }
173  self.__platformParams = _params
174 
def _make_url
Craft patform service full url path.
def persistent
Service persistent connection value getter.
__cloudObj
Cloud Object passed to the RappPlatformService.