1
1
import logging
2
2
import os
3
3
from abc import ABC , abstractmethod
4
+ from functools import cached_property
4
5
5
6
from google .cloud import bigquery
6
7
@@ -35,15 +36,18 @@ class BasePostsInsightsParser(ABC):
35
36
CREATE_POSTS_TABLE_SQL : str = ""
36
37
CREATE_INSIGHTS_TABLE_SQL : str = ""
37
38
39
+ @cached_property
40
+ def bq_client (self ) -> bigquery .Client :
41
+ return bigquery .Client (project = os .getenv ("BIGQUERY_PROJECT" , "" ))
42
+
38
43
def create_tables_if_not_exists (self ) -> None :
39
44
if not self .CREATE_POSTS_TABLE_SQL and not self .CREATE_INSIGHTS_TABLE_SQL :
40
45
raise ValueError (
41
46
"Both the SQLs to create table for posts and insights must be set"
42
47
)
43
48
44
- client = bigquery .Client (project = os .getenv ("BIGQUERY_PROJECT" , "" ))
45
49
for sql in [self .CREATE_POSTS_TABLE_SQL , self .CREATE_INSIGHTS_TABLE_SQL ]:
46
- client .query (sql )
50
+ self . bq_client .query (sql )
47
51
48
52
def save_posts_and_insights (self ) -> None :
49
53
posts = self ._request_posts_data ()
@@ -65,7 +69,6 @@ def _request_posts_data(self) -> list[dict]: ...
65
69
def _filter_new_posts (self , posts : list [dict ], last_post : dict ) -> list [dict ]: ...
66
70
67
71
def _query_last_post (self ) -> dict | None :
68
- client = bigquery .Client (project = os .getenv ("BIGQUERY_PROJECT" ))
69
72
sql = f"""
70
73
SELECT
71
74
created_at
@@ -75,7 +78,7 @@ def _query_last_post(self) -> dict | None:
75
78
created_at DESC
76
79
LIMIT 1
77
80
"""
78
- result = client .query (sql )
81
+ result = self . bq_client .query (sql )
79
82
data = list (result )
80
83
return data [0 ] if data else None
81
84
@@ -87,7 +90,6 @@ def _dump_posts_to_bigquery(self, posts: list[dict]) -> None:
87
90
logger .info ("No posts to dump!" )
88
91
return
89
92
90
- client = bigquery .Client (project = os .getenv ("BIGQUERY_PROJECT" ))
91
93
job_config = bigquery .LoadJobConfig (
92
94
schema = [
93
95
bigquery .SchemaField ("id" , "STRING" , mode = "REQUIRED" ),
@@ -97,7 +99,7 @@ def _dump_posts_to_bigquery(self, posts: list[dict]) -> None:
97
99
write_disposition = "WRITE_APPEND" ,
98
100
)
99
101
try :
100
- job = client .load_table_from_json (
102
+ job = self . bq_client .load_table_from_json (
101
103
posts ,
102
104
f"pycontw-225217.ods.{ self .POST_TABLE_NAME } " ,
103
105
job_config = job_config ,
@@ -115,7 +117,6 @@ def _dump_posts_insights_to_bigquery(self, posts: list[dict]) -> None:
115
117
logger .info ("No post insights to dump!" )
116
118
return
117
119
118
- client = bigquery .Client (project = os .getenv ("BIGQUERY_PROJECT" ))
119
120
job_config = bigquery .LoadJobConfig (
120
121
schema = [
121
122
bigquery .SchemaField ("post_id" , "STRING" , mode = "REQUIRED" ),
@@ -129,7 +130,7 @@ def _dump_posts_insights_to_bigquery(self, posts: list[dict]) -> None:
129
130
write_disposition = "WRITE_APPEND" ,
130
131
)
131
132
try :
132
- job = client .load_table_from_json (
133
+ job = self . bq_client .load_table_from_json (
133
134
posts ,
134
135
f"pycontw-225217.ods.{ self .INSIGHT_TABLE_NAME } " ,
135
136
job_config = job_config ,
0 commit comments