1515 import json
1616
1717__all__ = ["gen_rule_payload" , "gen_params_from_config" , "load_credentials" ,
18+ "infer_endpoint" ,
1819 "validate_count_api" , "GNIP_RESP_CODES" , "change_to_count_endpoint" ]
1920
2021logger = logging .getLogger (__name__ )
@@ -192,6 +193,15 @@ def gen_params_from_config(config_dict):
192193 return _dict
193194
194195
196+ def infer_endpoint (rule_payload ):
197+ """
198+ Infer which endpoint should be used for a given rule payload.
199+ """
200+ bucket = (rule_payload if isinstance (rule_payload , dict )
201+ else json .loads (rule_payload )).get ("bucket" )
202+ return "counts" if bucket else "search"
203+
204+
195205def validate_count_api (rule_payload , endpoint ):
196206 """
197207 Ensures that the counts api is set correctly in a payload.
@@ -230,11 +240,13 @@ def load_credentials(filename=None, account_type=None):
230240 default '~/.twitter_keys.yaml'
231241 account_type (str): pass your account type, "premium" or "enterprise"
232242
233- returns: tuple of (dict, dict), both search_api args and count_api args .
243+ Returns: dict of your access credentials .
234244
235245 Example:
236246 >>> from twittersearch.api_utils import load_credentials
237- >>> search_args, count_args = load_credentials(account_type="enterprise")
247+ >>> search_args = load_credentials(account_type="premium")
248+ >>> search_args.keys()
249+ dict_keys(['bearer_token', 'endpoint'])
238250
239251 """
240252 if account_type is None or account_type not in {"premium" , "enterprise" }:
@@ -251,6 +263,4 @@ def load_credentials(filename=None, account_type=None):
251263 search_args = {"username" : search_creds ["username" ],
252264 "password" : search_creds ["password" ],
253265 "endpoint" : search_creds ["endpoint" ]}
254- count_args = {** search_args ,
255- ** {"endpoint" : change_to_count_endpoint (search_args ["endpoint" ])}}
256- return search_args , count_args
266+ return search_args
0 commit comments