alfred-python 0.6.3__tar.gz → 0.6.4__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 (42) hide show
  1. {alfred_python-0.6.3 → alfred_python-0.6.4}/PKG-INFO +1 -1
  2. alfred_python-0.6.4/alfred/rest/jobs/v1.py +113 -0
  3. {alfred_python-0.6.3 → alfred_python-0.6.4}/alfred_python.egg-info/PKG-INFO +1 -1
  4. {alfred_python-0.6.3 → alfred_python-0.6.4}/alfred_python.egg-info/SOURCES.txt +2 -1
  5. {alfred_python-0.6.3 → alfred_python-0.6.4}/pyproject.toml +1 -1
  6. alfred_python-0.6.4/tests/test_smoke.py +96 -0
  7. alfred_python-0.6.3/alfred/rest/jobs/v1.py +0 -48
  8. {alfred_python-0.6.3 → alfred_python-0.6.4}/LICENSE +0 -0
  9. {alfred_python-0.6.3 → alfred_python-0.6.4}/README.md +0 -0
  10. {alfred_python-0.6.3 → alfred_python-0.6.4}/alfred/__init__.py +0 -0
  11. {alfred_python-0.6.3 → alfred_python-0.6.4}/alfred/base/__init__.py +0 -0
  12. {alfred_python-0.6.3 → alfred_python-0.6.4}/alfred/base/config.py +0 -0
  13. {alfred_python-0.6.3 → alfred_python-0.6.4}/alfred/base/constants.py +0 -0
  14. {alfred_python-0.6.3 → alfred_python-0.6.4}/alfred/base/exceptions.py +0 -0
  15. {alfred_python-0.6.3 → alfred_python-0.6.4}/alfred/base/typed.py +0 -0
  16. {alfred_python-0.6.3 → alfred_python-0.6.4}/alfred/exceptions/__init__.py +0 -0
  17. {alfred_python-0.6.3 → alfred_python-0.6.4}/alfred/http/__init__.py +0 -0
  18. {alfred_python-0.6.3 → alfred_python-0.6.4}/alfred/http/http_client.py +0 -0
  19. {alfred_python-0.6.3 → alfred_python-0.6.4}/alfred/http/typed.py +0 -0
  20. {alfred_python-0.6.3 → alfred_python-0.6.4}/alfred/realtime/__init__.py +0 -0
  21. {alfred_python-0.6.3 → alfred_python-0.6.4}/alfred/rest/__init__.py +0 -0
  22. {alfred_python-0.6.3 → alfred_python-0.6.4}/alfred/rest/data_points/__init__.py +0 -0
  23. {alfred_python-0.6.3 → alfred_python-0.6.4}/alfred/rest/data_points/base.py +0 -0
  24. {alfred_python-0.6.3 → alfred_python-0.6.4}/alfred/rest/data_points/v1.py +0 -0
  25. {alfred_python-0.6.3 → alfred_python-0.6.4}/alfred/rest/files/__init__.py +0 -0
  26. {alfred_python-0.6.3 → alfred_python-0.6.4}/alfred/rest/files/base.py +0 -0
  27. {alfred_python-0.6.3 → alfred_python-0.6.4}/alfred/rest/files/typed.py +0 -0
  28. {alfred_python-0.6.3 → alfred_python-0.6.4}/alfred/rest/files/v1.py +0 -0
  29. {alfred_python-0.6.3 → alfred_python-0.6.4}/alfred/rest/jobs/__init__.py +0 -0
  30. {alfred_python-0.6.3 → alfred_python-0.6.4}/alfred/rest/jobs/base.py +0 -0
  31. {alfred_python-0.6.3 → alfred_python-0.6.4}/alfred/rest/jobs/typed.py +0 -0
  32. {alfred_python-0.6.3 → alfred_python-0.6.4}/alfred/rest/sessions/__init__.py +0 -0
  33. {alfred_python-0.6.3 → alfred_python-0.6.4}/alfred/rest/sessions/base.py +0 -0
  34. {alfred_python-0.6.3 → alfred_python-0.6.4}/alfred/rest/sessions/v1.py +0 -0
  35. {alfred_python-0.6.3 → alfred_python-0.6.4}/alfred/typings/__init__.py +0 -0
  36. {alfred_python-0.6.3 → alfred_python-0.6.4}/alfred/typings/misc.py +0 -0
  37. {alfred_python-0.6.3 → alfred_python-0.6.4}/alfred/utils/__init__.py +0 -0
  38. {alfred_python-0.6.3 → alfred_python-0.6.4}/alfred/utils/logging.py +0 -0
  39. {alfred_python-0.6.3 → alfred_python-0.6.4}/alfred_python.egg-info/dependency_links.txt +0 -0
  40. {alfred_python-0.6.3 → alfred_python-0.6.4}/alfred_python.egg-info/requires.txt +0 -0
  41. {alfred_python-0.6.3 → alfred_python-0.6.4}/alfred_python.egg-info/top_level.txt +0 -0
  42. {alfred_python-0.6.3 → alfred_python-0.6.4}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: alfred-python
