Skip to content

Add support for accounts.current endpoint #221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions replicate/account.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from typing import Any, Dict, Literal

from replicate.resource import Namespace, Resource


class Account(Resource):
"""
A user or organization account on Replicate.
"""

type: Literal["user", "organization"]
"""The type of account."""

username: str
"""The username of the account."""

name: str
"""The name of the account."""

github_url: str
"""The GitHub URL of the account."""


class Accounts(Namespace):
"""
Namespace for operations related to accounts.
"""

def current(self) -> Account:
"""
Get the current account.

Returns:
Account: The current account.
"""

resp = self._client._request("GET", "/v1/account")
obj = resp.json()

return _json_to_account(obj)

async def async_current(self) -> Account:
"""
Get the current account.

Returns:
Account: The current account.
"""

resp = await self._client._async_request("GET", "/v1/account")
obj = resp.json()

return _json_to_account(obj)


def _json_to_account(json: Dict[str, Any]) -> Account:
return Account(**json)
9 changes: 9 additions & 0 deletions replicate/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from typing_extensions import Unpack

from replicate.__about__ import __version__
from replicate.account import Accounts
from replicate.collection import Collections
from replicate.deployment import Deployments
from replicate.exceptions import ReplicateError
Expand Down Expand Up @@ -92,6 +93,14 @@ async def _async_request(self, method: str, path: str, **kwargs) -> httpx.Respon

return resp

@property
def accounts(self) -> Accounts:
"""
Namespace for operations related to accounts.
"""

return Accounts(client=self)

@property
def collections(self) -> Collections:
"""
Expand Down
44 changes: 44 additions & 0 deletions tests/test_account.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import httpx
import pytest
import respx

from replicate.account import Account
from replicate.client import Client

router = respx.Router(base_url="https://api.replicate.com/v1")
router.route(
method="GET",
path="/account",
name="accounts.current",
).mock(
return_value=httpx.Response(
200,
json={
"type": "organization",
"username": "replicate",
"name": "Replicate",
"github_url": "https://github.com/replicate",
},
)
)
router.route(host="api.replicate.com").pass_through()


@pytest.mark.asyncio
@pytest.mark.parametrize("async_flag", [True, False])
async def test_account_current(async_flag):
client = Client(
api_token="test-token", transport=httpx.MockTransport(router.handler)
)

if async_flag:
account = await client.accounts.async_current()
else:
account = client.accounts.current()

assert router["accounts.current"].called
assert isinstance(account, Account)
assert account.type == "organization"
assert account.username == "replicate"
assert account.name == "Replicate"
assert account.github_url == "https://github.com/replicate"