def set_api(): api = API(client_id='CLIENT_ID', client_secret='CLIENT_SECRET', access_token='ACCESS_TOKEN') # profile = Profiles(api=api).filter(service='twitter') - is this needed? setup = Updates(api=api, profile_id='PROFILE_ID') return setup
def test_api_get_request_no_access_token(): """ Should raise ValueError if the API is called without an access_token. """ with patch("buffpy.api.OAuth2Session", return_value=MagicMock(access_token=None)), \ pytest.raises(ValueError): API(client_id="1", client_secret="2").get(url="hey")
def test_api_post_parse_buffpy_error(): httpretty.register_uri(httpretty.POST, "https://api.bufferapp.com/1/hey", body="{u'message': u\"Whoops, it looks like you've posted that one recently. Unfortunately, we're not able to post the same thing again so soon!\", u'code': 1025, u'success': False}", status=400) api = API(client_id='1', client_secret='2', access_token='access_token') api.post(url='hey', data='new=True')
def test_api_post_request_no_access_token(): """ Should raise ValueError if the API is called without an access_token. """ with patch("buffpy.api.OAuth2Session", return_value=MagicMock(access_token=None)), \ pytest.raises(ValueError): api = API(client_id="1", client_secret="2", access_token="access_token") api.post(url="hey", data="new=True")
def oath(self, authentication): from buffpy.api import API self.client_id = authentication["client_id"] self.client_secret = authentication["client_secret"] if "token" not in authentication.keys(): self.get_token() self.token = authentication["token"] self.api = API(client_id=self.client_id, client_secret=self.client_secret, access_token=self.token)
def test_api_post_parse_buffpy_error(): """ Should raise a BuffpyRestException, if the API's response is >= 400. """ httpretty.register_uri(httpretty.POST, "https://api.bufferapp.com/1/hey", status=400) with pytest.raises(BuffpyRestException): api = API(client_id="1", client_secret="2", access_token="access_token") api.post(url="hey", data="new=True")
def test_api_get_request(): """ Should call Buffer for a given GET request. """ mocked_session = MagicMock() mocked_session.get.return_value = MOCKED_RESPONSE with patch("buffpy.api.OAuth2Session", return_value=mocked_session): API(client_id="1", client_secret="2", access_token="access_token").get(url="hey") mocked_session.get.assert_called_once_with( url="https://api.bufferapp.com/1/hey")
def test_api_get_request_no_access_token(): ''' Test simply api get request without access_token ''' with patch('buffpy.api.OAuth2Session') as mocked_oauth2: mocked_session = MagicMock() mocked_session.access_token = None mocked_oauth2.return_value = mocked_session api = API(client_id='1', client_secret='2') api.get(url="hey")
def test_api_post_request_no_access_token(): ''' Test simply api post request without access_token ''' with patch('buffpy.api.OAuth2Session') as mocked_oauth2: mocked_session = MagicMock() mocked_session.access_token = None mocked_oauth2.return_value = mocked_session api = API(client_id='1', client_secret='2', access_token='access_token') api.post(url='hey', data='new=True')
def test_api_info(): """ Should request Buffer's configuration. """ mocked_session = MagicMock() mocked_session.get.return_value = MOCKED_RESPONSE with patch("buffpy.api.OAuth2Session", return_value=mocked_session): api = API(client_id="1", client_secret="2", access_token="access_token") info = api.info expected_url = "https://api.bufferapp.com/1/info/configuration.json" mocked_session.get.assert_called_once_with(url=expected_url) assert info.status == "ok"
def connectBuffer(): config = configparser.ConfigParser() config.read([os.path.expanduser('~/.rssBuffer')]) clientId = config.get("appKeys", "client_id") clientSecret = config.get("appKeys", "client_secret") redirectUrl = config.get("appKeys", "redirect_uri") accessToken = config.get("appKeys", "access_token") # instantiate the api object api = API(client_id=clientId, client_secret=clientSecret, access_token=accessToken) logging.debug(api.info) return (api)
def test_api_post_request(): """ Should call Buffer for a given POST request. """ mocked_session = MagicMock() mocked_session.post.return_value = MOCKED_RESPONSE with patch("buffpy.api.OAuth2Session", return_value=mocked_session): api = API(client_id="1", client_secret="2", access_token="access_token") api.post(url="hey", data="new=True") headers = {"Content-Type": "application/x-www-form-urlencoded"} mocked_session.post.assert_called_once_with( url="https://api.bufferapp.com/1/hey", headers=headers, data="new=True")
def test_api_get_request(): ''' Test simply api get request ''' with patch('buffpy.api.OAuth2Session') as mocked_oauth2: mocked_session = MagicMock() mocked_response = MagicMock() mocked_response.content = json.dumps({'status': 'ok'}) mocked_session.get.return_value = mocked_response mocked_oauth2.return_value = mocked_session api = API(client_id='1', client_secret='2', access_token='access_token') api.get(url="hey") mocked_session.get.assert_called_once_with(url='https://api.bufferapp.com/1/hey')
def test_api_post_request(): ''' Test simply api post request ''' with patch('buffpy.api.OAuth2Session') as mocked_oauth2: mocked_session = MagicMock() mocked_response = MagicMock() mocked_response.content = json.dumps({'status': 'ok'}) mocked_session.post.return_value = mocked_response mocked_oauth2.return_value = mocked_session api = API(client_id='1', client_secret='2', access_token='access_token') api.post(url='hey', data='new=True') headers = {'Content-Type':'application/x-www-form-urlencoded'} mocked_session.post.assert_called_once_with( url='https://api.bufferapp.com/1/hey', headers=headers, data='new=True')
def test_api_info(): ''' Test simple configuration retrieving ''' with patch('buffpy.api.OAuth2Session') as mocked_oauth2: mocked_session = MagicMock() mocked_response = MagicMock() mocked_response.content = json.dumps({'status': 'ok'}) mocked_session.get.return_value = mocked_response mocked_oauth2.return_value = mocked_session api = API(client_id='1', client_secret='2', access_token='access_token') info = api.info url = 'https://api.bufferapp.com/1/info/configuration.json' mocked_session.get.assert_called_once_with(url=url) eq_(info.status, 'ok')
def connectBuffer(): config = configparser.ConfigParser() config.read([os.path.expanduser('~/.rssBuffer')]) clientId = config.get("appKeys", "client_id") clientSecret = config.get("appKeys", "client_secret") redirectUrl = config.get("appKeys", "redirect_uri") accessToken = config.get("appKeys", "access_token") try: # instantiate the api object api = API(client_id=clientId, client_secret=clientSecret, access_token=accessToken) logging.debug(api.info) except: print("Buffer authentication failed!\n") print("Unexpected error:", sys.exc_info()[0]) return (api)
from buffpy.models.link import Link from buffpy.api import API # check http://bufferapp.com/developers/apps to retrieve a token # or generate one with the example token = 'awesome_tokne' # instantiate the api object api = API(client_id='client_id', client_secret='client_secret', access_token=token) # get a link's shares print(Link(api=api, url='http%3A%2F%2Fbufferapp.com').shares)
from buffpy.api import API from buffpy.models.link import Link # check http://bufferapp.com/developers/apps to retrieve a token # or generate one with the example token = "awesome_tokne" # instantiate the api object api = API(client_id="client_id", client_secret="client_secret", access_token=token) # get a link"s shares print(Link(api=api, url="http%3A%2F%2Fbufferapp.com").shares)
- Add code for other profiles - Add "promo category" - Other categories as necessary, show them how to add one ''' from buffpy.managers.profiles import Profiles from buffpy.managers.updates import Updates from buffpy.api import API from bufferinfo import * from quote_bot_quotes import * import random api = API(client_id=CLIENTID, client_secret=CLIENTSECRET, access_token=ACCESSTOKEN) profile = Profiles(api=api).filter(service='twitter')[0] def test_code(): profiles = Profiles(api=api) print(profiles.all) if __name__ == "__main__": test_code() ''' def add_to_buffer(count): bufferQuotes = random.sample(quotes, count)