From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from tama50.ecl.ntt.co.jp (tama50.ecl.ntt.co.jp [129.60.39.147]) by dpdk.org (Postfix) with ESMTP id 0E9BB5F14 for ; Thu, 18 Oct 2018 13:25:32 +0200 (CEST) Received: from vc2.ecl.ntt.co.jp (vc2.ecl.ntt.co.jp [129.60.86.154]) by tama50.ecl.ntt.co.jp (8.13.8/8.13.8) with ESMTP id w9IBPVPi020278; Thu, 18 Oct 2018 20:25:31 +0900 Received: from vc2.ecl.ntt.co.jp (localhost [127.0.0.1]) by vc2.ecl.ntt.co.jp (Postfix) with ESMTP id 4841A639489; Thu, 18 Oct 2018 20:25:31 +0900 (JST) Received: from localhost.localdomain (unknown [129.60.13.51]) by vc2.ecl.ntt.co.jp (Postfix) with ESMTP id 3951563947C; Thu, 18 Oct 2018 20:25:31 +0900 (JST) From: ogawa.yasufumi@lab.ntt.co.jp To: spp@dpdk.org, ferruh.yigit@intel.com, ogawa.yasufumi@lab.ntt.co.jp Date: Thu, 18 Oct 2018 20:25:10 +0900 Message-Id: <20181018112518.77556-2-ogawa.yasufumi@lab.ntt.co.jp> X-Mailer: git-send-email 2.13.1 In-Reply-To: <20181018112518.77556-1-ogawa.yasufumi@lab.ntt.co.jp> References: <20181018112518.77556-1-ogawa.yasufumi@lab.ntt.co.jp> X-TM-AS-MML: disable Subject: [spp] [PATCH 1/9] controller: add spp-ctl client for SPP controller X-BeenThere: spp@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: Soft Patch Panel List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 18 Oct 2018 11:25:33 -0000 From: Yasufumi Ogawa Add SppCtlClient class which is a utility to create requests and responses for spp-ctl. Signed-off-by: Yasufumi Ogawa --- 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