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

feat: fix test cases and CI #4

Merged
merged 4 commits into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
- name: Setup semantic-release
run: |
npm init -y
npm install -g semantic-release @semantic-release/github @semantic-release/commit-analyzer @semantic-release/release-notes-generator
npm install -g semantic-release @semantic-release/github @semantic-release/changelog @semantic-release/commit-analyzer @semantic-release/git @semantic-release/release-notes-generator semantic-release-pypi

- name: Set up Python
uses: actions/setup-python@v2
Expand All @@ -67,5 +67,5 @@ jobs:
- name: Release
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }}
PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }}
run: npx semantic-release
26 changes: 18 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "casvisor-python-sdk",
"version": "0.1.0",
"version": "1.0.0",
"repository": {
"type": "git",
"url": "https://github.com/ljl66-66/casvisor-python-sdk.git"
"url": "https://github.com/casvisor/casvisor-python-sdk.git"
},
"release": {
"branches": [
Expand All @@ -12,14 +12,24 @@
"plugins": [
"@semantic-release/commit-analyzer",
"@semantic-release/release-notes-generator",
"semantic-release-pypi",
"@semantic-release/github",
[
"@semantic-release/github",
"@semantic-release/changelog",
{
"successComment": false,
"failComment": false,
"failTitle": false,
"releasedLabels": false,
"addReleases": "bottom"
"changelogFile": "CHANGELOG.md",
"changelogTitle": "# Semantic Versioning Changelog"
}
],
[
"@semantic-release/git",
{
"message": "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}",
"assets": [
"CHANGELOG.md",
"setup.py",
"setup.cfg"
]
}
]
]
Expand Down
28 changes: 26 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
[project]
name = "casvisor-python-sdk"
version = "0.1.0"
version = "1.0.0"
description = "Python SDK for Casvisor"
readme = "README.md"
requires-python = ">=3.8"
authors = [
{ name = "Casvisor", email = "[email protected]" }
]
license = { file = "LICENSE" }
keywords = ["Casvisor"]
classifiers = [
"Intended Audience :: Developers",
"License :: OSI Approved :: Apache Software License",
"Operating System :: OS Independent",
"Programming Language :: Python :: 3",
"Programming Language :: Python",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
]

dynamic = ["dependencies"]
Expand All @@ -29,3 +31,25 @@ dependencies = {file = ["requirements.txt"]}
[build-system]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta"

[tool.black]
line-length = 88
target-version = ["py38", "py39", "py310", "py311"]
include = '\.pyi?$'

[tool.ruff]
line-length = 88

[tool.ruff.lint]
select = [
"E", # pycodestyle errors
"W", # pycodestyle warnings
"F", # pyflakes
"I", # isort
"C", # flake8-comprehensions
"B", # flake8-bugbear
]

# Allow autofix for all enabled rules (when `--fix`) is provided.
fixable = ["I", "F"]
unfixable = []
5 changes: 3 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = casvisor
version = 0.1.0
name = casvisor-python-sdk
version = 1.0.0
author = Casvisor
author_email = [email protected]
url = https://github.com/casvisor/casvisor-python-sdk
Expand All @@ -19,6 +19,7 @@ classifiers =
Programming Language :: Python :: 3.9
Programming Language :: Python :: 3.10
Programming Language :: Python :: 3.11
Programming Language :: Python :: 3.12

[options]
package_dir =
Expand Down
2 changes: 1 addition & 1 deletion src/casvisor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
from .main import CasvisorSDK
from .base import BaseClient, Response
from .main import CasvisorSDK
from .record import Record, _RecordSDK

__all__ = ["CasvisorSDK", "BaseClient", "Response", "Record", "_RecordSDK"]
23 changes: 12 additions & 11 deletions src/casvisor/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
# limitations under the License.

import json
from typing import Dict, Union, List, Tuple
from typing import Dict, List, Tuple, Union

import requests

from . import util


Expand Down Expand Up @@ -43,9 +45,9 @@ def set_http_client(http_client: HttpClient):


class BaseClient:
def __init__(self, client_id: str, client_secret: str, endpoint: str):
self.client_id = client_id
self.client_secret = client_secret
def __init__(self, clientId: str, clientSecret: str, endpoint: str):
self.clientId = clientId
self.clientSecret = clientSecret
self.endpoint = endpoint

def do_get_response(self, url: str) -> Response:
Expand Down Expand Up @@ -89,16 +91,15 @@ def do_post(
def do_post_bytes_raw(self, url: str, content_type: str, body: bytes) -> bytes:
if not content_type:
content_type = "text/plain;charset=UTF-8"
headers = {
"Content-Type": content_type,
"Authorization": f"Basic {self.client_id}:{self.client_secret}",
}
resp = client.post(url, headers=headers, data=body)

headers = {"Content-Type": content_type}
resp = client.post(
url, headers=headers, data=body, auth=(self.clientId, self.clientSecret)
)
return resp.content

def do_get_bytes_raw_without_check(self, url: str) -> bytes:
headers = {"Authorization": f"Basic {self.client_id}:{self.client_secret}"}
resp = client.get(url, headers=headers)
resp = client.get(url, auth=(self.clientId, self.clientSecret))
return resp.content

def prepare_body(
Expand Down
18 changes: 9 additions & 9 deletions src/casvisor/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ class CasvisorSDK(_RecordSDK):
def __init__(
self,
endpoint: str,
client_id: str,
client_secret: str,
organization_name: str,
application_name: str,
clientId: str,
clientSecret: str,
organizationName: str,
applicationName: str,
):
self.endpoint = endpoint
self.client_id = client_id
self.client_secret = client_secret
self.organization_name = organization_name
self.application_name = application_name
self.clientId = clientId
self.clientSecret = clientSecret
self.organizationName = organizationName
self.applicationName = applicationName

# Initialize the base client
self.base_client = BaseClient(client_id, client_secret, endpoint)
self.baseClient = BaseClient(clientId, clientSecret, endpoint)
81 changes: 41 additions & 40 deletions src/casvisor/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,46 +14,47 @@

import json
from typing import Dict, List, Optional, Tuple
from .base import BaseClient

from . import util
from .base import BaseClient, Response


class Record:
def __init__(
self,
id: int,
owner: str,
name: str,
created_time: str,
organization: str,
client_ip: str,
user: str,
method: str,
request_uri: str,
action: str,
language: str,
object: str,
response: str,
provider: str,
block: str,
is_triggered: bool,
createdTime: Optional[str] = None,
organization: Optional[str] = None,
clientIp: Optional[str] = None,
user: Optional[str] = None,
method: Optional[str] = None,
requestUri: Optional[str] = None,
action: Optional[str] = None,
language: Optional[str] = None,
object: Optional[str] = None,
response: Optional[str] = None,
provider: Optional[str] = None,
block: Optional[str] = None,
isTriggered: Optional[bool] = None,
id: Optional[int] = None,
owner: Optional[str] = None,
name: Optional[str] = None,
):
self.id = id
self.owner = owner
self.name = name
self.created_time = created_time
self.createdTime = createdTime
self.organization = organization
self.client_ip = client_ip
self.clientIp = clientIp
self.user = user
self.method = method
self.request_uri = request_uri
self.requestUri = requestUri
self.action = action
self.language = language
self.object = object
self.response = response
self.provider = provider
self.block = block
self.is_triggered = is_triggered
self.isTriggered = isTriggered

def to_dict(self) -> Dict:
return self.__dict__
Expand All @@ -64,30 +65,30 @@ def from_dict(cls, data: Dict) -> "Record":


class _RecordSDK:
def __init__(self, base_client: BaseClient, organization_name: str):
self.base_client = base_client
self.organization_name = organization_name
def __init__(self, base_client: BaseClient, organizationName: str):
self.baseClient = base_client
self.organizationName = organizationName

def get_records(self) -> List[Record]:
query_map = {"owner": self.organization_name}
url = util.get_url(self.base_client.endpoint, "get-records", query_map)
bytes = self.base_client.do_get_bytes(url)
query_map = {"owner": self.organizationName}
url = util.get_url(self.baseClient.endpoint, "get-records", query_map)
bytes = self.baseClient.do_get_bytes(url)
return [Record.from_dict(record) for record in json.loads(bytes)]

def get_record(self, name: str) -> Record:
query_map = {"id": f"{self.organization_name}/{name}"}
url = util.get_url(self.base_client.endpoint, "get-record", query_map)
bytes = self.base_client.do_get_bytes(url)
query_map = {"id": f"{self.organizationName}/{name}"}
url = util.get_url(self.baseClient.endpoint, "get-record", query_map)
bytes = self.baseClient.do_get_bytes(url)
return Record.from_dict(json.loads(bytes))

def get_pagination_records(
self, p: int, page_size: int, query_map: Dict[str, str]
self, p: int, pageSize: int, query_map: Dict[str, str]
) -> Tuple[List[Record], int]:
query_map["owner"] = self.organization_name
query_map["owner"] = self.organizationName
query_map["p"] = str(p)
query_map["page_size"] = str(page_size)
url = util.get_url(self.base_client.endpoint, "get-records", query_map)
response = self.base_client.do_get_response(url)
query_map["pageSize"] = str(pageSize)
url = util.get_url(self.baseClient.endpoint, "get-records", query_map)
response = self.baseClient.do_get_response(url)
return [Record.from_dict(record) for record in response.data], response.data2

def update_record(self, record: Record) -> bool:
Expand All @@ -96,9 +97,9 @@ def update_record(self, record: Record) -> bool:

def add_record(self, record: Record) -> bool:
if not record.owner:
record.owner = self.organization_name
record.owner = self.organizationName
if not record.organization:
record.organization = self.organization_name
record.organization = self.organizationName
_, affected = self.modify_record("add-record", record, None)
return affected

Expand All @@ -108,12 +109,12 @@ def delete_record(self, record: Record) -> bool:

def modify_record(
self, action: str, record: Record, columns: Optional[List[str]]
) -> Tuple[Dict, bool]:
) -> Tuple[Response, bool]:
query_map = {"id": f"{record.owner}/{record.name}"}
if columns:
query_map["columns"] = ",".join(columns)
if not record.owner:
record.owner = "admin"
post_bytes = json.dumps(record.to_dict()).encode("utf-8")
resp = self.base_client.do_post(action, query_map, post_bytes, False, False)
return resp, resp["data"] == "Affected"
resp = self.baseClient.do_post(action, query_map, post_bytes, False, False)
return resp, resp.data == "Affected"
3 changes: 2 additions & 1 deletion src/casvisor/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
# limitations under the License.


from requests_toolbelt.multipart.encoder import MultipartEncoder
from typing import Dict, Tuple

from requests_toolbelt.multipart.encoder import MultipartEncoder


def get_url(base_url: str, action: str, query_map: Dict[str, str]) -> str:
query = "&".join([f"{k}={v}" for k, v in query_map.items()])
Expand Down
Loading
Loading