werkflow-appito 0.1.0__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 (23) hide show
  1. werkflow_appito-0.1.0/LICENSE +21 -0
  2. werkflow_appito-0.1.0/PKG-INFO +21 -0
  3. werkflow_appito-0.1.0/README.md +0 -0
  4. werkflow_appito-0.1.0/pyproject.toml +40 -0
  5. werkflow_appito-0.1.0/setup.cfg +4 -0
  6. werkflow_appito-0.1.0/werkflow_appito/__init__.py +1 -0
  7. werkflow_appito-0.1.0/werkflow_appito/appito.py +256 -0
  8. werkflow_appito-0.1.0/werkflow_appito/models/__init__.py +8 -0
  9. werkflow_appito-0.1.0/werkflow_appito/models/api/__init__.py +3 -0
  10. werkflow_appito-0.1.0/werkflow_appito/models/api/appito_get_environment_request.py +7 -0
  11. werkflow_appito-0.1.0/werkflow_appito/models/api/appito_get_environment_response.py +6 -0
  12. werkflow_appito-0.1.0/werkflow_appito/models/api/appito_upload_table_request.py +16 -0
  13. werkflow_appito-0.1.0/werkflow_appito/models/environment/__init__.py +1 -0
  14. werkflow_appito-0.1.0/werkflow_appito/models/environment/appito_environment.py +5 -0
  15. werkflow_appito-0.1.0/werkflow_appito/models/shared/__init__.py +4 -0
  16. werkflow_appito-0.1.0/werkflow_appito/models/shared/appito_credentials.py +7 -0
  17. werkflow_appito-0.1.0/werkflow_appito/models/shared/appito_region.py +11 -0
  18. werkflow_appito-0.1.0/werkflow_appito/models/shared/appito_region_url_map.py +18 -0
  19. werkflow_appito-0.1.0/werkflow_appito.egg-info/PKG-INFO +21 -0
  20. werkflow_appito-0.1.0/werkflow_appito.egg-info/SOURCES.txt +21 -0
  21. werkflow_appito-0.1.0/werkflow_appito.egg-info/dependency_links.txt +1 -0
  22. werkflow_appito-0.1.0/werkflow_appito.egg-info/requires.txt +7 -0
  23. werkflow_appito-0.1.0/werkflow_appito.egg-info/top_level.txt +1 -0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Ada Lundhe
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,21 @@
1
+ Metadata-Version: 2.4
2
+ Name: werkflow-appito
3
+ Version: 0.1.0
4
+ Summary: An IBM Appito module for Werkflow.
5
+ Author-email: Ada Lundhe <adalundhe@lundhe.audio>
6
+ Keywords: pypi,cicd,python,werkflow,workflow,werkflow-core,appito,boto3,ibm
7
+ Classifier: Programming Language :: Python :: 3.11
8
+ Classifier: Programming Language :: Python :: 3.12
9
+ Classifier: Programming Language :: Python :: 3.13
10
+ Classifier: Operating System :: OS Independent
11
+ Requires-Python: >=3.11
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE
14
+ Requires-Dist: werkflow-core
15
+ Requires-Dist: werkflow-file
16
+ Requires-Dist: werkflow-secrets
17
+ Requires-Dist: werkflow-shell
18
+ Requires-Dist: werkflow-system
19
+ Requires-Dist: werkflow-http
20
+ Requires-Dist: pydantic
21
+ Dynamic: license-file
File without changes
@@ -0,0 +1,40 @@
1
+ [project]
2
+ name = "werkflow-appito"
3
+ version = "0.1.0"
4
+ description = "An IBM Appito module for Werkflow."
5
+ readme = "README.md"
6
+ requires-python = ">=3.11"
7
+ authors = [
8
+ {name = "Ada Lundhe", email = "adalundhe@lundhe.audio"}
9
+ ]
10
+ classifiers = [
11
+ "Programming Language :: Python :: 3.11",
12
+ "Programming Language :: Python :: 3.12",
13
+ "Programming Language :: Python :: 3.13",
14
+ "Operating System :: OS Independent",
15
+ ]
16
+ keywords = [
17
+ 'pypi',
18
+ 'cicd',
19
+ 'python',
20
+ 'werkflow',
21
+ 'workflow',
22
+ 'werkflow-core',
23
+ 'appito',
24
+ 'boto3',
25
+ 'ibm'
26
+ ]
27
+ license-files = [ "LICENSE" ]
28
+ dependencies = [
29
+ 'werkflow-core',
30
+ 'werkflow-file',
31
+ 'werkflow-secrets',
32
+ 'werkflow-shell',
33
+ 'werkflow-system',
34
+ "werkflow-http",
35
+ 'pydantic',
36
+ ]
37
+
38
+ [build-system]
39
+ requires = ["setuptools>=61.0"]
40
+ build-backend = "setuptools.build_meta"
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1 @@
1
+ from .appito import Appito as Appito
@@ -0,0 +1,256 @@
1
+ import os
2
+ import pathlib
3
+ from urllib.parse import quote
4
+
5
+ from werkflow_http import HTTP
6
+ from werkflow_http.request import File
7
+ from werkflow_shell import Shell
8
+
9
+ from .models import (
10
+ AppitoCredentials,
11
+ AppitoGetEnviornmentRequest,
12
+ AppitoGetEnvironmentResponse,
13
+ AppitoUploadTableRequest,
14
+ AppitoRegionUrlMap,
15
+ AppitoRegionName,
16
+ )
17
+
18
+
19
+ class Appito:
20
+
21
+ """Handles file uploads to Apptio API using API key authentication"""
22
+
23
+ def __init__(
24
+ self,
25
+ region: AppitoRegionName = "US",
26
+ ):
27
+
28
+ self._region_url_map = AppitoRegionUrlMap()
29
+
30
+ self._base_url = self._region_url_map.get(region)
31
+ self._client = HTTP()
32
+ self._shell = Shell()
33
+
34
+ async def upload_file(
35
+ self,
36
+ request: AppitoUploadTableRequest,
37
+ ):
38
+
39
+ if request.appito_credentials is None:
40
+ appito_access_key = await self._shell.get_envar(
41
+ "APPITO_ACCESS_KEY",
42
+ )
43
+
44
+ appito_secret_key = await self._shell.get_envar(
45
+ "APPITO_SECRET_KEY",
46
+ )
47
+
48
+ request = request.model_copy(
49
+ update={
50
+ 'appito_credentials': AppitoCredentials(
51
+ appito_access_key=appito_access_key,
52
+ appito_secret_key=appito_secret_key,
53
+ )
54
+ },
55
+ )
56
+
57
+ data = {}
58
+ if request.appito_action and request.appito_force is not None:
59
+ data['action'] = request.appito_action
60
+ data['force'] = str(request.appito_force).lower()
61
+
62
+ # Get authentication token
63
+ auth_token = await self._authenticate(request.appito_credentials)
64
+ if isinstance(auth_token, Exception):
65
+ return auth_token
66
+
67
+ request = await self._update_request_with_environment(
68
+ AppitoGetEnviornmentRequest(
69
+ auth_token=auth_token,
70
+ domain=request.appito_domain,
71
+ environment_id=request.appito_environment_id,
72
+ ),
73
+ )
74
+
75
+ if isinstance(request, Exception):
76
+ return request
77
+
78
+ url_path = self._encode_to_url_path(request)
79
+
80
+ err: Exception | None = None
81
+ if isinstance(request.appito_csv_file, str):
82
+ upload_file = File(
83
+ name=pathlib.Path(request.appito_csv_file).stem,
84
+ path=request.appito_csv_file,
85
+ content_type="text/csv",
86
+ )
87
+
88
+ err = await self._check_filepath(upload_file.path)
89
+
90
+ else:
91
+ upload_file = request.appito_csv_file
92
+
93
+ if err:
94
+ return err
95
+
96
+ response = await self._client.post(
97
+ f'{self._base_url}/{url_path}',
98
+ data=data,
99
+ files=[upload_file],
100
+ headers={
101
+ 'apptio-opentoken': auth_token,
102
+ 'apptio-current-environment': request.appito_environment_id,
103
+ 'app-type': 'Flagship',
104
+ 'app-version': 'NA'
105
+ },
106
+ )
107
+
108
+ if not response.check_success():
109
+ return Exception(
110
+ f'Err. - CSV upload failed - {response.status}: {response.status_message}',
111
+ )
112
+
113
+ # Parse JSON response
114
+ result = response.json()
115
+ if isinstance(result, Exception):
116
+ return result
117
+
118
+ # Validate response structure
119
+ if 'message' in result:
120
+ # This is an error response
121
+ raise Exception(f"Apptio API Error: {result['message']}")
122
+
123
+ return result
124
+
125
+ async def _authenticate(
126
+ self,
127
+ credentials: AppitoCredentials,
128
+ ):
129
+ """
130
+ Authenticate using API keys and get apptio-opentoken
131
+
132
+ Returns:
133
+ Authentication token for use in subsequent requests
134
+ """
135
+
136
+ response = await self._client.post(
137
+ f"{self._base_url}/service/apikeylogin",
138
+ headers={
139
+ "Content-Type": "application/json",
140
+ "Accept": "application/json",
141
+ "apptio-current-environment": credentials.appito_current_environment
142
+ },
143
+ data={
144
+ "keyAccess": credentials.appito_access_key,
145
+ "keySecret": credentials.appito_secret_key,
146
+ },
147
+ )
148
+
149
+ if not response.check_success():
150
+ return Exception(
151
+ f'Err. - could not authenticate - {response.status}: {response.text()}',
152
+ )
153
+
154
+ # Extract the apptio-opentoken from response headers
155
+ token: bytes | None = response.headers.get(b'apptio-opentoken')
156
+ if token is None:
157
+ return Exception("No apptio-opentoken found in response headers")
158
+
159
+ return token.decode()
160
+
161
+ async def _get_matching_environment(
162
+ self,
163
+ request: AppitoGetEnviornmentRequest
164
+ ):
165
+
166
+ response = await self._client.get(
167
+ f"{self._base_url}/api/environments/{request.domain}",
168
+ headers={
169
+ "apptio-opentoken": request.auth_token,
170
+ "Accept": "application/json"
171
+ },
172
+ )
173
+
174
+ if not response.check_success():
175
+ return Exception(
176
+ f'Err. - environment ID fetch failed - {response.status}: {response.text()}',
177
+ )
178
+
179
+ data = response.json()
180
+ if isinstance(data, Exception):
181
+ return data
182
+
183
+ environment_response = AppitoGetEnvironmentResponse(environments=data)
184
+
185
+ if request.environment_id:
186
+ for env in environment_response.environments:
187
+ if env.id == request.environment_id:
188
+ return env
189
+
190
+ return Exception('No matching id found.')
191
+
192
+ for env in environment_response.environments:
193
+ if env.id is not None:
194
+ return env
195
+
196
+ return Exception('No matching id found')
197
+
198
+ async def _update_request_with_environment(
199
+ self,
200
+ environment_request: AppitoGetEnviornmentRequest,
201
+ table_request: AppitoUploadTableRequest
202
+ ) -> AppitoUploadTableRequest:
203
+ environment = await self._get_matching_environment(environment_request)
204
+ if isinstance(environment, Exception):
205
+ return environment
206
+
207
+ return table_request.model_copy(
208
+ update={
209
+ "appito_environment_id": environment.id,
210
+ },
211
+ )
212
+
213
+ def _encode_to_url_path(
214
+ self,
215
+ request: AppitoUploadTableRequest,
216
+ ) -> str:
217
+ url_parts = [
218
+ "domains",
219
+ request.appito_domain,
220
+ "projects",
221
+ request.appito_project,
222
+ "tables",
223
+ request.appito_table_name,
224
+ "dates",
225
+ request.appito_time_period,
226
+ ]
227
+
228
+ if request.appito_action and request.appito_environment_id is not None:
229
+ pass
230
+ elif request.appito_action:
231
+ url_parts.append(request.appito_action)
232
+ elif request.appito_force is not None:
233
+ url_parts.append("force")
234
+ else:
235
+ url_parts.append("overwrite")
236
+
237
+ return '/'.join([
238
+ quote(part, safe='') for part in url_parts
239
+ ])
240
+
241
+ async def _check_filepath(self, csv_filepath: str):
242
+ if not await self._shell.path_is_file(csv_filepath):
243
+ return FileNotFoundError(f"CSV file not found: {csv_filepath}")
244
+
245
+ if not await self._shell.check_file_access(
246
+ csv_filepath,
247
+ os.R_OK,
248
+ ):
249
+ return PermissionError(f"Cannot read CSV file: {csv_filepath}")
250
+
251
+ # Validate file extension
252
+ file_ext = pathlib.Path(csv_filepath).suffix.lower()
253
+ supported_formats = ['.csv', '.tsv', '.csv.gz', '.tsv.gz']
254
+ if file_ext not in supported_formats:
255
+ return ValueError(f"Unsupported file format: {file_ext}. Supported formats: {', '.join(supported_formats)}")
256
+
@@ -0,0 +1,8 @@
1
+ from .api import AppitoGetEnviornmentRequest as AppitoGetEnviornmentRequest
2
+ from .api import AppitoGetEnvironmentResponse as AppitoGetEnvironmentResponse
3
+ from .api import AppitoUploadTableRequest as AppitoUploadTableRequest
4
+ from .environment import AppitoEnvironment as AppitoEnvironment
5
+ from .shared import AppitoCredentials as AppitoCredentials
6
+ from .shared import AppitoRegion as AppitoRegion
7
+ from .shared import AppitoRegionName as AppitoRegionName
8
+ from .shared import AppitoRegionUrlMap as AppitoRegionUrlMap
@@ -0,0 +1,3 @@
1
+ from .appito_get_environment_request import AppitoGetEnviornmentRequest as AppitoGetEnviornmentRequest
2
+ from .appito_get_environment_response import AppitoGetEnvironmentResponse as AppitoGetEnvironmentResponse
3
+ from .appito_upload_table_request import AppitoUploadTableRequest as AppitoUploadTableRequest
@@ -0,0 +1,7 @@
1
+ from pydantic import BaseModel, StrictStr
2
+
3
+
4
+ class AppitoGetEnviornmentRequest(BaseModel):
5
+ auth_token: StrictStr
6
+ domain: StrictStr
7
+ environment_id: StrictStr | None = None
@@ -0,0 +1,6 @@
1
+ from pydantic import BaseModel
2
+ from werkflow_appito.models.environment.appito_environment import AppitoEnvironment
3
+
4
+
5
+ class AppitoGetEnvironmentResponse(BaseModel):
6
+ environments: list[AppitoEnvironment]
@@ -0,0 +1,16 @@
1
+ from pydantic import BaseModel, StrictStr, StrictBool
2
+ from werkflow_appito.models.shared.appito_credentials import AppitoCredentials
3
+ from werkflow_http.request import File
4
+
5
+ class AppitoUploadTableRequest(BaseModel):
6
+ appito_domain: StrictStr
7
+ appito_csv_file: StrictStr | File
8
+ appito_project: StrictStr
9
+ appito_table_name: StrictStr
10
+ appito_time_period: StrictStr = "current"
11
+ appito_environment_id: StrictStr | None = None
12
+ appito_action: StrictStr | None = None
13
+ appito_force: StrictBool | None = None
14
+ appito_credentials: AppitoCredentials | None = None
15
+
16
+
@@ -0,0 +1 @@
1
+ from .appito_environment import AppitoEnvironment as AppitoEnvironment
@@ -0,0 +1,5 @@
1
+ from pydantic import BaseModel, StrictStr
2
+
3
+
4
+ class AppitoEnvironment(BaseModel):
5
+ id: StrictStr | None = None
@@ -0,0 +1,4 @@
1
+ from .appito_region import AppitoRegion as AppitoRegion
2
+ from .appito_region import AppitoRegionName as AppitoRegionName
3
+ from .appito_region_url_map import AppitoRegionUrlMap as AppitoRegionUrlMap
4
+ from .appito_credentials import AppitoCredentials as AppitoCredentials
@@ -0,0 +1,7 @@
1
+ from pydantic import BaseModel, StrictStr
2
+
3
+
4
+ class AppitoCredentials(BaseModel):
5
+ appito_access_key: StrictStr | None = None
6
+ appito_secret_key: StrictStr | None = None
7
+ appito_current_environment: StrictStr
@@ -0,0 +1,11 @@
1
+ from enum import Enum
2
+ from typing import Literal
3
+
4
+
5
+ AppitoRegionName = Literal["AU", "DEFAULT", "EU", "US"]
6
+
7
+ class AppitoRegion(Enum):
8
+ AU = "AU"
9
+ DEFAULT = "DEFAULT"
10
+ EU = "EU"
11
+ US = "US"
@@ -0,0 +1,18 @@
1
+ from .appito_region import AppitoRegion, AppitoRegionName
2
+
3
+
4
+ class AppitoRegionUrlMap:
5
+
6
+ def __init__(self):
7
+ self._region_url_map: dict[AppitoRegion, str] = {
8
+ AppitoRegion.EU: "https://frontdoor-eu.apptio.com",
9
+ AppitoRegion.AU: "https://frontdoor-au.apptio.com",
10
+ AppitoRegion.DEFAULT: "https://frontdoor.apptio.com",
11
+ AppitoRegion.US: "https://frontdoor.apptio.com",
12
+ }
13
+
14
+ def get(self, name: AppitoRegionName):
15
+ return self._region_url_map.get(
16
+ AppitoRegion[name],
17
+ self._region_url_map[AppitoRegion.DEFAULT],
18
+ )
@@ -0,0 +1,21 @@
1
+ Metadata-Version: 2.4
2
+ Name: werkflow-appito
3
+ Version: 0.1.0
4
+ Summary: An IBM Appito module for Werkflow.
5
+ Author-email: Ada Lundhe <adalundhe@lundhe.audio>
6
+ Keywords: pypi,cicd,python,werkflow,workflow,werkflow-core,appito,boto3,ibm
7
+ Classifier: Programming Language :: Python :: 3.11
8
+ Classifier: Programming Language :: Python :: 3.12
9
+ Classifier: Programming Language :: Python :: 3.13
10
+ Classifier: Operating System :: OS Independent
11
+ Requires-Python: >=3.11
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE
14
+ Requires-Dist: werkflow-core
15
+ Requires-Dist: werkflow-file
16
+ Requires-Dist: werkflow-secrets
17
+ Requires-Dist: werkflow-shell
18
+ Requires-Dist: werkflow-system
19
+ Requires-Dist: werkflow-http
20
+ Requires-Dist: pydantic
21
+ Dynamic: license-file
@@ -0,0 +1,21 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ werkflow_appito/__init__.py
5
+ werkflow_appito/appito.py
6
+ werkflow_appito.egg-info/PKG-INFO
7
+ werkflow_appito.egg-info/SOURCES.txt
8
+ werkflow_appito.egg-info/dependency_links.txt
9
+ werkflow_appito.egg-info/requires.txt
10
+ werkflow_appito.egg-info/top_level.txt
11
+ werkflow_appito/models/__init__.py
12
+ werkflow_appito/models/api/__init__.py
13
+ werkflow_appito/models/api/appito_get_environment_request.py
14
+ werkflow_appito/models/api/appito_get_environment_response.py
15
+ werkflow_appito/models/api/appito_upload_table_request.py
16
+ werkflow_appito/models/environment/__init__.py
17
+ werkflow_appito/models/environment/appito_environment.py
18
+ werkflow_appito/models/shared/__init__.py
19
+ werkflow_appito/models/shared/appito_credentials.py
20
+ werkflow_appito/models/shared/appito_region.py
21
+ werkflow_appito/models/shared/appito_region_url_map.py
@@ -0,0 +1,7 @@
1
+ werkflow-core
2
+ werkflow-file
3
+ werkflow-secrets
4
+ werkflow-shell
5
+ werkflow-system
6
+ werkflow-http
7
+ pydantic
@@ -0,0 +1 @@
1
+ werkflow_appito