3
- Version: 0.6.3
3
+ Version: 0.6.4
4
4
  Summary: Python library designed to simplify and streamline interactions with the Alfred API
5
5
  Author-email: Tagshelf LLC <support@tagshelf.com>
6
6
  License: Copyright (c) 2018 The Python Packaging Authority
@@ -0,0 +1,113 @@
1
+ # Native imports
2
+ import json
3
+ from typing import Any, Dict, Text
4
+
5
+ # Project imports
6
+ from alfred.rest.jobs.typed import CreateJobDict
7
+ from alfred.http.http_client import HttpClient
8
+ from alfred.rest.jobs.base import JobsBase
9
+
10
+
11
+ class Jobs(JobsBase):
12
+ def __init__(self, http_client: HttpClient):
13
+ self.http_client = http_client
14
+
15
+ def create(self, job: CreateJobDict):
16
+ """
17
+ Creates a new Job.
18
+
19
+ Args:
20
+ - job: Job creation parameters.
21
+ """
22
+ parsed_resp, _ = self.http_client.post("/api/job/create", data=job)
23
+ return parsed_resp
24
+
25
+ def get(self, job_id: Text):
26
+ """
27
+ Fetches a Job by its ID.
28
+
29
+ Args:
30
+ - job_id: Unique identifier of the Job.
31
+ """
32
+ parsed_resp, _ = self.http_client.get(f"/api/job/detail/{job_id}")
33
+ return self.__normalize_job_response(parsed_resp)
34
+
35
+ def get_all(self, page_size: int = None, current_page: int = None):
36
+ """
37
+ Fetches all jobs for a company
38
+
39
+ Args:
40
+ - page_size: Number of jobs to fetch per page.
41
+ - current_page: Page number to fetch.
42
+ """
43
+ params = {}
44
+ if page_size:
45
+ params["pageSize"] = page_size
46
+ if current_page:
47
+ params["currentPage"] = current_page
48
+ parsed_resp, _ = self.http_client.get("/api/job/all", params=params)
49
+ return self.__normalize_job_response(parsed_resp)
50
+
51
+ def __normalize_job_response(self, payload: Any):
52
+ """
53
+ Normalize job payloads returned by job endpoints.
54
+
55
+ Supports direct job objects as well as wrapped responses where jobs
56
+ are returned under a `result` key.
57
+ """
58
+ if isinstance(payload, list):
59
+ return [self.__normalize_job(item) for item in payload]
60
+
61
+ if not isinstance(payload, dict):
62
+ return payload
63
+
64
+ response = dict(payload)
65
+ result = response.get("result")
66
+
67
+ if isinstance(result, list):
68
+ response["result"] = [self.__normalize_job(item) for item in result]
69
+ return response
70
+
71
+ if isinstance(result, dict):
72
+ response["result"] = self.__normalize_job(result)
73
+ return response
74
+
75
+ return self.__normalize_job(response)
76
+
77
+ def __normalize_job(self, job: Any):
78
+ """
79
+ Normalize a single job object and coerce its metadata to a dictionary.
80
+ """
81
+ if not isinstance(job, dict):
82
+ return job
83
+
84
+ normalized_job = dict(job)
85
+ normalized_job["metadata"] = self.__normalize_metadata(
86
+ normalized_job.get("metadata")
87
+ )
88
+ return normalized_job
89
+
90
+ @staticmethod
91
+ def __normalize_metadata(metadata: Any) -> Dict[str, Any]:
92
+ """
93
+ Convert upstream job metadata into a dictionary when possible.
94
+
95
+ Metadata may arrive as a JSON-encoded string, an already parsed
96
+ dictionary, or an empty/invalid value. Non-dictionary results are
97
+ normalized to an empty dictionary.
98
+ """
99
+ if isinstance(metadata, dict):
100
+ return metadata
101
+
102
+ if not metadata or not isinstance(metadata, str):
103
+ return {}
104
+
105
+ try:
106
+ parsed_metadata = json.loads(metadata)
107
+ except (TypeError, ValueError, json.JSONDecodeError):
108
+ return {}
109
+
110
+ if isinstance(parsed_metadata, dict):
111
+ return parsed_metadata
112
+
113
+ return {}
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: alfred-python
3
- Version: 0.6.3
3
+ Version: 0.6.4
4
4
  Summary: Python library designed to simplify and streamline interactions with the Alfred API
5
5
  Author-email: Tagshelf LLC <support@tagshelf.com>
6
6
  License: Copyright (c) 2018 The Python Packaging Authority
@@ -35,4 +35,5 @@ alfred_python.egg-info/PKG-INFO
35
35
  alfred_python.egg-info/SOURCES.txt
36
36
  alfred_python.egg-info/dependency_links.txt
37
37
  alfred_python.egg-info/requires.txt
