diff --git a/neon_api/client.py b/neon_api/client.py index 6a90511..b8918e5 100644 --- a/neon_api/client.py +++ b/neon_api/client.py @@ -215,6 +215,20 @@ def project(self, project_id: str) -> t.Dict[str, t.Any]: return self._request("GET", r_path) + @returns_model(schema.ConnectionUri) + def connection_uri(self, project_id: str, database_name: str = None, role_name: str = None, pooled: bool = False) -> t.Dict[str, t.Any]: + """Get a connection URI for a project. + + :param project_id: The ID of the project. + :param database_name: The name of the database. + :param role_name: The name of the role. + :return: A dataclass representing the connection URI. + + More info: https://api-docs.neon.tech/reference/getconnectionuri + """ + r_params = compact_mapping({"database_name": database_name, "role_name": role_name, "pooled": pooled}) + return self._request("GET", f"projects/{project_id}/connection_uri", params=r_params) + @returns_model(schema.ProjectResponse) def project_create(self, **json: dict) -> t.Dict[str, t.Any]: """Create a new project. Accepts all keyword arguments for json body. diff --git a/neon_api/schema.py b/neon_api/schema.py index 3ce2f8b..d6698db 100644 --- a/neon_api/schema.py +++ b/neon_api/schema.py @@ -185,6 +185,11 @@ class ConnectionDetails: connection_parameters: ConnectionParameters +@dataclass +class ConnectionUri: + uri: str + + class EndpointState(Enum): init = "init" active = "active" diff --git a/tests/cassettes/test_integration/test_project.yaml b/tests/cassettes/test_integration/test_project.yaml index c65809b..32d2cf2 100644 --- a/tests/cassettes/test_integration/test_project.yaml +++ b/tests/cassettes/test_integration/test_project.yaml @@ -257,6 +257,44 @@ interactions: status: code: 200 message: OK +- request: + body: null + headers: + Accept: + - application/json + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + Content-Length: + - '0' + Content-Type: + - application/json + User-Agent: + - neon-client/python version=(0.1.0) + method: GET + uri: https://console.neon.tech/api/v2/projects/icy-dream-08641192/connection_uri?database_name=neondb&role_name=neondb_owner&pooled=True + response: + body: + string: '{"uri":"postgres://kennethreitz:SfveBzuq78bs@ep-floral-shape-a5lure6e.us-east-2.aws.neon.tech/neondb?sslmode=require"}' + headers: + Connection: + - keep-alive + Content-Length: + - '965' + Content-Type: + - application/json + Date: + - Wed, 07 Feb 2024 14:04:03 GMT + Strict-Transport-Security: + - max-age=15724800; includeSubDomains + Vary: + - Origin + X-Neon-Ret-Request-Id: + - c2dc90aab245f1de6f981b1b99e0adb6 + status: + code: 200 + message: OK - request: body: null headers: diff --git a/tests/test_integration.py b/tests/test_integration.py index 02b6a96..a042c86 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -54,6 +54,8 @@ def test_project(neon, ensure_no_projects, random_name): for project in neon.projects(shared=True).projects: assert hasattr(project, "id") + neon.connection_uri(project.id, "neondb", "neondb_owner", True) + # Delete the project. neon.project_delete(project.id)