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

Commit

Permalink
Merge pull request #9 from datadesk/return_story
Browse files Browse the repository at this point in the history
return story as a json
  • Loading branch information
jperezlatimes authored Apr 18, 2018
2 parents 2a96ea4 + b057ebb commit 3a38849
Showing 1 changed file with 33 additions and 10 deletions.
43 changes: 33 additions & 10 deletions py-bns.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,25 @@ def __init__(self, *args, **kwargs):
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)
r = requests.post(url=url, data=payload, headers=self.headers)
# raise an exception if it's not 200
r.raise_for_status()
return r

def get(self, url):
# headers for the get request
get_header = {
'content-type': 'application/json',
'authorization': 'Bearer {0}'.format(self.access_token)
}
# set up the url
url = self.api_url.format(url)
# try requesting a story
r = requests.get(url=url, headers=get_header)
# raise exception if it's not 200
r.raise_for_status()
# return the response
return r

def connect(self):
# If there's not a username and password, raise an exception
Expand All @@ -58,6 +68,11 @@ def connect(self):
# save access token for later use
self.access_token = str(response.json()['access_token'])

def get_story(self, story_id):
# request the json from this url with the story id at the end
r = self.get('syndication/stage/portal/adapter/v1/api/v1/articles/{0}'.format(story_id))
return r.json()

def disconnect(self):
try:
payload = 'token={0}'.format(self.access_token)
Expand All @@ -68,9 +83,17 @@ def disconnect(self):


# Create an instance, passing arguments as keyword arguments
bb = pyBNS()
bb = pyBNS(
username=login_info.username,
password=login_info.password
)


# post credentials and header to /syndication/token
bb.connect()

# get the story; use the story_id as an argument
bb.get_story('P6ZNX36JIJUP')

# request api token be revoked
bb.disconnect()

0 comments on commit 3a38849

Please sign in to comment.