Skip to content

Commit

Permalink
Merge branch 'master' into feature/#92-docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
erichare authored Nov 13, 2023
2 parents 6730957 + fdd5a92 commit 7f4ed5f
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 55 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ on:
jobs:
test:
env:
ASTRA_DB_ID: ${{ secrets.ASTRA_DB_ID }}
ASTRA_DB_REGION: ${{ secrets.ASTRA_DB_REGION }}
ASTRA_DB_API_ENDPOINT: ${{ secrets.ASTRA_DB_API_ENDPOINT }}
ASTRA_DB_APPLICATION_TOKEN: ${{ secrets.ASTRA_DB_APPLICATION_TOKEN }}
ASTRA_DB_REGION: ${{ secrets.ASTRA_DB_REGION }}
runs-on: ubuntu-latest

steps:
Expand Down
2 changes: 1 addition & 1 deletion astrapy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
__version__ = "0.6.0"
__version__ = "0.6.1"
30 changes: 16 additions & 14 deletions astrapy/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@

from astrapy.defaults import (
DEFAULT_AUTH_HEADER,
DEFAULT_JSON_API_PATH,
DEFAULT_JSON_API_VERSION,
DEFAULT_KEYSPACE_NAME,
DEFAULT_BASE_PATH,
)
from astrapy.utils import make_payload, make_request, http_methods, parse_endpoint_url
from astrapy.utils import make_payload, make_request, http_methods

