Skip to content

Commit 343d4ab

Browse files
author
Wasin Waeosri
committed
Create TRKD Authentication Notebook
1 parent a9d9bce commit 343d4ab

File tree

3 files changed

+207
-1
lines changed

3 files changed

+207
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ market_price_edpgw_authentication.py
88
interday_result.txt
99
interday_result2.txt
1010
interday_result3.txt
11-
interday_result4.txt
11+
interday_result4.txt
12+
notebook/test/
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Ignore everything in this directory
2+
*
3+
# Except this file
4+
!.gitignore

notebook/trkd_authentication.ipynb

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"# Install requests package in a current Jupyter kernal\n",
10+
"import sys\n",
11+
"!{sys.executable} -m pip install requests"
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": null,
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"# import required libraries\n",
21+
"\n",
22+
"import requests\n",
23+
"import json"
24+
]
25+
},
26+
{
27+
"cell_type": "code",
28+
"execution_count": null,
29+
"metadata": {},
30+
"outputs": [],
31+
"source": [
32+
"# functions"
33+
]
34+
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": null,
38+
"metadata": {},
39+
"outputs": [],
40+
"source": [
41+
"# HTTP operations function\n",
42+
"def doSendRequest(url, requestMsg, headers):\n",
43+
" result = None\n",
44+
" try:\n",
45+
" result = requests.post(url, data=json.dumps(requestMsg), headers=headers)\n",
46+
" except requests.exceptions.RequestException as e:\n",
47+
" print('Exception!!!')\n",
48+
" print(e)\n",
49+
" return result"
50+
]
51+
},
52+
{
53+
"cell_type": "code",
54+
"execution_count": null,
55+
"metadata": {},
56+
"outputs": [],
57+
"source": [
58+
"# main process"
59+
]
60+
},
61+
{
62+
"cell_type": "code",
63+
"execution_count": null,
64+
"metadata": {},
65+
"outputs": [],
66+
"source": [
67+
"# Input your TRKD credentials here\n",
68+
"\n",
69+
"username = \"\"\n",
70+
"password = \"\"\n",
71+
"appid = \"\""
72+
]
73+
},
74+
{
75+
"cell_type": "code",
76+
"execution_count": null,
77+
"metadata": {},
78+
"outputs": [],
79+
"source": [
80+
"# Authentication parameters\n",
81+
"\n",
82+
"token = None\n",
83+
"expire = None"
84+
]
85+
},
86+
{
87+
"cell_type": "code",
88+
"execution_count": null,
89+
"metadata": {},
90+
"outputs": [],
91+
"source": [
92+
"# TRKD Authentication Post Request message\n",
93+
"\n",
94+
"authenMsg = {'CreateServiceToken_Request_1': {'ApplicationID': appid, 'Username': username, 'Password': password}}"
95+
]
96+
},
97+
{
98+
"cell_type": "code",
99+
"execution_count": null,
100+
"metadata": {},
101+
"outputs": [],
102+
"source": [
103+
"# TRKD Authentication Service URL\n",
104+
"\n",
105+
"authenURL = (\n",
106+
" 'https://api.trkd.thomsonreuters.com/api/'\n",
107+
" 'TokenManagement/TokenManagement.svc/REST/'\n",
108+
" 'Anonymous/TokenManagement_1/CreateServiceToken_1'\n",
109+
")"
110+
]
111+
},
112+
{
113+
"cell_type": "code",
114+
"execution_count": null,
115+
"metadata": {},
116+
"outputs": [],
117+
"source": [
118+
"# TRKD Authentication Request headers\n",
119+
"\n",
120+
"authen_headers = {'content-type': 'application/json;charset=utf-8'}"
121+
]
122+
},
123+
{
124+
"cell_type": "code",
125+
"execution_count": null,
126+
"metadata": {},
127+
"outputs": [],
128+
"source": [
129+
"print('############### Sending Authentication request message to TRKD ###############')"
130+
]
131+
},
132+
{
133+
"cell_type": "code",
134+
"execution_count": null,
135+
"metadata": {},
136+
"outputs": [],
137+
"source": [
138+
"# Send Authentication request\n",
139+
"\n",
140+
"authenResult = doSendRequest(authenURL, authenMsg, authen_headers)"
141+
]
142+
},
143+
{
144+
"cell_type": "code",
145+
"execution_count": null,
146+
"metadata": {},
147+
"outputs": [],
148+
"source": [
149+
"if authenResult and authenResult.status_code == 200:\n",
150+
" print('Authenticaion success')\n",
151+
" print('JSON resonse: %s' % (json.dumps(authenResult.json(),sort_keys=True, indent=2, separators=(',', ':'))))\n",
152+
" token = authenResult.json()['CreateServiceToken_Response_1']['Token']\n",
153+
" expire = authenResult.json()['CreateServiceToken_Response_1']['Expiration']\n",
154+
"elif authenResult.status_code is not 200:\n",
155+
" print('Authenticaion fail with status code %s' % authenResult.status_code)\n",
156+
" if authenResult.status_code == 500:\n",
157+
" print('Error: %s' % (json.dumps(authenResult.json(), sort_keys=True, indent=2, separators=(',', ':'))))"
158+
]
159+
},
160+
{
161+
"cell_type": "code",
162+
"execution_count": null,
163+
"metadata": {},
164+
"outputs": [],
165+
"source": [
166+
"# Print Authentication information\n",
167+
"\n",
168+
"print('Token = : %s' % token)\n",
169+
"print('Expiration = %s' % expire)"
170+
]
171+
},
172+
{
173+
"cell_type": "code",
174+
"execution_count": null,
175+
"metadata": {},
176+
"outputs": [],
177+
"source": []
178+
}
179+
],
180+
"metadata": {
181+
"kernelspec": {
182+
"display_name": "Python 3",
183+
"language": "python",
184+
"name": "python3"
185+
},
186+
"language_info": {
187+
"codemirror_mode": {
188+
"name": "ipython",
189+
"version": 3
190+
},
191+
"file_extension": ".py",
192+
"mimetype": "text/x-python",
193+
"name": "python",
194+
"nbconvert_exporter": "python",
195+
"pygments_lexer": "ipython3",
196+
"version": "3.7.3"
197+
}
198+
},
199+
"nbformat": 4,
200+
"nbformat_minor": 2
201+
}

0 commit comments

Comments
 (0)