Login() - Eoddata.com web service blog series
Sat 25 March 2017 by Adrian TorrieThis post is part 2 of the "Using Python with Eoddata.com web service" series:
- Master post - Eoddata.com web service blog series
- Login() - Eoddata.com web service blog series
- ExchangeList() - Eoddata.com web service blog series
- CountryList() - Eoddata.com web service blog series
- SymbolList() - Eoddata.com web service blog series
- FundamentalList() - Eoddata.com web service blog series
Summary¶
Part of the blog series related to making web service calls to Eoddata.com. Overview of the web service can be found here.
- View the master post of this series to build a secure credentials file. It is used in all posts related to this series.
- Download the class definition file for an easy to use client, which is demonstrated below
- This post covers the
Login
call: http://ws.eoddata.com/data.asmx?op=Login
Version Control¶
In [1]:
%run ../../code/version_check.py
Change Log¶
Date Created: 2017-03-25
Date of Change Change Notes
-------------- ----------------------------------------------------------------
2017-03-25 Initial draft
Setup¶
In [2]:
%run ../../code/eoddata.py
from getpass import getpass
import requests as r
import xml.etree.cElementTree as etree
ws = 'http://ws.eoddata.com/data.asmx'
ns='http://ws.eoddata.com/Data'
session = r.Session()
In [3]:
username = getpass()
In [4]:
password = getpass()
Login()¶
Web service call¶
In [5]:
call = 'Login'
url = '/'.join((ws, call))
payload = {'Username': username, 'Password': password}
response = session.get(url, params=payload, stream=True)
if response.status_code == 200:
root = etree.parse(response.raw).getroot()
Get data¶
In [6]:
token = root.get('Token')
token
Out[6]:
Data inspection (Login)¶
In [7]:
dir(root)
Out[7]:
In [8]:
for item in root.items():
print (item)
In [9]:
for key in root.keys():
print (key)
In [10]:
print(root.get('Message'))
print(root.get('Token'))
print(root.get('DataFormat'))
print(root.get('Header'))
print(root.get('Suffix'))
Helper function¶
In [11]:
def Login(session, username, password):
call = 'Login'
url = '/'.join((ws, call))
payload = {'Username': username, 'Password': password}
response = session.get(url, params=payload, stream=True)
if response.status_code == 200:
root = etree.parse(response.raw).getroot()
return root.get('Token')
Usage¶
In [12]:
token = Login(session, username, password)
token
Out[12]:
Client function¶
In [13]:
# pass in username and password
eoddata = Client(username, password)
token = eoddata.get_token()
eoddata.close_session()
print('token: {}'.format(token))
In [14]:
# initialise using secure credentials file
eoddata = Client()
token = eoddata.get_token()
eoddata.close_session()
print('token: {}'.format(token))
In [15]:
# no need to manually close the session when using a with block
with (Client()) as eoddata:
print('token: {}'.format(eoddata.get_token()))