amochka 0.4.1__tar.gz → 0.4.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (27) hide show
  1. {amochka-0.4.1 → amochka-0.4.3}/PKG-INFO +1 -1
  2. {amochka-0.4.1 → amochka-0.4.3}/amochka/__init__.py +1 -1
  3. {amochka-0.4.1 → amochka-0.4.3}/amochka/client.py +22 -4
  4. {amochka-0.4.1 → amochka-0.4.3}/amochka/etl.py +2 -0
  5. {amochka-0.4.1 → amochka-0.4.3}/amochka.egg-info/PKG-INFO +1 -1
  6. {amochka-0.4.1 → amochka-0.4.3}/pyproject.toml +1 -1
  7. {amochka-0.4.1 → amochka-0.4.3}/tests/test_etl.py +24 -1
  8. {amochka-0.4.1 → amochka-0.4.3}/tests/test_http.py +16 -1
  9. {amochka-0.4.1 → amochka-0.4.3}/tests/test_notes_events.py +17 -1
  10. {amochka-0.4.1 → amochka-0.4.3}/tests/test_security.py +32 -0
  11. {amochka-0.4.1 → amochka-0.4.3}/tests/test_utils.py +2 -0
  12. {amochka-0.4.1 → amochka-0.4.3}/README.md +0 -0
  13. {amochka-0.4.1 → amochka-0.4.3}/amochka/errors.py +0 -0
  14. {amochka-0.4.1 → amochka-0.4.3}/amochka.egg-info/SOURCES.txt +0 -0
  15. {amochka-0.4.1 → amochka-0.4.3}/amochka.egg-info/dependency_links.txt +0 -0
  16. {amochka-0.4.1 → amochka-0.4.3}/amochka.egg-info/requires.txt +0 -0
  17. {amochka-0.4.1 → amochka-0.4.3}/amochka.egg-info/top_level.txt +0 -0
  18. {amochka-0.4.1 → amochka-0.4.3}/etl/__init__.py +0 -0
  19. {amochka-0.4.1 → amochka-0.4.3}/etl/config.py +0 -0
  20. {amochka-0.4.1 → amochka-0.4.3}/etl/extractors.py +0 -0
  21. {amochka-0.4.1 → amochka-0.4.3}/etl/loaders.py +0 -0
  22. {amochka-0.4.1 → amochka-0.4.3}/etl/migrations/001_create_tables.sql +0 -0
  23. {amochka-0.4.1 → amochka-0.4.3}/etl/run_etl.py +0 -0
  24. {amochka-0.4.1 → amochka-0.4.3}/etl/transformers.py +0 -0
  25. {amochka-0.4.1 → amochka-0.4.3}/setup.cfg +0 -0
  26. {amochka-0.4.1 → amochka-0.4.3}/tests/test_cache.py +0 -0
  27. {amochka-0.4.1 → amochka-0.4.3}/tests/test_client.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: amochka
3
- Version: 0.4.1
3
+ Version: 0.4.3
4
4
  Summary: Python library for working with amoCRM API with ETL capabilities
5
5
  Author-email: Timur <timurdt@gmail.com>
6
6
  License: MIT
@@ -2,7 +2,7 @@
2
2
  amochka: Библиотека для работы с API amoCRM.
