| Total Complexity | 5 |
| Total Lines | 33 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | import logging |
||
|
|
|||
| 2 | |||
| 3 | import defusedxml.ElementTree as Xml |
||
| 4 | from pocketutils.core.dot_dict import NestedDotDict |
||
| 5 | |||
| 6 | |||
| 7 | logger = logging.getLogger("mandos") |
||
| 8 | |||
| 9 | |||
| 10 | class HmdbApi: |
||
| 11 | def fetch(self, hmdb_id: str) -> NestedDotDict: |
||
| 12 | raise NotImplementedError() |
||
| 13 | |||
| 14 | |||
| 15 | class QueryingHmdbApi(HmdbApi): |
||
| 16 | def fetch(self, hmdb_id: str) -> NestedDotDict: |
||
| 17 | url = f"https://hmdb.ca/metabolites/{hmdb_id}.xml" |
||
| 18 | # e.g. https://hmdb.ca/metabolites/HMDB0001925.xml |
||
| 19 | |||
| 20 | def _to_json(self, xml): |
||
| 21 | response = {} |
||
| 22 | for child in list(xml): |
||
| 23 | if len(list(child)) > 0: |
||
| 24 | response[child.tag] = self._to_json(child) |
||
| 25 | else: |
||
| 26 | response[child.tag] = child.text or "" |
||
| 27 | # one-liner equivalent |
||
| 28 | # response[child.tag] = parseXmlToJson(child) if len(list(child)) > 0 else child.text or '' |
||
| 29 | return response |
||
| 30 | |||
| 31 | |||
| 32 | __all__ = ["HmdbApi", "QueryingHmdbApi"] |
||
| 33 |