Skip to content

Commit e1d49a9

Browse files
Merge pull request #43 from Preponderous-Software/removed-service-config
refactor: remove ServiceConfig class and update service initializatio…
2 parents 27ebd3f + b2683aa commit e1d49a9

File tree

9 files changed

+100
-111
lines changed

9 files changed

+100
-111
lines changed

src/main/python/preponderous/viron/services/entityService.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@
88

99
class EntityService:
1010
def __init__(self, viron_host: str, viron_port: int):
11-
"""Initialize EntityService with host and port"""
1211
self.viron_host = viron_host
1312
self.viron_port = viron_port
1413

1514
def get_base_url(self) -> str:
16-
"""Get the base URL for the API"""
1715
return f"{self.viron_host}:{self.viron_port}/api/v1/entities"
1816

1917
def get_all_entities(self) -> List[Entity]:

src/main/python/preponderous/viron/services/environmentService.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,44 @@
66
from src.main.python.preponderous.viron.models.environment import Environment
77

88
class EnvironmentService:
9-
def __init__(self, base_url):
10-
self.base_url = f"{base_url}/api/v1/environments"
9+
def __init__(self, viron_host: str, viron_port: int):
10+
self.viron_host = viron_host
11+
self.viron_port = viron_port
12+
13+
def get_base_url(self) -> str:
14+
return f"{self.viron_host}:{self.viron_port}/api/v1/environments"
1115

1216
def get_all_environments(self) -> list[Environment]:
13-
response = requests.get(f"{self.base_url}")
17+
response = requests.get(f"{self.get_base_url()}")
1418
response.raise_for_status()
1519
return [Environment(**env) for env in response.json()]
1620

1721
def get_environment_by_id(self, environment_id: int) -> Environment:
18-
response = requests.get(f"{self.base_url}/{environment_id}")
22+
response = requests.get(f"{self.get_base_url()}/{environment_id}")
1923
response.raise_for_status()
2024
return Environment(**response.json())
2125

2226
def get_environment_by_name(self, name: str) -> Environment:
23-
response = requests.get(f"{self.base_url}/name/{name}")
27+
response = requests.get(f"{self.get_base_url()}/name/{name}")
2428
response.raise_for_status()
2529
return Environment(**response.json())
2630

2731
def get_environment_of_entity(self, entity_id: int) -> Environment:
28-
response = requests.get(f"{self.base_url}/entity/{entity_id}")
32+
response = requests.get(f"{self.get_base_url()}/entity/{entity_id}")
2933
response.raise_for_status()
3034
return Environment(**response.json())
3135

3236
def create_environment(self, name: str, num_grids: int, grid_size: int) -> Environment:
33-
response = requests.post(f"{self.base_url}/{name}/{num_grids}/{grid_size}")
37+
response = requests.post(f"{self.get_base_url()}/{name}/{num_grids}/{grid_size}")
3438
response.raise_for_status()
3539
return Environment(**response.json())
3640

3741
def delete_environment(self, environment_id: int) -> bool:
38-
response = requests.delete(f"{self.base_url}/{environment_id}")
42+
response = requests.delete(f"{self.get_base_url()}/{environment_id}")
3943
response.raise_for_status()
4044
return response.status_code == 200
4145

4246
def update_environment_name(self, environment_id: int, name: str) -> bool:
43-
response = requests.patch(f"{self.base_url}/{environment_id}/name/{name}")
47+
response = requests.patch(f"{self.get_base_url()}/{environment_id}/name/{name}")
4448
response.raise_for_status()
4549
return response.status_code == 200

src/main/python/preponderous/viron/services/gridService.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,29 @@
1010
logger = logging.getLogger(__name__)
1111

1212
class GridService:
13-
def __init__(self, base_url: str):
14-
self.base_url = f"{base_url}/api/v1/grids"
13+
def __init__(self, viron_host: str, viron_port: int):
14+
self.viron_host = viron_host
15+
self.viron_port = viron_port
16+
17+
def get_base_url(self) -> str:
18+
return f"{self.viron_host}:{self.viron_port}/api/v1/grids"
1519

1620
def get_all_grids(self) -> List[Grid]:
17-
response = requests.get(self.base_url)
21+
response = requests.get(self.get_base_url())
1822
response.raise_for_status()
1923
return [Grid(**grid) for grid in response.json()]
2024

2125
def get_grid_by_id(self, grid_id: int) -> Optional[Grid]:
22-
response = requests.get(f"{self.base_url}/{grid_id}")
26+
response = requests.get(f"{self.get_base_url()}/{grid_id}")
2327
response.raise_for_status()
2428
return Grid(**response.json())
2529

2630
def get_grids_in_environment(self, environment_id: int) -> List[Grid]:
27-
response = requests.get(f"{self.base_url}/environment/{environment_id}")
31+
response = requests.get(f"{self.get_base_url()}/environment/{environment_id}")
2832
response.raise_for_status()
2933
return [Grid(**grid) for grid in response.json()]
3034