3
3
  """
4
4
 
5
- __version__ = "0.4.1"
5
+ __version__ = "0.4.3"
6
6
 
7
7
  from .client import AmoCRMClient, CacheConfig
8
8
  from .errors import (
@@ -477,11 +477,15 @@ class AmoCRMClient:
477
477
  'redirect_uri': os.environ.get('AMOCRM_REDIRECT_URI'),
478
478
  }
479
479
  # 2. Загружаем из файла или строки
480
- elif os.path.exists(self.token_file):
480
+ elif self.token_file and os.path.exists(self.token_file):
481
481
  with open(self.token_file, 'r') as f:
482
482
  data = json.load(f)
483
483
  self.logger.debug(f"Token loaded from file: {self.token_file}")
484
484
  else:
485
+ if not self.token_file:
486
+ raise AuthenticationError(
487
+ "Токен не найден: ни в environment variables, ни в файле, ни в переданной строке."
488
+ )
485
489
  try:
486
490
  data = json.loads(self.token_file)
487
491
  self.logger.debug("Token parsed from provided string.")
@@ -583,7 +587,11 @@ class AmoCRMClient:
583
587
  if response.status_code in (200, 204):
584
588
  if response.status_code == 204:
585
589
  return None
586
- return response.json()
590
+ try:
591
+ return response.json()
592
+ except ValueError:
593
+ preview = response.text[:200] if response.text else ""
594
+ raise APIError(response.status_code, f"Invalid JSON response: {preview}")
587
595
 
588
596
  # Retryable ошибки (429, 5xx)
589
597
  if response.status_code in retryable_status_codes:
@@ -675,7 +683,10 @@ class AmoCRMClient:
675
683
  self.logger.error(f"Не удалось обновить токен: {resp.status_code}")
676
684
  raise AuthenticationError(f"Не удалось обновить токен: {resp.status_code}")
677
685
 
678
- data = resp.json() or {}
686
+ try:
687
+ data = resp.json() or {}
688
+ except ValueError as exc:
689
+ raise AuthenticationError("Ответ refresh_token не является валидным JSON") from exc
679
690
  access_token = data.get("access_token")
680
691
  refresh_token = data.get("refresh_token", self.refresh_token)
681
692
  expires_in = data.get("expires_in")
@@ -921,7 +932,10 @@ class AmoCRMClient:
921
932
  return int(value)
922
933
  if isinstance(value, str):
923
934
  try:
924
- return int(datetime.fromisoformat(value).timestamp())
935
+ value_str = value.strip()
936
+ if value_str.endswith("Z"):
937
+ value_str = value_str[:-1] + "+00:00"
938
+ return int(datetime.fromisoformat(value_str).timestamp())
925
939
  except ValueError as exc:
926
940
  raise ValueError(f"Не удалось преобразовать '{value}' в timestamp") from exc
927
941
  raise TypeError(f"Неподдерживаемый тип для timestamp: {type(value)}")
@@ -1628,6 +1642,8 @@ class AmoCRMClient:
1628
1642
  endpoint = f"/api/v4/{plural}/{entity_id}/notes/{note_id}"
1629
1643
  self.logger.debug(f"Fetching note {note_id} for {entity} {entity_id}")
1630
1644
  note_data = self._make_request("GET", endpoint)
1645
+ if not note_data or not isinstance(note_data, dict):
1646
+ raise APIError(0, f"Invalid response for note {note_id} {entity} {entity_id}.")
1631
1647
  self.logger.debug(f"Note {note_id} for {entity} {entity_id} fetched successfully.")
1632
1648
  return note_data
1633
1649
 
@@ -1742,6 +1758,8 @@ class AmoCRMClient:
1742
1758
  endpoint = f"/api/v4/events/{event_id}"
1743
1759
  self.logger.debug(f"Fetching event with ID {event_id}")
1744
1760
  event_data = self._make_request("GET", endpoint)
1761
+ if not event_data or not isinstance(event_data, dict):
1762
+ raise APIError(0, f"Invalid response for event {event_id}.")
1745
1763
  self.logger.debug(f"Event {event_id} details fetched successfully.")
1746
1764
  return event_data
1747
1765
 
@@ -123,6 +123,8 @@ def export_contacts_to_ndjson(
123
123
  params["limit"] = limit
124
124
  while True:
125
125
  response = client._make_request("GET", "/api/v4/contacts", params=params)
126
+ if not response:
127
+ break
126
128
  embedded = (response or {}).get("_embedded", {})
127
129
  contacts = embedded.get("contacts") or []
128
130
  if not contacts:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: amochka
3
- Version: 0.4.1
3
+ Version: 0.4.3
4
4
  Summary: Python library for working with amoCRM API with ETL capabilities
5
5
  Author-email: Timur <timurdt@gmail.com>
6
6
  License: MIT
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "amochka"
7
- version = "0.4.1"
7
+ version = "0.4.3"
8
8
  description = "Python library for working with amoCRM API with ETL capabilities"
9
9
  readme = "README.md"
10
10
  authors = [
@@ -4,7 +4,7 @@ import sys
4
4
 
5
5
  sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")))
6
6
 
7
- from amochka.etl import write_ndjson
7
+ from amochka.etl import export_contacts_to_ndjson, write_ndjson
8
8
 
9
9
 
10
10
  def test_write_ndjson_transform_and_on_record(tmp_path):
@@ -32,3 +32,26 @@ def test_write_ndjson_transform_and_on_record(tmp_path):
32
32
  assert payload["account_id"] == 42
33
33
  assert payload["payload"]["flag"] is True
34
34
  assert seen == [1]
35
+
36
+
37
+ def test_export_contacts_handles_empty_response(tmp_path):
38
+ class DummyClient:
39
+ def _make_request(self, *args, **kwargs):
40
+ return None
41
+
42
+ def get_contact_by_id(self, *_args, **_kwargs):
43
+ raise Exception("not found")
44
+
45
+ def iter_contacts(self, *args, **kwargs):
46
+ return iter(())
47
+
48
+ client = DummyClient()
49
+ output_path = tmp_path / "contacts.ndjson"
50
+ count = export_contacts_to_ndjson(
51
+ client,
52
+ output_path,
53
+ account_id=1,
54
+ contact_ids=[123],
55
+ )
56
+ assert count == 0
57
+ assert output_path.read_text() == ""
@@ -12,13 +12,16 @@ from amochka import AmoCRMClient, CacheConfig, APIError, NotFoundError, RateLimi
12
12
 
13
13
 
14
14
  class FakeResponse:
15
- def __init__(self, status_code, json_data=None, text="", headers=None):
15
+ def __init__(self, status_code, json_data=None, text="", headers=None, raise_on_json=False):
16
16
  self.status_code = status_code
17
17
  self._json_data = json_data
18
18
  self.text = text
19
19
  self.headers = headers or {}
20
+ self._raise_on_json = raise_on_json
20
21
 
21
22
  def json(self):
23
+ if self._raise_on_json:
24
+ raise ValueError("invalid json")
22
25
  return self._json_data
23
26
 
24
27
 
@@ -43,6 +46,18 @@ def test_make_request_success_json(monkeypatch):
43
46
  assert client._make_request("GET", "/api/v4/leads") == {"ok": True}
44
47
 
45
48
 
49
+ def test_make_request_invalid_json_raises(monkeypatch):
50
+ client = make_client()
51
+
52
+ def fake_request(*args, **kwargs):
53
+ return FakeResponse(200, text="not json", raise_on_json=True)
54
+
55
+ monkeypatch.setattr(client_module.requests, "request", fake_request)
56
+ with pytest.raises(APIError) as excinfo:
57
+ client._make_request("GET", "/api/v4/leads")
58
+ assert "Invalid JSON response" in str(excinfo.value)
59
+
60
+
46
61
  def test_make_request_204_returns_none(monkeypatch):
47
62
  client = make_client()
48
63
 
@@ -5,7 +5,9 @@ import time
5
5
 
6
6
  sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")))
7
7
 
8
- from amochka import AmoCRMClient, CacheConfig
8
+ import pytest
9
+
10
+ from amochka import AmoCRMClient, CacheConfig, APIError
9
11
 
10
12
 
11
13
  def make_client():
@@ -28,3 +30,17 @@ def test_get_entity_events_handles_none_response(monkeypatch):
28
30
  client = make_client()
29
31
  monkeypatch.setattr(client, "_make_request", lambda *args, **kwargs: None)
30
32
  assert client.get_entity_events("lead", 1, get_all=True) == []
33
+
34
+
35
+ def test_get_entity_note_invalid_response_raises(monkeypatch):
36
+ client = make_client()
37
+ monkeypatch.setattr(client, "_make_request", lambda *args, **kwargs: None)
38
+ with pytest.raises(APIError):
39
+ client.get_entity_note("lead", 1, 2)
40
+
41
+
42
+ def test_get_event_invalid_response_raises(monkeypatch):
43
+ client = make_client()
44
+ monkeypatch.setattr(client, "_make_request", lambda *args, **kwargs: None)
45
+ with pytest.raises(APIError):
46
+ client.get_event(123)
@@ -93,6 +93,16 @@ def test_invalid_token_string_raises():
93
93
  )
94
94
 
95
95
 
96
+ def test_missing_token_file_and_env_raises():
97
+ with pytest.raises(AuthenticationError):
98
+ AmoCRMClient(
99
+ base_url="https://example.amocrm.ru",
100
+ token_file=None,
101
+ cache_config=CacheConfig.disabled(),
102
+ disable_logging=True,
103
+ )
104
+
105
+
96
106
  def test_refresh_access_token_success(monkeypatch, tmp_path):
97
107
  token_path = tmp_path / "token.json"
98
108
  token_path.write_text(json.dumps(_token_payload(time.time() - 10)))
@@ -139,6 +149,28 @@ def test_refresh_access_token_failure(monkeypatch, tmp_path):
139
149
  )
140
150
 
141
151
 
152
+ def test_refresh_access_token_invalid_json(monkeypatch, tmp_path):
153
+ token_path = tmp_path / "token.json"
154
+ token_path.write_text(json.dumps(_token_payload(time.time() - 10)))
155
+
156
+ class BadJsonResponse:
157
+ status_code = 200
158
+ text = "not json"
159
+
160
+ def json(self):
161
+ raise ValueError("invalid json")
162
+
163
+ monkeypatch.setattr(client_module.requests, "post", lambda *args, **kwargs: BadJsonResponse())
164
+
165
+ with pytest.raises(AuthenticationError):
166
+ AmoCRMClient(
167
+ base_url="https://example.amocrm.ru",
168
+ token_file=str(token_path),
169
+ cache_config=CacheConfig.disabled(),
170
+ disable_logging=True,
171
+ )
172
+
173
+
142
174
  def test_cache_path_validation():
143
175
  with pytest.raises(ValueError):
144
176
  CacheConfig._validate_path("../secret", "base_dir")
@@ -46,6 +46,8 @@ def test_to_timestamp():
46
46
  assert client._to_timestamp(1700000000.5) == 1700000000
47
47
  iso = "2020-01-01T00:00:00"
48
48
  assert client._to_timestamp(iso) == int(datetime.fromisoformat(iso).timestamp())
49
+ iso_z = "2020-01-01T00:00:00Z"
50
+ assert client._to_timestamp(iso_z) == int(datetime.fromisoformat("2020-01-01T00:00:00+00:00").timestamp())
49
51
  with pytest.raises(ValueError):
50
52
  client._to_timestamp("bad-date")
51
53
  with pytest.raises(TypeError):
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes