Created
August 20, 2013 19:04
-
-
Save ojas/6285768 to your computer and use it in GitHub Desktop.
Get BIR report from D&B Direct 1.x API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
""" | |
Usage: | |
python bir.py <duns_number> | |
""" | |
import sys | |
import requests | |
import traceback | |
import base64 | |
DIRECT_ENDPOINT = 'http://dnbdirect-api.dnb.com/DnBAPI-13/rest/' | |
DIRECT_CREDENTIALS = { | |
'API-KEY' : 'YOUR_API_KEY', | |
'username' : 'YOUR_USERNAME', | |
'password' : 'YOUR_PASSWORD', | |
} | |
def main(duns): | |
response = None | |
all_headers = dict(DIRECT_CREDENTIALS.items() + { 'Content-Type': 'application/json', 'Accept' : 'application/json' }.items()) | |
uri = 'company/%s/report/BIR' % duns | |
print DIRECT_ENDPOINT + uri | |
try: | |
r = requests.get(DIRECT_ENDPOINT + uri, headers=all_headers) | |
obj = r.json() | |
if 'Error:' in obj: | |
response = obj['Error:'] | |
elif 'error' in obj: | |
response = obj['error'][0]['message'] | |
else: | |
contents = [base64.b64decode(obj['ContentObject']) for obj in obj['rep$OrderCompanyReportResponse']['OrderCompanyReportResponseDetail']['Product']['ObjectAttachment']] | |
response = contents[0] | |
except Exception as e: | |
response = traceback.format_exc() | |
print response | |
if __name__ == '__main__': | |
duns = sys.argv[1] if len(sys.argv)>1 else '884114609' | |
main(duns) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment