Skip to content

Commit c32eebf

Browse files
hive-botclaude
andcommitted
feat: add extra_body_parameters to the developer key methods
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 2af96e1 commit c32eebf

4 files changed

Lines changed: 110 additions & 5 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3535
`CustomTagUsage` and `CustomTagUsageBreakdown`, and the request options
3636
`DeveloperKeyUsageLimitsUpdate`, `AdminUsageGroupBy` and
3737
`CustomTagUsageAggregateBy`.
38+
The developer key methods that send a request body accept
39+
`extra_body_parameters`.
3840
- Added `get_languages_for_resource()` method to the `DeepLClient` class to
3941
retrieve the languages supported by a resource, with per-feature support
4042
information, via the `/v3/languages` endpoint.

‎README.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,12 @@ admin_client.deactivate_developer_key(key)
12961296
`deactivate_developer_key()` accept either a key ID or a `DeveloperKey` object
12971297
and return the updated `DeveloperKey`.
12981298

1299+
`create_developer_key()`, `rename_developer_key()`,
1300+
`set_developer_key_usage_limits()` and `deactivate_developer_key()` also accept
1301+
`extra_body_parameters`, a dict of additional key/value pairs to include in the
1302+
JSON request body. They are added last and replace keys of the same name that
1303+
the client sets.
1304+
12991305
#### Retrieving usage analytics
13001306

13011307
`get_usage_analytics()` returns an `AdminUsageReport` for a date range. Dates

