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: format the code, fix CI errors #3

Merged
merged 4 commits into from
Jan 13, 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
8 changes: 5 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
pip install .

- name: Run linter
run: git diff --name-only HEAD~10 HEAD | xargs pre-commit run --files
run: pre-commit run --all-files

- name: Run tests
run: python -m unittest discover src/tests -v
Expand All @@ -52,7 +52,9 @@ jobs:
node-version: '20'

- name: Setup semantic-release
run: 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
run: |
npm init -y
npm install -g semantic-release @semantic-release/github @semantic-release/commit-analyzer @semantic-release/release-notes-generator

- name: Set up Python
uses: actions/setup-python@v2
Expand All @@ -65,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
27 changes: 27 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
default_stages: [ pre-commit ]
fail_fast: true

repos:
- repo: local
hooks:
- id: black
name: black
entry: black
language: system
types: [ python ]
description: "Black code formatter"

- id: ruff
name: ruff
description: "Ruff code checker"
entry: ruff check
args: [ --fix, --exit-non-zero-on-fix ]
language: system
types: [ python ]


- repo: https://github.com/commitizen-tools/commitizen
rev: v3.2.2
hooks:
- id: commitizen
stages: [ commit-msg ]
27 changes: 27 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "casvisor-python-sdk",
"version": "0.1.0",
"repository": {
"type": "git",
"url": "https://github.com/ljl66-66/casvisor-python-sdk.git"
},
"release": {
"branches": [
"master"
],
"plugins": [
"@semantic-release/commit-analyzer",
"@semantic-release/release-notes-generator",
[
"@semantic-release/github",
{
"successComment": false,
"failComment": false,
"failTitle": false,
"releasedLabels": false,
"addReleases": "bottom"
}
]
]
}
}
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -15,4 +15,3 @@
from setuptools import setup

setup()

6 changes: 3 additions & 3 deletions src/casvisor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -13,7 +13,7 @@
# limitations under the License.
#
from .main import CasvisorSDK
from .base import BaseClient,Response
from .base import BaseClient, Response
from .record import Record, _RecordSDK

__all__ = ["CasvisorSDK","BaseClient", "Response", "Record", "_RecordSDK"]
__all__ = ["CasvisorSDK", "BaseClient", "Response", "Record", "_RecordSDK"]
35 changes: 25 additions & 10 deletions src/casvisor/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -22,13 +22,17 @@ class HttpClient:
def do(self, request):
pass


class Response:
def __init__(self, status: str, msg: str, data: Union[Dict, List], data2: Union[Dict, List]):
def __init__(
self, status: str, msg: str, data: Union[Dict, List], data2: Union[Dict, List]
):
self.status = status
self.msg = msg
self.data = data
self.data2 = data2


# Global HTTP client
client = requests.Session()

Expand All @@ -49,7 +53,9 @@ def do_get_response(self, url: str) -> Response:
response = json.loads(resp_bytes)
if response["status"] != "ok":
raise Exception(response["msg"])
return Response(response["status"], response["msg"], response["data"], response["data2"])
return Response(
response["status"], response["msg"], response["data"], response["data2"]
)

def do_get_bytes(self, url: str) -> bytes:
response = self.do_get_response(url)
Expand All @@ -62,33 +68,42 @@ def do_get_bytes_raw(self, url: str) -> bytes:
raise Exception(response["msg"])
return resp_bytes

def do_post(self, action: str, query_map: Dict[str, str], post_bytes: bytes, is_form: bool, is_file: bool) -> Response:
def do_post(
self,
action: str,
query_map: Dict[str, str],
post_bytes: bytes,
is_form: bool,
is_file: bool,
) -> Response:
url = util.get_url(self.endpoint, action, query_map)
content_type, body = self.prepare_body(post_bytes, is_form, is_file)
resp_bytes = self.do_post_bytes_raw(url, content_type, body)
response = json.loads(resp_bytes)
if response["status"] != "ok":
raise Exception(response["msg"])
return Response(response["status"], response["msg"], response["data"], response["data2"])
return Response(
response["status"], response["msg"], response["data"], response["data2"]
)

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}"
"Authorization": f"Basic {self.client_id}:{self.client_secret}",
}
resp = client.post(url, headers=headers, data=body)
return resp.content

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

def prepare_body(self, post_bytes: bytes, is_form: bool, is_file: bool) -> Tuple[str, bytes]:
def prepare_body(
self, post_bytes: bytes, is_form: bool, is_file: bool
) -> Tuple[str, bytes]:
if is_form:
if is_file:
return util.create_form_file({"file": post_bytes})
Expand Down
2 changes: 1 addition & 1 deletion src/casvisor/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
Expand Down
24 changes: 11 additions & 13 deletions src/casvisor/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
Expand Down Expand Up @@ -69,22 +69,20 @@ def __init__(self, base_client: BaseClient, organization_name: str):
self.organization_name = organization_name

def get_records(self) -> List[Record]:
query_map = {
"owner": self.organization_name
}
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)
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}"
}
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)
return Record.from_dict(json.loads(bytes))

def get_pagination_records(self, p: int, page_size: int, query_map: Dict[str, str]) -> Tuple[List[Record], int]:
def get_pagination_records(
self, p: int, page_size: int, query_map: Dict[str, str]
) -> Tuple[List[Record], int]:
query_map["owner"] = self.organization_name
query_map["p"] = str(p)
query_map["page_size"] = str(page_size)
Expand All @@ -108,14 +106,14 @@ def delete_record(self, record: Record) -> bool:
_, affected = self.modify_record("delete-record", record, None)
return affected

def modify_record(self, action: str, record: Record, columns: Optional[List[str]]) -> tuple[Dict, bool]:
query_map = {
"id": f"{record.owner}/{record.name}"
}
def modify_record(
self, action: str, record: Record, columns: Optional[List[str]]
) -> Tuple[Dict, 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"
return resp, resp["data"] == "Affected"
15 changes: 7 additions & 8 deletions src/casvisor/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -13,21 +13,20 @@
# limitations under the License.


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


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()])
return f"{base_url}/api/{action}?{query}"

def create_form_file(form_data: Dict[str, bytes]) -> tuple[str, bytes]:
body = BytesIO()

def create_form_file(form_data: Dict[str, bytes]) -> Tuple[str, bytes]:
encoder = MultipartEncoder(fields={k: ("file", v) for k, v in form_data.items()})
return encoder.content_type, encoder.to_string()

def create_form(form_data: Dict[str, str]) -> tuple[str, bytes]:
body = BytesIO()

def create_form(form_data: Dict[str, str]) -> Tuple[str, bytes]:
encoder = MultipartEncoder(fields=form_data)
return encoder.content_type, encoder.to_string()
return encoder.content_type, encoder.to_string()
4 changes: 2 additions & 2 deletions src/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# 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.
# limitations under the License.
Loading
Loading