Skip to content

Commit 6d0b396

Browse files
JiamingFBRitu
authored andcommitted
Add api version to FacebookAdsApi object so that user-specified version can be passed around.
Test Plan: python -m unit & python -m integration
1 parent f5a39dd commit 6d0b396

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

facebookads/api.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
api module contains classes that make http requests to Facebook's graph API.
4848
"""
4949

50+
5051
class FacebookResponse(object):
5152

5253
"""Encapsulates an http response from Facebook's Graph API."""
@@ -126,7 +127,7 @@ def error(self):
126127
self._call,
127128
self.status(),
128129
self.headers(),
129-
self.body()
130+
self.body(),
130131
)
131132
else:
132133
return None
@@ -161,15 +162,17 @@ class FacebookAdsApi(object):
161162
_default_api = None
162163
_default_account_id = None
163164

164-
def __init__(self, session):
165+
def __init__(self, session, api_version=None):
165166
"""Initializes the api instance.
166167
Args:
167168
session: FacebookSession object that contains a requests interface
168169
and attribute GRAPH (the Facebook GRAPH API URL).
170+
api_version: API version
169171
"""
170172
self._session = session
171173
self._num_requests_succeeded = 0
172174
self._num_requests_attempted = 0
175+
self._api_version = api_version or self.API_VERSION
173176

174177
def get_num_requests_attempted(self):
175178
"""Returns the number of calls attempted."""
@@ -185,10 +188,11 @@ def init(
185188
app_id=None,
186189
app_secret=None,
187190
access_token=None,
188-
account_id=None
191+
account_id=None,
192+
api_version=None
189193
):
190194
session = FacebookSession(app_id, app_secret, access_token)
191-
api = cls(session)
195+
api = cls(session, api_version)
192196
cls.set_default_api(api)
193197

194198
if account_id:
@@ -216,7 +220,7 @@ def set_default_account_id(cls, account_id):
216220
if account_id.find('act_') == -1:
217221
raise ValueError(
218222
"Account ID provided in FacebookAdsApi.set_default_account_id "
219-
"expects a string that begins with 'act_'"
223+
"expects a string that begins with 'act_'",
220224
)
221225
cls._default_account_id = account_id
222226

@@ -262,10 +266,12 @@ def call(
262266
if not files:
263267
files = {}
264268

269+
api_version = api_version or self._api_version
270+
265271
if api_version and not re.search('v[0-9]+\.[0-9]+', api_version):
266272
raise FacebookBadObjectError(
267273
'Please provide the API version in the following format: %s'
268-
% self.API_VERSION
274+
% self.API_VERSION,
269275
)
270276

271277
self._num_requests_attempted += 1
@@ -274,7 +280,7 @@ def call(
274280
# Path is not a full path
275281
path = "/".join((
276282
self._session.GRAPH or url_override,
277-
api_version or self.API_VERSION,
283+
api_version,
278284
'/'.join(map(str, path)),
279285
))
280286

@@ -515,6 +521,7 @@ def execute(self):
515521
else:
516522
return None
517523

524+
518525
class FacebookRequest:
519526
"""
520527
Represents an API request
@@ -577,7 +584,8 @@ def add_file(self, file_path):
577584
self._file_counter += 1
578585
else:
579586
raise FacebookBadParameterError(
580-
'Cannot find file ' + file_path + '!')
587+
'Cannot find file ' + file_path + '!',
588+
)
581589
return self
582590

583591
def add_files(self, files):
@@ -670,6 +678,7 @@ def _extract_value(self, value):
670678
else:
671679
return value
672680

681+
673682
class Cursor(object):
674683

675684
"""Cursor is an cursor over an object's connections.
@@ -807,6 +816,7 @@ def get_one(self):
807816
def build_objects_from_response(self, response):
808817
return self._object_parser.parse_multiple(response)
809818

819+
810820
@contextmanager
811821
def open_files(files):
812822
opened_files = {}
@@ -816,6 +826,7 @@ def open_files(files):
816826
for file in opened_files.values():
817827
file.close()
818828

829+
819830
def _top_level_param_json_encode(params):
820831
params = params.copy()
821832

0 commit comments

Comments
 (0)