Skip to content

Commit b0bba07

Browse files
committed
comment function added
1 parent 4153fa1 commit b0bba07

6 files changed

+56
-81
lines changed

Diff for: authentication_manager.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,21 @@ def __init__(self, client):
1515
self.client = client
1616
self.token = None # Initialize the token attribute
1717

18-
@retry(wait_exponential_multiplier=1000, wait_exponential_max=10000, stop_max_attempt_number=3)
19-
def execute(self, query, variables=None):
18+
@retry(wait_exponential_multiplier=3000, wait_exponential_max=10000, stop_max_attempt_number=3, retry_on_exception=lambda e: True)
19+
def execute(self, query, variables=None, attempt=1):
2020
gql_query = gql(query)
2121
try:
22-
return self.client.execute(gql_query, variable_values=variables)
22+
result = self.client.execute(gql_query, variable_values=variables)
23+
if attempt > 1:
24+
logger.info(f"Query succeeded on attempt {attempt}.")
25+
return result
2326
except Exception as e:
24-
logger.error(f"Error executing query: {str(e)}")
27+
logger.error(f"Error executing query on attempt {attempt}: {str(e)}")
28+
if attempt < 3: # If it's not the last attempt
29+
return self.execute(query, variables, attempt + 1)
2530
raise
2631

32+
2733
def refresh_session(self):
2834
# Fetching the SN_AUTH_COOKIE from environment variables
2935
sn_auth_cookie = os.environ.get('SN_AUTH_COOKIE')

Diff for: item_manager.py

+19-5
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,35 @@
22
from retrying import retry
33
from gql import gql
44
from logger import logger
5-
from queries import get_items_query, search_items_query, get_item_by_id_query, check_duplicate_query, get_rss_url_query
5+
from queries import get_items_query, search_items_query, get_item_by_id_query, check_duplicate_query, create_comment_query
66

77
class ItemManager:
88
def __init__(self, client):
99
self.client = client
1010

11-
@retry(wait_exponential_multiplier=1000, wait_exponential_max=10000, stop_max_attempt_number=3)
12-
def execute(self, query, variables=None):
11+
@retry(wait_exponential_multiplier=3000, wait_exponential_max=10000, stop_max_attempt_number=3, retry_on_exception=lambda e: True)
12+
def execute(self, query, variables=None, attempt=1):
1313
gql_query = gql(query)
1414
try:
15-
return self.client.execute(gql_query, variable_values=variables)
15+
result = self.client.execute(gql_query, variable_values=variables)
16+
if attempt > 1:
17+
logger.info(f"Query succeeded on attempt {attempt}.")
18+
return result
1619
except Exception as e:
17-
logger.error(f"Error executing query: {str(e)}")
20+
logger.error(f"Error executing query on attempt {attempt}: {str(e)}")
21+
if attempt < 3: # If it's not the last attempt
22+
return self.execute(query, variables, attempt + 1)
1823
raise
1924

25+
26+
def create_comment(self, parent_id, text):
27+
variables = {
28+
"text": text,
29+
"parentId": parent_id
30+
}
31+
response = self.execute(create_comment_query, variables)
32+
return response["upsertComment"]["id"]
33+
2034
def get_items(self, limit=10, cursor=None, sort="NEW", type=None, sub=None, name=None, when=None, by=None):
2135
variables = {
2236
"limit": limit,

Diff for: main_client.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from authentication_manager import AuthenticationManager
77
from item_manager import ItemManager
88
from notification_manager import NotificationManager
9-
from push_subscription_manager import PushSubscriptionManager
109
import os
1110
import requests
1211
from dotenv import load_dotenv
@@ -26,7 +25,6 @@ def __init__(self, endpoint=ENDPOINT):
2625
self.authentication_manager = AuthenticationManager(self.client)
2726
self.item_manager = ItemManager(self.client)
2827
self.notification_manager = NotificationManager(self.client)
29-
self.push_subscription_manager = PushSubscriptionManager(self.client)
3028

3129
def refresh_session(self):
3230
return self.authentication_manager.refresh_session()
@@ -46,18 +44,11 @@ def get_item_by_id(self, item_id):
4644
def check_duplicate(self, url):
4745
return self.item_manager.check_duplicate(url)
4846

49-
5047
def has_new_notifications(self):
5148
return self.notification_manager.has_new_notifications()
5249

5350
def get_notifications(self, cursor=None, inc=None):
5451
return self.notification_manager.get_notifications(cursor, inc)
55-
56-
def delete_push_subscription(self, endpoint):
57-
return self.push_subscription_manager.delete_push_subscription(endpoint)
58-
59-
def save_push_subscription(self, endpoint, p256dh, auth, old_endpoint=None):
60-
return self.push_subscription_manager.save_push_subscription(endpoint, p256dh, auth, old_endpoint)
6152

6253
def fetch_rss_feed(self):
6354
response = requests.get(STACKER_NEWS_RSS_FEED_URL)
@@ -66,3 +57,14 @@ def fetch_rss_feed(self):
6657
return response.text
6758
else:
6859
response.raise_for_status()
60+
61+
def create_comment(self, parent_id, text):
62+
return self.item_manager.create_comment(parent_id, text)
63+
64+
65+
66+
67+
68+
69+
70+
#todo: make a post and make a comment function

Diff for: notification_manager.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,21 @@ class NotificationManager:
77
def __init__(self, client):
88
self.client = client
99

10-
@retry(wait_exponential_multiplier=1000, wait_exponential_max=10000, stop_max_attempt_number=3)
11-
def execute(self, query, variables=None):
10+
@retry(wait_exponential_multiplier=3000, wait_exponential_max=10000, stop_max_attempt_number=3, retry_on_exception=lambda e: True)
11+
def execute(self, query, variables=None, attempt=1):
1212
gql_query = gql(query)
1313
try:
14-
return self.client.execute(gql_query, variable_values=variables)
14+
result = self.client.execute(gql_query, variable_values=variables)
15+
if attempt > 1:
16+
logger.info(f"Query succeeded on attempt {attempt}.")
17+
return result
1518
except Exception as e:
16-
logger.error(f"Error executing query: {str(e)}")
19+
logger.error(f"Error executing query on attempt {attempt}: {str(e)}")
20+
if attempt < 3: # If it's not the last attempt
21+
return self.execute(query, variables, attempt + 1)
1722
raise
1823

24+
1925
def has_new_notifications(self):
2026
query = '''
2127
{

Diff for: push_subscription_manager.py

-54
This file was deleted.

Diff for: queries.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,10 @@
125125
}
126126
"""
127127

128-
# Get RSS URL query
129-
get_rss_url_query = """
130-
query ($tag: String) {
131-
rss(tag: $tag)
128+
create_comment_query = """
129+
mutation upsertComment($text: String!, $parentId: ID!) {
130+
upsertComment(text: $text, parentId: $parentId) {
131+
id
132+
}
132133
}
133-
"""
134+
"""

0 commit comments

Comments
 (0)