Skip to content

Commit

Permalink
refactor(types): add type-hinting for requests and errror_handler dec…
Browse files Browse the repository at this point in the history
…orator
  • Loading branch information
psadi committed Jul 15, 2024
1 parent 15082fc commit f6b7fa1
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 64 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@ jobs:

- name: Build Docker Image
run: |
docker build -t docker.io/psadi/bbcli:${{ github.event_name == 'release' && steps.meta.outputs.tags || github.sha }} .
docker build -t docker.io/psadi/bbcli:${{ steps.meta.outputs.tags || github.sha }} .
- name: Run Trivy vulnerability scanner
uses: aquasecurity/[email protected]
with:
image-ref: docker.io/psadi/bbcli:${{ github.event_name == 'release' && steps.meta.outputs.tags || github.sha }}
image-ref: docker.io/psadi/bbcli:${{ steps.meta.outputs.tags || github.sha }}
format: "sarif"
output: "trivy-results.sarif"
exit-code: "1"
Expand All @@ -159,4 +159,4 @@ jobs:
- name: Push Docker Image
if: ${{ github.event_name == 'release' }}
run: |
docker push docker.io/psadi/bbcli:${{ github.event_name == 'release' && steps.meta.outputs.tags || github.sha }}
docker push docker.io/psadi/bbcli:${{ steps.meta.outputs.tags || github.sha }}
7 changes: 5 additions & 2 deletions bb/utils/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@
"""

from functools import wraps
from typing import Any, Callable
from typing import Any, Callable, ParamSpec, TypeVar

from typer import Exit, prompt

from bb.utils import constants, request, richprint
from bb.utils.api import bitbucket_api

P = ParamSpec("P")
T = TypeVar("T")


def validate_config() -> None:
"""
Expand Down Expand Up @@ -104,7 +107,7 @@ def error_tip() -> None:
)


def error_handler(func: Callable) -> Callable:
def error_handler(func: Callable[P, T]) -> Callable[P, T]:
"""
Decorator function that handles exceptions raised by the decorated function.
Expand Down
13 changes: 7 additions & 6 deletions bb/utils/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

from http import HTTPStatus
from json import JSONDecodeError
from typing import Union

import httpx

Expand Down Expand Up @@ -56,7 +57,7 @@ def http_response_definitions(status_code: int) -> str:
return "Unknown Status Code"


def get(url: str) -> list[int, dict]:
def get(url: str) -> list:
"""
Sends a GET request to the specified URL and returns the response status code and data.
Expand Down Expand Up @@ -97,14 +98,14 @@ def get(url: str) -> list[int, dict]:
)

try:
data: dict = request.json()
response_data: Union[dict, str] = request.json()
except JSONDecodeError:
data: str = request.content.decode()
response_data: Union[dict, str] = request.content.decode()

return [request.status_code, data]
return [request.status_code, response_data]


def post(url: str, body: dict) -> list[int, dict]:
def post(url: str, body: dict) -> list:
"""
Send a POST request to the specified URL with the given body.
Expand Down Expand Up @@ -136,7 +137,7 @@ def post(url: str, body: dict) -> list[int, dict]:
return [request.status_code, json_data]


def put(url: str, body: dict) -> list[int, dict]:
def put(url: str, body: dict) -> list:
"""
Sends a PUT request to the specified URL with the given body.
Expand Down
73 changes: 25 additions & 48 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ name = "bb"
dynamic = ["version"]
description = "Work seamlessly with BitBucket from the command line."
authors = [{ name = "psadi", email = "[email protected]" }]
dependencies = ["typer[all]>=0.7.0", "httpx>=0.24.0"]
requires-python = ">=3.8"
dependencies = ["typer>=0.7.0", "httpx>=0.24.0"]
requires-python = ">=3.9"
readme = "README.md"
license = { text = "AGPL" }

Expand Down
6 changes: 3 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,21 @@ packaging==24.1
pbr==6.0.0
platformdirs==4.2.2
pluggy==1.5.0
pre-commit==3.5.0
pre-commit==3.7.1
pygments==2.18.0
pyproject-api==1.7.1
pytest==8.2.2
pytest-cov==5.0.0
pyyaml==6.0.1
rich==13.7.1
ruff==0.5.1
ruff==0.5.2
shellingham==1.5.4
sniffio==1.3.1
stevedore==5.2.0
tomli==2.0.1; python_version < "3.11"
tox==4.16.0
tox-pdm==0.7.2
typer[all]==0.12.3
typer==0.12.3
typing-extensions==4.12.2
virtualenv==20.26.3
zipp==3.19.2

0 comments on commit f6b7fa1

Please sign in to comment.