‎deepl/admin_client.py‎

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,10 @@ def get_custom_tag_usage_analytics(
166166
return CustomTagUsageReport.from_json(json)
167167

168168
def create_developer_key(
169-
self, *, label: Optional[str] = None
169+
self,
170+
*,
171+
label: Optional[str] = None,
172+
extra_body_parameters: Optional[dict] = None,
170173
) -> DeveloperKey:
171174
"""Creates a developer key in the organization. The request is only
172175
retried if it cannot have been processed (HTTP 429 or a DNS
@@ -175,11 +178,15 @@ def create_developer_key(
175178
176179
:param label: (Optional) Label for the new key. The API uses
177180
"DeepL API Key" if no label is given.
181+
:param extra_body_parameters: (Optional) Additional key/value pairs to
182+
include in the JSON request body sent to the API. They are added
183+
last and replace keys of the same name that the client sets.
178184
:return: DeveloperKey describing the created key.
179185
"""
180186
request_data = {}
181187
if label is not None:
182188
request_data["label"] = label
189+
_add_extra_body_parameters(request_data, extra_body_parameters)
183190

184191
status, content, json = self._api_call(
185192
"v2/admin/developer-keys",
@@ -208,36 +215,54 @@ def list_developer_keys(self) -> List[DeveloperKey]:
208215
]
209216

210217
def deactivate_developer_key(
211-
self, key: Union[str, DeveloperKey]
218+
self,
219+
key: Union[str, DeveloperKey],
220+
*,
221+
extra_body_parameters: Optional[dict] = None,
212222
) -> DeveloperKey:
213223
"""Deactivates a developer key. Deactivation is permanent.
214224
215225
:param key: Key ID or DeveloperKey to deactivate.
226+
:param extra_body_parameters: (Optional) Additional key/value pairs to
227+
include in the JSON request body sent to the API. They are added
228+
last and replace keys of the same name that the client sets.
216229
:return: DeveloperKey describing the deactivated key.
217230
"""
218231
return self._put_developer_key(
219-
"v2/admin/developer-keys/deactivate", {"key_id": _key_id(key)}
232+
"v2/admin/developer-keys/deactivate",
233+
{"key_id": _key_id(key)},
234+
extra_body_parameters,
220235
)
221236

222237
def rename_developer_key(
223-
self, key: Union[str, DeveloperKey], label: str
238+
self,
239+
key: Union[str, DeveloperKey],
240+
label: str,
241+
*,
242+
extra_body_parameters: Optional[dict] = None,
224243
) -> DeveloperKey:
225244
"""Changes the label of a developer key.
226245
227246
:param key: Key ID or DeveloperKey to rename.
228247
:param label: New label for the key.
248+
:param extra_body_parameters: (Optional) Additional key/value pairs to
249+
include in the JSON request body sent to the API. They are added
250+
last and replace keys of the same name that the client sets.
229251
:return: DeveloperKey describing the renamed key.
230252
"""
231253
key_id = _key_id(key)
232254
return self._put_developer_key(
233255
"v2/admin/developer-keys/label",
234256
{"key_id": key_id, "label": label},
257+
extra_body_parameters,
235258
)
236259

237260
def set_developer_key_usage_limits(
238261
self,
239262
key: Union[str, DeveloperKey],
240263
limits: DeveloperKeyUsageLimitsUpdate,
264+
*,
265+
extra_body_parameters: Optional[dict] = None,
241266
) -> DeveloperKey:
242267
"""Changes the monthly usage limits of a developer key. Only the
243268
limits set in `limits` are changed; the others keep their current
@@ -247,22 +272,40 @@ def set_developer_key_usage_limits(
247272
:param limits: DeveloperKeyUsageLimitsUpdate with the limits to
248273
change. None removes a limit (unlimited usage) and 0 blocks that
249274
usage.
275+
:param extra_body_parameters: (Optional) Additional key/value pairs to
276+
include in the JSON request body sent to the API. They are added
277+
last and replace keys of the same name that the client sets.
250278
:return: DeveloperKey describing the modified key.
251279
"""
252280
key_id = _key_id(key)
253281
return self._put_developer_key(
254282
"v2/admin/developer-keys/limits",
255283
{"key_id": key_id, **limits._to_request_dict()},
284+
extra_body_parameters,
256285
)
257286

258-
def _put_developer_key(self, url: str, request_data: dict) -> DeveloperKey:
287+
def _put_developer_key(
288+
self,
289+
url: str,
290+
request_data: dict,
291+
extra_body_parameters: Optional[dict],
292+
) -> DeveloperKey:
293+
_add_extra_body_parameters(request_data, extra_body_parameters)
259294
status, content, json = self._api_call(
260295
url, method="PUT", json=request_data
261296
)
262297
self._raise_for_status(status, content, json)
263298
return _parse_key(json)
264299

265300

301+
def _add_extra_body_parameters(
302+
request_data: dict, extra_body_parameters: Optional[dict]
303+
) -> None:
304+
# Entries replace keys of the same name set by the client.
305+
if extra_body_parameters:
306+
request_data.update(extra_body_parameters)
307+
308+
266309
def _date_range_params(
267310
start_date: Union[datetime.date, str], end_date: Union[datetime.date, str]
268311
) -> Dict[str, str]:

‎tests/offline/test_admin_client.py‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,3 +505,57 @@ def test_admin_client_applies_retry_and_timeout_options(mocked, no_retries):
505505

506506
assert len(mocked.calls) == 2
507507
assert mocked.calls[1].request.req_kwargs["timeout"] == 42.0
508+
509+
510+
@pytest.mark.parametrize(
511+
"method, path, call, body",
512+
[
513+
(
514+
"POST",
515+
"v2/admin/developer-keys",
516+
lambda c, extra: c.create_developer_key(
517+
label="a", extra_body_parameters=extra
518+
),
519+
{"label": "replaced", "flag": True},
520+
),
521+
(
522+
"PUT",
523+
"v2/admin/developer-keys/deactivate",
524+
lambda c, extra: c.deactivate_developer_key(
525+
KEY_ID, extra_body_parameters=extra
526+
),
527+
{"key_id": KEY_ID, "label": "replaced", "flag": True},
528+
),
529+
(
530+
"PUT",
531+
"v2/admin/developer-keys/label",
532+
lambda c, extra: c.rename_developer_key(
533+
KEY_ID, "a", extra_body_parameters=extra
534+
),
535+
{"key_id": KEY_ID, "label": "replaced", "flag": True},
536+
),
537+
(
538+
"PUT",
539+
"v2/admin/developer-keys/limits",
540+
lambda c, extra: c.set_developer_key_usage_limits(
541+
KEY_ID,
542+
deepl.DeveloperKeyUsageLimitsUpdate(characters=1),
543+
extra_body_parameters=extra,
544+
),
545+
{
546+
"key_id": KEY_ID,
547+
"characters": 1,
548+
"label": "replaced",
549+
"flag": True,
550+
},
551+
),
552+
],
553+
)
554+
def test_developer_key_methods_send_extra_body_parameters(
555+
mocked, method, path, call, body
556+
):
557+
mocked.add(
558+
method, url(path), match=json_request(body), json=DEVELOPER_KEY_JSON
559+
)
560+
561+
call(make_admin_client(), {"label": "replaced", "flag": True})

0 commit comments

Comments
 (0)