Skip to content

Commit 156dcee

Browse files
author
Charles Larivier
committed
docs: add docstrings from Metabase API docs
Signed-off-by: Charles Larivier <charles@dribbble.com>
1 parent 0954cd1 commit 156dcee

File tree

9 files changed

+138
-21
lines changed

9 files changed

+138
-21
lines changed

metabase/resources/dataset.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class Dataset(CreateResource):
3939

4040
@classmethod
4141
def create(cls, database: int, type: str, query: dict, **kwargs) -> Dataset:
42+
"""Execute a query and retrieve the results in the usual format."""
4243
dataset = super(Dataset, cls).create(database=database, type=type, query=query)
4344
dataset.data = Data(**dataset.data)
4445
return dataset

metabase/resources/field.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ class VisibilityType(str, Enum):
112112

113113
@classmethod
114114
def get(cls, id: int) -> Field:
115+
"""Get Field with ID."""
115116
return super(Field, cls).get(id)
116117

117118
def update(
@@ -128,6 +129,7 @@ def update(
128129
coercion_strategy: str = MISSING,
129130
**kwargs,
130131
) -> None:
132+
"""Update Field with ID."""
131133
return super(Field, self).update(
132134
display_name=display_name,
133135
description=description,
@@ -142,18 +144,32 @@ def update(
142144
)
143145

144146
def related(self) -> Dict[str, Any]:
147+
"""Return related entities."""
145148
return (
146149
self.connection()
147150
.get(self.ENDPOINT + f"/{getattr(self, self.PRIMARY_KEY)}" + "/related")
148151
.json()
149152
)
150153

151154
def discard_values(self):
152-
self.connection().post(
155+
"""
156+
Discard the FieldValues belonging to this Field. Only applies to fields
157+
that have FieldValues. If this Field’s Database is set up to automatically
158+
sync FieldValues, they will be recreated during the next cycle.
159+
160+
You must be a superuser to do this.
161+
"""
162+
return self.connection().post(
153163
self.ENDPOINT + f"/{getattr(self, self.PRIMARY_KEY)}" + "/discard_values"
154164
)
155165

156166
def rescan_values(self):
157-
self.connection().post(
167+
"""
168+
Manually trigger an update for the FieldValues for this Field. Only applies
169+
to Fields that are eligible for FieldValues.
170+
171+
You must be a superuser to do this.
172+
"""
173+
return self.connection().post(
158174
self.ENDPOINT + f"/{getattr(self, self.PRIMARY_KEY)}" + "/rescan_values"
159175
)

metabase/resources/metric.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ def update(
8282
)
8383

8484
def archive(self):
85+
"""Archive a Metric."""
8586
return self.update(
8687
archived=True, revision_message="Archived by metabase-python."
8788
)

metabase/resources/permission_group.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,43 @@ class PermissionGroup(
2222

2323
@classmethod
2424
def list(cls) -> List[PermissionGroup]:
25+
"""
26+
Fetch all PermissionsGroups, including a count of the number of :members in that group.
27+
28+
You must be a superuser to do this.
29+
"""
2530
return super(PermissionGroup, cls).list()
2631

2732
@classmethod
2833
def get(cls, id: int) -> PermissionGroup:
34+
"""
35+
Fetch the details for a certain permissions group.
36+
37+
You must be a superuser to do this.
38+
"""
2939
return super(PermissionGroup, cls).get(id)
3040

3141
@classmethod
3242
def create(cls, name: str, **kwargs) -> PermissionGroup:
43+
"""
44+
Create a new PermissionsGroup.
45+
46+
You must be a superuser to do this.
47+
"""
3348
return super(PermissionGroup, cls).create(name=name, **kwargs)
3449

3550
def update(self, name: str, **kwargs) -> None:
51+
"""
52+
Update the name of a PermissionsGroup.
53+
54+
You must be a superuser to do this.
55+
"""
3656
return super(PermissionGroup, self).update(name=name, **kwargs)
57+
58+
def delete(self) -> None:
59+
"""
60+
Delete a specific PermissionsGroup.
61+
62+
You must be a superuser to do this.
63+
"""
64+
return super(PermissionGroup, self).delete()

metabase/resources/permission_membership.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ class PermissionMembership(ListResource, CreateResource, DeleteResource):
2525

2626
@classmethod
2727
def list(cls) -> List[PermissionMembership]:
28+
"""
29+
Fetch a map describing the group memberships of various users. This map’s format is:
30+
31+
{<user-id> [{:membership_id <id>
32+
:group_id <id>}]}.
33+
You must be a superuser to do this.
34+
"""
2835
response = cls.connection().get(cls.ENDPOINT)
2936
all_memberships = [
3037
item for sublist in response.json().values() for item in sublist
@@ -34,6 +41,11 @@ def list(cls) -> List[PermissionMembership]:
3441

3542
@classmethod
3643
def create(cls, group_id: int, user_id: int, **kwargs) -> PermissionMembership:
44+
"""
45+
Add a User to a PermissionsGroup. Returns updated list of members belonging to the group.
46+
47+
You must be a superuser to do this.
48+
"""
3749
response = cls.connection().post(
3850
cls.ENDPOINT, json={"group_id": group_id, "user_id": user_id}
3951
)

metabase/resources/segment.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ def update(
7676
)
7777

7878
def archive(self):
79+
"""Archive a Segment."""
7980
return self.update(
8081
archived=True, revision_message="Archived by metabase-python."
8182
)

metabase/resources/table.py

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,12 @@ class FieldOrder(str, Enum):
6161

6262
@classmethod
6363
def list(cls) -> List[Table]:
64+
"""Get all Tables."""
6465
return super(Table, cls).list()
6566

6667
@classmethod
6768
def get(cls, id: int) -> Table:
69+
"""Get Table with ID."""
6870
return super(Table, cls).get(id)
6971

7072
def update(
@@ -79,6 +81,7 @@ def update(
7981
show_in_getting_started: bool = MISSING,
8082
**kwargs,
8183
) -> None:
84+
"""Update Table with ID."""
8285
return super(Table, self).update(
8386
display_name=display_name,
8487
description=description,
@@ -90,14 +93,25 @@ def update(
9093
show_in_getting_started=show_in_getting_started,
9194
)
9295

93-
def foreign_keys(self) -> List[dict]:
96+
def fks(self) -> List[dict]:
97+
"""Get all foreign keys whose destination is a Field that belongs to this Table."""
9498
return (
9599
self.connection()
96100
.get(self.ENDPOINT + f"/{getattr(self, self.PRIMARY_KEY)}" + "/fks")
97101
.json()
98102
)
99103

100104
def query_metadata(self) -> Dict[str, Any]:
105+
"""
106+
Get metadata about a Table useful for running queries. Returns DB, fields,
107+
field FKs, and field values.
108+
109+
Passing include_hidden_fields=true will include any hidden Fields in the response.
110+
Defaults to false Passing include_sensitive_fields=true will include any sensitive
111+
Fields in the response. Defaults to false.
112+
113+
These options are provided for use in the Admin Edit Metadata page.
114+
"""
101115
return (
102116
self.connection()
103117
.get(
@@ -109,26 +123,42 @@ def query_metadata(self) -> Dict[str, Any]:
109123
)
110124

111125
def related(self) -> Dict[str, Any]:
126+
"""Return related entities."""
112127
return (
113128
self.connection()
114129
.get(self.ENDPOINT + f"/{getattr(self, self.PRIMARY_KEY)}" + "/related")
115130
.json()
116131
)
117132

118133
def discard_values(self):
134+
"""
135+
Discard the FieldValues belonging to the Fields in this Table. Only applies to
136+
fields that have FieldValues. If this Table’s Database is set up to automatically
137+
sync FieldValues, they will be recreated during the next cycle.
138+
139+
You must be a superuser to do this.
140+
"""
119141
self.connection().post(
120142
self.ENDPOINT + f"/{getattr(self, self.PRIMARY_KEY)}" + "/discard_values"
121143
)
122144

123145
def rescan_values(self):
146+
"""
147+
Manually trigger an update for the FieldValues for the Fields belonging to this Table.
148+
Only applies to Fields that are eligible for FieldValues.
149+
150+
You must be a superuser to do this.
151+
"""
124152
self.connection().post(
125153
self.ENDPOINT + f"/{getattr(self, self.PRIMARY_KEY)}" + "/rescan_values"
126154
)
127155

128156
def fields(self) -> List[Field]:
157+
"""Get all Fields associated with this Table.."""
129158
return [Field(**field) for field in self.query_metadata().get("fields")]
130159

131160
def dimensions(self) -> List[Dimension]:
161+
"""Get all Dimensions associated with this Table."""
132162
return [
133163
Dimension(id=id, **dimension)
134164
for id, dimension in self.query_metadata()
@@ -137,19 +167,9 @@ def dimensions(self) -> List[Dimension]:
137167
]
138168

139169
def metrics(self) -> List[Metric]:
170+
"""Get all Metrics associated with this Table."""
140171
return [Metric(**metric) for metric in self.related().get("metrics")]
141172

142173
def segments(self) -> List[Segment]:
174+
"""Get all Segments associated with this Table."""
143175
return [Segment(**segment) for segment in self.related().get("segments")]
144-
145-
def get_field(self, id: int) -> Field:
146-
return next(filter(lambda field: field.id == id, self.fields()))
147-
148-
def get_dimension(self, id: str) -> Dimension:
149-
return next(filter(lambda dimension: dimension.id == id, self.dimensions()))
150-
151-
def get_metric(self, id: int) -> Metric:
152-
return next(filter(lambda metric: metric.id == id, self.metrics()))
153-
154-
def get_segment(self, id: int) -> Segment:
155-
return next(filter(lambda segment: segment.id == id, self.segments()))

metabase/resources/user.py

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ class User(ListResource, CreateResource, GetResource, UpdateResource, DeleteReso
3838

3939
@classmethod
4040
def list(cls) -> List[User]:
41+
"""
42+
Fetch a list of Users. By default returns every active user but only active users.
43+
44+
If status is deactivated, include deactivated users only. If status is all, include all users (active and inactive). Also supports include_deactivated, which if true, is equivalent to status=all. status and included_deactivated requires superuser permissions.
45+
46+
For users with segmented permissions, return only themselves.
47+
48+
Takes limit, offset for pagination. Takes query for filtering on first name, last name, email. Also takes group_id, which filters on group id.
49+
"""
4150
response = cls.connection().get(cls.ENDPOINT)
4251
records = [cls(**user) for user in response.json().get("data", [])]
4352
return records
@@ -53,6 +62,11 @@ def create(
5362
login_attributes: Dict[str, Any] = None,
5463
**kwargs,
5564
) -> User:
65+
"""
66+
Create a new User, return a 400 if the email address is already taken.
67+
68+
You must be a superuser to do this.
69+
"""
5670
return super(User, cls).create(
5771
first_name=first_name,
5872
last_name=last_name,
@@ -68,28 +82,52 @@ def update(
6882
first_name: str = MISSING,
6983
last_name: str = MISSING,
7084
email: str = MISSING,
71-
password: str = MISSING,
7285
group_ids: List[int] = MISSING,
7386
is_superuser: bool = MISSING,
7487
locale: str = MISSING,
7588
**kwargs,
7689
) -> None:
90+
"""Update an existing, active User."""
7791
return super(User, self).update(
7892
first_name=first_name,
7993
last_name=last_name,
8094
email=email,
81-
password=password,
8295
group_ids=group_ids,
8396
locale=locale,
8497
**kwargs,
8598
)
8699

100+
def delete(self) -> None:
101+
"""
102+
Disable a User. This does not remove the User from the DB, but instead disables their account.
103+
104+
You must be a superuser to do this.
105+
"""
106+
return super(User, self).delete()
107+
108+
def password(self, password: str, old_password: str):
109+
"""Update a user’s password."""
110+
return self.connection().put(
111+
self.ENDPOINT + f"/{getattr(self, self.PRIMARY_KEY)}" + "/password",
112+
json={"password": password, "old_password": old_password},
113+
)
114+
87115
def send_invite(self):
88-
self.connection().put(
116+
"""
117+
Resend the user invite email for a given user.
118+
119+
You must be a superuser to do this.
120+
"""
121+
return self.connection().put(
89122
self.ENDPOINT + f"/{getattr(self, self.PRIMARY_KEY)}" + "/send_invite"
90123
)
91124

92125
def reactivate(self):
93-
self.connection().put(
126+
"""
127+
Reactivate user at :id.
128+
129+
You must be a superuser to do this.
130+
"""
131+
return self.connection().put(
94132
self.ENDPOINT + f"/{getattr(self, self.PRIMARY_KEY)}" + "/reactivate"
95133
)

tests/resources/test_table.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ def test_update(self):
4949
t.update(display_name=display_name)
5050

5151
def test_foreign_keys(self):
52-
"""Ensure Table.foreign_keys() returns a list of foreign keys as dict."""
52+
"""Ensure Table.fks() returns a list of foreign keys as dict."""
5353
table = Table.get(1)
54-
fks = table.foreign_keys()
54+
fks = table.fks()
5555

5656
self.assertIsInstance(fks, list)
5757
self.assertTrue(len(fks) > 0)

0 commit comments

Comments
 (0)