Skip to content

Commit 1a2417d

Browse files
feat(api): fix spec indentation
1 parent 8803680 commit 1a2417d

5 files changed

Lines changed: 229 additions & 46 deletions

File tree

.stats.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
configured_endpoints: 48
2-
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/imagekit-inc%2Fimagekit-1422f7513f230162270b197061e5768c2e0c803b94b8cd03a5e72544ac75a27f.yml
3-
openapi_spec_hash: 41175e752e6f6ce900b36aecba687fa7
2+
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/imagekit-inc%2Fimagekit-f4cd00365ba96133e0675eae3d5d3c6ac13874789e2ce69a84310ab64a4f87dd.yml
3+
openapi_spec_hash: dce632cfbb5464a98c0f5d8eb9573d68
44
config_hash: 17e408231b0b01676298010c7405f483

README.md

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ pip install imagekitio
5050
The full API of this library can be found in [api.md](api.md).
5151

5252
```python
53+
import os
5354
from imagekitio import ImageKit
5455

5556
client = ImageKit(
@@ -121,6 +122,7 @@ print(response.file_id)
121122
Simply import `AsyncImageKit` instead of `ImageKit` and use `await` with each API call:
122123

123124
```python
125+
import os
124126
import asyncio
125127
from imagekitio import AsyncImageKit
126128

@@ -161,6 +163,7 @@ pip install imagekitio[aiohttp]
161163
Then you can enable it by instantiating the client with `http_client=DefaultAioHttpClient()`:
162164

163165
```python
166+
import os
164167
import asyncio
165168
from imagekitio import DefaultAioHttpClient
166169
from imagekitio import AsyncImageKit
@@ -503,9 +506,7 @@ from pathlib import Path
503506
from imagekitio import ImageKit
504507
import io
505508

506-
client = ImageKit(
507-
private_key="My Private Key",
508-
)
509+
client = ImageKit()
509510

510511
# Method 1: Upload from bytes
511512
# Read file into memory first, then upload
@@ -579,9 +580,7 @@ All errors inherit from `imagekitio.APIError`.
579580
import imagekitio
580581
from imagekitio import ImageKit
581582

582-
client = ImageKit(
583-
private_key="My Private Key",
584-
)
583+
client = ImageKit()
585584

586585
try:
587586
# Read file into memory and upload
@@ -629,7 +628,6 @@ from imagekitio import ImageKit
629628

630629
# Configure the default for all requests:
631630
client = ImageKit(
632-
private_key="My Private Key",
633631
# default is 2
634632
max_retries=0,
635633
)
@@ -654,14 +652,12 @@ from imagekitio import ImageKit
654652

655653
# Configure the default for all requests:
656654
client = ImageKit(
657-
private_key="My Private Key",
658655
# 20 seconds (default is 1 minute)
659656
timeout=20.0,
660657
)
661658

662659
# More granular control:
663660
client = ImageKit(
664-
private_key="My Private Key",
665661
timeout=httpx.Timeout(60.0, read=5.0, write=10.0, connect=2.0),
666662
)
667663

@@ -800,7 +796,6 @@ import httpx
800796
from imagekitio import ImageKit, DefaultHttpxClient
801797

802798
client = ImageKit(
803-
private_key="My Private Key",
804799
# Or use the `IMAGE_KIT_BASE_URL` env var
805800
base_url="http://my.test.server.example.com:8083",
806801
http_client=DefaultHttpxClient(
@@ -823,9 +818,7 @@ By default the library closes underlying HTTP connections whenever the client is
823818
```py
824819
from imagekitio import ImageKit
825820

826-
with ImageKit(
827-
private_key="My Private Key",
828-
) as client:
821+
with ImageKit() as client:
829822
# make requests here
830823
...
831824

src/imagekitio/_client.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import annotations
44

55
import os
6+
import base64
67
from typing import TYPE_CHECKING, Any, Mapping
78
from typing_extensions import Self, override
89

