From 1a58286f188fbc71dc75c87bd7714c46dcdf23c2 Mon Sep 17 00:00:00 2001 From: Gioele Falcetti Date: Thu, 8 Oct 2020 22:59:57 +0200 Subject: Add to the Api class users list parser and additional userID parameter to the feeds parser Signed-off-by: Gioele Falcetti --- nextcloud_news_updater/api/api.py | 29 +++++++++++++++++++---------- nextcloud_news_updater/api/cli.py | 13 +++++++------ nextcloud_news_updater/api/web.py | 8 ++++---- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/nextcloud_news_updater/api/api.py b/nextcloud_news_updater/api/api.py index 837583c..0d5c7e8 100644 --- a/nextcloud_news_updater/api/api.py +++ b/nextcloud_news_updater/api/api.py @@ -13,17 +13,26 @@ class Feed: class Api: - def parse_feed(self, json_string: str) -> List[Feed]: - """ - Wrapper around json.loads for better error messages - """ + """API JSON results parser""" + + def parse_users(self, json_str: str) -> List[str]: + """Returns a list of userIDs from JSON data""" + try: + users_dict = json.loads(json_str) + return list(users_dict.keys()) + except ValueError: + msg = 'Could not parse the JSON user list: %s' % json_str + raise ValueError(msg) + + def parse_feeds(self, json_str: str, userID: str = None) -> List[Feed]: + """Returns a list of feeds from JSON data""" try: - feed_json = json.loads(json_string) - return self._parse_json(feed_json) + feeds_json = json.loads(json_str) + return self._parse_feeds_json(feeds_json, userID) except ValueError: - msg = "Could not parse given JSON: %s" % json_string + msg = 'Could not parse given JSON: %s' % json_str raise ValueError(msg) - def _parse_json(self, feed_json: Any) -> List[Feed]: - feed_json = feed_json['feeds'] - return [Feed(info['id'], info['userId']) for info in feed_json] + def _parse_feeds_json(self, feeds: dict, userID: str) -> List[Feed]: + feeds = feeds['feeds'] + return [Feed(info['id'], info['userId']) for info in feeds] diff --git a/nextcloud_news_updater/api/cli.py b/nextcloud_news_updater/api/cli.py index 1a7a315..eece702 100644 --- a/nextcloud_news_updater/api/cli.py +++ b/nextcloud_news_updater/api/cli.py @@ -13,6 +13,8 @@ class Cli: class CliApi(Api): + """Cli API for Nextcloud News up to v14 (API version 1.2)""" + def __init__(self, config: Config) -> None: directory = config.url phpini = config.phpini @@ -31,12 +33,11 @@ class CliApi(Api): class CliApiV2(CliApi): - def __init__(self, config: Config) -> None: - super().__init__(config) + """Cli API for Nextcloud News up to v14 (API version 2)""" - def _parse_json(self, feed_json: Any) -> List[Feed]: - feed_json = feed_json['updater'] - return [Feed(info['feedId'], info['userId']) for info in feed_json] + def _parse_feeds_json(self, feeds: dict, userID: str) -> List[Feed]: + feeds = feeds['updater'] + return [Feed(info['feedId'], info['userId']) for info in feeds] def create_cli_api(config: Config) -> CliApi: @@ -81,7 +82,7 @@ class CliUpdater(Updater): self.logger.info('Running get all feeds command: %s' % ' '.join(self.api.all_feeds_command)) self.logger.info('Received these feeds to update: %s' % feeds_json) - return self.api.parse_feed(feeds_json) + return self.api.parse_feeds(feeds_json) def after_update(self) -> None: self.logger.info('Running after update command: %s' % diff --git a/nextcloud_news_updater/api/web.py b/nextcloud_news_updater/api/web.py index 5516a58..b7d5f53 100644 --- a/nextcloud_news_updater/api/web.py +++ b/nextcloud_news_updater/api/web.py @@ -36,9 +36,9 @@ class WebApiV2(WebApi): self.all_feeds_url = '%s/updater/all-feeds' % self.base_url self.update_url = '%s/updater/update-feed' % self.base_url - def _parse_json(self, feed_json: Any) -> List[Feed]: - feed_json = feed_json['updater'] - return [Feed(info['feedId'], info['userId']) for info in feed_json] + def _parse_feeds_json(self, feeds: dict, userID: str) -> List[Feed]: + feeds = feeds['updater'] + return [Feed(info['feedId'], info['userId']) for info in feeds] def create_web_api(config: Config) -> WebApi: @@ -82,7 +82,7 @@ class WebUpdater(Updater): def all_feeds(self) -> List[Feed]: feeds_json = self.client.get(self.api.all_feeds_url, self.auth) self.logger.info('Received these feeds to update: %s' % feeds_json) - return self.api.parse_feed(feeds_json) + return self.api.parse_feeds(feeds_json) def after_update(self) -> None: self.logger.info( -- cgit v1.2.3