Skip to content

Commit

Permalink
comment function added
Browse files Browse the repository at this point in the history
  • Loading branch information
Leon-Sander committed Sep 16, 2023
1 parent 4153fa1 commit b0bba07
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 81 deletions.
14 changes: 10 additions & 4 deletions authentication_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,21 @@ def __init__(self, client):
self.client = client
self.token = None # Initialize the token attribute

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


def refresh_session(self):
# Fetching the SN_AUTH_COOKIE from environment variables
sn_auth_cookie = os.environ.get('SN_AUTH_COOKIE')
Expand Down
24 changes: 19 additions & 5 deletions item_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,35 @@
from retrying import retry
from gql import gql
from logger import logger
from queries import get_items_query, search_items_query, get_item_by_id_query, check_duplicate_query, get_rss_url_query
from queries import get_items_query, search_items_query, get_item_by_id_query, check_duplicate_query, create_comment_query

class ItemManager:
def __init__(self, client):
self.client = client

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


def create_comment(self, parent_id, text):
variables = {
"text": text,
"parentId": parent_id
}
response = self.execute(create_comment_query, variables)
return response["upsertComment"]["id"]

def get_items(self, limit=10, cursor=None, sort="NEW", type=None, sub=None, name=None, when=None, by=None):
variables = {
"limit": limit,
Expand Down
20 changes: 11 additions & 9 deletions main_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from authentication_manager import AuthenticationManager
from item_manager import ItemManager
from notification_manager import NotificationManager
from push_subscription_manager import PushSubscriptionManager
import os
import requests
from dotenv import load_dotenv
Expand All @@ -26,7 +25,6 @@ def __init__(self, endpoint=ENDPOINT):
self.authentication_manager = AuthenticationManager(self.client)
self.item_manager = ItemManager(self.client)
self.notification_manager = NotificationManager(self.client)
self.push_subscription_manager = PushSubscriptionManager(self.client)

def refresh_session(self):
return self.authentication_manager.refresh_session()
Expand All @@ -46,18 +44,11 @@ def get_item_by_id(self, item_id):
def check_duplicate(self, url):
return self.item_manager.check_duplicate(url)


def has_new_notifications(self):
return self.notification_manager.has_new_notifications()

def get_notifications(self, cursor=None, inc=None):
return self.notification_manager.get_notifications(cursor, inc)

def delete_push_subscription(self, endpoint):
return self.push_subscription_manager.delete_push_subscription(endpoint)

def save_push_subscription(self, endpoint, p256dh, auth, old_endpoint=None):
return self.push_subscription_manager.save_push_subscription(endpoint, p256dh, auth, old_endpoint)

def fetch_rss_feed(self):
response = requests.get(STACKER_NEWS_RSS_FEED_URL)
Expand All @@ -66,3 +57,14 @@ def fetch_rss_feed(self):
return response.text
else:
response.raise_for_status()

def create_comment(self, parent_id, text):
return self.item_manager.create_comment(parent_id, text)







#todo: make a post and make a comment function
14 changes: 10 additions & 4 deletions notification_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@ class NotificationManager:
def __init__(self, client):
self.client = client

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


def has_new_notifications(self):
query = '''
{
Expand Down
54 changes: 0 additions & 54 deletions push_subscription_manager.py

This file was deleted.

11 changes: 6 additions & 5 deletions queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,10 @@
}
"""

# Get RSS URL query
get_rss_url_query = """
query ($tag: String) {
rss(tag: $tag)
create_comment_query = """
mutation upsertComment($text: String!, $parentId: ID!) {
upsertComment(text: $text, parentId: $parentId) {
id
}
}
"""
"""

0 comments on commit b0bba07

Please sign in to comment.