#Begin of server global configuration and imports
#Please do not add any service imports above this.

from common.configuration import config
from common import eflogging
logger = eflogging.getLogger("rest_service_startup")

#End of Server global configuration imports

#Please add the imports to new rest controller services in this section
#
#   Start of Rest Service Controller import section
#
import deployables.partner.user_location_service
#
#   End of Rest Service Controller import section
#
from bottle import run, Bottle, default_app
app = default_app()
#run(app,host=config.get("webserver", "hostname"), port=config.get("webserver", "port"), server="cherrypy", debug=config.get("webserver", "debug"),options={"numthreads":30})
Esempio n. 2
0
from bottle import get,put, request,hook
from common.invoker import invoke
# from validator.thermostat_validator import validate_update_thermostat
from common.error_view import Error
import logging
from common import eflogging

logger = eflogging.getLogger("test_service")

@get('/ws/consumer/v1.0/thermostat/<thermostat_id:int>')
def readThermostat(thermostat_id=False):
    requestparams={'thermostat_id' :thermostat_id}
    contextparams={}
    if thermostat_id is False:
        raise Exception("thermostat_id cannot be empty")
    result = invoke( 'GET', 'thermostat', requestparams,contextparams)
    return result

@put('/ws/consumer/v1.0/thermostat/<thermostat_id:int>')
def updateThermostat(thermostat_id=None,guid=None):
    requestparams={'thermostat_id':thermostat_id}
    requestparams.update(request.json)
    contextparams={'guid' : requestparams.pop('guid', 0)}
    if request.json is None:
        return Error(10004).render()
#     result = validate_update_thermostat(requestparams)
#     if result is not None:
#         return result
    result =invoke( 'PUT', 'thermostat',requestparams,contextparams)
    return result
Esempio n. 3
0
# This is our service invoker.  It takes in a request with a standard format, and then decides how to route it based on configuration.
# NOTE: "service" will mean "controller" when it's making internal calls.  However, a "service" may also reside on a different process,
# or even cause an asynchronous HTTP call to be made.
# Even though remote calls may be asynchronous, the call to 'invoke' will block, with a per-service configured timeout
# In the event of a failed service call, a ServiceException will be raised with details of the failure

# method - a RESTful method of calling in.  One of GET, POST, PUT, or DELETE.  Please resist the temptation to add additional method types.
# service - the name of the service being called
# params - an optional set of parameters.  They'll be understood only by the service being called.  In order to prevent custom logic everywhere
#        for translating between invoked params and what needs to be passed via a webservice call in the future, params must be formatted as
#        a string that is acceptable in a URL.

from common import eflogging

logger = eflogging.getLogger("invoker")


def invoke(method, service, request_params=None, context_params=None):
    # Parse out and validate the URI
    if method != "GET" and method != "POST" and method != "PUT" and method != "DELETE":
        raise Exception("Only RESTful methods are permissible")

    #     if uriparams is not None :
    #         raise Exception( "parameters must be passed in a format usable directly in a URL")

    # TODO: Check config for how to call the service

    # Execute the call
    # For now, assume direct call
    try:
        return _invokeDirect(method, service, request_params, context_params)
#Begin of server global configuration and imports
#Please do not add any service imports above this.

from common.configuration import config
from common import eflogging
logger = eflogging.getLogger("rest_service_startup")

#End of Server global configuration imports

#Please add the imports to new rest controller services in this section
#
#   Start of Rest Service Controller import section
#
import deployables.partner.user_location_service
#
#   End of Rest Service Controller import section
#
from bottle import run, Bottle, default_app
app=default_app()
#run(app,host=config.get("webserver", "hostname"), port=config.get("webserver", "port"), server="cherrypy", debug=config.get("webserver", "debug"),options={"numthreads":30}) 
from common import eflogging
from common.error_view import Error
from common.exception import ServiceException
from common.invoker import invoke

logger=eflogging.getLogger("onboarding")
#Add parameter validation here
def POST(request_params,context_params):
    logger.info("Start of user onboarding")
    location_params = {}
    location_params['locations']= request_params.pop('locations')
    user_params = request_params
    
    try:
        user_response = invoke('POST','user',user_params,context_params)
        location_params['user_id'] = user_response['user_id']
        location_response = invoke('PUT','location',location_params,context_params)
    except ServiceException as ex:
        logger.error("Onboarding failed for user %s %s" %(user_params,location_params))
        return Error(ex.errorCode)

    response={}
    response.update(location_response)
    response.update(user_response)
    logger.info("User onboarding succesful")
    
    return response
Esempio n. 6
0
# This is our service invoker.  It takes in a request with a standard format, and then decides how to route it based on configuration.
# NOTE: "service" will mean "controller" when it's making internal calls.  However, a "service" may also reside on a different process,
# or even cause an asynchronous HTTP call to be made.
# Even though remote calls may be asynchronous, the call to 'invoke' will block, with a per-service configured timeout
# In the event of a failed service call, a ServiceException will be raised with details of the failure