38
- alfred_python.egg-info/top_level.txt
38
+ alfred_python.egg-info/top_level.txt
39
+ tests/test_smoke.py
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "alfred-python"
7
- version = "0.6.3"
7
+ version = "0.6.4"
8
8
  authors = [{ name = "Tagshelf LLC", email = "support@tagshelf.com" }]
9
9
  description = "Python library designed to simplify and streamline interactions with the Alfred API"
10
10
  readme = "README.md"
@@ -0,0 +1,96 @@
1
+ import unittest
2
+
3
+ from alfred.rest.jobs.v1 import Jobs
4
+
5
+
6
+ class FakeHttpClient:
7
+ def __init__(self, post_response=None, get_response=None):
8
+ self.post_response = post_response
9
+ self.get_response = get_response
10
+
11
+ def post(self, *_args, **_kwargs):
12
+ return self.post_response, None
13
+
14
+ def get(self, *_args, **_kwargs):
15
+ return self.get_response, None
16
+
17
+
18
+ class TestJobsSmoke(unittest.TestCase):
19
+ def test_create_returns_raw_job_creation_response(self):
20
+ jobs = Jobs(
21
+ FakeHttpClient(
22
+ post_response={
23
+ "id": "job-1",
24
+ }
25
+ )
26
+ )
27
+
28
+ result = jobs.create({})
29
+
30
+ self.assertEqual(result, {"id": "job-1"})
31
+
32
+ def test_get_normalizes_invalid_job_metadata_to_empty_dict(self):
33
+ jobs = Jobs(
34
+ FakeHttpClient(
35
+ get_response={
36
+ "id": "job-1",
37
+ "metadata": "not-json",
38
+ }
39
+ )
40
+ )
41
+
42
+ result = jobs.get("job-1")
43
+
44
+ self.assertEqual(result.get("metadata"), {})
45
+
46
+ def test_get_normalizes_wrapped_single_job_metadata(self):
47
+ jobs = Jobs(
48
+ FakeHttpClient(
49
+ get_response={
50
+ "result": {
51
+ "id": "job-1",
52
+ "metadata": "{\"metadata_key\": \"metadata_value\"}",
53
+ }
54
+ }
55
+ )
56
+ )
57
+
58
+ result = jobs.get("job-1")
59
+
60
+ self.assertEqual(
61
+ result.get("result"),
62
+ {"id": "job-1", "metadata": {"metadata_key": "metadata_value"}},
63
+ )
64
+
65
+ def test_get_all_normalizes_metadata_in_paginated_results(self):
66
+ jobs = Jobs(
67
+ FakeHttpClient(
68
+ get_response={
69
+ "result": [
70
+ {
71
+ "id": "job-1",
72
+ "metadata": "{\"metadata_key\": \"metadata_value\"}",
73
+ },
74
+ {
75
+ "id": "job-2",
76
+ "metadata": "",
77
+ },
78
+ ],
79
+ "total": 2,
80
+ }
81
+ )
82
+ )
83
+
84
+ result = jobs.get_all()
85
+
86
+ self.assertEqual(
87
+ result.get("result"),
88
+ [
89
+ {"id": "job-1", "metadata": {"metadata_key": "metadata_value"}},
90
+ {"id": "job-2", "metadata": {}},
91
+ ],
92
+ )
93
+
94
+
95
+ if __name__ == "__main__":
96
+ unittest.main()
@@ -1,48 +0,0 @@
1
- # Native imports
2
- from typing import Text
3
-
4
- # Project imports
5
- from alfred.rest.jobs.typed import CreateJobDict
6
- from alfred.http.http_client import HttpClient
7
- from alfred.rest.jobs.base import JobsBase
8
-
9
-
10
- class Jobs(JobsBase):
11
- def __init__(self, http_client: HttpClient):
12
- self.http_client = http_client
13
-
14
- def create(self, job: CreateJobDict):
15
- """
16
- Creates a new Job.
17
-
18
- Args:
19
- - job: Job creation parameters.
20
- """
21
- parsed_resp, _ = self.http_client.post("/api/job/create", data=job)
22
- return parsed_resp
23
-
24
- def get(self, job_id: Text):
25
- """
26
- Fetches a Job by its ID.
27
-
28
- Args:
29
- - job_id: Unique identifier of the Job.
30
- """
31
- parsed_resp, _ = self.http_client.get(f"/api/job/detail/{job_id}")
32
- return parsed_resp
33
-
34
- def get_all(self, page_size: int = None, current_page: int = None):
35
- """
36
- Fetches all jobs for a company
37
-
38
- Args:
39
- - page_size: Number of jobs to fetch per page.
40
- - current_page: Page number to fetch.
41
- """
42
- params = {}
43
- if page_size:
44
- params["pageSize"] = page_size
45
- if current_page:
46
- params["currentPage"] = current_page
47
- parsed_resp, _ = self.http_client.get("/api/job/all", params=params)
48
- return parsed_resp
File without changes
File without changes
File without changes