-
-
Notifications
You must be signed in to change notification settings - Fork 138
/
table.py
682 lines (592 loc) · 22.9 KB
/
table.py
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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
import posixpath
import urllib.parse
import warnings
from typing import Any, Dict, Iterable, Iterator, List, Optional, Union, overload
import pyairtable.models
from pyairtable.api.retrying import Retry
from pyairtable.api.types import (
FieldName,
RecordDeletedDict,
RecordDict,
RecordId,
UpdateRecordDict,
UpsertResultDict,
WritableFields,
assert_typed_dict,
assert_typed_dicts,
)
from pyairtable.formulas import Formula, to_formula_str
from pyairtable.models.schema import FieldSchema, TableSchema, parse_field_schema
from pyairtable.utils import is_table_id
class Table:
"""
Represents an Airtable table.
Usage:
>>> api = Api(access_token)
>>> table = api.table("base_id", "table_name")
>>> records = table.all()
"""
#: The base that this table belongs to.
base: "pyairtable.api.base.Base"
#: Can be either the table name or the table ID (``tblXXXXXXXXXXXXXX``).
name: str
# Cached schema information to reduce API calls
_schema: Optional[TableSchema] = None
@overload
def __init__(
self,
api_key: str,
base_id: str,
table_name: str,
*,
timeout: Optional["pyairtable.api.api.TimeoutTuple"] = None,
retry_strategy: Optional[Retry] = None,
endpoint_url: str = "https://api.airtable.com",
): ...
@overload
def __init__(
self,
api_key: None,
base_id: "pyairtable.api.base.Base",
table_name: str,
): ...
@overload
def __init__(
self,
api_key: None,
base_id: "pyairtable.api.base.Base",
table_name: TableSchema,
): ...
def __init__(
self,
api_key: Union[None, str],
base_id: Union["pyairtable.api.base.Base", str],
table_name: Union[str, TableSchema],
**kwargs: Any,
):
"""
Old style constructor takes ``str`` arguments, and will create its own
instance of :class:`Api`. This constructor can also be provided with
keyword arguments to the :class:`Api` class.
This approach is deprecated, and will likely be removed in the future.
>>> Table("api_key", "base_id", "table_name", timeout=(1, 1))
New style constructor has an odd signature to preserve backwards-compatibility
with the old style (above), requiring ``None`` as the first argument, followed by
an instance of :class:`Base`, followed by the table name.
>>> Table(None, base, "table_name")
These signatures may change in the future. Developers using this library are
encouraged to not construct Table instances directly, and instead fetch tables
via :meth:`Api.table() <pyairtable.Api.table>`.
"""
if isinstance(api_key, str) and isinstance(base_id, str):
warnings.warn(
"Passing API keys or base IDs to pyairtable.Table is deprecated;"
" use Api.table() or Base.table() instead."
" See https://pyairtable.rtfd.org/en/latest/migrations.html for details.",
category=DeprecationWarning,
stacklevel=2,
)
api = pyairtable.api.api.Api(api_key, **kwargs)
self.base = api.base(base_id)
elif api_key is None and isinstance(base := base_id, pyairtable.api.base.Base):
self.base = base
else:
raise TypeError(
"Table() expects (None, Base, str | TableSchema);"
f" got ({type(api_key)}, {type(base_id)}, {type(table_name)})"
)
if isinstance(table_name, str):
self.name = table_name
elif isinstance(schema := table_name, TableSchema):
self._schema = schema
self.name = schema.name
else:
raise TypeError(
"Table() expects (None, Base, str | TableSchema);"
f" got ({type(api_key)}, {type(base_id)}, {type(table_name)})"
)
def __repr__(self) -> str:
if self._schema:
return f"<Table base={self.base.id!r} id={self._schema.id!r} name={self._schema.name!r}>"
return f"<Table base={self.base.id!r} name={self.name!r}>"
@property
def id(self) -> str:
"""
Get the table's Airtable ID.
If the instance was created with a name rather than an ID, this property will perform
an API request to retrieve the base's schema. For example:
.. code-block:: python
# This will not create any network traffic
>>> table = base.table('tbl00000000000123')
>>> table.id
'tbl00000000000123'
# This will fetch schema for the base when `table.id` is called
>>> table = base.table('Table Name')
>>> table.id
'tbl00000000000123'
"""
if is_table_id(self.name):
return self.name
return self.schema().id
@property
def url(self) -> str:
"""
Build the URL for this table.
"""
token = self._schema.id if self._schema else self.name
return self.api.build_url(self.base.id, urllib.parse.quote(token, safe=""))
def meta_url(self, *components: str) -> str:
"""
Build a URL to a metadata endpoint for this table.
"""
return self.api.build_url(
f"meta/bases/{self.base.id}/tables/{self.id}", *components
)
def record_url(self, record_id: RecordId, *components: str) -> str:
"""
Build the URL for the given record ID, with optional trailing components.
"""
return posixpath.join(self.url, record_id, *components)
@property
def api(self) -> "pyairtable.api.api.Api":
"""
The API connection used by the table's :class:`~pyairtable.Base`.
"""
return self.base.api
def get(self, record_id: RecordId, **options: Any) -> RecordDict:
"""
Retrieve a record by its ID.
>>> table.get('recwPQIfs4wKPyc9D')
{'id': 'recwPQIfs4wKPyc9D', 'fields': {'First Name': 'John', 'Age': 21}}
Args:
record_id: |arg_record_id|
Keyword Args:
cell_format: |kwarg_cell_format|
time_zone: |kwarg_time_zone|
user_locale: |kwarg_user_locale|
use_field_ids: |kwarg_use_field_ids|
"""
record = self.api.get(self.record_url(record_id), options=options)
return assert_typed_dict(RecordDict, record)
def iterate(self, **options: Any) -> Iterator[List[RecordDict]]:
"""
Iterate through each page of results from `List records <https://airtable.com/developers/web/api/list-records>`_.
To get all records at once, use :meth:`all`.
>>> it = table.iterate()
>>> next(it)
[{"id": ...}, {"id": ...}, {"id": ...}, ...]
>>> next(it)
[{"id": ...}, {"id": ...}, {"id": ...}, ...]
>>> next(it)
[{"id": ...}]
>>> next(it)
Traceback (most recent call last):
StopIteration
Keyword Args:
view: |kwarg_view|
page_size: |kwarg_page_size|
max_records: |kwarg_max_records|
fields: |kwarg_fields|
sort: |kwarg_sort|
formula: |kwarg_formula|
cell_format: |kwarg_cell_format|
user_locale: |kwarg_user_locale|
time_zone: |kwarg_time_zone|
use_field_ids: |kwarg_use_field_ids|
"""
if isinstance(formula := options.get("formula"), Formula):
options["formula"] = to_formula_str(formula)
for page in self.api.iterate_requests(
method="get",
url=self.url,
fallback=("post", f"{self.url}/listRecords"),
options=options,
):
yield assert_typed_dicts(RecordDict, page.get("records", []))
def all(self, **options: Any) -> List[RecordDict]:
"""
Retrieve all matching records in a single list.
>>> table = api.table('base_id', 'table_name')
>>> table.all(view='MyView', fields=['ColA', '-ColB'])
[{'fields': ...}, ...]
>>> table.all(max_records=50)
[{'fields': ...}, ...]
Keyword Args:
view: |kwarg_view|
page_size: |kwarg_page_size|
max_records: |kwarg_max_records|
fields: |kwarg_fields|
sort: |kwarg_sort|
formula: |kwarg_formula|
cell_format: |kwarg_cell_format|
user_locale: |kwarg_user_locale|
time_zone: |kwarg_time_zone|
use_field_ids: |kwarg_use_field_ids|
"""
return [record for page in self.iterate(**options) for record in page]
def first(self, **options: Any) -> Optional[RecordDict]:
"""
Retrieve the first matching record.
Returns ``None`` if no records are returned.
This is similar to :meth:`~pyairtable.Table.all`, except
it sets ``page_size`` and ``max_records`` to ``1``.
Keyword Args:
view: |kwarg_view|
fields: |kwarg_fields|
sort: |kwarg_sort|
formula: |kwarg_formula|
cell_format: |kwarg_cell_format|
user_locale: |kwarg_user_locale|
time_zone: |kwarg_time_zone|
use_field_ids: |kwarg_use_field_ids|
"""
options.update(dict(page_size=1, max_records=1))
for page in self.iterate(**options):
for record in page:
return record
return None
def create(
self,
fields: WritableFields,
typecast: bool = False,
use_field_ids: bool = False,
) -> RecordDict:
"""
Create a new record
>>> record = {'Name': 'John'}
>>> table = api.table('base_id', 'table_name')
>>> table.create(record)
Args:
fields: Fields to insert. Must be a dict with field names or IDs as keys.
typecast: |kwarg_typecast|
use_field_ids: |kwarg_use_field_ids|
"""
created = self.api.post(
url=self.url,
json={
"fields": fields,
"typecast": typecast,
"returnFieldsByFieldId": use_field_ids,
},
)
return assert_typed_dict(RecordDict, created)
def batch_create(
self,
records: Iterable[WritableFields],
typecast: bool = False,
use_field_ids: bool = False,
) -> List[RecordDict]:
"""
Create a number of new records in batches.
>>> table.batch_create([{'Name': 'John'}, {'Name': 'Marc'}])
[
{
'id': 'recW9e0c9w0er9gug',
'createdTime': '2017-03-14T22:04:31.000Z',
'fields': {'Name': 'John'}
},
{
'id': 'recW9e0c9w0er9guh',
'createdTime': '2017-03-14T22:04:31.000Z',
'fields': {'Name': 'Marc'}
}
]
Args:
records: Iterable of dicts representing records to be created.
typecast: |kwarg_typecast|
use_field_ids: |kwarg_use_field_ids|
"""
inserted_records = []
# If we got an iterator, exhaust it and collect it into a list.
records = list(records)
for chunk in self.api.chunked(records):
new_records = [{"fields": fields} for fields in chunk]
response = self.api.post(
url=self.url,
json={
"records": new_records,
"typecast": typecast,
"returnFieldsByFieldId": use_field_ids,
},
)
inserted_records += assert_typed_dicts(RecordDict, response["records"])
return inserted_records
def update(
self,
record_id: RecordId,
fields: WritableFields,
replace: bool = False,
typecast: bool = False,
use_field_ids: bool = False,
) -> RecordDict:
"""
Update a particular record ID with the given fields.
>>> table.update('recwPQIfs4wKPyc9D', {"Age": 21})
{'id': 'recwPQIfs4wKPyc9D', 'fields': {'First Name': 'John', 'Age': 21}}
>>> table.update('recwPQIfs4wKPyc9D', {"Age": 22}, replace=True)
{'id': 'recwPQIfs4wKPyc9D', 'fields': {'Age': 22}}
Args:
record_id: |arg_record_id|
fields: Fields to update. Must be a dict with column names or IDs as keys.
replace: |kwarg_replace|
typecast: |kwarg_typecast|
use_field_ids: |kwarg_use_field_ids|
"""
method = "put" if replace else "patch"
updated = self.api.request(
method=method,
url=self.record_url(record_id),
json={
"fields": fields,
"typecast": typecast,
"returnFieldsByFieldId": use_field_ids,
},
)
return assert_typed_dict(RecordDict, updated)
def batch_update(
self,
records: Iterable[UpdateRecordDict],
replace: bool = False,
typecast: bool = False,
use_field_ids: bool = False,
) -> List[RecordDict]:
"""
Update several records in batches.
Args:
records: Records to update.
replace: |kwarg_replace|
typecast: |kwarg_typecast|
use_field_ids: |kwarg_use_field_ids|
Returns:
The list of updated records.
"""
updated_records = []
method = "put" if replace else "patch"
# If we got an iterator, exhaust it and collect it into a list.
records = list(records)
for chunk in self.api.chunked(records):
chunk_records = [{"id": x["id"], "fields": x["fields"]} for x in chunk]
response = self.api.request(
method=method,
url=self.url,
json={
"records": chunk_records,
"typecast": typecast,
"returnFieldsByFieldId": use_field_ids,
},
)
updated_records += assert_typed_dicts(RecordDict, response["records"])
return updated_records
def batch_upsert(
self,
records: Iterable[Dict[str, Any]],
key_fields: List[FieldName],
replace: bool = False,
typecast: bool = False,
use_field_ids: bool = False,
) -> UpsertResultDict:
"""
Update or create records in batches, either using ``id`` (if given) or using a set of
fields (``key_fields``) to look for matches. For more information on how this operation
behaves, see Airtable's API documentation for `Update multiple records <https://airtable.com/developers/web/api/update-multiple-records#request-performupsert-fieldstomergeon>`__.
.. versionadded:: 1.5.0
Args:
records: Records to update.
key_fields: List of field names that Airtable should use to match
records in the input with existing records on the server.
replace: |kwarg_replace|
typecast: |kwarg_typecast|
use_field_ids: |kwarg_use_field_ids|
Returns:
Lists of created/updated record IDs, along with the list of all records affected.
"""
# If we got an iterator, exhaust it and collect it into a list.
records = list(records)
# The API will reject a request where a record is missing any of fieldsToMergeOn,
# but we might not reach that error until we've done several batch operations.
# To spare implementers from having to recover from a partially applied upsert,
# and to simplify our API, we will raise an exception before any network calls.
for record in records:
if "id" in record:
continue
missing = set(key_fields) - set(record.get("fields", []))
if missing:
raise ValueError(f"missing {missing!r} in {record['fields'].keys()!r}")
method = "put" if replace else "patch"
result: UpsertResultDict = {
"updatedRecords": [],
"createdRecords": [],
"records": [],
}
for chunk in self.api.chunked(records):
formatted_records = [
{k: v for (k, v) in record.items() if k in ("id", "fields")}
for record in chunk
]
response = self.api.request(
method=method,
url=self.url,
json={
"records": formatted_records,
"typecast": typecast,
"returnFieldsByFieldId": use_field_ids,
"performUpsert": {"fieldsToMergeOn": key_fields},
},
)
result["updatedRecords"].extend(response["updatedRecords"])
result["createdRecords"].extend(response["createdRecords"])
result["records"].extend(
assert_typed_dicts(RecordDict, response["records"])
)
return result
def delete(self, record_id: RecordId) -> RecordDeletedDict:
"""
Delete the given record.
>>> table.delete('recwPQIfs4wKPyc9D')
{'id': 'recwPQIfs4wKPyc9D', 'deleted': True}
Args:
record_id: |arg_record_id|
Returns:
Confirmation that the record was deleted.
"""
return assert_typed_dict(
RecordDeletedDict,
self.api.delete(self.record_url(record_id)),
)
def batch_delete(self, record_ids: Iterable[RecordId]) -> List[RecordDeletedDict]:
"""
Delete the given records, operating in batches.
>>> table.batch_delete(['recwPQIfs4wKPyc9D', 'recwDxIfs3wDPyc3F'])
[
{'id': 'recwPQIfs4wKPyc9D', 'deleted': True},
{'id': 'recwDxIfs3wDPyc3F', 'deleted': True}
]
Args:
record_ids: Record IDs to delete
Returns:
Confirmation that the records were deleted.
"""
deleted_records = []
# If we got an iterator, exhaust it and collect it into a list.
record_ids = list(record_ids)
for chunk in self.api.chunked(record_ids):
result = self.api.delete(self.url, params={"records[]": chunk})
deleted_records += assert_typed_dicts(RecordDeletedDict, result["records"])
return deleted_records
def comments(self, record_id: RecordId) -> List["pyairtable.models.Comment"]:
"""
Retrieve all comments on the given record.
Usage:
>>> table = Api.table("appNxslc6jG0XedVM", "tblslc6jG0XedVMNx")
>>> table.comments("recMNxslc6jG0XedV")
[
Comment(
id='comdVMNxslc6jG0Xe',
text='Hello, @[usrVMNxslc6jG0Xed]!',
created_time=datetime.datetime(...),
last_updated_time=None,
mentioned={
'usrVMNxslc6jG0Xed': Mentioned(
display_name='Alice',
email='alice@example.com',
id='usrVMNxslc6jG0Xed',
type='user'
)
},
author=Collaborator(
id='usr0000pyairtable',
email='pyairtable@example.com',
name='Your pyairtable access token'
)
)
]
Args:
record_id: |arg_record_id|
"""
url = self.record_url(record_id, "comments")
return [
pyairtable.models.Comment.from_api(
comment, self.api, context={"record_url": self.record_url(record_id)}
)
for page in self.api.iterate_requests("GET", url)
for comment in page["comments"]
]
def add_comment(
self,
record_id: RecordId,
text: str,
) -> "pyairtable.models.Comment":
"""
Create a comment on a record.
See `Create comment <https://airtable.com/developers/web/api/create-comment>`_ for details.
Usage:
>>> table = Api.table("appNxslc6jG0XedVM", "tblslc6jG0XedVMNx")
>>> comment = table.add_comment("recMNxslc6jG0XedV", "Hello, @[usrVMNxslc6jG0Xed]!")
>>> comment.text = "Never mind!"
>>> comment.save()
>>> comment.delete()
Args:
record_id: |arg_record_id|
text: The text of the comment. Use ``@[usrIdentifier]`` to mention users.
"""
url = self.record_url(record_id, "comments")
response = self.api.post(url, json={"text": text})
return pyairtable.models.Comment.from_api(
response, self.api, context={"record_url": self.record_url(record_id)}
)
def schema(self, *, force: bool = False) -> TableSchema:
"""
Retrieve the schema of the current table.
Usage:
>>> table.schema()
TableSchema(
id='tblslc6jG0XedVMNx',
name='My Table',
primary_field_id='fld6jG0XedVMNxFQW',
fields=[...],
views=[...]
)
Args:
force: |kwarg_force_metadata|
"""
if force or not self._schema:
self._schema = self.base.schema(force=force).table(self.name)
return self._schema
def create_field(
self,
name: str,
type: str,
description: Optional[str] = None,
options: Optional[Dict[str, Any]] = None,
) -> FieldSchema:
"""
Create a field on the table.
Args:
name: The unique name of the field.
field_type: One of the `Airtable field types <https://airtable.com/developers/web/api/model/field-type>`__.
description: A long form description of the table.
options: Only available for some field types. For more information, read about the
`Airtable field model <https://airtable.com/developers/web/api/field-model>`__.
"""
request: Dict[str, Any] = {"name": name, "type": type}
if description:
request["description"] = description
if options:
request["options"] = options
response = self.api.post(self.meta_url("fields"), json=request)
# This hopscotch ensures that the FieldSchema object we return has an API and a URL,
# and that developers don't need to reload our schema to be able to access it.
field_schema = parse_field_schema(response)
field_schema._set_api(
self.api,
context={
"base": self.base,
"table_schema": self._schema or self,
},
)
if self._schema:
self._schema.fields.append(field_schema)
return field_schema
# These are at the bottom of the module to avoid circular imports
import pyairtable.api.api # noqa
import pyairtable.api.base # noqa