Skip to content
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

Update http.py for 2.x support #113

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
17 changes: 15 additions & 2 deletions surrealdb/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class SurrealHTTP:
database: The database to use for the connection.
username: The username to use for the connection.
password: The password to use for the connection.
version: The version of SurrealDB (1 or 2). Defaults to 1.
"""

def __init__(
Expand All @@ -71,12 +72,24 @@ def __init__(
database: str,
username: str,
password: str,
version: int = 1,
) -> None:
self._url = url
self._namespace = namespace
self._database = database
self._username = username
self._password = password
self._version = version

# Determine header names based on version
if self._version == 1:
ns_header_key = "NS"
db_header_key = "DB"
elif self._version == 2:
ns_header_key = "surreal-ns"
db_header_key = "surreal-db"
else:
raise ValueError(f"Unsupported SurrealDB version: {self._version}")

self._http = httpx.AsyncClient(
base_url=self._url,
Expand All @@ -85,8 +98,8 @@ def __init__(
password=self._password,
),
headers={
"NS": self._namespace,
"DB": self._database,
ns_header_key: self._namespace,
db_header_key: self._database,
"Accept": "application/json",
"Content-Type": "application/json",
},
Expand Down