From 56d11e1372d355d8d564eb13ed33b0857d57555c Mon Sep 17 00:00:00 2001 From: Roman Isecke Date: Fri, 3 Nov 2023 12:38:17 -0400 Subject: [PATCH] decouple check connection from initialization method in reddit --- unstructured/ingest/connector/reddit.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/unstructured/ingest/connector/reddit.py b/unstructured/ingest/connector/reddit.py index 0e482217ab..361296c32f 100644 --- a/unstructured/ingest/connector/reddit.py +++ b/unstructured/ingest/connector/reddit.py @@ -16,6 +16,9 @@ from unstructured.ingest.logger import logger from unstructured.utils import requires_dependencies +if t.TYPE_CHECKING: + from praw import Reddit + @dataclass class SimpleRedditConfig(BaseConnectorConfig): @@ -110,16 +113,23 @@ def version(self) -> t.Optional[str]: @dataclass class RedditSourceConnector(SourceConnectorCleanupMixin, BaseSourceConnector): connector_config: SimpleRedditConfig + _reddit: t.Optional["Reddit"] = field(init=False, default=None) - @requires_dependencies(["praw"], extras="reddit") - def initialize(self): + @property + def reddit(self) -> "Reddit": from praw import Reddit - self.reddit = Reddit( - client_id=self.connector_config.client_id, - client_secret=self.connector_config.client_secret, - user_agent=self.connector_config.user_agent, - ) + if self._reddit is None: + self._reddit = Reddit( + client_id=self.connector_config.client_id, + client_secret=self.connector_config.client_secret, + user_agent=self.connector_config.user_agent, + ) + return self._reddit + + @requires_dependencies(["praw"], extras="reddit") + def initialize(self): + _ = self.reddit def check_connection(self): from praw.endpoints import API_PATH