|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import Any, cast |
| 4 | + |
| 5 | +from linkedapi.errors import LinkedApiError |
| 6 | +from linkedapi.mappers.base import ( |
| 7 | + BaseMapper, |
| 8 | + MappedResponse, |
| 9 | + as_action_dict, |
| 10 | + parse_action_error, |
| 11 | + parse_result, |
| 12 | +) |
| 13 | +from linkedapi.types import ( |
| 14 | + EndorsePersonSkillsParams, |
| 15 | + PersonSkillEndorsement, |
| 16 | + WorkflowCompletion, |
| 17 | + WorkflowDefinition, |
| 18 | + serialize_model, |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +class EndorsePersonSkillsMapper( |
| 23 | + BaseMapper[EndorsePersonSkillsParams, list[PersonSkillEndorsement]] |
| 24 | +): |
| 25 | + def map_request(self, params: EndorsePersonSkillsParams | None = None) -> WorkflowDefinition: |
| 26 | + serialized = serialize_model(params) |
| 27 | + skill_name_patterns = serialized.pop("skillNamePatterns", None) |
| 28 | + do_for_skills: dict[str, Any] = { |
| 29 | + "actionType": "st.doForSkills", |
| 30 | + "then": {"actionType": "st.endorsePersonSkill"}, |
| 31 | + } |
| 32 | + if skill_name_patterns is not None: |
| 33 | + do_for_skills["skillNamePatterns"] = skill_name_patterns |
| 34 | + |
| 35 | + return { |
| 36 | + "actionType": "st.openPersonPage", |
| 37 | + "basicInfo": False, |
| 38 | + **serialized, |
| 39 | + "then": {"actionType": "st.retrievePersonSkills", "then": do_for_skills}, |
| 40 | + } |
| 41 | + |
| 42 | + def map_response( |
| 43 | + self, completion: WorkflowCompletion |
| 44 | + ) -> MappedResponse[list[PersonSkillEndorsement]]: |
| 45 | + action = as_action_dict(completion) |
| 46 | + error = parse_action_error(action.get("error")) |
| 47 | + if error is not None: |
| 48 | + return MappedResponse(data=None, errors=[error]) |
| 49 | + |
| 50 | + data = action.get("data") |
| 51 | + if data is None: |
| 52 | + raise LinkedApiError.unknown_error() |
| 53 | + then_actions = as_action_dict(data).get("then") |
| 54 | + child_actions = then_actions if isinstance(then_actions, list) else [then_actions] |
| 55 | + for child in child_actions: |
| 56 | + skills_action = as_action_dict(child) |
| 57 | + if skills_action.get("actionType") == "st.retrievePersonSkills": |
| 58 | + return self._map_skills(skills_action) |
| 59 | + raise LinkedApiError.unknown_error() |
| 60 | + |
| 61 | + def _map_skills(self, action: dict[str, Any]) -> MappedResponse[list[PersonSkillEndorsement]]: |
| 62 | + error = parse_action_error(action.get("error")) |
| 63 | + if error is not None: |
| 64 | + return MappedResponse(data=None, errors=[error]) |
| 65 | + |
| 66 | + selected_skills: list[dict[str, Any]] = [] |
| 67 | + for skill in action["data"]: |
| 68 | + for child in skill.get("then", []): |
| 69 | + endorsement = as_action_dict(child) |
| 70 | + if endorsement.get("actionType") == "st.endorsePersonSkill": |
| 71 | + selected_skills.append( |
| 72 | + { |
| 73 | + "skillName": skill["name"], |
| 74 | + "success": endorsement["success"], |
| 75 | + "error": endorsement.get("error"), |
| 76 | + } |
| 77 | + ) |
| 78 | + break |
| 79 | + return MappedResponse( |
| 80 | + data=cast( |
| 81 | + list[PersonSkillEndorsement], parse_result(selected_skills, PersonSkillEndorsement) |
| 82 | + ), |
| 83 | + errors=[], |
| 84 | + ) |
0 commit comments