-
Notifications
You must be signed in to change notification settings - Fork 672
Expand file tree
/
Copy pathabstractcrudobject.py
More file actions
653 lines (578 loc) · 22 KB
/
Copy pathabstractcrudobject.py
File metadata and controls
653 lines (578 loc) · 22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
# Copyright 2014 Facebook, Inc.
# You are hereby granted a non-exclusive, worldwide, royalty-free license to
# use, copy, modify, and distribute this software in source code or binary
# form for use in connection with the web services and APIs provided by
# Facebook.
# As with any software that integrates with the Facebook platform, your use
# of this software is subject to the Facebook Developer Principles and
# Policies [http://developers.facebook.com/policy/]. This copyright notice
# shall be included in all copies or substantial portions of the software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
from facebook_business.exceptions import (
FacebookBadObjectError,
)
from facebook_business.api import (
FacebookAdsApi,
Cursor,
FacebookRequest,
)
from facebook_business.adobjects.abstractobject import AbstractObject
from facebook_business.adobjects.objectparser import ObjectParser
import logging
class AbstractCrudObject(AbstractObject):
"""
Extends AbstractObject and implements methods to create, read, update,
and delete.
Attributes:
parent_id: The object's parent's id. (default None)
api: The api instance associated with this object. (default None)
"""
def __init__(self, fbid=None, parent_id=None, api=None):
"""Initializes a CRUD object.
Args:
fbid (optional): The id of the object ont the Graph.
parent_id (optional): The id of the object's parent.
api (optional): An api object which all calls will go through. If
an api object is not specified, api calls will revert to going
through the default api.
"""
super(AbstractCrudObject, self).__init__()
self._api = api or FacebookAdsApi.get_default_api()
self._changes = {}
if (parent_id is not None):
warning_message = "parent_id as a parameter of constructor is " \
"being deprecated."
logging.warning(warning_message)
self._parent_id = parent_id
self._data['id'] = fbid
self._include_summary = True
def __setitem__(self, key, value):
"""Sets an item in this CRUD object while maintaining a changelog."""
if key not in self._data or self._data[key] != value:
self._changes[key] = value
super(AbstractCrudObject, self).__setitem__(key, value)
if '_setitem_trigger' in dir(self):
self._setitem_trigger(key, value)
return self
def __delitem__(self, key):
del self._data[key]
self._changes.pop(key, None)
def __eq__(self, other):
"""Two objects are the same if they have the same fbid."""
return (
# Same class
isinstance(other, self.__class__) and
# Both have id's
self.get_id() and other.get_id() and
# Both have same id
self.get_id() == other.get_id()
)
def __ne__(self, other):
return not self.__eq__(other)
@classmethod
def get_by_ids(cls, ids, params=None, fields=None, api=None):
api = api or FacebookAdsApi.get_default_api()
params = dict(params or {})
cls._assign_fields_to_params(fields, params)
params['ids'] = ','.join(map(str, ids))
response = api.call(
'GET',
['/'],
params=params,
)
result = []
for fbid, data in response.json().items():
obj = cls(fbid, api=api)
obj._set_data(data)
result.append(obj)
return result
# Getters
def get_id(self):
"""Returns the object's fbid if set. Else, it returns None."""
return self[self.Field.id] if hasattr(self, 'Field') and hasattr(self.Field, 'Field') else self['id']
# @deprecated deprecate parent_id in AbstractCrudObject
def get_parent_id(self):
warning_message = "parent_id is being deprecated."
logging.warning(warning_message)
"""Returns the object's parent's id."""
return self._parent_id or FacebookAdsApi.get_default_account_id()
def get_api(self):
"""
Returns the api associated with the object.
"""
return self._api
def get_id_assured(self):
"""Returns the fbid of the object.
Raises:
FacebookBadObjectError if the object does not have an id.
"""
if not self.get(self.Field.id):
raise FacebookBadObjectError(
"%s object needs an id for this operation."
% self.__class__.__name__,
)
return self.get_id()
# @deprecated deprecate parent_id in AbstractCrudObject
def get_parent_id_assured(self):
"""Returns the object's parent's fbid.
Raises:
FacebookBadObjectError if the object does not have a parent id.
"""
warning_message = "parent_id is being deprecated."
logging.warning(warning_message)
if not self.get_parent_id():
raise FacebookBadObjectError(
"%s object needs a parent_id for this operation."
% self.__class__.__name__,
)
return self.get_parent_id()
def get_api_assured(self):
"""Returns the fbid of the object.
Raises:
FacebookBadObjectError if get_api returns None.
"""
api = self.get_api()
if not api:
raise FacebookBadObjectError(
"%s does not yet have an associated api object.\n"
"Did you forget to instantiate an API session with: "
"FacebookAdsApi.init(app_id, app_secret, access_token)"
% self.__class__.__name__,
)
return api
# Data management
def _clear_history(self):
self._changes = {}
if 'filename' in self._data:
del self._data['filename']
return self
def _set_data(self, data):
"""
Sets object's data as if it were read from the server.
Warning: Does not log changes.
"""
for key in map(str, data):
self[key] = data[key]
# clear history due to the update
self._changes.pop(key, None)
self._json = data
return self
def export_changed_data(self):
"""
Returns a dictionary of property names mapped to their values for
properties modified from their original values.
"""
return self.export_value(self._changes)
def export_data(self):
"""
Deprecated. Use export_all_data() or export_changed_data() instead.
"""
return self.export_changed_data()
# CRUD Helpers
def clear_id(self):
"""Clears the object's fbid."""
del self[self.Field.id]
return self
def get_node_path(self):
"""Returns the node's relative path as a tuple of tokens."""
return (self.get_id_assured(),)
def get_node_path_string(self):
"""Returns the node's path as a tuple."""
return '/'.join(self.get_node_path())
# CRUD
# @deprecated
# use Object(parent_id).create_xxx() instead
def remote_create(
self,
batch=None,
failure=None,
files=None,
params=None,
success=None,
api_version=None,
):
"""Creates the object by calling the API.
Args:
batch (optional): A FacebookAdsApiBatch object. If specified,
the call will be added to the batch.
params (optional): A mapping of request parameters where a key
is the parameter name and its value is a string or an object
which can be JSON-encoded.
files (optional): An optional mapping of file names to binary open
file objects. These files will be attached to the request.
success (optional): A callback function which will be called with
the FacebookResponse of this call if the call succeeded.
failure (optional): A callback function which will be called with
the FacebookResponse of this call if the call failed.
Returns:
self if not a batch call.
the return value of batch.add if a batch call.
"""
warning_message = "`remote_create` is being deprecated, please update your code with new function."
logging.warning(warning_message)
if self.get_id():
raise FacebookBadObjectError(
"This %s object was already created."
% self.__class__.__name__,
)
if not 'get_endpoint' in dir(self):
raise TypeError('Cannot create object of type %s.'
% self.__class__.__name__)
params = {} if not params else params.copy()
params.update(self.export_all_data())
request = None
if hasattr(self, 'api_create'):
request = self.api_create(self.get_parent_id_assured(), pending=True)
else:
request = FacebookRequest(
node_id=self.get_parent_id_assured(),
method='POST',
endpoint=self.get_endpoint(),
api=self._api,
target_class=self.__class__,
response_parser=ObjectParser(
reuse_object=self
),
)
request.add_params(params)
request.add_files(files)
if batch is not None:
def callback_success(response):
self._set_data(response.json())
self._clear_history()
if success:
success(response)
def callback_failure(response):
if failure:
failure(response)
return batch.add_request(
request=request,
success=callback_success,
failure=callback_failure,
)
else:
response = request.execute()
self._set_data(response._json)
self._clear_history()
return self
# @deprecated
# use Object(id).api_get() instead
def remote_read(
self,
batch=None,
failure=None,
fields=None,
params=None,
success=None,
api_version=None,
):
"""Reads the object by calling the API.
Args:
batch (optional): A FacebookAdsApiBatch object. If specified,
the call will be added to the batch.
fields (optional): A list of fields to read.
params (optional): A mapping of request parameters where a key
is the parameter name and its value is a string or an object
which can be JSON-encoded.
files (optional): An optional mapping of file names to binary open
file objects. These files will be attached to the request.
success (optional): A callback function which will be called with
the FacebookResponse of this call if the call succeeded.
failure (optional): A callback function which will be called with
the FacebookResponse of this call if the call failed.
Returns:
self if not a batch call.
the return value of batch.add if a batch call.
"""
warning_message = "`remote_read` is being deprecated, please update your code with new function."
logging.warning(warning_message)
params = dict(params or {})
if hasattr(self, 'api_get'):
request = self.api_get(pending=True)
else:
request = FacebookRequest(
node_id=self.get_id_assured(),
method='GET',
endpoint='/',
api=self._api,
target_class=self.__class__,
response_parser=ObjectParser(
reuse_object=self
),
)
request.add_params(params)
request.add_fields(fields)
if batch is not None:
def callback_success(response):
self._set_data(response.json())
if success:
success(response)
def callback_failure(response):
if failure:
failure(response)
batch_call = batch.add_request(
request=request,
success=callback_success,
failure=callback_failure,
)
return batch_call
else:
self = request.execute()
return self
# @deprecated
# use Object(id).api_update() instead
def remote_update(
self,
batch=None,
failure=None,
files=None,
params=None,
success=None,
api_version=None,
):
"""Updates the object by calling the API with only the changes recorded.
Args:
batch (optional): A FacebookAdsApiBatch object. If specified,
the call will be added to the batch.
params (optional): A mapping of request parameters where a key
is the parameter name and its value is a string or an object
which can be JSON-encoded.
files (optional): An optional mapping of file names to binary open
file objects. These files will be attached to the request.
success (optional): A callback function which will be called with
the FacebookResponse of this call if the call succeeded.
failure (optional): A callback function which will be called with
the FacebookResponse of this call if the call failed.
Returns:
self if not a batch call.
the return value of batch.add if a batch call.
"""
warning_message = "`remote_update` is being deprecated, please update your code with new function."
logging.warning(warning_message)
params = {} if not params else params.copy()
params.update(self.export_changed_data())
self._set_data(params)
if hasattr(self, 'api_update'):
request = self.api_update(pending=True)
else:
request = FacebookRequest(
node_id=self.get_id_assured(),
method='POST',
endpoint='/',
api=self._api,
target_class=self.__class__,
response_parser=ObjectParser(
reuse_object=self
),
)
request.add_params(params)
request.add_files(files)
if batch is not None:
def callback_success(response):
self._clear_history()
if success:
success(response)
def callback_failure(response):
if failure:
failure(response)
batch_call = batch.add_request(
request=request,
success=callback_success,
failure=callback_failure,
)
return batch_call
else:
request.execute()
self._clear_history()
return self
# @deprecated
# use Object(id).api_delete() instead
def remote_delete(
self,
batch=None,
failure=None,
params=None,
success=None,
api_version=None,
):
"""Deletes the object by calling the API with the DELETE http method.
Args:
batch (optional): A FacebookAdsApiBatch object. If specified,
the call will be added to the batch.
params (optional): A mapping of request parameters where a key
is the parameter name and its value is a string or an object
which can be JSON-encoded.
success (optional): A callback function which will be called with
the FacebookResponse of this call if the call succeeded.
failure (optional): A callback function which will be called with
the FacebookResponse of this call if the call failed.
Returns:
self if not a batch call.
the return value of batch.add if a batch call.
"""
warning_message = "`remote_delete` is being deprecated, please update your code with new function."
logging.warning(warning_message)
if hasattr(self, 'api_delete'):
request = self.api_delete(pending=True)
else:
request = FacebookRequest(
node_id=self.get_id_assured(),
method='DELETE',
endpoint='/',
api=self._api,
)
request.add_params(params)
if batch is not None:
def callback_success(response):
self.clear_id()
if success:
success(response)
def callback_failure(response):
if failure:
failure(response)
batch_call = batch.add_request(
request=request,
success=callback_success,
failure=callback_failure,
)
return batch_call
else:
request.execute()
self.clear_id()
return self
# Helpers
# @deprecated
def remote_save(self, *args, **kwargs):
"""
Calls remote_create method if object has not been created. Else, calls
the remote_update method.
"""
warning_message = "`remote_save` is being deprecated, please update your code with new function."
logging.warning(warning_message)
if self.get_id():
return self.remote_update(*args, **kwargs)
else:
return self.remote_create(*args, **kwargs)
def remote_archive(
self,
batch=None,
failure=None,
success=None
):
if 'Status' not in dir(self) or 'archived' not in dir(self.Status):
raise TypeError('Cannot archive object of type %s.'
% self.__class__.__name__)
return self.api_update(
params={
'status': self.Status.archived,
},
batch=batch,
failure=failure,
success=success,
)
# @deprecated
save = remote_save
def iterate_edge(
self,
target_objects_class,
fields=None,
params=None,
fetch_first_page=True,
include_summary=True,
endpoint=None
):
"""
Returns Cursor with argument self as source_object and
the rest as given __init__ arguments.
Note: list(iterate_edge(...)) can prefetch all the objects.
"""
source_object = self
cursor = Cursor(
source_object,
target_objects_class,
fields=fields,
params=params,
include_summary=include_summary,
endpoint=endpoint,
)
if fetch_first_page:
cursor.load_next_page()
return cursor
def iterate_edge_async(self, target_objects_class, fields=None,
params=None, is_async=False, include_summary=True,
endpoint=None):
from facebook_business.adobjects.adreportrun import AdReportRun
"""
Behaves as iterate_edge(...) if parameter is_async if False
(Default value)
If is_async is True:
Returns an AsyncJob which can be checked using remote_read()
to verify when the job is completed and the result ready to query
or download using get_result()
Example:
>>> job = object.iterate_edge_async(
TargetClass, fields, params, is_async=True)
>>> time.sleep(10)
>>> job.remote_read()
>>> if job:
result = job.read_result()
print result
"""
synchronous = not is_async
synchronous_iterator = self.iterate_edge(
target_objects_class,
fields,
params,
fetch_first_page=synchronous,
include_summary=include_summary,
)
if synchronous:
return synchronous_iterator
if not params:
params = {}
else:
params = dict(params)
self.__class__._assign_fields_to_params(fields, params)
# To force an async response from an edge, do a POST instead of GET.
# The response comes in the format of an AsyncJob which
# indicates the progress of the async request.
if endpoint is None:
endpoint = target_objects_class.get_endpoint()
response = self.get_api_assured().call(
'POST',
(self.get_id_assured(), endpoint),
params=params,
).json()
# AsyncJob stores the real iterator
# for when the result is ready to be queried
result = AdReportRun()
if 'report_run_id' in response:
response['id'] = response['report_run_id']
result._set_data(response)
return result
def edge_object(self, target_objects_class, fields=None, params=None, endpoint=None):
"""
Returns first object when iterating over Cursor with argument
self as source_object and the rest as given __init__ arguments.
"""
params = {} if not params else params.copy()
params['limit'] = '1'
for obj in self.iterate_edge(
target_objects_class,
fields=fields,
params=params,
endpoint=endpoint,
):
return obj
# if nothing found, return None
return None
def assure_call(self):
if not self._api:
raise FacebookBadObjectError(
'Api call cannot be made if api is not set')