Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#2229] feat(client-python): client module initialization (#2280)

Brandon Luda55fc784e02
diff --git a/clients/client-python/.gitignore b/clients/client-python/.gitignore
new file mode 100644
index 0000000000..e0549b75b5
--- /dev/null
+++ b/clients/client-python/.gitignore
@@ -0,0 +1,5 @@
+# Copyright 2024 Datastrato Pvt Ltd.
+# This software is licensed under the Apache License version 2.
+
+__pycache__/
+.pytest_cache/
\ No newline at end of file
diff --git a/clients/client-python/README.md b/clients/client-python/README.md
new file mode 100644
index 0000000000..8515e25546
--- /dev/null
+++ b/clients/client-python/README.md
@@ -0,0 +1,13 @@
+<!--
+- Copyright 2024 Datastrato Pvt Ltd.
+- This software is licensed under the Apache License version 2.
+-->
+
+# Quick Start
+
+1. Install dependency
+ ```bash
+ pip install -r requirements.txt
+ ```
+
+2. After the packages is installed, you can simply run `pytest` in any directory inside gravitino/
\ No newline at end of file
diff --git a/clients/client-python/__init__.py b/clients/client-python/__init__.py
new file mode 100644
index 0000000000..c064f0b0f0
--- /dev/null
+++ b/clients/client-python/__init__.py
@@ -0,0 +1,4 @@
+"""
+Copyright 2024 Datastrato Pvt Ltd.
+This software is licensed under the Apache License version 2.
+"""
\ No newline at end of file
diff --git a/clients/client-python/gravitino_client/__init__.py b/clients/client-python/gravitino_client/__init__.py
new file mode 100644
index 0000000000..c064f0b0f0
--- /dev/null
+++ b/clients/client-python/gravitino_client/__init__.py
@@ -0,0 +1,4 @@
+"""
+Copyright 2024 Datastrato Pvt Ltd.
+This software is licensed under the Apache License version 2.
+"""
\ No newline at end of file
diff --git a/clients/client-python/gravitino_client/core/__init__.py b/clients/client-python/gravitino_client/core/__init__.py
new file mode 100644
index 0000000000..16d5ed38fd
--- /dev/null
+++ b/clients/client-python/gravitino_client/core/__init__.py
@@ -0,0 +1,6 @@
+"""
+Copyright 2024 Datastrato Pvt Ltd.
+This software is licensed under the Apache License version 2.
+"""
+from .gravitino_client import GravitinoClient
+from .dto import VersionDTO
\ No newline at end of file
diff --git a/clients/client-python/gravitino_client/core/dto.py b/clients/client-python/gravitino_client/core/dto.py
new file mode 100644
index 0000000000..f6fc0f52c4
--- /dev/null
+++ b/clients/client-python/gravitino_client/core/dto.py
@@ -0,0 +1,11 @@
+"""
+Copyright 2024 Datastrato Pvt Ltd.
+This software is licensed under the Apache License version 2.
+"""
+from dataclasses import dataclass
+
+@dataclass
+class VersionDTO:
+ version: str
+ compile_date: str
+ git_commit: str
diff --git a/clients/client-python/gravitino_client/core/gravitino_client.py b/clients/client-python/gravitino_client/core/gravitino_client.py
new file mode 100644
index 0000000000..eed39431b0
--- /dev/null
+++ b/clients/client-python/gravitino_client/core/gravitino_client.py
@@ -0,0 +1,56 @@
+"""
+Copyright 2024 Datastrato Pvt Ltd.
+This software is licensed under the Apache License version 2.
+"""
+import requests
+from requests.exceptions import HTTPError
+from gravitino_client.core.dto import VersionDTO
+
+
+class GravitinoClient:
+ """
+ Gravitino Client for interacting with the Gravitino API, allowing the client to list, load,
+ create, and alter Metalakes.
+
+ Attributes:
+ base_url (str): The base URL of the Gravitino API to which the client will make requests.
+
+ Args:
+ base_url (str): The base URL for the Gravitino API.
+ """
+
+ def __init__(self, base_url):
+ """
+ Initializes a new instance of the GravitinoClient.
+
+ Args:
+ base_url (str): The base URL for the Gravitino API where the client will send requests.
+ """
+ self.base_url = base_url
+
+ def getVersion(self) -> VersionDTO:
+ """
+ Retrieves the version information from the Gravitino API.
+
+ This method makes a GET request to the Gravitino API and extracts the version information from the response,
+ wrapping it into a VersionDTO object.
+
+ Returns:
+ VersionDTO: An object containing the version details, including version, compile date, and git commit hash.
+
+ Raises:
+ HTTPError: An error from the requests library if the HTTP request returned an unsuccessful status code.
+ """
+ try:
+ response = requests.get(f"{self.base_url}/api/version")
+ response.raise_for_status()
+ version_data = response.json()
+ version_info = version_data.get("version")
+
+ return VersionDTO(
+ version=version_info['version'],
+ compile_date=version_info['compileDate'],
+ git_commit=version_info['gitCommit']
+ )
+ except HTTPError as e:
+ raise HTTPError(f"Failed to retrieve version information: {e}")
diff --git a/clients/client-python/requirements.txt b/clients/client-python/requirements.txt
new file mode 100644
index 0000000000..5f11843658
--- /dev/null
+++ b/clients/client-python/requirements.txt
@@ -0,0 +1,4 @@
+# Copyright 2024 Datastrato Pvt Ltd.
+# This software is licensed under the Apache License version 2.
+pytest~=8.0.1
+requests~=2.31.0
\ No newline at end of file
diff --git a/clients/client-python/tests/__init__.py b/clients/client-python/tests/__init__.py
new file mode 100644
index 0000000000..c064f0b0f0
--- /dev/null
+++ b/clients/client-python/tests/__init__.py
@@ -0,0 +1,4 @@
+"""
+Copyright 2024 Datastrato Pvt Ltd.
+This software is licensed under the Apache License version 2.
+"""
\ No newline at end of file
diff --git a/clients/client-python/tests/core/__init__.py b/clients/client-python/tests/core/__init__.py
new file mode 100644
index 0000000000..c064f0b0f0
--- /dev/null
+++ b/clients/client-python/tests/core/__init__.py
@@ -0,0 +1,4 @@
+"""
+Copyright 2024 Datastrato Pvt Ltd.
+This software is licensed under the Apache License version 2.
+"""
\ No newline at end of file
diff --git a/clients/client-python/tests/core/test_gravitino_client.py b/clients/client-python/tests/core/test_gravitino_client.py
new file mode 100644
index 0000000000..7d5ffba9ec
--- /dev/null
+++ b/clients/client-python/tests/core/test_gravitino_client.py
@@ -0,0 +1,45 @@
+"""
+Copyright 2024 Datastrato Pvt Ltd.
+This software is licensed under the Apache License version 2.
+"""
+import pytest
+import requests
+from unittest.mock import MagicMock
+from gravitino_client.core import GravitinoClient, VersionDTO
+
+
+def mock_get(monkeypatch):
+ mock = MagicMock()
+ monkeypatch.setattr("requests.get", mock)
+ return mock
+
+
+def test_get_version_success(mock_get):
+ expected_version_dto = VersionDTO(
+ version="0.3.2-SNAPSHOT",
+ compile_date="25/01/2024 00:04:59",
+ git_commit="cb7a604bf19b6f992f00529e938cdd1d37af0187"
+ )
+ mock_get.return_value.json.return_value = {
+ "code": 0,
+ "version": {
+ "version": "0.3.2-SNAPSHOT",
+ "compileDate": "25/01/2024 00:04:59",
+ "gitCommit": "cb7a604bf19b6f992f00529e938cdd1d37af0187"
+ }
+ }
+
+ client = GravitinoClient(base_url="http://localhost:8090")
+ version_data = client.getVersion()
+
+ assert version_data == expected_version_dto
+
+
+def test_get_version_http_error(mock_get):
+ mock_get.side_effect = requests.exceptions.HTTPError
+
+ client = GravitinoClient(base_url="http://localhost:8090")
+
+ with pytest.raises(requests.exceptions.HTTPError):
+ client.getVersion()