Login() - Eoddata.com web service blog series

Sat 25 March 2017 by Adrian Torrie

This post is part 2 of the "Using Python with Eoddata.com web service" series:

  1. Master post - Eoddata.com web service blog series
  2. Login() - Eoddata.com web service blog series
  3. ExchangeList() - Eoddata.com web service blog series
  4. CountryList() - Eoddata.com web service blog series
  5. SymbolList() - Eoddata.com web service blog series
  6. 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.

Version Control

In [1]:
%run ../../code/version_check.py
Python: 3.5.3 |Continuum Analytics, Inc.| (default, Feb 22 2017, 21:13:27) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]

matplotlib:	2.0.0
numpy:		1.12.0
pandas:		0.19.2
quandl:		3.0.1
requests:	2.12.4
sklearn:	0.18.1
scipy:		0.18.1
statsmodels:	0.8.0
tensorflow:	1.0.1

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]:
'018558Q2G41P'

Data inspection (Login)

In [7]:
dir(root)
Out[7]:
['__class__',
 '__copy__',
 '__deepcopy__',
 '__delattr__',
 '__delitem__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getitem__',
 '__getstate__',
 '__gt__',
 '__hash__',
 '__init__',
 '__le__',
 '__len__',
 '__lt__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__setitem__',
 '__setstate__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 'append',
 'clear',
 'extend',
 'find',
 'findall',
 'findtext',
 'get',
 'getchildren',
 'getiterator',
 'insert',
 'items',
 'iter',
 'iterfind',
 'itertext',
 'keys',
 'makeelement',
 'remove',
 'set']
In [8]:
for item in root.items():
    print (item)
('Suffix', 'true')
('Header', 'false')
('Token', '018558Q2G41P')
('DataFormat', 'MSAI')
('Message', 'Login Successful')
In [9]:
for key in root.keys():
    print (key)
Suffix
Header
Token
DataFormat
Message
In [10]:
print(root.get('Message'))
print(root.get('Token'))
print(root.get('DataFormat'))
print(root.get('Header'))
print(root.get('Suffix'))
Login Successful
018558Q2G41P
MSAI
false
true

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]:
'0185588RVBEM'

Client function

In [13]:
# pass in username and password
eoddata = Client(username, password)
token = eoddata.get_token()
eoddata.close_session()

print('token: {}'.format(token))
token: 018558BRKYV5
In [14]:
# initialise using secure credentials file
eoddata = Client()
token = eoddata.get_token()
eoddata.close_session()

print('token: {}'.format(token))
token: 0185589B7QMS
In [15]:
# no need to manually close the session when using a with block
with (Client()) as eoddata:
    print('token: {}'.format(eoddata.get_token()))
token: 018558MN617X

Comments

Fork me on GitHub