X-Git-Url: https://gerrit.akraino.org/r/gitweb?a=blobdiff_plain;f=cmframework%2Fsrc%2Fcmframework%2Fserver%2Fcmrestapifactory.py;fp=cmframework%2Fsrc%2Fcmframework%2Fserver%2Fcmrestapifactory.py;h=6cfd6fbc0dc2726dc2a68d1dc09ce64156cb4f22;hb=c389bdee7b3845b55f443dbf04c0ce4083a55886;hp=0000000000000000000000000000000000000000;hpb=5030f0c004701dd422c78c71c014ef60f48139fc;p=ta%2Fconfig-manager.git diff --git a/cmframework/src/cmframework/server/cmrestapifactory.py b/cmframework/src/cmframework/server/cmrestapifactory.py new file mode 100644 index 0000000..6cfd6fb --- /dev/null +++ b/cmframework/src/cmframework/server/cmrestapifactory.py @@ -0,0 +1,76 @@ +# Copyright 2019 Nokia + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import json +import logging + +from cmframework.server.cmhttperrors import CMHTTPErrors +from cmframework.server import cmrestapiv1 + + +class CMRestAPIFactory(object): + def __init__(self, processor, base_url): + self.apis = {} + api = cmrestapiv1.CMRestAPIV1(processor) + self.apis[api.get_version()] = api + self.base_url = base_url + + def get_apis(self, rpc): + """ + Request: GET http:///cm/apis + Response: + { + "versions": [ + { + "id": "", + "href": "" + "min-version": "", + "status": "" + }, + ... + ] + } + """ + try: + logging.debug('get_apis called') + reply = {} + versions = [] + for key, value in self.apis.iteritems(): + version = {} + version['id'] = value.get_version() + version['href'] = '%s%s/' % (self.base_url, key) + version['min-version'] = value.get_minimum_version() + version['status'] = value.get_status() + versions.append(version) + reply['versions'] = versions + rpc.rep_status = CMHTTPErrors.get_ok_status() + rpc.rep_body = json.dumps(reply) + logging.debug('returning, rpc=%s', str(rpc)) + except Exception as exp: # pylint: disable=broad-except + logging.error('Got exception %s', str(exp)) + rpc.rep_status = CMHTTPErrors.get_internal_error_status() + rpc.rep_status += ',' + rpc.rep_status += str(exp) + + def get_api(self, version): + logging.debug('get_api called with version %s', version) + api = None + try: + api = self.apis[version] + except KeyError: + logging.warn('Could not find API with version %s', version) + return api + + +if __name__ == '__main__': + rest_api_factory = CMRestAPIFactory(None, None)