quick-start-connectors
122 строки · 3.8 Кб
1import requests
2from flask import current_app as app
3
4from . import UpstreamProviderError
5
6client = None
7
8
9class AhaApiClient:
10ALLOWED_ENTITY_TYPES = {
11"users": {
12"search_fields": ["name", "email"],
13"mapping": {"name": "text", "email": "title"},
14},
15"capacity_scenarios": {
16"search_fields": ["name"],
17"mapping": {"name": "text"},
18},
19"epics": {
20"search_fields": ["q"],
21"mapping": {"name": "text", "reference_num": "title"},
22},
23"features": {
24"search_fields": ["q"],
25"mapping": {"name": "text", "reference_num": "title"},
26},
27"goals": {
28"search_fields": ["name", "reference_num", "description"],
29"mapping": {"description": "text", "name": "title"},
30},
31"ideas": {
32"search_fields": ["q"],
33"mapping": {"description": "text", "name": "title"},
34},
35"initiatives": {
36"search_fields": ["q"],
37"mapping": {"description": "text", "name": "title"},
38},
39"integrations": {"search_fields": ["name"], "mapping": {"name": "text"}},
40"products": {
41"search_fields": ["name"],
42"mapping": {"name": "text", "reference_prefix": "title"},
43},
44"release_phases": {
45"search_fields": ["name", "description"],
46"mapping": {"description": "text", "name": "title"},
47},
48"strategy_models": {
49"search_fields": ["name", "kind"],
50"mapping": {"name": "text", "kind": "title"},
51},
52"strategy_positions": {
53"search_fields": ["name", "kind"],
54"mapping": {"name": "text", "kind": "title"},
55},
56"strategy_visions": {
57"search_fields": ["name", "description"],
58"mapping": {"description": "text", "name": "title"},
59},
60"teams": {"search_fields": ["name"], "mapping": {"name": "text"}},
61"tasks": {"search_fields": ["name"], "mapping": {"name": "text"}},
62}
63
64def __init__(self, domain, api_key, allowed_entities, search_limit):
65self.api_url = f"https://{domain}.aha.io/api/v1/"
66self.headers = {"Authorization": f"Bearer {api_key}"}
67self.allowed_entities = allowed_entities
68self.search_limit = search_limit
69
70def get_allowed_entities(self):
71return self.allowed_entities
72
73def get_allowed_entity_types(self):
74return self.ALLOWED_ENTITY_TYPES
75
76def get_search_limit(self):
77return self.search_limit
78
79def get(self, url, params={}):
80response = requests.get(url, headers=self.headers, params=params)
81
82if response.status_code != 200:
83message = response.text or f"Error: HTTP {response.status_code}"
84raise UpstreamProviderError(message)
85
86return response.json()
87
88def get_entities_by_type(self, entity_type, params={}):
89url = f"{self.api_url}/{entity_type}"
90return self.get(url, params)
91
92
93def get_client():
94global client
95assert (domain := app.config.get("DOMAIN")), "AHA_DOMAIN must be set"
96assert (api_key := app.config.get("API_KEY")), "AHA_API_KEY must be set"
97search_limit = app.config.get("SEARCH_LIMIT", 20)
98allowed_entities = app.config.get(
99"ALLOWED_ENTITIES",
100[
101"users",
102"capacity_scenarios",
103"epics",
104"features",
105"goals",
106"ideas",
107"initiatives",
108"integrations",
109"products",
110"release_phases",
111"strategy_models",
112"strategy_positions",
113"strategy_visions",
114"teams",
115"tasks",
116],
117)
118
119if not client:
120client = AhaApiClient(domain, api_key, allowed_entities, search_limit)
121
122return client
123