@@ -12,6 +13,7 @@
1213
from ._qs import Querystring
1314
from ._types import (
1415
Omit,
16+
Headers,
1517
Timeout,
1618
NotGiven,
1719
Transport,
@@ -216,6 +218,15 @@ def with_streaming_response(self) -> ImageKitWithStreamedResponse:
216218
def qs(self) -> Querystring:
217219
return Querystring(array_format="comma")
218220

221+
@property
222+
@override
223+
def auth_headers(self) -> dict[str, str]:
224+
if self.password is None:
225+
return {}
226+
credentials = f"{self.private_key}:{self.password}".encode("ascii")
227+
header = f"Basic {base64.b64encode(credentials).decode('ascii')}"
228+
return {"Authorization": header}
229+
219230
@property
220231
@override
221232
def default_headers(self) -> dict[str, str | Omit]:
@@ -225,6 +236,15 @@ def default_headers(self) -> dict[str, str | Omit]:
225236
**self._custom_headers,
226237
}
227238

239+
@override
240+
def _validate_headers(self, headers: Headers, custom_headers: Headers) -> None:
241+
if headers.get("Authorization") or isinstance(custom_headers.get("Authorization"), Omit):
242+
return
243+
244+
raise TypeError(
245+
'"Could not resolve authentication method. Expected the private_key or password to be set. Or for the `Authorization` headers to be explicitly omitted"'
246+
)
247+
228248
def copy(
229249
self,
230250
*,
@@ -466,6 +486,15 @@ def with_streaming_response(self) -> AsyncImageKitWithStreamedResponse:
466486
def qs(self) -> Querystring:
467487
return Querystring(array_format="comma")
468488

489+
@property
490+
@override
491+
def auth_headers(self) -> dict[str, str]:
492+
if self.password is None:
493+
return {}
494+
credentials = f"{self.private_key}:{self.password}".encode("ascii")
495+
header = f"Basic {base64.b64encode(credentials).decode('ascii')}"
496+
return {"Authorization": header}
497+
469498
@property
470499
@override
471500
def default_headers(self) -> dict[str, str | Omit]:
@@ -475,6 +504,15 @@ def default_headers(self) -> dict[str, str | Omit]:
475504
**self._custom_headers,
476505
}
477506

507+
@override
508+
def _validate_headers(self, headers: Headers, custom_headers: Headers) -> None:
509+
if headers.get("Authorization") or isinstance(custom_headers.get("Authorization"), Omit):
510+
return
511+
512+
raise TypeError(
513+
'"Could not resolve authentication method. Expected the private_key or password to be set. Or for the `Authorization` headers to be explicitly omitted"'
514+
)
515+
478516
def copy(
479517
self,
480518
*,

tests/conftest.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def pytest_collection_modifyitems(items: list[pytest.Function]) -> None:
4646
base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
4747

4848
private_key = "My Private Key"
49+
password = "My Password"
4950

5051

5152
@pytest.fixture(scope="session")
@@ -54,7 +55,9 @@ def client(request: FixtureRequest) -> Iterator[ImageKit]:
5455
if not isinstance(strict, bool):
5556
raise TypeError(f"Unexpected fixture parameter type {type(strict)}, expected {bool}")
5657

57-
with ImageKit(base_url=base_url, private_key=private_key, _strict_response_validation=strict) as client:
58+
with ImageKit(
59+
base_url=base_url, private_key=private_key, password=password, _strict_response_validation=strict
60+
) as client:
5861
yield client
5962

6063

@@ -79,6 +82,10 @@ async def async_client(request: FixtureRequest) -> AsyncIterator[AsyncImageKit]:
7982
raise TypeError(f"Unexpected fixture parameter type {type(param)}, expected bool or dict")
8083

8184
async with AsyncImageKit(
82-
base_url=base_url, private_key=private_key, _strict_response_validation=strict, http_client=http_client
85+
base_url=base_url,
86+
private_key=private_key,
87+
password=password,
88+
_strict_response_validation=strict,
89+
http_client=http_client,
8390
) as client:
8491
yield client

0 commit comments

Comments
 (0)