Skip to content

Commit

Permalink
Adds support for connection_uri GET request
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgomes committed Nov 10, 2024
1 parent 15176cf commit 55c57a8
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
14 changes: 14 additions & 0 deletions neon_api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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) -> 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})
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.
Expand Down
3 changes: 3 additions & 0 deletions neon_api/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ class ConnectionDetails:
connection_uri: str
connection_parameters: ConnectionParameters

@dataclass
class ConnectionUri:
uri: str

class EndpointState(Enum):
init = "init"
Expand Down
38 changes: 38 additions & 0 deletions tests/cassettes/test_integration/test_project.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
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:
Expand Down
2 changes: 2 additions & 0 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

# Delete the project.
neon.project_delete(project.id)

Expand Down

0 comments on commit 55c57a8

Please sign in to comment.