按层次结构重定向服务路由

2024-09-30 18:22:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一组使用Flask库在Python中运行的微服务。 我有一个country服务,它通过参数接收子字符串并返回包含该字符串的国家。 我有一个state服务,它接收state的country参数和子字符串,并从该国家返回与该子字符串匹配的国家。 我有一个城市服务,它通过参数接收一个国家、一个州和一个子串,然后从这个国家和那个州返回,这些城市与这个链重合。 服务的调用方式如下:

for country

http://127.0.0.1:5100/GeographicAddressManagement/Countries?country=AR

Passing by parameter the Country and the substring of the state

http://127.0.0.1:5102/GeographicAddressManagement/State?country=ARGENTINA&stateOrProvince=CO

Passing by parameter the Country, State and the substring of the City

http://127.0.0.1:5102/GeographicAddressManagement/Cities?country=ARGENTINA&stateOrProvince=CORDOBA&city=C

但是现在,他们让我把服务调整到一个标准,他们希望它是一个单一的服务,如果是这样的话,它允许分层搜索。 例如,如果我只按国家搜索,参数应该是

http://127.0.0.1:5100/GeographicAddressManagement/AREA?country=AR

如果我要在一个特定的国家寻找一个州,它应该是 http://127.0.0.1:5100/GeographicAddressManagement/ARGENTINA/stateOrProvince=CO

如果来自城市: http://127.0.0.1:5100/GeographicAddressManagement/ARGENTINA/CORDOBA/city=C

他们要求我遵循的标准是:

---/地理位置/国家/地区 找到所有有特定字符串的国家

---/地理服装管理/阿根廷/各州 找到所有属于阿根廷的州

---/地理服装管理/阿根廷/科尔多瓦/城市 找到所有城市的某个字符串属于阿根廷,科尔多瓦州

每个单独的服务都以下面的方式配置它,通过示例将它放在服务的名称空间之后

ns = api.namespace('GeographicAddressManagement', description='Prediction of countries')
@ns.route('/Countries', methods=['POST'])
@ns.param('country', 'Substring de Pais (ej:ARGE)', required=True)

如何使同一个端点分层,并根据组合调用单个服务中的每个元素


Tags: ofthe字符串http参数方式国家country