Skip to content
This repository has been archived by the owner on Aug 31, 2021. It is now read-only.

Commit

Permalink
Clean commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jperezlatimes committed Apr 13, 2018
0 parents commit 2a96ea4
Show file tree
Hide file tree
Showing 9 changed files with 294 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[flake8]
ignore = E501, E722
101 changes: 101 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# dotenv
.env

# virtualenv
.venv
venv/
ENV/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Los Angeles Times Data Desk

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# py-bns
A wrapper for the Bloomberg News Service API
2 changes: 2 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from __future__ import (absolute_import, division, print_function, unicode_literals)
from builtins import *
76 changes: 76 additions & 0 deletions py-bns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from __future__ import (absolute_import, division, print_function, unicode_literals)
from future import standard_library # noqa
from builtins import * # noqa

import requests
try:
from urllib.parse import quote
except ImportError:
from urllib import quote


class LoginError(Exception):
pass


class pyBNS(object):
# Declare properties
# Username and password are pulled from arguments given in instance.
def __init__(self, *args, **kwargs):
# go through keyword arguments
for key, value in list(kwargs.items()):
# if the keys are username and password, set self attributes
if key in ['username', 'password']:
setattr(self, key, value)
# base url
self.api_url = 'https://api.bloomberg.com/{0}'
self.headers = {
'content-type': 'application/x-www-form-urlencoded',
'authorization': 'Basic YkFSeWpFbGxWTGZHSmlYd2FlOFJpMHUzZVFRYTpKOVhDYmhiMG9zMFBPOGNEYUIwSlY5Z1JDMW9h'
}

def post(self, url, payload):
url = self.api_url.format(url)
# try posting the complete url, credentials and headers
try:
response = requests.post(url=url, data=payload, headers=self.headers)
# raise an exception if it's not 200
response.raise_for_status()
return response
except requests.exceptions.HTTPError as e:
status = response.status_code
e.msg = 'Received a {0} response, instead of 200'.format(status)
print(e.msg)

def connect(self):
# If there's not a username and password, raise an exception
try:
# quote username and password to get the special url encoding
username = quote(self.username)
password = quote(self.password)
except AttributeError:
msg = 'pybBNS instance requires a username and password'
raise LoginError(msg)

login_data = 'username={0}&password={1}&remember=false&grant_type=password'.format(username, password)
# pass through url ending and payload
response = self.post('syndication/token', login_data)
# save access token for later use
self.access_token = str(response.json()['access_token'])

def disconnect(self):
try:
payload = 'token={0}'.format(self.access_token)
# post token to api
self.post('syndication/revoke', payload)
except AttributeError as e:
print(e)


# Create an instance, passing arguments as keyword arguments
bb = pyBNS()

# post credentials and header to /syndication/token
bb.connect()
# request api token be revoked
bb.disconnect()
71 changes: 71 additions & 0 deletions py-bns.py.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from future import standard_library
from builtins import *
import requests
from urllib import quote


class LoginError(Exception):
pass


class pyBNS():
# Declare properties
# Username and password are pulled from arguments given in instance.
def __init__(self, *args, **kwargs):
# go through keyword arguments
for key, value in kwargs.items():
# if the keys are username and password, set self attributes
if key in ['username', 'password']:
setattr(self, key, value)
# base url
self.api_url = 'https://api.bloomberg.com/{0}'
self.headers = {
'content-type': 'application/x-www-form-urlencoded',
'authorization': 'Basic YkFSeWpFbGxWTGZHSmlYd2FlOFJpMHUzZVFRYTpKOVhDYmhiMG9zMFBPOGNEYUIwSlY5Z1JDMW9h'
}

def post(self, url, payload):
url = self.api_url.format(url)
# try posting the complete url, credentials and headers
try:
response = requests.post(url=url, data=payload, headers=self.headers)
# raise an exception if it's not 200
response.raise_for_status()
return response
except requests.exceptions.HTTPError as e:
status = response.status_code
e.msg = 'Received a {0} response, instead of 200'.format(status)
print(e.msg)

def connect(self):
# If there's not a username and password, raise an exception
try:
# quote username and password to get the special url encoding
username = quote(self.username)
password = quote(self.password)
except AttributeError:
msg = 'pybBNS instance requires a username and password'
raise LoginError(msg)

login_data = 'username={0}&password={1}&remember=false&grant_type=password'.format(username, password)
# pass through url ending and payload
response = self.post('syndication/token', login_data)
# save access token for later use
self.access_token = str(response.json()['access_token'])

def disconnect(self):
try:
payload = 'token={0}'.format(self.access_token)
# post token to api
self.post('syndication/revoke', payload)
except AttributeError as e:
print(e)


# Create an instance, passing arguments as keyword arguments
bb = pyBNS()

# post credentials and header to /syndication/token
bb.connect()
# request api token be revoked
bb.disconnect()
19 changes: 19 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
backports.functools-lru-cache==1.5
caniusepython3==6.0.0
certifi==2018.1.18
chardet==3.0.4
configparser==3.5.0
distlib==0.2.6
enum34==1.1.6
flake8==3.5.0
future==0.16.0
futures==3.2.0
idna==2.6
mccabe==0.6.1
packaging==17.1
pycodestyle==2.3.1
pyflakes==1.6.0
pyparsing==2.2.0
requests==2.18.4
six==1.11.0
urllib3==1.22

0 comments on commit 2a96ea4

Please sign in to comment.