[spp] [PATCH 1/9] controller: add spp-ctl client for SPP controller
ogawa.yasufumi at lab.ntt.co.jp
ogawa.yasufumi at lab.ntt.co.jp
Thu Oct 18 13:25:10 CEST 2018
From: Yasufumi Ogawa <ogawa.yasufumi at lab.ntt.co.jp>
Add SppCtlClient class which is a utility to create requests and
responses for spp-ctl.
Signed-off-by: Yasufumi Ogawa <ogawa.yasufumi at lab.ntt.co.jp>
---
src/controller/spp_ctl_client.py | 58 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 58 insertions(+)
create mode 100644 src/controller/spp_ctl_client.py
diff --git a/src/controller/spp_ctl_client.py b/src/controller/spp_ctl_client.py
new file mode 100644
index 0000000..6de1ae4
--- /dev/null
+++ b/src/controller/spp_ctl_client.py
@@ -0,0 +1,58 @@
+#!/usr/bin/env python
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2018 Nippon Telegraph and Telephone Corporation
+
+import requests
+
+
+class SppCtlClient(object):
+
+ def __init__(self):
+ api_ver = 'v1'
+ ip_addr = '127.0.0.1'
+ port = 7777
+ self.base_url = 'http://%s:%d/%s' % (ip_addr, port, api_ver)
+
+ def request_handler(func):
+ """Request handler for spp-ctl.
+
+ Decorator for handling a http request of 'requests' library.
+ It receives one of the methods 'get', 'put', 'post' or 'delete'
+ as 'func' argment.
+ """
+
+ def wrapper(self, *args, **kwargs):
+ try:
+ res = func(self, *args, **kwargs)
+
+ # TODO(yasufum) revise print message to more appropriate
+ # for spp.py.
+ if res.status_code == 400:
+ print('Syntax or lexical error, or SPP returns' +
+ 'error for the request.')
+ elif res.status_code == 404:
+ print('URL is not supported, or no SPP process' +
+ 'of client-id in a URL.')
+ elif res.status_code == 500:
+ print('System error occured in spp-ctl.')
+
+ return res
+ except requests.exceptions.ConnectionError:
+ print('Error: Failed to connect to spp-ctl.')
+ return None
+ return wrapper
+
+ @request_handler
+ def get(self, req):
+ url = '%s/%s' % (self.base_url, req)
+ return requests.get(url)
+
+ @request_handler
+ def put(self, req, params):
+ url = '%s/%s' % (self.base_url, req)
+ return requests.put(url, json=params)
+
+ @request_handler
+ def delete(self, req):
+ url = '%s/%s' % (self.base_url, req)
+ return requests.delete(url)
--
2.13.1
More information about the spp
mailing list