-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch_rsc_api.py
39 lines (33 loc) · 1.07 KB
/
fetch_rsc_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""The example code to fetch RSC plumber API
"""
import requests
import json
import pandas as pd
import os
def rsc_key() -> str:
"""the way of setting up the env var: `export RSC_KEY=xxxx`"""
key = os.environ.get("RSC_KEY")
if key is None:
raise RuntimeError("You should define environment variable RSC_KEY")
return key
def read_db(con: str, sql: str) -> pd.DataFrame:
key: str = rsc_key()
url: str = "http://icube.allianziamc.com.cn/sql-api/db_read"
headers: dict[str, str] = {
"accept": "application/json",
"Authorization": f"Key {key}",
}
data: dict[str, str] = {"con": con, "sql": sql}
ret: requests.Response = requests.post(
url=url, data=data, headers=headers, timeout=10
)
if ret.status_code == 200:
out = pd.DataFrame(json.loads(ret.text))
return out
else:
raise RuntimeError(f"the status code is {ret.status_code}")
if __name__ == "__main__":
# run a test
sql = "select * from pq.idb_param_hs_fund where rownum <= 10"
df = read_db("pqread", sql)
print(df)