3135
def get_grid_of_entity(self, entity_id: int) -> Optional[Grid]:
32-
response = requests.get(f"{self.base_url}/entity/{entity_id}")
36+
response = requests.get(f"{self.get_base_url()}/entity/{entity_id}")
3337
response.raise_for_status()
3438
return Grid(**response.json())

src/main/python/preponderous/viron/services/locationService.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,59 +4,57 @@
44
from dataclasses import dataclass
55
from src.main.python.preponderous.viron.models.location import Location
66

7-
@dataclass
8-
class ServiceConfig:
9-
viron_host: str
10-
viron_port: int
11-
127
class LocationService:
13-
def __init__(self, service_config: ServiceConfig):
14-
self.logger = logging.getLogger(__name__)
15-
self.base_url = f"{service_config.viron_host}:{service_config.viron_port}/api/v1/locations"
8+
def __init__(self, viron_host: str, viron_port: int):
9+
self.viron_host = viron_host
10+
self.viron_port = viron_port
11+
12+
def get_base_url(self) -> str:
13+
return f"{self.viron_host}:{self.viron_port}/api/v1/locations"
1614

1715
def get_all_locations(self) -> List[Location]:
18-
response = requests.get(self.base_url)
16+
response = requests.get(self.get_base_url())
1917
response.raise_for_status()
2018
return response.json()
2119

2220
def get_location_by_id(self, id: int) -> Location:
23-
response = requests.get(f"{self.base_url}/{id}")
21+
response = requests.get(f"{self.get_base_url()}/{id}")
2422
if response.status_code == 404:
2523
raise Exception(f"Location not found with id: {id}")
2624
response.raise_for_status()
2725
return response.json()
2826

2927
def get_locations_in_environment(self, environment_id: int) -> List[Location]:
30-
response = requests.get(f"{self.base_url}/environment/{environment_id}")
28+
response = requests.get(f"{self.get_base_url()}/environment/{environment_id}")
3129
response.raise_for_status()
3230
return response.json()
3331

3432
def get_locations_in_grid(self, grid_id: int) -> List[Location]:
35-
response = requests.get(f"{self.base_url}/grid/{grid_id}")
33+
response = requests.get(f"{self.get_base_url()}/grid/{grid_id}")
3634
response.raise_for_status()
3735
return response.json()
3836

3937
def get_location_of_entity(self, entity_id: int) -> Location:
40-
response = requests.get(f"{self.base_url}/entity/{entity_id}")
38+
response = requests.get(f"{self.get_base_url()}/entity/{entity_id}")
4139
if response.status_code == 404:
4240
raise Exception(f"Location not found for entity: {entity_id}")
4341
response.raise_for_status()
4442
return response.json()
4543

4644
def add_entity_to_location(self, entity_id: int, location_id: int) -> None:
47-
response = requests.put(f"{self.base_url}/{location_id}/entity/{entity_id}")
45+
response = requests.put(f"{self.get_base_url()}/{location_id}/entity/{entity_id}")
4846
if response.status_code == 404:
4947
raise Exception("Location or entity not found")
5048
response.raise_for_status()
5149

5250
def remove_entity_from_location(self, entity_id: int, location_id: int) -> None:
53-
response = requests.delete(f"{self.base_url}/{location_id}/entity/{entity_id}")
51+
response = requests.delete(f"{self.get_base_url()}/{location_id}/entity/{entity_id}")
5452
if response.status_code == 404:
5553
raise Exception("Location or entity not found")
5654
response.raise_for_status()
5755

5856
def remove_entity_from_current_location(self, entity_id: int) -> None:
59-
response = requests.delete(f"{self.base_url}/entity/{entity_id}")
57+
response = requests.delete(f"{self.get_base_url()}/entity/{entity_id}")
6058
if response.status_code == 404:
6159
raise Exception("Entity not found")
6260
response.raise_for_status()

src/test/java/preponderous/viron/config/ServiceConfigTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ void testGetVironHost() {
2424
@Test
2525
void testGetVironPort() {
2626
ServiceConfig serviceConfig = new ServiceConfig();
27-
serviceConfig.setVironPort(8080);
28-
assert(serviceConfig.getVironPort() == 8080);
27+
serviceConfig.setVironPort(9999);
28+
assert(serviceConfig.getVironPort() == 9999);
2929
}
3030
}

src/test/python/preponderous/viron/services/test_entityService.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,21 @@
1515
{'entityId': 2, 'name': 'Entity2', 'creationDate': '2024-01-01'}
1616
]
1717

18-
@pytest.fixture
19-
def service():
20-
return EntityService("http://localhost", 8080)
18+
service = EntityService("http://localhost", 9999)
2119

2220

23-
def test_init(service):
21+
def test_init():
2422
assert service.viron_host == "http://localhost"
25-
assert service.viron_port == 8080
23+
assert service.viron_port == 9999
2624

