From 6631134c9f66bc31c0ac4e95f21a4c54cc7fb028 Mon Sep 17 00:00:00 2001 From: "Q.hongtao" Date: Fri, 7 Aug 2020 10:50:40 +0800 Subject: [PATCH] Update tmfapi639 datasource Optimize code structure. Change-Id: I989dca596d1d8183a31a16969c51308d925e99e0 --- vitrage/datasources/tmfapi639/driver.py | 27 +++++++++---------------- 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/vitrage/datasources/tmfapi639/driver.py b/vitrage/datasources/tmfapi639/driver.py index f552dd9a8..b295381bb 100644 --- a/vitrage/datasources/tmfapi639/driver.py +++ b/vitrage/datasources/tmfapi639/driver.py @@ -13,15 +13,11 @@ # under the License. from oslo_log import log - -from vitrage.datasources.driver_base import DriverBase -from vitrage.datasources.tmfapi639 import TMFAPI639_DATASOURCE - -from vitrage.datasources.tmfapi639.config import TmfApi639Config - -import json import requests +from vitrage.datasources.driver_base import DriverBase +from vitrage.datasources.tmfapi639.config import TmfApi639Config +from vitrage.datasources.tmfapi639 import TMFAPI639_DATASOURCE LOG = log.getLogger(__name__) @@ -64,15 +60,11 @@ class TmfApi639Driver(DriverBase): total = [] for pairs in self.endpoints: try: - if type(pairs) is tuple: # Contains an update URL - LOG.info("Connecting to " + pairs[0] + - "with updates in " + pairs[1]) - r = requests.get(pairs[0]) - elif type(pairs) is str: # Doesn't contain update URL + if isinstance(pairs, str): # Doesn't contain update URL LOG.info("Connecting to " + pairs) - r = requests.get(pairs) - r_dict = json.loads(r.text) - total += r_dict + pairs = (pairs, "") + r = requests.get(pairs[0]) + total += r.json() except Exception as e: LOG.error("Couldn't establish connection:" + str(e)) return total @@ -81,12 +73,11 @@ class TmfApi639Driver(DriverBase): total = [] for pairs in self.endpoints: try: - if type(pairs) is tuple: # Contains an update URL + if isinstance(pairs, tuple): # Contains an update URL LOG.info("Connecting to " + pairs[0] + "with updates in " + pairs[1]) r = requests.get(pairs[1]) - r_dict = json.loads(r.text) - for e in r_dict: + for e in r.json(): if e["eventId"] < self.event_lambda: continue total.append(e["event"]["resource"])