update server eventstream to be read with JS eventSource

This commit is contained in:
Johan Le Baut 2023-04-04 22:14:43 +02:00 committed by Félix Baylac Jacqué
parent 1764b5ce52
commit ae9d35eb5d
2 changed files with 23 additions and 20 deletions

View file

@ -3,7 +3,7 @@ from flask import Flask, request
from coordinates import check_coordinates_args, adapt_coordinates_to_max_area
from eligibility_api.elig_api_exceptions import ApiParamException
from ipe_fetcher.axione import AXIONE_MAX_AREA, Axione
from netwo.netwo import Netwo, NetwooEligibility, NETWO_DEPLOYED_STATUS
from netwo.netwo import Netwo, NetwooEligibility
class EligibilityApiRoutes:
@ -119,11 +119,6 @@ class EligibilityApiRoutes:
except ValueError:
raise ApiParamException("timeout_sec param must be an integer")
if imb_info.get("imb_status") != NETWO_DEPLOYED_STATUS:
return NetwooEligibility(
imb_info=imb_info,
)
print(f"start elig with {imb_info}")
return self.netwo.start_netwo_eligibility(
imb_info, search_ftto, timeout_sec
)

View file

@ -1,14 +1,15 @@
from flask import Response
import requests
import time
import json
import time
from urllib.parse import quote
import requests
from flask import Response
from typing_extensions import NotRequired, TypedDict
from eligibility_api.elig_api_exceptions import NetwoApiErrorException
from ipe_fetcher import FAIEligibilityStatus
NETWO_DEPLOYED_STATUS = "Deployed"
NETWO_NOT_FOUND_STATUS = "not_found"
TVA_INCREASE_COEFF = 1.2
@ -48,9 +49,7 @@ class Netwo:
)
status_code = response.status_code
if status_code != 200 or not response.json():
raise NetwoApiErrorException(
f"Could not GET netwo imb ref {ref_imb}", status_code
)
return {"imb_status": NETWO_NOT_FOUND_STATUS, "imb_id": ref_imb}
imb_payload = response.json()
return imb_payload
@ -106,6 +105,10 @@ class Netwo:
)
return sort_elig
@staticmethod
def _prepare_event_string(data: dict):
return "data: %s\n\n" % json.dumps(data)
def get_netwo_eligibility_results(self, elig_id: str, search_ftto: bool):
response = requests.get(
f"https://api.netwo.io/api/v1/eligibility/{elig_id}",
@ -132,15 +135,22 @@ class Netwo:
nbOperatorsOk=0,
nbOperatorsErrors=0,
nbOperatorsPending=0,
eligStatus={},
timeoutReached=False,
timeoutSec=timeout_sec,
eligOffers={},
imb_info=imb_info,
)
if imb_info.get("imb_status") != NETWO_DEPLOYED_STATUS:
netwo_elig["eligDone"] = True
yield "data: %s\n\n" % json.dumps(netwo_elig)
return
json_data = {
"latitude": str(imb_info.get("lat")),
"longitude": str(imb_info.get("lng")),
}
print(json_data)
response = requests.post(
"https://api.netwo.io/api/v1/eligibility/preselect",
headers=self.netwo_api_headers,
@ -160,7 +170,6 @@ class Netwo:
"imb_ref": imb_info.get("imb_id"),
"pm_ref": imb_info.get("pm_id"),
}
netwo_elig["imb_info"] = imb_info
response = requests.post(
"https://api.netwo.io/api/v1/eligibility",
headers=self.netwo_api_headers,
@ -193,6 +202,7 @@ class Netwo:
status_code,
)
status_res = response.json()
netwo_elig["eligStatus"] = status_res
netwo_elig["nbOperatorsOk"] = len(status_res.get("successes", []) or [])
netwo_elig["nbOperatorsErrors"] = len(
status_res.get("errors", []) or []
@ -210,12 +220,10 @@ class Netwo:
)
if timeout and time.time() > timeout:
netwo_elig["timeoutReached"] = True
yield json.dumps(netwo_elig, indent=2)
yield "\n"
yield self._prepare_event_string(netwo_elig)
break
else:
yield json.dumps(netwo_elig, indent=2)
yield "\n"
yield self._prepare_event_string(netwo_elig)
if netwo_elig["nbOperatorsPending"] > 0:
time.sleep(1)
@ -226,6 +234,6 @@ class Netwo:
id_elig, search_ftto
)
netwo_elig["eligDone"] = True
yield json.dumps(netwo_elig, indent=2)
yield self._prepare_event_string(netwo_elig)
return Response(event_stream(), mimetype="text/event-stream")