Add netwo API #8

Merged
johan.le.baut merged 13 commits from netwo into master 2024-05-07 20:01:27 +02:00
2 changed files with 23 additions and 20 deletions
Showing only changes of commit ae9d35eb5d - Show all commits

View file

@ -3,7 +3,7 @@ from flask import Flask, request
from coordinates import check_coordinates_args, adapt_coordinates_to_max_area from coordinates import check_coordinates_args, adapt_coordinates_to_max_area
from eligibility_api.elig_api_exceptions import ApiParamException from eligibility_api.elig_api_exceptions import ApiParamException
from ipe_fetcher.axione import AXIONE_MAX_AREA, Axione 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: class EligibilityApiRoutes:
@ -119,11 +119,6 @@ class EligibilityApiRoutes:
except ValueError: except ValueError:
raise ApiParamException("timeout_sec param must be an integer") 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( return self.netwo.start_netwo_eligibility(
imb_info, search_ftto, timeout_sec imb_info, search_ftto, timeout_sec
) )

View file

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