11from leetcode .models import *
22
3+ """ This module contains the QueryTemplate class for the problemsetQuestionList query.
4+ The class is used to fetch a list of LeetCode problems by the 'list' command.
5+ """
6+
37@dataclass
48class QueryResult (JSONWizard ):
59 @dataclass
@@ -53,44 +57,53 @@ class ProblemsetQuestionList(QueryTemplate):
5357 - 'difficulty' (str, optional): Difficulty level. Valid values: 'EASY', 'MEDIUM', 'HARD'.
5458 - 'status' (str, optional): Status of the problem. Valid values: 'NOT_STARTED', 'TRIED', 'AC'.
5559 limit (int, optional): Maximum number of problems to retrieve. Defaults to None.
60+ If not provided, the value is taken from the user config file.
5661 skip (int, optional): Number of problems to skip. Defaults to 0.
5762 """
5863
5964 def __init__ (self , filters = {}, limit = None , skip = 0 ):
6065 super ().__init__ ()
61-
6266 # Instance specific variables
6367 self .page : int = 1
6468 self .max_page : int = 0
6569 self .filters = filters
6670 self .limit = limit or self .config .user_config .get ('question_list_limit' )
6771 self .skip = skip
72+ self ._data_fetched : bool = False
6873
69- self .params = {'categorySlug' : "" ,
74+ self ._params = {'categorySlug' : "" ,
7075 'skip' :self .skip ,
7176 'limit' : self .limit ,
7277 'filters' : self .filters }
73-
74- self .graphql_query = None
75- self .result = None
78+
79+ self .data = None
7680
7781 def fetch_data (self , parameters : Dict = None ) -> QueryResult :
78- """ Fetches the data from the LeetCode API.
82+ """ Fetches the data from the LeetCode API.
83+ Updates the state of the object.
7984
8085 Args:
8186 parameters (dict, optional): Parameters to pass to the query. Defaults to None.
8287
8388 Returns:
8489 QueryResult: The result of the query.
8590 """
86-
87- if parameters is None :
88- parameters = self .params
89- with Loader ('Fetching problems...' , '' ):
90- self .graphql_query = GraphQLQuery (self .query , parameters )
91- self .result = self .leet_API .post_query (self .graphql_query ) # Take the response from the API
92- self .result = QueryResult .from_dict (self .result ['data' ])
93- return self .result
91+ try :
92+ with Loader ('Fetching problems...' , '' ):
93+ if parameters is not None and parameters != self .params :
94+ self .params = parameters
95+
96+ if self .data_fetched :
97+ return self .data
98+
99+ self .graphql_query = GraphQLQuery (self .query , parameters )
100+ self .data = self .leet_API .post_query (self .graphql_query ) # Take the response from the API
101+ self .data = QueryResult .from_dict (self .data ['data' ])
102+ self .data_fetched = True
103+ return self .data
104+ except Exception as e :
105+ console .print (f"{ e .__class__ .__name__ } : { e } " , style = ALERT )
106+ sys .exit (1 )
94107
95108 def _execute (self , args ):
96109 """ Executes the query with the given arguments and displays the result.
@@ -100,32 +113,31 @@ def _execute(self, args):
100113 """
101114
102115 self .__parse_args (args )
103- self .result = self .fetch_data ()
104- self .show (self .result )
116+ self .data = self .fetch_data ()
117+ self .show (self .data )
105118
106- def show (self , query_result : Optional [ QueryResult ] = None ) -> None :
119+ def show (self ) -> None :
107120 """ Displays the query result in a table.
108121
109122 Args:
110123 query_result (QueryResult, optional): The result of the query. Defaults to None.
111124 If the result is None, the method will try to fetch the data with defauly parameters and than display it.
112125 """
113-
114- if query_result is None :
115- query_result = self .fetch_data ()
116-
117- displayed : int = self .limit * self .page if self .limit * self .page < query_result .total else self .result .total
118-
119- table = LeetTable (title = f'Total number of problems retrieved: { query_result .total } \n ' ,
120- caption = f"Page #{ self .page } / ({ displayed } /{ self .result .total } )" )
121-
122- table .add_column ('ID' )
123- table .add_column ('Title' )
124- table .add_column ('Status' )
125- table .add_column ('Difficulty' )
126- for item in query_result .questions :
127- table .add_row (item .questionId , item .title , item .status , item .difficulty )
128- console .print (table )
126+ if self .data_fetched :
127+ displayed : int = self .limit * self .page if self .limit * self .page < self .data .total else self .data .total
128+
129+ table = LeetTable (title = f'Total number of problems retrieved: { self .data .total } \n ' ,
130+ caption = f"Page #{ self .page } / ({ displayed } /{ self .data .total } )" )
131+
132+ table .add_column ('ID' )
133+ table .add_column ('Title' )
134+ table .add_column ('Status' )
135+ table .add_column ('Difficulty' )
136+ for item in self .data .questions :
137+ table .add_row (item .questionId , item .title , item .status , item .difficulty )
138+ console .print (table )
139+ else :
140+ raise Exception ("Data is not fetched yet." )
129141
130142 def __validate_page (self ):
131143 """ Validates the current page number.
@@ -160,4 +172,21 @@ def __parse_args(self, args):
160172 self .page = getattr (args , 'page' )
161173 self .params ['skip' ] = self .limit * self .page - self .limit
162174
163- self .__validate_page ()
175+ self .__validate_page ()
176+
177+ @property
178+ def data_fetched (self ):
179+ return self ._data_fetched
180+
181+ @data_fetched .setter
182+ def data_fetched (self , data_fetched : bool ):
183+ self ._data_fetched = data_fetched
184+
185+ @property
186+ def params (self ):
187+ return self ._params
188+
189+ @params .setter
190+ def params (self , params : dict ):
191+ self ._params = params
192+ self .data_fetched = False
0 commit comments