http_service.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import requests
  2. import logging
  3. logger = logging.getLogger(__name__)
  4. DEBUG_DATA = {
  5. "id": 2224,
  6. "comment": None,
  7. "start_voltage": 3.6,
  8. "battery_id": 91,
  9. "cell_type_id": 16,
  10. "state": "Unvermessen",
  11. "remaining_capacity": None,
  12. "soh": None,
  13. "created_at": "2025-01-04T11:53:01.820Z",
  14. "updated_at": "2025-01-04T11:53:01.820Z",
  15. "cell_type": {
  16. "id": 16,
  17. "name": "INR18650-35E",
  18. "comment": "",
  19. "manufacturer": "Samsung",
  20. "capacity": 3450,
  21. "nominal_voltage": 3.7,
  22. "max_voltage": 4.2,
  23. "min_voltage": 2.5,
  24. "created_at": "2024-11-05T15:39:15.107Z",
  25. "updated_at": "2024-11-05T15:39:19.564Z"
  26. }
  27. }
  28. class HTTPService:
  29. def __init__(self, config: dict):
  30. self.config = config
  31. self.debug = config['http'].get('debug', False)
  32. self.base_url = config['http'].get('server_url')
  33. self.endpoint = config['http'].get('endpoint')
  34. self.username = config['http'].get('username')
  35. self.password = config['http'].get('password')
  36. def fetch_cell_info(self, cell_id):
  37. if self.debug:
  38. return DEBUG_DATA
  39. url = f"{self.base_url}/{self.endpoint}/{cell_id}/"
  40. # Basic Authentication (if required)
  41. auth = (self.username, self.password)
  42. # Headers
  43. headers = {
  44. "Accept": "application/json"
  45. }
  46. # Making the GET request
  47. response = requests.get(url, auth=auth, headers=headers)
  48. # Check if the request was successful
  49. if response.status_code == 200:
  50. return response.json() # Return parsed JSON
  51. else:
  52. raise ConnectionError(f"Info for cell {cell_id} could not be retreived: {response.status_code}")