Axione-IPE-Viewer/webapp/eligibility_api/elig_api_routes.py

125 lines
5 KiB
Python
Raw Normal View History

2022-11-16 16:10:30 +01:00
from flask import Flask, request
2023-02-28 22:31:35 +01:00
from coordinates import check_coordinates_args, adapt_coordinates_to_max_area
2022-11-16 16:10:30 +01:00
from eligibility_api.elig_api_exceptions import ApiParamException
2023-01-11 15:35:12 +01:00
from ipe_fetcher.axione import AXIONE_MAX_AREA, Axione
from netwo.netwo import Netwo, NetwooEligibility
2022-11-16 16:10:30 +01:00
2023-02-28 22:31:35 +01:00
2022-11-16 16:10:30 +01:00
class EligibilityApiRoutes:
2023-03-03 14:16:34 +01:00
def __init__(self, flask_app: Flask, axione_ipe: Axione, netwo: Netwo):
2022-11-16 16:10:30 +01:00
self.flask_app = flask_app
self.axione_ipe = axione_ipe
2023-03-03 14:16:34 +01:00
self.netwo = netwo
2022-11-16 16:10:30 +01:00
def add_routes(self):
@self.flask_app.route("/eligibilite/axione", methods=["GET"])
def get_axione_eligibility_per_immeuble():
refimmeuble = request.args.get("refimmeuble")
if not refimmeuble:
2023-02-28 22:31:35 +01:00
raise ApiParamException(
"You need to specify path parameter 'refimmeuble'"
)
2022-11-16 16:10:30 +01:00
return self.axione_ipe.get_eligibilite_per_id_immeuble(refimmeuble)
2023-01-11 15:35:12 +01:00
@self.flask_app.route("/eligibilite/axione/coord", methods=["GET"])
def get_axione_eligibility_per_coordinates():
args = request.args
try:
processed_args = check_coordinates_args(args)
2023-02-28 22:31:35 +01:00
coordinates = adapt_coordinates_to_max_area(
processed_args, AXIONE_MAX_AREA
)
return self.axione_ipe.get_area_buildings(coordinates, {})
2023-01-11 15:35:12 +01:00
except ValueError:
2023-02-28 22:31:35 +01:00
raise ApiParamException(
"You need to specify path parameters 'swx' 'swy' 'nex' 'ney'"
)
2023-03-03 14:16:34 +01:00
@self.flask_app.route("/eligibilite/netwo", methods=["GET"])
def get_netwo_eligibility():
2023-03-06 15:10:55 +01:00
"""
Intérroge l'éligibilité Netwo
---
tags:
- API
description: |
Déclenche une recherche d'éligibilité chez Netwo:
- soit par une ref d'immeuble
- soit par les coordonnées d'un immeuble
La recherche est longue, l'API renvoie un event stream qui renvoie un statut toutes les secondes
Il est possible de retrouver les résultats d'une éligibilité via son ID.
Par défaut on renvoie uniquement les offres ftth, il est possible via ftto=true d'ajouter la ftto
parameters:
- in: query
name: ftto
required: false
schema:
type: boolean
nullable: true
allowEmptyValue: true
description: |
Si 'ftto' set alors on rajoutera les résultats FTTO à la recherche
- in: query
name: timeout_sec
required: false
schema:
type: int
nullable: false
allowEmptyValue: false
description: |
Timeout au bout du quel on retourne les offres trouvées même si la recherche n'est pas terminée
responses:
200:
description: Retourne le statut d'éligibilité et d'éventuelles offres
schema:
$ref: '#/definitions/Contribution'
examples:
application/json:
400:
description: Bad inputs
schema:
$ref: '#/definitions/ApiError'
examples:
application/json:
code: 400
name: Bad input parameter
description: Bad json format
500:
description: Erreur lors d'un appel à l'API de Netwo
schema:
$ref: '#/definitions/ApiError'
examples:
application/json:
netwo_status_code: 404
name: Error contacting Netwo API
description: ID not found
"""
2023-03-03 14:16:34 +01:00
args = request.args
ref_imb = args.get("ref_imb")
2023-03-06 15:10:55 +01:00
elig_id = args.get("id")
2023-03-03 14:16:34 +01:00
timeout_sec = None
2023-03-06 15:10:55 +01:00
search_ftto = args.get("ftto") is not None
imb_info = self.netwo.get_netwo_imb_coordinates(ref_imb)
if elig_id:
elig_offers = self.netwo.get_netwo_eligibility_results(
elig_id, search_ftto
)
return NetwooEligibility(
imb_info=imb_info,
eligOffers=elig_offers,
eligDone=True,
elig_id=elig_id,
)
2023-03-03 14:16:34 +01:00
if args.get("timeout_sec"):
try:
timeout_sec = int(args.get("timeout_sec"))
except ValueError:
raise ApiParamException("timeout_sec param must be an integer")
2023-03-06 15:10:55 +01:00
2023-03-03 14:16:34 +01:00
return self.netwo.start_netwo_eligibility(
2023-03-06 15:10:55 +01:00
imb_info, search_ftto, timeout_sec
2023-03-03 14:16:34 +01:00
)