import logging
import json
Expand Down Expand Up @@ -53,7 +54,7 @@ def __init__(

self.astra_db = astra_db
self.collection_name = collection_name
self.base_path = f"{self.astra_db.base_path}/{collection_name}"
self.base_path = f"{self.astra_db.base_path}/{self.collection_name}"

def __repr__(self):
return f'Astra DB Collection[name="{self.collection_name}", endpoint="{self.astra_db.base_url}"]'
Expand Down Expand Up @@ -659,6 +660,8 @@ def __init__(
self,
token=None,
api_endpoint=None,
api_path=None,
api_version=None,
namespace=None,
):
"""
Expand All @@ -677,23 +680,22 @@ def __init__(
)
namespace = DEFAULT_KEYSPACE_NAME

# Store the initial parameters
# Store the API token
self.token = token
(
self.database_id,
self.database_region,
self.database_domain,
) = parse_endpoint_url(api_endpoint)

# Set the Base URL for the API calls
self.base_url = (
f"https://{self.database_id}-{self.database_region}.{self.database_domain}"
)
self.base_path = f"{DEFAULT_BASE_PATH}/{namespace}"
self.base_url = api_endpoint.strip("/")

# Set the API version and path from the call
self.api_path = (api_path or DEFAULT_JSON_API_PATH).strip("/")
self.api_version = (api_version or DEFAULT_JSON_API_VERSION).strip("/")

# Set the namespace parameter
# Set the namespace
self.namespace = namespace

# Finally, construct the full base path
self.base_path = f"/{self.api_path}/{self.api_version}/{self.namespace}"

def __repr__(self):
return f'Astra DB[endpoint="{self.base_url}"]'

Expand Down
10 changes: 6 additions & 4 deletions astrapy/defaults.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
DEFAULT_AUTH_PATH = "/api/rest/v1/auth"
DEFAULT_BASE_PATH = "/api/json/v1"

DEFAULT_JSON_API_PATH = "/api/json"
DEFAULT_JSON_API_VERSION = "v1"

DEFAULT_DEV_OPS_URL = "api.astra.datastax.com"
DEFAULT_DEV_OPS_API_VERSION = "v2"

DEFAULT_TIMEOUT = 30000
DEFAULT_AUTH_HEADER = "X-Cassandra-Token"
DEFAULT_KEYSPACE_NAME = "default_keyspace"
DEFAULT_REGION = "us-east1"

DEFAULT_DEV_OPS_PATH_PREFIX = "/v2"
DEFAULT_DEV_OPS_URL = "api.astra.datastax.com"
11 changes: 7 additions & 4 deletions astrapy/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,22 @@
# limitations under the License.

from astrapy.utils import make_request, http_methods
from astrapy.defaults import DEFAULT_DEV_OPS_PATH_PREFIX, DEFAULT_DEV_OPS_URL
from astrapy.defaults import DEFAULT_DEV_OPS_API_VERSION, DEFAULT_DEV_OPS_URL

import logging

logger = logging.getLogger(__name__)


class AstraDBOps:
def __init__(self, token, dev_ops_url=None):
dev_ops_url = dev_ops_url or DEFAULT_DEV_OPS_URL
def __init__(self, token, dev_ops_url=None, dev_ops_api_version=None):
dev_ops_url = (dev_ops_url or DEFAULT_DEV_OPS_URL).strip("/")
dev_ops_api_version = (
dev_ops_api_version or DEFAULT_DEV_OPS_API_VERSION
).strip("/")

self.token = "Bearer " + token
self.base_url = f"https://{dev_ops_url}{DEFAULT_DEV_OPS_PATH_PREFIX}"
self.base_url = f"https://{dev_ops_url}/{dev_ops_api_version}"

def _ops_request(self, method, path, options=None, json_data=None):
options = {} if options is None else options
Expand Down
16 changes: 0 additions & 16 deletions astrapy/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import requests
import logging
import re

from astrapy import __version__
from astrapy.defaults import DEFAULT_TIMEOUT
Expand Down Expand Up @@ -103,18 +102,3 @@ def make_payload(top_level, **kwargs):
json_query[top_level][key] = value

return json_query


def parse_endpoint_url(url):
# Regular expression pattern to match the given URL format
pattern = r"https://(?P<db_id>[a-fA-F0-9\-]{36})-(?P<db_region>[a-zA-Z0-9\-]+)\.(?P<db_hostname>[a-zA-Z0-9\-\.]+\.com)"

match = re.match(pattern, url)
if match:
return (
match.group("db_id"),
match.group("db_region"),
match.group("db_hostname"),
)
else:
raise ValueError("Invalid URL format")
48 changes: 43 additions & 5 deletions tests/astrapy/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

from astrapy.db import AstraDB, AstraDBCollection
from astrapy.defaults import DEFAULT_KEYSPACE_NAME, DEFAULT_REGION
from astrapy.defaults import DEFAULT_KEYSPACE_NAME

import uuid
import pytest
Expand All @@ -31,11 +31,9 @@
load_dotenv()


ASTRA_DB_ID = os.environ.get("ASTRA_DB_ID")
ASTRA_DB_REGION = os.environ.get("ASTRA_DB_REGION", DEFAULT_REGION)
ASTRA_DB_APPLICATION_TOKEN = os.environ.get("ASTRA_DB_APPLICATION_TOKEN")
ASTRA_DB_API_ENDPOINT = os.environ.get("ASTRA_DB_API_ENDPOINT")
ASTRA_DB_KEYSPACE = os.environ.get("ASTRA_DB_KEYSPACE", DEFAULT_KEYSPACE_NAME)
ASTRA_DB_BASE_URL = os.environ.get("ASTRA_DB_BASE_URL", "apps.astra.datastax.com")

TEST_COLLECTION_NAME = "test_collection"
TEST_FIXTURE_COLLECTION_NAME = "test_fixture_collection"
Expand All @@ -56,7 +54,7 @@ def vv_uuid():
def db():
astra_db = AstraDB(
token=ASTRA_DB_APPLICATION_TOKEN,
api_endpoint=f"https://{ASTRA_DB_ID}-{ASTRA_DB_REGION}.{ASTRA_DB_BASE_URL}",
api_endpoint=ASTRA_DB_API_ENDPOINT,
namespace=ASTRA_DB_KEYSPACE,
)

Expand Down Expand Up @@ -120,6 +118,46 @@ def projection_collection(db):
db.delete_collection(collection_name=TEST_FIXTURE_PROJECTION_COLLECTION_NAME)


@pytest.mark.describe("should confirm path handling in constructor")
def test_path_handling():
astra_db_1 = AstraDB(
token=ASTRA_DB_APPLICATION_TOKEN,
api_endpoint=ASTRA_DB_API_ENDPOINT,
namespace=ASTRA_DB_KEYSPACE,
)

url_1 = astra_db_1.base_path

astra_db_2 = AstraDB(
token=ASTRA_DB_APPLICATION_TOKEN,
api_endpoint=ASTRA_DB_API_ENDPOINT,
namespace=ASTRA_DB_KEYSPACE,
api_version="v1",
)

url_2 = astra_db_2.base_path

astra_db_3 = AstraDB(
token=ASTRA_DB_APPLICATION_TOKEN,
api_endpoint=ASTRA_DB_API_ENDPOINT,
namespace=ASTRA_DB_KEYSPACE,
api_version="/v1",
)

url_3 = astra_db_3.base_path

astra_db_4 = AstraDB(
token=ASTRA_DB_APPLICATION_TOKEN,
api_endpoint=ASTRA_DB_API_ENDPOINT,
namespace=ASTRA_DB_KEYSPACE,
api_version="/v1/",
)

url_4 = astra_db_4.base_path

assert url_1 == url_2 == url_3 == url_4


@pytest.mark.describe("should create a vector collection")
def test_create_collection(db):
res = db.create_collection(collection_name=TEST_COLLECTION_NAME, dimension=5)
Expand Down
9 changes: 5 additions & 4 deletions tests/astrapy/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import pytest
import logging
import os
import uuid

from faker import Faker

logger = logging.getLogger(__name__)
Expand All @@ -30,11 +32,10 @@


# Parameter for the ops testing
ASTRA_DB_ID = os.environ.get("ASTRA_DB_ID")
ASTRA_DB_REGION = os.environ.get("ASTRA_DB_REGION", DEFAULT_REGION)
ASTRA_DB_APPLICATION_TOKEN = os.environ.get("ASTRA_DB_APPLICATION_TOKEN")
ASTRA_DB_ID = os.environ.get("ASTRA_DB_ID")
ASTRA_DB_KEYSPACE = os.environ.get("ASTRA_DB_KEYSPACE", DEFAULT_KEYSPACE_NAME)
ASTRA_DB_BASE_URL = os.environ.get("ASTRA_DB_BASE_URL", "apps.astra.datastax.com")
ASTRA_DB_REGION = os.environ.get("ASTRA_DB_REGION", DEFAULT_REGION)


pytestmark = pytest.mark.skip("Currently skipping all ops tests")
Expand Down Expand Up @@ -83,7 +84,7 @@ def test_create_database(devops_client):
@pytest.mark.describe("should create a keyspace")
def test_create_keyspace(devops_client):
response = devops_client.create_keyspace(
keyspace="test_namespace", database=os.environ["ASTRA_DB_ID"]
keyspace="test_namespace", database=str(uuid.uuid4())
)

assert response is not None
8 changes: 3 additions & 5 deletions tests/astrapy/test_pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import logging
from typing import Iterable, TypeVar

from astrapy.db import AstraDBCollection, AstraDB
from astrapy.db import AstraDB
from astrapy.defaults import DEFAULT_KEYSPACE_NAME, DEFAULT_REGION

from dotenv import load_dotenv
Expand All @@ -29,11 +29,9 @@
load_dotenv()


ASTRA_DB_ID = os.environ.get("ASTRA_DB_ID")
ASTRA_DB_REGION = os.environ.get("ASTRA_DB_REGION", DEFAULT_REGION)
ASTRA_DB_APPLICATION_TOKEN = os.environ.get("ASTRA_DB_APPLICATION_TOKEN")
ASTRA_DB_API_ENDPOINT = os.environ.get("ASTRA_DB_API_ENDPOINT")
ASTRA_DB_KEYSPACE = os.environ.get("ASTRA_DB_KEYSPACE", DEFAULT_KEYSPACE_NAME)
ASTRA_DB_BASE_URL = os.environ.get("ASTRA_DB_BASE_URL", "apps.astra.datastax.com")


TEST_COLLECTION_NAME = "test_collection"
Expand Down Expand Up @@ -64,7 +62,7 @@ def _batch_iterable(iterable: Iterable[T], batch_size: int) -> Iterable[Iterable
def test_collection():
astra_db = AstraDB(
token=ASTRA_DB_APPLICATION_TOKEN,
api_endpoint=f"https://{ASTRA_DB_ID}-{ASTRA_DB_REGION}.{ASTRA_DB_BASE_URL}",
api_endpoint=ASTRA_DB_API_ENDPOINT,
namespace=ASTRA_DB_KEYSPACE,
)

Expand Down

0 comments on commit 7f4ed5f

Please sign in to comment.