@@ -27,13 +27,27 @@ def __init__(
2727 self .base_url = base_url .rstrip ("/" )
2828 self .session = session
2929 self .get_topics_query_id = get_topics_query_id
30+ self .api_key = api_key
31+ self .api_username = api_username
3032
3133 if api_key and api_username :
3234 self .session .headers = {
3335 "Api-Key" : api_key ,
3436 "Api-Username" : api_username ,
3537 }
3638
39+ def _require_authentication (self ):
40+ """
41+ Check if API credentials are available and raise an error if not.
42+ This should be called before accessing authenticated endpoints.
43+ a.k.a. Data Explorer endpoints.
44+ """
45+ if not self .api_key or not self .api_username :
46+ raise ValueError (
47+ "API authentication required: API key and username "
48+ "are required to access this endpoint"
49+ )
50+
3751 def __del__ (self ):
3852 self .session .close ()
3953
@@ -54,6 +68,8 @@ def get_topics(self, topic_ids):
5468 we are using it to obtain multiple Tutorials content without
5569 doing multiple API calls.
5670 """
71+ self ._require_authentication ()
72+
5773 headers = {
5874 "Accept" : "application/json" ,
5975 "Content-Type" : "multipart/form-data;" ,
@@ -167,6 +183,8 @@ def get_topic_list_by_category(self, category_id, limit=100, offset=0):
167183 - limit [int]: 100 by default, also set in data explorer
168184 - offset [int]: 0 by default (first page)
169185 """
186+ self ._require_authentication ()
187+
170188 # See https://discourse.ubuntu.com/admin/plugins/explorer?id=89
171189 data_explorer_id = 89
172190 headers = {
@@ -203,6 +221,8 @@ def get_topics_last_activity_time(self, topic_id):
203221 Args:
204222 - topic_id [int]: The topic ID
205223 """
224+ self ._require_authentication ()
225+
206226 # See https://discourse.ubuntu.com/admin/plugins/explorer?id=122
207227 data_explorer_id = 122
208228 headers = {
@@ -228,6 +248,8 @@ def get_categories_last_activity_time(self, category_id):
228248 Args:
229249 - category_id [int]: The category ID
230250 """
251+ self ._require_authentication ()
252+
231253 # See https://discourse.ubuntu.com/admin/plugins/explorer?id=123
232254 data_explorer_id = 123
233255 headers = {
@@ -331,6 +353,8 @@ def get_engage_pages_by_param(
331353 - limit [int]: 50 by default, also set in data explorer
332354 - offset [int]: 0 by default (first page)
333355 """
356+ self ._require_authentication ()
357+
334358 headers = {
335359 "Accept" : "application/json" ,
336360 "Content-Type" : "multipart/form-data;" ,
@@ -392,6 +416,8 @@ def get_engage_pages_by_tag(self, category_id, tag, limit=50, offset=0):
392416 - limit [int]: 50 by default, also set in data explorer
393417 - offset [int]: 0 by default (first page)
394418 """
419+ self ._require_authentication ()
420+
395421 headers = {
396422 "Accept" : "application/json" ,
397423 "Content-Type" : "multipart/form-data;" ,
0 commit comments