2725

28-
def test_get_base_url(service):
29-
expected = "http://localhost:8080/api/v1/entities"
26+
def test_get_base_url():
27+
expected = "http://localhost:9999/api/v1/entities"
3028
assert service.get_base_url() == expected
3129

3230

3331
@patch('requests.get')
34-
def test_get_all_entities_success(mock_get, service):
32+
def test_get_all_entities_success(mock_get):
3533
mock_response = Mock()
3634
mock_response.json.return_value = MOCK_ENTITIES_DATA
3735
mock_get.return_value = mock_response
@@ -43,7 +41,7 @@ def test_get_all_entities_success(mock_get, service):
4341

4442

4543
@patch('requests.get')
46-
def test_get_all_entities_empty(mock_get, service):
44+
def test_get_all_entities_empty(mock_get):
4745
mock_response = Mock()
4846
mock_response.json.return_value = []
4947
mock_get.return_value = mock_response
@@ -53,7 +51,7 @@ def test_get_all_entities_empty(mock_get, service):
5351

5452

5553
@patch('requests.get')
56-
def test_get_entity_by_id_success(mock_get, service):
54+
def test_get_entity_by_id_success(mock_get):
5755
mock_response = Mock()
5856
mock_response.json.return_value = MOCK_ENTITY_DATA
5957
mock_get.return_value = mock_response
@@ -65,7 +63,7 @@ def test_get_entity_by_id_success(mock_get, service):
6563

6664

6765
@patch('requests.get')
68-
def test_get_entities_in_environment_success(mock_get, service):
66+
def test_get_entities_in_environment_success(mock_get):
6967
mock_response = Mock()
7068
mock_response.json.return_value = MOCK_ENTITIES_DATA
7169
mock_get.return_value = mock_response
@@ -76,7 +74,7 @@ def test_get_entities_in_environment_success(mock_get, service):
7674

7775

7876
@patch('requests.get')
79-
def test_get_entities_in_grid_success(mock_get, service):
77+
def test_get_entities_in_grid_success(mock_get):
8078
mock_response = Mock()
8179
mock_response.json.return_value = MOCK_ENTITIES_DATA
8280
mock_get.return_value = mock_response
@@ -87,7 +85,7 @@ def test_get_entities_in_grid_success(mock_get, service):
8785

8886

8987
@patch('requests.get')
90-
def test_get_entities_in_location_success(mock_get, service):
88+
def test_get_entities_in_location_success(mock_get):
9189
mock_response = Mock()
9290
mock_response.json.return_value = MOCK_ENTITIES_DATA
9391
mock_get.return_value = mock_response
@@ -98,7 +96,7 @@ def test_get_entities_in_location_success(mock_get, service):
9896

9997

10098
@patch('requests.get')
101-
def test_get_entities_not_in_any_location_success(mock_get, service):
99+
def test_get_entities_not_in_any_location_success(mock_get):
102100
mock_response = Mock()
103101
mock_response.json.return_value = MOCK_ENTITIES_DATA
104102
mock_get.return_value = mock_response
@@ -109,7 +107,7 @@ def test_get_entities_not_in_any_location_success(mock_get, service):
109107

110108

111109
@patch('requests.post')
112-
def test_create_entity_success(mock_post, service):
110+
def test_create_entity_success(mock_post):
113111
mock_response = Mock()
114112
mock_response.json.return_value = MOCK_ENTITY_DATA
115113
mock_post.return_value = mock_response
@@ -121,7 +119,7 @@ def test_create_entity_success(mock_post, service):
121119

122120

123121
@patch('requests.delete')
124-
def test_delete_entity_success(mock_delete, service):
122+
def test_delete_entity_success(mock_delete):
125123
mock_response = Mock()
126124
mock_delete.return_value = mock_response
127125

@@ -131,7 +129,7 @@ def test_delete_entity_success(mock_delete, service):
131129

132130

133131
@patch('requests.patch')
134-
def test_update_entity_name_success(mock_patch, service):
132+
def test_update_entity_name_success(mock_patch):
135133
mock_response = Mock()
136134
mock_patch.return_value = mock_response
137135

@@ -142,7 +140,7 @@ def test_update_entity_name_success(mock_patch, service):
142140

143141
# Error cases
144142
@patch('requests.get')
145-
def test_get_all_entities_error(mock_get, service):
143+
def test_get_all_entities_error(mock_get):
146144
mock_get.side_effect = Exception("Network error")
147145

148146
with pytest.raises(Exception) as exc_info:
@@ -151,7 +149,7 @@ def test_get_all_entities_error(mock_get, service):
151149

152150

153151
@patch('requests.post')
154-
def test_create_entity_null_response(mock_post, service):
152+
def test_create_entity_null_response(mock_post):
155153
mock_response = Mock()
156154
mock_response.json.return_value = None
157155
mock_post.return_value = mock_response

0 commit comments

Comments
 (0)