Skip to content

Commit a449deb

Browse files
committed
refactor
1 parent 17b275f commit a449deb

4 files changed

Lines changed: 23 additions & 12 deletions

File tree

codeinsight_sdk/client.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import requests
22
import logging
33

4-
from .handlers import Handler
4+
from .handlers import ProjectHandler, Handler, ReportHandler
55
from .models import Project, ProjectInventory, Report
66
from .exceptions import CodeInsightError
77

@@ -21,7 +21,7 @@ def __init__(self,
2121
"User-Agent": "codeinsight_sdk_python",
2222
}
2323

24-
def request(self, method, url_part: str, params: dict = None):
24+
def request(self, method, url_part: str, params: dict = None, body: any = None ):
2525
url = f"{self.api_url}/{url_part}"
2626

2727
# Iterate over params and remove any that are None (Empty)
@@ -31,7 +31,7 @@ def request(self, method, url_part: str, params: dict = None):
3131
del params[k]
3232

3333
response = requests.request(method, url,
34-
headers=self.__api_headers, params=params)
34+
headers=self.__api_headers, params=params, json=body)
3535

3636
if not response.ok:
3737
logger.error(f"Error: {response.status_code} - {response.reason}")
@@ -41,12 +41,12 @@ def request(self, method, url_part: str, params: dict = None):
4141
return response
4242

4343
@property
44-
def projects(self) -> Handler:
45-
return Handler.create(self, Project)
44+
def projects(self) -> ProjectHandler:
45+
return ProjectHandler(self)
4646

4747
@property
48-
def reports(self) -> Handler:
49-
return Handler.create(self, Report)
48+
def reports(self) -> ReportHandler:
49+
return ReportHandler(self)
5050

5151

5252
# Coming soon...?

codeinsight_sdk/handlers.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import abc
22
from typing import List
33

4-
from codeinsight_sdk.models import Project, ProjectInventory, ProjectInventoryItem
4+
from codeinsight_sdk.models import Project, ProjectInventory, ProjectInventoryItem, Report
55
from codeinsight_sdk.exceptions import CodeInsightError
66

77
class Handler(abc.ABC):
8-
def __init__(self, client, cls):
8+
def __init__(self, client):
99
self.client = client
10-
self.cls = cls
10+
self.cls = None
1111

1212
@staticmethod
1313
def create(client, cls):
@@ -18,13 +18,17 @@ def create(client, cls):
1818
handler = handlers.get(k)
1919
if handler is None:
2020
raise ValueError(f"Handler not found for class '{k}'")
21-
return handler(client, cls)
21+
return handler(client)
2222

2323
@abc.abstractmethod
2424
def get(self):
2525
pass
2626

2727
class ProjectHandler(Handler):
28+
def __init__(self, client):
29+
super().__init__(client)
30+
self.cls = Project
31+
2832
#Note API endpoints switch between projects and project...
2933
def all(self) -> List[Project]:
3034
"""
@@ -154,6 +158,10 @@ def get_inventory(self,project_id:int,
154158

155159

156160
class ReportHandler(Handler):
161+
def __init__(self, client):
162+
super().__init__(client)
163+
self.cls = Report
164+
157165
def get(self, id:int):
158166
path = f"reports/{id}"
159167
resp = self.client.request("GET", url_part=path)

examples/requirements.examples.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
codeinsight-sdk==0.0.4
2+
pandas~=2.0.0

tests/test_handlers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
from codeinsight_sdk.handlers import Handler, ProjectHandler, ReportHandler
55
from codeinsight_sdk.models import Project, Report
66

7+
78
class TestHandlers(object):
89
@pytest.fixture
910
def client(self):
1011
return CodeInsightClient("","")
1112

1213
def test_bad_handler(self, client):
1314
with pytest.raises(Exception):
14-
Handler.create(client, "BadClass")
15+
Handler.create(client)
1516

1617
def test_project_handler(self, client):
1718
project_handler = Handler.create(client, Project)

0 commit comments

Comments
 (0)