solis: handle list of links (#33559)

This commit is contained in:
Thomas NOËL 2019-05-29 17:15:57 +02:00
parent 1be5d77be9
commit 6792df1742
2 changed files with 43 additions and 13 deletions

View File

@ -456,20 +456,25 @@ class Solis(BaseResource):
})
return response.get('token')
def rsa_get_link_content(self, link):
'''returns content of a '_links' entry in a grsa referential'''
def rsa_fill_with_link_content(self, link):
'''fill one or several link (_links entry in grsa referential object)'''
if isinstance(link, list):
for sublink in link:
self.rsa_fill_with_link_content(sublink)
return
if (not isinstance(link, dict) or not link.get('href')
or not link['href'].startswith(self.service_url)):
return None
return
endpoint = link['href'][len(self.service_url):]
try:
return self.request(endpoint)
value = self.request(endpoint)
except APIError as e: # do not raise on linked informations
return {
value = {
'err': 1,
'err_class': e.__class__.__name__,
'err_desc': force_text(e)
}
link['content'] = value
def rsa_get_information(self, information, user_id=None, code=None, token=None,
index='search', links=None):
@ -494,20 +499,17 @@ class Solis(BaseResource):
information = self.request(endpoint)
if isinstance(information, dict) and '_links' in information:
# return links in non-underscored key, usable in Django template
# return linked objects in non-underscored key, usable in Django template
information['rsa_links'] = copy.deepcopy(information['_links'])
if links is not None:
if not links.strip(): # get all links
if not links.strip(): # links is empty: get all
links = information['rsa_links'].keys()
else:
links = [link.strip() for link in links.split(',') if link.strip()]
links = [link for link in links if link in information['rsa_links']
and information['rsa_links'][link].get('href')]
links = [x.strip() for x in links.split(',') if x.strip()]
links = [x for x in links if x in information['rsa_links']]
for link in links:
content = self.rsa_get_link_content(information['rsa_links'][link])
if content is not None:
information['rsa_links'][link]['content'] = content
self.rsa_fill_with_link_content(information['rsa_links'][link])
return information

View File

@ -54,6 +54,17 @@ RSAALLOCATAIRES = '''{
}
}
}'''
RSA_3LINKS = '''{
"_links": {
"etatCivil": {
"href": "https://solis.example.net/solisapi/referentiels/civi/individu/4242/"
},
"refOrientStructAcc": [
{"href": "https://solis.example.net/solisapi/referentiels/civi/individu/4242/"},
{"href": "https://solis.example.net/solisapi/referentiels/civi/individu/4242/"}
]
}
}'''
@ -847,6 +858,23 @@ def test_solis_rsa_link_infos_unlink(app, solis):
assert resp.json['data']['rsa_links']['etatCivil']['content']['index'] == 4273
assert resp.json['data']['rsa_links']['conjoint']['content']['index'] == 4273
# complex links
requests_post.reset_mock()
requests_get.reset_mock()
requests_get.side_effect = [
utils.FakedResponse(status_code=200, content=RSA_3LINKS), # base info
utils.FakedResponse(status_code=200, content=CIVI_INDIVIDU), # link 1
utils.FakedResponse(status_code=200, content=CIVI_INDIVIDU), # link 2.1
utils.FakedResponse(status_code=200, content=CIVI_INDIVIDU)] # link 2.2
endpoint = endpoint_base + '?name_id=%s&user_id=4242&information=evaluations&links' % NAMEID
resp = app.get(endpoint, status=200)
assert requests_post.call_count == 1 # get a token
assert requests_get.call_count == 4 # get informations + 2+1 links
assert resp.json['err'] == 0
assert resp.json['data']['rsa_links']['etatCivil']['content']['index'] == 4273
assert resp.json['data']['rsa_links']['refOrientStructAcc'][0]['content']['index'] == 4273
assert resp.json['data']['rsa_links']['refOrientStructAcc'][1]['content']['index'] == 4273
# get only conjoint
requests_post.reset_mock()
requests_get.reset_mock()