def __init__( self, app_id=None, app_secret=None, short_token=None, long_term_token=None, version=None, timeout=None, interval_between_request=None, # if loop get data. should use this. sleep_on_rate_limit=False, proxies=None, ): self.app_id = app_id self.app_secret = app_secret self.short_token = short_token self.__timeout = timeout self.base_url = Api.GRAPH_URL self.proxies = proxies self.session = requests.Session() self.sleep_on_rate_limit = sleep_on_rate_limit self.rate_limit = RateLimit() self.interval_between_request = interval_between_request if self.interval_between_request is None: self.interval_between_request = Api.INTERVAL_BETWEEN_REQUEST if self.interval_between_request < 1: raise PyFacebookError({"message": "Min interval is 1"}) if version is None: # default version is last new. self.version = Api.VALID_API_VERSIONS[-1] else: version = str(version) if not version.startswith('v'): version = 'v' + version version_regex = re.compile(r"^v\d.\d{1,2}$") match = version_regex.search(str(version)) if match is not None: if version not in Api.VALID_API_VERSIONS: raise PyFacebookError({ "message": "Valid API version are {}".format(",".join( Api.VALID_API_VERSIONS)) }) else: self.version = version else: self.version = Api.VALID_API_VERSIONS[-1] if not (long_term_token or all([self.app_id, self.app_secret, self.short_token])): raise PyFacebookError( {'message': 'Missing long term token or app account'}) if long_term_token: self.token = long_term_token else: self.set_token(app_id=self.app_id, app_secret=self.app_secret, short_token=self.short_token)
def testBusinessLimit(self): r = RateLimit() headers = { "x-business-use-case-usage": "{\"112130216863063\":[{\"type\":\"pages\",\"call_count\":1,\"total_cputime\":1,\"total_time\":1,\"estimated_time_to_regain_access\":0}]}"} r.set_limit(headers) self.assertEqual(r.get_limit(object_id="112130216863063", endpoint="pages").call_count, 1)
def testAppLimit(self): headers = {'x-app-usage': '{"call_count":91,"total_cputime":15,"total_time":12}'} r = RateLimit() r.set_limit(headers) self.assertEqual(r.get_limit().call_count, 91) self.assertEqual(r.get_limit().max_percent(), 91) self.assertEqual(r.get_max_percent(), 91) self.assertEqual(r.get_sleep_seconds(), 2)
def __init__( self, app_id=None, app_secret=None, short_token=None, long_term_token=None, version=None, timeout=None, interval_between_request=None, # if loop get data. should use this. sleep_on_rate_limit=False, proxies=None, ): BaseApi.__init__(self, app_id=app_id, app_secret=app_secret, short_token=short_token, long_term_token=long_term_token, version=version, timeout=timeout, interval_between_request=interval_between_request, sleep_on_rate_limit=sleep_on_rate_limit, proxies=proxies) self.rate_limit = RateLimit()
def __init__( self, app_id=None, # type: Optional[str] app_secret=None, # type: Optional[str] short_token=None, # type: Optional[str] long_term_token=None, # type: Optional[str] application_only_auth=False, # type: bool initial_access_token=True, # type: bool version=None, # type: Optional[str] timeout=None, # type: Optional[int] sleep_on_rate_limit=False, # type: bool sleep_seconds_mapping=None, # type: Dict[int, int] proxies=None, # type: Optional[dict] debug_http=False # type: bool ): # type: (...) -> None """ :param app_id: Your app id. :param app_secret: Your app secret. :param short_token: short-lived token :param long_term_token: long-lived token. :param application_only_auth: Use the `App Access Token` only. :param initial_access_token: If you want use api do authorize, set this with False. :param version: The version for the graph api. :param timeout: Request time out :param sleep_on_rate_limit: Use this will sleep between two request. :param sleep_seconds_mapping: Mapping for percent to sleep. :param proxies: Your proxies config. :param debug_http: Set to True to enable debug output from urllib when performing any HTTP requests. Defaults to False. """ self.app_id = app_id self.app_secret = app_secret self.short_token = short_token self.__timeout = timeout self.base_url = self.GRAPH_URL self.proxies = proxies self.session = requests.Session() self.sleep_on_rate_limit = sleep_on_rate_limit self.instagram_business_id = None self._debug_http = debug_http self.authorization_url = self.DEFAULT_AUTHORIZATION_URL self.exchange_access_token_url = self.DEFAULT_EXCHANGE_ACCESS_TOKEN_URL self.redirect_uri = self.DEFAULT_REDIRECT_URI self.scope = self.DEFAULT_SCOPE self.auth_session = None # Authorization session self.rate_limit = RateLimit() if version is None: # default version is last new. self.version = self.VALID_API_VERSIONS[-1] else: version = str(version) if not version.startswith('v'): version = 'v' + version version_regex = re.compile(r"^v\d.\d{1,2}$") match = version_regex.search(str(version)) if match is not None: if version not in self.VALID_API_VERSIONS: raise PyFacebookException( ErrorMessage(code=ErrorCode.INVALID_PARAMS, message="Valid API version are {}".format( ",".join(self.VALID_API_VERSIONS)))) else: self.version = version else: raise PyFacebookException( ErrorMessage( code=ErrorCode.INVALID_PARAMS, message= "Version string is invalid for {0}. You can provide with like: 5.0 or v5.0" .format(version), )) self.sleep_seconds_mapping = self._build_sleep_seconds_resource( sleep_seconds_mapping) if long_term_token: self._access_token = long_term_token elif short_token and all([self.app_id, self.app_secret]): token = self.get_long_token(app_id=self.app_id, app_secret=self.app_secret, short_token=self.short_token) self._access_token = token.access_token elif application_only_auth and all([self.app_id, app_secret]): token = self.get_app_token() self._access_token = token.access_token elif not initial_access_token and all([self.app_id, app_secret]): self._access_token = None else: raise PyFacebookException( ErrorMessage( code=ErrorCode.MISSING_PARAMS, message= ("You can initial api with three methods: \n" "1. Just provide long(short) lived token or app access token with param `long_term_token`.\n" "2. Provide a short lived token and app credentials. Api will auto exchange long term token.\n" "3. Provide app credentials and with application_only_auth set to true. " "Api will auto get and use app access token.\n" "4. Provide app credentials and prepare for do authorize(This will not retrieve access token)" ))) if debug_http: from six.moves import http_client http_client.HTTPConnection.debuglevel = 1 logging.basicConfig( ) # you need to initialize logging, otherwise you will not see anything from requests logging.getLogger().setLevel(logging.DEBUG) requests_log = logging.getLogger("requests.packages.urllib3") requests_log.setLevel(logging.DEBUG) requests_log.propagate = True
def testAppLimit(self): headers = {'x-app-usage': '{"call_count":91,"total_cputime":15,"total_time":12}'} r = RateLimit() r.set_limit(headers) self.assertEqual(r.get_limit().call_count, 91)