# method - a RESTful method of calling in.  One of GET, POST, PUT, or DELETE.  Please resist the temptation to add additional method types.
# service - the name of the service being called
# params - an optional set of parameters.  They'll be understood only by the service being called.  In order to prevent custom logic everywhere
#        for translating between invoked params and what needs to be passed via a webservice call in the future, params must be formatted as
#        a string that is acceptable in a URL.

from common import eflogging

logger = eflogging.getLogger('invoker')


def invoke(method, service, request_params=None, context_params=None):
    # Parse out and validate the URI
    if method != 'GET' and method != 'POST' and method != 'PUT' and method != 'DELETE':
        raise Exception("Only RESTful methods are permissible")

#     if uriparams is not None :
#         raise Exception( "parameters must be passed in a format usable directly in a URL")

# TODO: Check config for how to call the service

# Execute the call
# For now, assume direct call
    try:
        return _invokeDirect(method, service, request_params, context_params)
Esempio n. 7
0
# This is a wrapper around the MySQLdb functionality, so that logic related to the mechanics of
# querying can be separated from the models that ask for the data.

# Connection pooling can happen here, as can standardized logging messages for failure modes

from common import eflogging

# A thin wrapper around cursor.execute().  See http://www.python.org/dev/peps/pep-0249/ for more details on this method
logger = eflogging.getLogger("db")


def fetchData(connection, sql, params=None):
    cursor = connection.cursor()
    cursor.execute(sql, params)
    data = cursor.fetchall()
    return data


def updateData(connection, sql, params=None):
    "used to update data to database"
    if connection is None:
        raise Exception("A connection to database is needed for updating the database")
    logger.debug("Updating data to database")
    cursor = connection.cursor()
    cursor.execute(sql, params)
    return True
from bottle import post,hook,request, Bottle,run,default_app
from common.invoker import invoke
from common.parameter_util import get_param
from common.request_util import get_request_id
from common import eflogging

logger = eflogging.getLogger("create_user_location")
app=default_app()

@app.post('/services/CMCSA/data/v1.0/user')
def create_user_location():
    request_params,context_params=get_param()
    context_params['partner_id']=8
    logger.info("Inside create_user_location")
    result = invoke("POST", "onboarding", request_params,context_params)
    return result

@hook('before_request')
def before_request():
    request.environ['guid']=get_request_id()
    logger.info('Before request to %s %s' % (request.method, request.path))
  
@hook('after_request')
def after_request():
    logger.info('After  request to %s %s' % (request.method, request.path))
Esempio n. 9
0
# This is a wrapper around the MySQLdb functionality, so that logic related to the mechanics of
# querying can be separated from the models that ask for the data.

# Connection pooling can happen here, as can standardized logging messages for failure modes

from common import eflogging

# A thin wrapper around cursor.execute().  See http://www.python.org/dev/peps/pep-0249/ for more details on this method
logger = eflogging.getLogger('db')


def fetchData(connection, sql, params=None):
    cursor = connection.cursor()
    cursor.execute(sql, params)
    data = cursor.fetchall()
    return data


def updateData(connection, sql, params=None):
    "used to update data to database"
    if connection is None:
        raise Exception(
            "A connection to database is needed for updating the database")
    logger.debug("Updating data to database")
    cursor = connection.cursor()
    cursor.execute(sql, params)
    return True
Esempio n. 10
0
from bottle import post, hook, request, Bottle, run, default_app
from common.invoker import invoke
from common.parameter_util import get_param
from common.request_util import get_request_id
from common import eflogging

logger = eflogging.getLogger("create_user_location")
app = default_app()


@app.post('/services/CMCSA/data/v1.0/user')
def create_user_location():
    request_params, context_params = get_param()
    context_params['partner_id'] = 8
    logger.info("Inside create_user_location")
    result = invoke("POST", "onboarding", request_params, context_params)
    return result


@hook('before_request')
def before_request():
    request.environ['guid'] = get_request_id()
    logger.info('Before request to %s %s' % (request.method, request.path))


@hook('after_request')
def after_request():
    logger.info('After  request to %s %s' % (request.method, request.path))
Esempio n. 11
0
from common import eflogging
from common.error_view import Error
from common.exception import ServiceException
from common.invoker import invoke

logger = eflogging.getLogger("onboarding")


#Add parameter validation here
def POST(request_params, context_params):
    logger.info("Start of user onboarding")
    location_params = {}
    location_params['locations'] = request_params.pop('locations')
    user_params = request_params

    try:
        user_response = invoke('POST', 'user', user_params, context_params)
        location_params['user_id'] = user_response['user_id']
        location_response = invoke('PUT', 'location', location_params,
                                   context_params)
    except ServiceException as ex:
        logger.error("Onboarding failed for user %s %s" %
                     (user_params, location_params))
        return Error(ex.errorCode)

    response = {}
    response.update(location_response)
    response.update(user_response)
    logger.info("User onboarding succesful")

    return response