forked from enviPath/enviPy
More on PES
This commit is contained in:
@ -1,3 +1,59 @@
|
||||
from django.shortcuts import render
|
||||
import requests
|
||||
from django.conf import settings as s
|
||||
from django.http import HttpResponse
|
||||
from pydantic import BaseModel
|
||||
|
||||
# Create your views here.
|
||||
from utilities.chem import FormatConverter
|
||||
|
||||
|
||||
class PES(BaseModel):
|
||||
pass
|
||||
|
||||
|
||||
def fetch_pes(request, pes_url) -> dict:
|
||||
proxies = {
|
||||
"http": "http://10.185.190.100:8080",
|
||||
"https": "http://10.185.190.100:8080",
|
||||
}
|
||||
|
||||
from epauth.views import get_access_token_from_request
|
||||
token = get_access_token_from_request(request)
|
||||
|
||||
if token:
|
||||
for k, v in s.PES_API_MAPPING.items():
|
||||
if pes_url.startsWith(k):
|
||||
pes_id = pes_url.split('/')[-1]
|
||||
headers = {"Authorization": f"Bearer {token['access_token']}"}
|
||||
params = {"pes_reg_entity_corporate_id": pes_id}
|
||||
|
||||
res = requests.get(v, headers=headers, params=params, proxies=proxies)
|
||||
|
||||
try:
|
||||
res.raise_for_status()
|
||||
pes_data = res.json()
|
||||
|
||||
if len(pes_data) == 0:
|
||||
raise ValueError(f"PES with id {pes_id} not found")
|
||||
|
||||
res_data = pes_data[0]
|
||||
res_data["pes_url"] = pes_url
|
||||
return res_data
|
||||
|
||||
except requests.exceptions.HTTPError as e:
|
||||
raise ValueError(f"Error fetching PES with id {pes_id}: {e}")
|
||||
else:
|
||||
raise ValueError(f"Unknown URL {pes_url}")
|
||||
else:
|
||||
raise ValueError("Could not fetch access token from request.")
|
||||
|
||||
|
||||
def visualize_pes(request):
|
||||
pes_link = request.GET.get('pesLink')
|
||||
|
||||
if pes_link:
|
||||
pes_data = fetch_pes(request, pes_link)
|
||||
print(pes_data)
|
||||
|
||||
return HttpResponse(
|
||||
FormatConverter.to_png("c1ccccc1"), content_type="image/png"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user