Netwo : add product details (upload)

This commit is contained in:
Johan Le Baut 2023-04-05 11:58:01 +02:00
parent e51317d725
commit b0531821dc
5 changed files with 154 additions and 28 deletions

3
webapp/.gitignore vendored
View file

@ -1,3 +1,4 @@
__pycache__
/config.ini
.vscode
.vscode
node_modules

View file

@ -53,8 +53,28 @@ class Netwo:
imb_payload = response.json()
return imb_payload
def _filter_netwo_raw_elig_results(self, raw_elig: dict, search_ftto: bool) -> list:
filtered_elig = []
def _get_netwo_product_entities_details(self, elig_id: str, product_id: str):
response = requests.get(
f"https://api.netwo.io/api/v1/eligibility/{elig_id}/details/{product_id}",
headers=self.netwo_api_headers,
)
status_code = response.status_code
if status_code != 200 or not response.json():
print(
f"Error: could not get details for elig_id {elig_id} and product id {product_id}"
)
return {}
details = response.json()
return details.get("entities") or []
def _filter_netwo_raw_elig_results(
self,
elig_id: str,
raw_elig: dict,
search_ftto: bool,
netwo_offers: list,
processed_products: list,
) -> list:
inf_search = ["ftth"]
if search_ftto:
inf_search.append("ftto")
@ -63,6 +83,13 @@ class Netwo:
if inf_type not in inf_search:
continue
product_id = r.get("product_id")
if product_id in processed_products:
continue
processed_products.append(product_id)
prod_entities = self._get_netwo_product_entities_details(
elig_id, product_id
)
operator = r.get("infrastructure_operator")
product = r.get("product_name")
for offer in r.get("entities"):
@ -80,26 +107,43 @@ class Netwo:
+ self.aquilenet_fixed_recurring_price,
2,
)
filtered_elig.append(
{
"entity_id": entity_id,
"product_id": product_id,
"product": f"{product} - {offer_name}",
"infrastructure_operator": operator,
"infrastructure_type": inf_type,
"debit": debit,
"access_fee": access_fee,
"access_fee_ttc": access_fee_ttc,
"recurring_price": recurring_price,
"total_recurring_price_ttc": total_recurring_price_ttc,
"commitment_duration": commitment_duration,
"per_month_price_one_year_ttc": round(
access_fee_ttc / 12 + total_recurring_price_ttc, 2
),
}
)
offer_info = {
"entity_id": entity_id,
"product_id": product_id,
"product": f"{product} - {offer_name}",
"infrastructure_operator": operator,
"infrastructure_type": inf_type,
"debit": debit,
"access_fee": access_fee,
"access_fee_ttc": access_fee_ttc,
"recurring_price": recurring_price,
"total_recurring_price_ttc": total_recurring_price_ttc,
"commitment_duration": commitment_duration,
"per_month_price_one_year_ttc": round(
access_fee_ttc / 12 + total_recurring_price_ttc, 2
),
}
search_entity = [
i for i in prod_entities if i.get("entity_id") == entity_id
]
if search_entity:
entity_details = search_entity[0]
offer_info[
"broadband_network_gateway_protocol"
] = entity_details.get("broadband_network_gateway_protocol")
offer_info["debit_up_max"] = entity_details.get("debit_up_max")
offer_info["interface_type"] = entity_details.get("interface_type")
offer_info["tariff_zone"] = entity_details.get("tariff_zone")
offer_info["collection_region"] = entity_details.get(
"collection_region"
)
offer_info["delivery_protocol"] = entity_details.get(
"delivery_protocol"
)
offer_info["mtu"] = entity_details.get("mtu")
netwo_offers.append(offer_info)
sort_elig = sorted(
filtered_elig,
netwo_offers,
key=lambda x: x["per_month_price_one_year_ttc"],
reverse=False,
)
@ -109,7 +153,9 @@ class Netwo:
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, netwo_offers, processed_products
):
response = requests.get(
f"https://api.netwo.io/api/v1/eligibility/{elig_id}",
headers=self.netwo_api_headers,
@ -120,7 +166,9 @@ class Netwo:
f"Netwo API: Could not get eligibility results for ID {elig_id}",
status_code,
)
return self._filter_netwo_raw_elig_results(response.json(), search_ftto)
return self._filter_netwo_raw_elig_results(
elig_id, response.json(), search_ftto, netwo_offers, processed_products
)
def start_netwo_eligibility(
self,
@ -189,6 +237,8 @@ class Netwo:
if timeout_sec:
timeout = time.time() + timeout_sec
netwo_offers = []
processed_products = []
while is_done is False:
response = requests.get(
f"https://api.netwo.io/api/v1/eligibility/{id_elig}/status",
@ -215,9 +265,13 @@ class Netwo:
+ netwo_elig["nbOperatorsErrors"]
+ netwo_elig["nbOperatorsPending"]
)
netwo_elig["eligOffers"] = self.get_netwo_eligibility_results(
id_elig, search_ftto
netwo_offers = self.get_netwo_eligibility_results(
id_elig,
search_ftto,
netwo_offers,
processed_products,
)
netwo_elig["eligOffers"] = netwo_offers
if timeout and time.time() > timeout:
netwo_elig["timeoutReached"] = True
yield self._prepare_event_string(netwo_elig)
@ -226,12 +280,12 @@ class Netwo:
yield self._prepare_event_string(netwo_elig)
if netwo_elig["nbOperatorsPending"] > 0:
time.sleep(1)
time.sleep(0.6)
else:
is_done = True
netwo_elig["eligOffers"] = self.get_netwo_eligibility_results(
id_elig, search_ftto
id_elig, search_ftto, netwo_offers, processed_products
)
netwo_elig["eligDone"] = True
yield self._prepare_event_string(netwo_elig)

View file

@ -0,0 +1,13 @@
{
"name": "test_event_stream",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"eventsource": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/eventsource/-/eventsource-2.0.2.tgz",
"integrity": "sha512-IzUmBGPR3+oUG9dUeXynyNmf91/3zUSJg1lCktzKw47OXuhco54U3r9B7O4XX+Rb1Itm9OZ2b0RkTs10bICOxA=="
}
}
}

View file

@ -0,0 +1,16 @@
{
"name": "test_event_stream",
"version": "1.0.0",
"description": "",
"main": "test_api_netwo.js",
"dependencies": {
"eventsource": "^2.0.2"
},
"devDependencies": {},
"scripts": {
"start": "node test_api_netwo.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}

View file

@ -0,0 +1,42 @@
//import { EventSource } from 'eventsource'
var EventSource = require('eventsource')
function handleNetwo(data) {
console.log("handle data")
console.log(data)
}
function startNetwo(refimmeuble) {
const evtSource = new EventSource(encodeURI(`http://localhost:5000/eligibilite/netwo?ref_imb=${refimmeuble}`));
evtSource.onmessage = function (event) {
try {
console.log("got data")
data = JSON.parse(event.data)
handleNetwo(data)
if (data.eligDone) {
console.log("elig done, stop stream")
evtSource.close();
}
} catch (error) {
console.log("error parsing data, stop stream: ", error)
evtSource.close();
}
}
evtSource.onerror = function (event) {
console.log("in onerror stop stream: ",event)
evtSource.close();
}
}
if (process.argv.length < 3) {
console.log("Need to specify imb param (e.g IMB/33063/S/A8DA )")
process.exit(1)
}
ref_imb = process.argv[2]
startNetwo(ref_imb)