47
47
api module contains classes that make http requests to Facebook's graph API.
48
48
"""
49
49
50
+
50
51
class FacebookResponse (object ):
51
52
52
53
"""Encapsulates an http response from Facebook's Graph API."""
@@ -126,7 +127,7 @@ def error(self):
126
127
self ._call ,
127
128
self .status (),
128
129
self .headers (),
129
- self .body ()
130
+ self .body (),
130
131
)
131
132
else :
132
133
return None
@@ -161,15 +162,17 @@ class FacebookAdsApi(object):
161
162
_default_api = None
162
163
_default_account_id = None
163
164
164
- def __init__ (self , session ):
165
+ def __init__ (self , session , api_version = None ):
165
166
"""Initializes the api instance.
166
167
Args:
167
168
session: FacebookSession object that contains a requests interface
168
169
and attribute GRAPH (the Facebook GRAPH API URL).
170
+ api_version: API version
169
171
"""
170
172
self ._session = session
171
173
self ._num_requests_succeeded = 0
172
174
self ._num_requests_attempted = 0
175
+ self ._api_version = api_version or self .API_VERSION
173
176
174
177
def get_num_requests_attempted (self ):
175
178
"""Returns the number of calls attempted."""
@@ -185,10 +188,11 @@ def init(
185
188
app_id = None ,
186
189
app_secret = None ,
187
190
access_token = None ,
188
- account_id = None
191
+ account_id = None ,
192
+ api_version = None
189
193
):
190
194
session = FacebookSession (app_id , app_secret , access_token )
191
- api = cls (session )
195
+ api = cls (session , api_version )
192
196
cls .set_default_api (api )
193
197
194
198
if account_id :
@@ -216,7 +220,7 @@ def set_default_account_id(cls, account_id):
216
220
if account_id .find ('act_' ) == - 1 :
217
221
raise ValueError (
218
222
"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_'" ,
220
224
)
221
225
cls ._default_account_id = account_id
222
226
@@ -262,10 +266,12 @@ def call(
262
266
if not files :
263
267
files = {}
264
268
269
+ api_version = api_version or self ._api_version
270
+
265
271
if api_version and not re .search ('v[0-9]+\.[0-9]+' , api_version ):
266
272
raise FacebookBadObjectError (
267
273
'Please provide the API version in the following format: %s'
268
- % self .API_VERSION
274
+ % self .API_VERSION ,
269
275
)
270
276
271
277
self ._num_requests_attempted += 1
@@ -274,7 +280,7 @@ def call(
274
280
# Path is not a full path
275
281
path = "/" .join ((
276
282
self ._session .GRAPH or url_override ,
277
- api_version or self . API_VERSION ,
283
+ api_version ,
278
284
'/' .join (map (str , path )),
279
285
))
280
286
@@ -515,6 +521,7 @@ def execute(self):
515
521
else :
516
522
return None
517
523
524
+
518
525
class FacebookRequest :
519
526
"""
520
527
Represents an API request
@@ -577,7 +584,8 @@ def add_file(self, file_path):
577
584
self ._file_counter += 1
578
585
else :
579
586
raise FacebookBadParameterError (
580
- 'Cannot find file ' + file_path + '!' )
587
+ 'Cannot find file ' + file_path + '!' ,
588
+ )
581
589
return self
582
590
583
591
def add_files (self , files ):
@@ -670,6 +678,7 @@ def _extract_value(self, value):
670
678
else :
671
679
return value
672
680
681
+
673
682
class Cursor (object ):
674
683
675
684
"""Cursor is an cursor over an object's connections.
@@ -807,6 +816,7 @@ def get_one(self):
807
816
def build_objects_from_response (self , response ):
808
817
return self ._object_parser .parse_multiple (response )
809
818
819
+
810
820
@contextmanager
811
821
def open_files (files ):
812
822
opened_files = {}
@@ -816,6 +826,7 @@ def open_files(files):
816
826
for file in opened_files .values ():
817
827
file .close ()
818
828
829
+
819
830
def _top_level_param_json_encode (params ):
820
831
params = params .copy ()
821
832
0 commit comments