-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add middlewares to Client in py-rattler (#915)
Co-authored-by: Bas Zalmstra <bas@prefix.dev>
- Loading branch information
1 parent
2042758
commit de51e4a
Showing
20 changed files
with
560 additions
and
1,183 deletions.
There are no files selected for viewing
4 changes: 3 additions & 1 deletion
4
py-rattler/docs/authenticated_client.md → py-rattler/docs/client.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
# AuthenticatedClient | ||
# Client | ||
|
||
::: rattler.networking | ||
|
||
::: rattler.networking.fetch_repo_data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#!/usr/bin/env -S pixi exec --spec py-rattler --spec typer -- python | ||
|
||
import asyncio | ||
from pathlib import Path | ||
from typing import get_args | ||
|
||
from rattler import install as rattler_install | ||
from rattler import LockFile, Platform | ||
from rattler.platform.platform import PlatformLiteral | ||
from rattler.networking import Client, MirrorMiddleware, AuthenticationMiddleware | ||
import typer | ||
|
||
|
||
app = typer.Typer() | ||
|
||
|
||
async def _install( | ||
lock_file_path: Path, | ||
environment_name: str, | ||
platform: Platform, | ||
target_prefix: Path, | ||
) -> None: | ||
lock_file = LockFile.from_path(lock_file_path) | ||
environment = lock_file.environment(environment_name) | ||
if environment is None: | ||
raise ValueError(f"Environment {environment_name} not found in lock file {lock_file_path}") | ||
records = environment.conda_repodata_records_for_platform(platform) | ||
if not records: | ||
raise ValueError(f"No records found for platform {platform} in lock file {lock_file_path}") | ||
await rattler_install( | ||
records=records, | ||
target_prefix=target_prefix, | ||
client=Client( | ||
middlewares=[ | ||
MirrorMiddleware({"https://conda.anaconda.org/conda-forge": ["https://repo.prefix.dev/conda-forge"]}), | ||
AuthenticationMiddleware(), | ||
] | ||
), | ||
) | ||
|
||
|
||
@app.command() | ||
def install( | ||
lock_file_path: Path = Path("pixi.lock").absolute(), | ||
environment_name: str = "default", | ||
platform: str = str(Platform.current()), | ||
target_prefix: Path = Path("env").absolute(), | ||
) -> None: | ||
""" | ||
Installs a pixi.lock file to a custom prefix. | ||
""" | ||
if platform not in get_args(PlatformLiteral): | ||
raise ValueError(f"Invalid platform {platform}. Must be one of {get_args(PlatformLiteral)}") | ||
asyncio.run( | ||
_install( | ||
lock_file_path=lock_file_path, | ||
environment_name=environment_name, | ||
platform=Platform(platform), # type: ignore[arg-type] | ||
target_prefix=target_prefix, | ||
) | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
app() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from rattler.networking.authenticated_client import AuthenticatedClient | ||
from rattler.networking.client import Client | ||
from rattler.networking.middleware import MirrorMiddleware, AuthenticationMiddleware | ||
from rattler.networking.fetch_repo_data import fetch_repo_data | ||
|
||
__all__ = ["AuthenticatedClient", "fetch_repo_data"] | ||
__all__ = ["fetch_repo_data", "Client", "MirrorMiddleware", "AuthenticationMiddleware"] |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from __future__ import annotations | ||
from rattler.rattler import PyClientWithMiddleware | ||
from rattler.networking.middleware import AuthenticationMiddleware, MirrorMiddleware | ||
|
||
|
||
class Client: | ||
""" | ||
A client that can be used to make requests. | ||
""" | ||
|
||
def __init__(self, middlewares: list[AuthenticationMiddleware | MirrorMiddleware] | None = None) -> None: | ||
self._client = PyClientWithMiddleware( | ||
[middleware._middleware for middleware in middlewares] if middlewares else None | ||
) | ||
|
||
@classmethod | ||
def _from_ffi_object(cls, client: PyClientWithMiddleware) -> Client: | ||
""" | ||
Construct py-rattler Client from PyClientWithMiddleware FFI object. | ||
""" | ||
client = cls.__new__(cls) | ||
client._client = client | ||
return client | ||
|
||
def __repr__(self) -> str: | ||
""" | ||
Returns a representation of the Client | ||
Examples | ||
-------- | ||
```python | ||
>>> Client() | ||
Client() | ||
>>> | ||
``` | ||
""" | ||
return f"{type(self).__name__}()" | ||
|
||
@staticmethod | ||
def authenticated_client() -> Client: | ||
""" | ||
Returns an authenticated client. | ||
Examples | ||
-------- | ||
```python | ||
>>> Client.authenticated_client() | ||
Client() | ||
>>> | ||
""" | ||
return Client([AuthenticationMiddleware()]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from __future__ import annotations | ||
from rattler.rattler import PyMirrorMiddleware, PyAuthenticationMiddleware | ||
|
||
|
||
class MirrorMiddleware: | ||
def __init__(self, mirrors: dict[str, list[str]]) -> None: | ||
""" | ||
Create a new MirrorMiddleware instance. | ||
The mirrors argument should be a dictionary where the keys are the | ||
original mirror URLs and the values are lists of mirror URLs to | ||
replace the original mirror with. | ||
Examples | ||
-------- | ||
```python | ||
>>> from rattler.networking import Client | ||
>>> middleware = MirrorMiddleware({"https://conda.anaconda.org/conda-forge": ["https://repo.prefix.dev/conda-forge"]}) | ||
>>> middleware | ||
MirrorMiddleware() | ||
>>> Client([middleware]) | ||
Client() | ||
>>> | ||
``` | ||
""" | ||
self._middleware = PyMirrorMiddleware(mirrors) | ||
|
||
def __repr__(self) -> str: | ||
""" | ||
Returns a representation of the Middleware | ||
Examples | ||
-------- | ||
```python | ||
>>> middleware = MirrorMiddleware({"https://conda.anaconda.org/conda-forge": ["https://repo.prefix.dev/conda-forge"]}) | ||
>>> middleware | ||
MirrorMiddleware() | ||
>>> | ||
``` | ||
""" | ||
return f"{type(self).__name__}()" | ||
|
||
|
||
class AuthenticationMiddleware: | ||
def __init__(self) -> None: | ||
self._middleware = PyAuthenticationMiddleware() | ||
|
||
def __repr__(self) -> str: | ||
""" | ||
Returns a representation of the Middleware | ||
Examples | ||
-------- | ||
```python | ||
>>> from rattler.networking import Client | ||
>>> middleware = AuthenticationMiddleware() | ||
>>> middleware | ||
AuthenticationMiddleware() | ||
>>> Client([middleware]) | ||
Client() | ||
>>> | ||
``` | ||
""" | ||
return f"{type(self).__name__}()" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.