Skip to content

Commit

Permalink
Merge pull request #88 from licenseware/feature/external-data-service
Browse files Browse the repository at this point in the history
external data service
  • Loading branch information
adrian-mih authored Mar 21, 2022
2 parents 085bba7 + 73f89f7 commit 5df809b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
2 changes: 1 addition & 1 deletion licenseware/app_builder/app_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ def register_app(self):
'style_attributes',
'attributes',
'title',
'component_type',
'type',
'filters'
]
}
Expand Down
1 change: 1 addition & 0 deletions licenseware/report_components/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .base_report_component import BaseReportComponent
from .external_data_service import ExternalDataService
56 changes: 56 additions & 0 deletions licenseware/report_components/external_data_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import requests
import traceback
import os

from licenseware.utils.logger import log


class ExternalDataService:
@staticmethod
def _get_all_components(headers):
registry_service_url = os.get("REGISTRY_SERVICE_URL")
try:
comp_data = requests.get(
url=f"{registry_service_url}/components"
)
return comp_data.json()
except Exception:
log.error(traceback.format_exc())


@staticmethod
def _get_component_url(components, app_id, component_id):
return [d['url'] for d in components if d['app_id'] == app_id and d['component_id'] == component_id][0]


@staticmethod
def get_data(_request, app_id, component_id, filter_payload=None):
try:
headers = {
"TenantId": _request.headers.get("TenantId"),
"Authorization": _request.headers.get("Authorization"),
}

registry_service_components = ExternalDataService._get_all_components(headers)
service_url = ExternalDataService._get_component_url(
components=registry_service_components,
app_id=app_id,
component_id=component_id
)

if filter_payload:
data = requests.post(
url=service_url, headers=headers, json=filter_payload
)
else:
data = requests.get(url=service_url, headers=headers)

if data.status_code == 200:
return data.json()
else:
log.warning(f"Could not retrieve data for {component_id} from {app_id}")
log.warning(f"GET {service_url} {data.status_code}")
return []
except Exception:
log.error(traceback.format_exc())
return False

0 comments on commit 5df809b

Please sign in to comment.