ahab-lib 1.0.1__py3-none-any.whl

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.
ahab/__init__.py ADDED
@@ -0,0 +1 @@
1
+ from ahab import job, cli
ahab/cli.py ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env python3
2
+
3
+ from ahab.job_command import JobCommand
4
+ from cleo.application import Application
5
+
6
+ def cli():
7
+ cli = Application()
8
+ cli.add(JobCommand())
9
+ cli.run()
10
+
11
+ if __name__ == '__main__':
12
+ cli()
ahab/job.py ADDED
@@ -0,0 +1,32 @@
1
+ ## Functions for to submit, get, and update jobs in the ahab API
2
+ import requests, json, logging
3
+
4
+ def submit_job(body = {}, api_base_url="", api_key=""):
5
+ api_url = f'{api_base_url}/api/job?code={api_key}'
6
+ headers = {'Content-Type': 'application/json'}
7
+ response = requests.request("POST", api_url, headers=headers, data=json.dumps(body))
8
+ if response.status_code >= 300:
9
+ logging.error(f"Failed to submit job: {response.json()}")
10
+ else:
11
+ logging.info(f"Job submitted: {response.json()}")
12
+ return response.json()
13
+
14
+ def get_job(job_type="demo", api_base_url="", api_key=""):
15
+ api_url = f'{api_base_url}/api/job?code={api_key}&job_type={job_type}'
16
+ response = requests.request("GET", api_url, headers={}, data={})
17
+ if response.status_code != 200:
18
+ logging.error(f"Failed to get job: {response.json()}")
19
+ else:
20
+ logging.info(f"Job retrieved: {response.json()}")
21
+ return response.json()
22
+
23
+ def update_job(id="", status="COMPLETE", body={}, api_base_url="", api_key=""):
24
+ api_url = f'{api_base_url}/api/job?code={api_key}&id={id}&status={status}'
25
+ headers = {'Content-Type': 'application/json'}
26
+ response = requests.request("PUT", api_url, headers=headers, data=json.dumps(body))
27
+ if response.status_code != 200:
28
+ logging.error(f"Failed to update job: {response.json()}")
29
+ else:
30
+ logging.info(f"Job updated: {response.json()}")
31
+ return response.json()
32
+
ahab/job_command.py ADDED
@@ -0,0 +1,98 @@
1
+ ## A Python package and CLI to integrate with the `ahab` API
2
+
3
+ import os, json
4
+ from ahab.job import get_job, submit_job, update_job
5
+ from cleo.commands.command import Command
6
+ from cleo.helpers import argument, option
7
+
8
+ class JobCommand(Command):
9
+
10
+ name = "job"
11
+ description = "Submit/Get/Update a job with the `ahab` API"
12
+
13
+ arguments = [
14
+ argument(
15
+ "action",
16
+ description="The action to run",
17
+ optional=True
18
+ )
19
+ ]
20
+
21
+ options = [
22
+ option(
23
+ "body",
24
+ "b",
25
+ description="The body of the job to submit/update (as a <comment>JSON</comment> string)",
26
+ flag=False
27
+ ),
28
+ option(
29
+ "job_type",
30
+ "j",
31
+ description="The job type from which to query a job with a `PENDING` status",
32
+ flag=False
33
+ ),
34
+ option(
35
+ "id",
36
+ None,
37
+ description="The ID of the job to run",
38
+ flag=False
39
+ ),
40
+ option(
41
+ "status",
42
+ "s",
43
+ description="The new status to be set for the job",
44
+ flag=False
45
+ ),
46
+ option(
47
+ "api_base_url",
48
+ "u",
49
+ description = "The base URL of the <fg=magenta>ahab</> Function API (e.g., <comment>https://func-ahab-dev-eastus-001.azurewebsites.net</comment>).\nIf not provided, the value of the <comment>AHAB_API_URL</comment> environment variable will be used.",
50
+ flag=False
51
+ ),
52
+ option(
53
+ "api_key",
54
+ "k",
55
+ description = "The key of the <fg=magenta>ahab</> Function API (e.g., <comment>DmXPlzmZ3Pq2chtVV_gggzVKh2uAIlFIGgcr9nkl29U0AzFuToWgIw==</comment>).\nIf not provided, the value of the <comment>AHAB_API_KEY</comment> environment variable will be used.",
56
+ flag=False
57
+ )
58
+ ]
59
+
60
+ def handle(self):
61
+ ## Get URL and key for API
62
+ if self.option('api_base_url') and self.option('api_key'):
63
+ api_base_url = self.option('api_base_url')
64
+ api_key = self.option('api_key')
65
+ elif 'AHAB_API_URL' and 'AHAB_API_KEY' not in os.environ:
66
+ self.line('Your <fg=magenta>ahab</> API URL and key must be defined in the environment as <comment>AHAB_API_URL</comment> and <comment>AHAB_API_KEY</comment>, respectively.')
67
+ return
68
+ else:
69
+ api_base_url = os.environ.get('AHAB_API_URL', '')
70
+ api_key = os.environ.get('AHAB_API_KEY', '')
71
+
72
+ action = self.argument('action')
73
+
74
+ ## Submit Job
75
+ if action == 'submit':
76
+ body = json.loads(self.option('body'))
77
+ ## Check keys in body
78
+ if set(['job_type']).issubset(set(body.keys())):
79
+ response = submit_job(body, api_base_url, api_key)
80
+ self.line(json.dumps(response))
81
+ else:
82
+ self.line('You must provide a job type for the job to submit.')
83
+ return
84
+
85
+
86
+ ## Get Job
87
+ if action == 'get':
88
+ job_type = self.option('job_type')
89
+ response = get_job(job_type, api_base_url, api_key)
90
+ self.line(json.dumps(response))
91
+
92
+ ## Update Job
93
+ if action == 'update':
94
+ id = self.option('id')
95
+ status = self.option('status')
96
+ body = json.loads(self.option('body'))
97
+ response = update_job(id, status, body, api_base_url, api_key)
98
+ self.line(json.dumps(response))