diff options
Diffstat (limited to 'python')
| -rw-r--r-- | python/api_quotes/__init__.py | 0 | ||||
| -rw-r--r-- | python/api_quotes/api.py | 42 |
2 files changed, 42 insertions, 0 deletions
diff --git a/python/api_quotes/__init__.py b/python/api_quotes/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/python/api_quotes/__init__.py diff --git a/python/api_quotes/api.py b/python/api_quotes/api.py new file mode 100644 index 00000000..41170d5d --- /dev/null +++ b/python/api_quotes/api.py @@ -0,0 +1,42 @@ +from serenitas.utils.misc import get_credential_path +import json +import posixpath +from urllib.parse import urljoin +from typing import ClassVar +import requests + + +class MarkitAPI: + api_key: ClassVar = None + base_url: ClassVar = None + + @classmethod + def set_api(cls): + creds = cls.get_creds() + r = requests.post( + urljoin(creds["url"], "apikey"), + data={"username": creds["username"], "password": creds["password"]}, + ) + cls.api_key = r.text + cls.base_url = creds["url"] + + @classmethod + def get_data(cls, asset_class, service="latest"): + _asset_class_to_markit = {"sn_cds": "CD"} + params = { + "format": "json", + "assetClass": _asset_class_to_markit[asset_class], + "apikey": cls.api_key, + "limit": 1000, + } + + path = posixpath.join("parsing", "Quote", service) + url = urljoin(cls.base_url, path) + r = requests.get(url, params) + return json.loads(r.text) + + @classmethod + def get_creds(cls): + cred_path = get_credential_path("markit_api") + creds = json.load(cred_path.open()) + return creds |
