From fd52d61fa1b36d4da3c3666f901389f73216f26d Mon Sep 17 00:00:00 2001 From: hacongbang92 Date: Wed, 23 Aug 2023 09:11:38 +0700 Subject: [PATCH 1/3] Upgrade version 16 --- facebook/__init__.py | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/facebook/__init__.py b/facebook/__init__.py index e91c5939..b259ebc9 100755 --- a/facebook/__init__.py +++ b/facebook/__init__.py @@ -41,7 +41,7 @@ FACEBOOK_GRAPH_URL = "https://graph.facebook.com/" FACEBOOK_WWW_URL = "https://www.facebook.com/" FACEBOOK_OAUTH_DIALOG_PATH = "dialog/oauth?" -VALID_API_VERSIONS = ["3.1", "3.2", "3.3", "4.0", "5.0", "6.0", "7.0", "8.0"] +DEFAULT_VERSION = "16.0" VALID_SEARCH_TYPES = ["place", "placetopic"] @@ -83,9 +83,6 @@ def __init__( session=None, app_secret=None, ): - # The default version is only used if the version kwarg does not exist. - default_version = VALID_API_VERSIONS[0] - self.access_token = access_token self.timeout = timeout self.proxies = proxies @@ -93,23 +90,18 @@ def __init__( self.app_secret_hmac = None if version: - version_regex = re.compile(r"^\d\.\d{1,2}$") + version_regex = re.compile(r"^\d+\.\d{1,2}$") match = version_regex.search(str(version)) if match is not None: - if str(version) not in VALID_API_VERSIONS: - raise GraphAPIError( - "Valid API versions are " - + str(VALID_API_VERSIONS).strip("[]") - ) - else: - self.version = "v" + str(version) + self.version = "v" + str(version) else: raise GraphAPIError( "Version number should be in the" " following format: #.# (e.g. 2.0)." ) else: - self.version = "v" + default_version + # The default version is only used if the version kwarg does not exist. + self.version = "v" + DEFAULT_VERSION if app_secret and access_token: self.app_secret_hmac = hmac.new( From 8923c77d7cc76a79322e401c3c0878eeff4ab8e1 Mon Sep 17 00:00:00 2001 From: hacongbang92 Date: Wed, 23 Aug 2023 09:15:12 +0700 Subject: [PATCH 2/3] Update test_versions.py --- test/test_versions.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/test_versions.py b/test/test_versions.py index 7cb33970..39d4eb5b 100644 --- a/test/test_versions.py +++ b/test/test_versions.py @@ -13,9 +13,9 @@ def test_no_version(self): ) def test_valid_versions(self): - for version in facebook.VALID_API_VERSIONS: - graph = facebook.GraphAPI(version=version) - self.assertEqual(str(graph.get_version()), version) + graph = facebook.GraphAPI(version=facebook.DEFAULT_VERSION) + self.assertEqual(str(graph.get_version()), facebook.DEFAULT_VERSION) + def test_invalid_version(self): self.assertRaises( From 67594afcfb02bbc96ff33486bc61af364365213d Mon Sep 17 00:00:00 2001 From: Thomas Date: Wed, 23 Aug 2023 10:20:00 +0700 Subject: [PATCH 3/3] Upgrade Version 16.0 --- facebook/__init__.py | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/facebook/__init__.py b/facebook/__init__.py index b259ebc9..d377ee2f 100755 --- a/facebook/__init__.py +++ b/facebook/__init__.py @@ -41,7 +41,7 @@ FACEBOOK_GRAPH_URL = "https://graph.facebook.com/" FACEBOOK_WWW_URL = "https://www.facebook.com/" FACEBOOK_OAUTH_DIALOG_PATH = "dialog/oauth?" -DEFAULT_VERSION = "16.0" +VALID_API_VERSIONS = ["4.0", "5.0", "6.0", "7.0", "8.0", "9.0", "10.0", "11.0", "12.0", "13.0", "14.0", "15.0", "16.0"] VALID_SEARCH_TYPES = ["place", "placetopic"] @@ -75,14 +75,17 @@ class GraphAPI(object): """ def __init__( - self, - access_token=None, - timeout=None, - version=None, - proxies=None, - session=None, - app_secret=None, + self, + access_token=None, + timeout=None, + version=None, + proxies=None, + session=None, + app_secret=None, ): + # The default version is only used if the version kwarg does not exist. + default_version = VALID_API_VERSIONS[0] + self.access_token = access_token self.timeout = timeout self.proxies = proxies @@ -90,18 +93,23 @@ def __init__( self.app_secret_hmac = None if version: - version_regex = re.compile(r"^\d+\.\d{1,2}$") + version_regex = re.compile(r"^\d{1,2}\.\d{1,2}$") match = version_regex.search(str(version)) if match is not None: - self.version = "v" + str(version) + if str(version) not in VALID_API_VERSIONS: + raise GraphAPIError( + "Valid API versions are " + + str(VALID_API_VERSIONS).strip("[]") + ) + else: + self.version = "v" + str(version) else: raise GraphAPIError( "Version number should be in the" " following format: #.# (e.g. 2.0)." ) else: - # The default version is only used if the version kwarg does not exist. - self.version = "v" + DEFAULT_VERSION + self.version = "v" + default_version if app_secret and access_token: self.app_secret_hmac = hmac.new( @@ -233,7 +241,7 @@ def get_version(self): params=args, timeout=self.timeout, proxies=self.proxies, - ) + ) except requests.HTTPError as e: response = json.loads(e.read()) raise GraphAPIError(response) @@ -246,7 +254,7 @@ def get_version(self): raise GraphAPIError("API version number not available") def request( - self, path, args=None, post_args=None, files=None, method=None + self, path, args=None, post_args=None, files=None, method=None ): """Fetches the given path in the Graph API. @@ -284,7 +292,7 @@ def _add_to_post_args_or_args(arg_name, arg_value): data=post_args, proxies=self.proxies, files=files, - ) + ) except requests.HTTPError as e: response = json.loads(e.read()) raise GraphAPIError(response) @@ -336,7 +344,7 @@ def get_app_access_token(self, app_id, app_secret, offline=False): )["access_token"] def get_access_token_from_code( - self, code, redirect_uri, app_id, app_secret + self, code, redirect_uri, app_id, app_secret ): """Get an access token from the "code" returned from an OAuth dialog. @@ -468,7 +476,7 @@ def get_user_from_cookie(cookies, app_id, app_secret): def parse_signed_request(signed_request, app_secret): - """Return dictionary with signed request data. + """ Return dictionary with signed request data. We return a dictionary containing the information in the signed_request. This includes a user_id if the user has authorised @@ -511,4 +519,4 @@ def parse_signed_request(signed_request, app_secret): if sig != expected_sig: return False - return data + return data \ No newline at end of file