Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions tests/common/helpers/platform_api/scripts/platform_api_server.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
import json
import argparse
import inspect
import sonic_platform
import json
import os
import syslog
from io import BytesIO

from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
from io import BytesIO

import sonic_platform

SYSLOG_IDENTIFIER = os.path.basename(__file__)

platform = sonic_platform.platform.Platform()


def obj_serialize(obj):
''' JSON serializer for objects not serializable by default json library code
We simply return a dictionary containing the object's class and module
'''
syslog.syslog(syslog.LOG_WARNING, 'Unserializable object: {}.{}'.format(obj.__module__, obj.__class__.__name__))

data = {
'__class__': obj.__class__.__name__,
'__module__': obj.__module__
}
return data


class PlatformAPITestService(BaseHTTPRequestHandler):
''' Handles HTTP POST requests and translated them into platform API call.
The expected URL path format is the following:
Expand Down Expand Up @@ -61,14 +79,19 @@ def do_platform_api(self):
res = getattr(obj, api)(*args)

response = BytesIO()
response.write(json.dumps({'res': res}))
response.write(json.dumps({'res': res}, default=obj_serialize))

self.wfile.write(response.getvalue())


if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--port', type=int, help='port to listent to', required=True)
parser.add_argument('-p', '--port', type=int, help='port to listen to', required=True)
args = parser.parse_args()

syslog.openlog(SYSLOG_IDENTIFIER)

httpd = HTTPServer(('', args.port), PlatformAPITestService)
httpd.serve_forever()

syslog.closelog()