From 3e4f0a30cfae2e70c2e05c447d70cd3787b4a4bf Mon Sep 17 00:00:00 2001 From: Vanessa Martinez Date: Thu, 12 Apr 2018 18:26:52 -0700 Subject: [PATCH 1/4] return story as a json --- .gitignore | 3 +++ py-bns.py | 32 +++++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 7bbc71c..570abf9 100644 --- a/.gitignore +++ b/.gitignore @@ -99,3 +99,6 @@ ENV/ # mypy .mypy_cache/ + +# login information +login_info.py diff --git a/py-bns.py b/py-bns.py index 028f26c..be47c47 100644 --- a/py-bns.py +++ b/py-bns.py @@ -7,6 +7,8 @@ from urllib.parse import quote except ImportError: from urllib import quote +# to import login information +import login_info class LoginError(Exception): @@ -42,6 +44,21 @@ def post(self, url, payload): e.msg = 'Received a {0} response, instead of 200'.format(status) print(e.msg) + 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 try: @@ -58,6 +75,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) @@ -68,9 +90,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() From 2e4ca181c257b6689498597bf9622bd10ad69c53 Mon Sep 17 00:00:00 2001 From: Vanessa Martinez Date: Wed, 18 Apr 2018 14:33:46 -0700 Subject: [PATCH 2/4] removed try/except from post --- py-bns.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/py-bns.py b/py-bns.py index be47c47..199cdde 100644 --- a/py-bns.py +++ b/py-bns.py @@ -34,15 +34,10 @@ 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 From 715bfaf7d1536ea2030a3410b9733bfe53a6914c Mon Sep 17 00:00:00 2001 From: James Perez Date: Wed, 18 Apr 2018 14:35:00 -0700 Subject: [PATCH 3/4] Update .gitignore removed login from gitignore --- .gitignore | 3 --- 1 file changed, 3 deletions(-) diff --git a/.gitignore b/.gitignore index 570abf9..7bbc71c 100644 --- a/.gitignore +++ b/.gitignore @@ -99,6 +99,3 @@ ENV/ # mypy .mypy_cache/ - -# login information -login_info.py From b057ebb9c0783eb6fc52e137e89d7cb738ac9872 Mon Sep 17 00:00:00 2001 From: James Perez Date: Wed, 18 Apr 2018 14:35:26 -0700 Subject: [PATCH 4/4] removed login import from py_bns --- py-bns.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/py-bns.py b/py-bns.py index 199cdde..526a999 100644 --- a/py-bns.py +++ b/py-bns.py @@ -7,8 +7,6 @@ from urllib.parse import quote except ImportError: from urllib import quote -# to import login information -import login_info class LoginError(Exception):