def test_prepare_batch_request_with_params(self): batch_request = FacebookBatchRequest(access_token='fake_token', requests=[self.req1, self.req4]) expected_batch = ( '[{"headers": {"Conent-Type": "application/json"}, "method": "GET", "relative_url": "/v2.12/123?access_token=fake_token", "name": "0"}, ' '{"headers": {"Conent-Type": "application/json"}, "method": "GET", "relative_url": "/v2.12/123?access_token=fake_token&fields=name%2C+descripti%C3%B3n", "name": "1"}]' ) batch_request.prepare_batch_request() self.assertEqual(sorted(batch_request.post_params['batch']), sorted(expected_batch)) self.assertTrue(batch_request.post_params['include_headers'])
def test_add_a_named_dict_of_requests(self): requests = { 'first': self.req1, 'second': self.req2, 'third': self.req3, } batch_request = FacebookBatchRequest(requests=requests, access_token='fake_token') batch_request.prepare_batch_request() for request in batch_request: self.assertEqual(requests[request['name']], request['request'])
def test_prepare_batch_request(self): batch_request = FacebookBatchRequest( access_token='fake_token', requests=[self.req1, self.req2, self.req3] ) expected_batch = ( '[{"headers": {"Conent-Type": "application/json"}, "method": "GET", "relative_url": "/v2.5/123?access_token=fake_token", "name": "0"}, ' '{"body": "foo=bar", "headers": {}, "method": "POST", "relative_url": "/v2.5/123", "name": "1"}, ' '{"access_token": "other_token", "headers": {}, "method": "DELETE", "relative_url": "/v2.5/123?access_token=other_token", "name": "2"}]' ) batch_request.prepare_batch_request() self.assertEqual(sorted(batch_request.post_params['batch']), sorted(expected_batch)) self.assertTrue(batch_request.post_params['include_headers'])
def test_send_over_limit_batch_request(self): requests = [self.request] * 51 with self.assertRaises(FacebookSDKException): self.client.send_batch_request(batch_request=FacebookBatchRequest( access_token='fake_token', requests=requests, ), )
def test_send_empty_batch_request(self): with self.assertRaises(FacebookSDKException): self.client.send_batch_request( batch_request=FacebookBatchRequest( access_token='fake_token', ), )
def test_add_a_list_of_requests(self): requests = [self.req1, self.req2] batch_request = FacebookBatchRequest(requests=[self.req1, self.req2], access_token='fake_token') for request in batch_request.requests: self.assertTrue(request['request'] in requests)
def test_batch_request_with_files(self): request = FacebookRequest( endpoint='/foo', method='POST', params={ 'message': 'foo', 'source': FacebookFile( path='{base_path}/foo.txt'.format( base_path=os.path.dirname(os.path.abspath(__file__)) ), ) } ) batch_request = FacebookBatchRequest( access_token='fake_token', requests=[request], ) expected_batch = '[{"body": "message=foo", "attached_files": "%(attached_files)s", "name": "0", "relative_url": "/v2.5/foo", "headers": {}, "method": "POST"}]' % dict(attached_files=batch_request.requests[0].get('attached_files')) batch_request.prepare_batch_request() self.assertEqual(sorted(batch_request.post_params['batch']), sorted(expected_batch))
def setUp(self): super(TestFacebookClient, self).setUp() self.request = FacebookRequest(endpoint='me', method='GET') self.batch_request = FacebookBatchRequest(access_token='fake_token', requests=[self.request]) self.fake_response = FakeResponse( status_code=200, content= '{"data":[{"id":"123","name":"Foo"},{"id":"1337","name":"Bar"}]}', ) self.fake_batch_response = FakeResponse( status_code=200, content='[{"code":"123","body":"Foo"}]', ) self.client = FakeFacebookClient(fake_response=self.fake_response)
def send_batch_request(self, requests, access_token=None, graph_version=None): access_token = access_token or getattr(self, 'default_access_token', None) graph_version = graph_version or self.default_graph_version batch_request = FacebookBatchRequest( app=self.app, requests=requests, access_token=access_token, graph_version=graph_version, ) response = self.client.send_batch_request(batch_request=batch_request) return response
def setUp(self): super(TestFacebookClient, self).setUp() self.request = FacebookRequest(endpoint='me', method='GET') self.batch_request = FacebookBatchRequest( access_token='fake_token', requests=[self.request] ) self.fake_response = FakeResponse( status_code=200, content='{"data":[{"id":"123","name":"Foo"},{"id":"1337","name":"Bar"}]}', headers='{"Content-Type": "application/json; charset=UTF-8","x-fb-trace-id": "1234"}', ) self.fake_batch_response = FakeResponse( status_code=200, content='[{"code":"123","body":"Foo"}]', headers='{"Content-Type": "application/json; charset=UTF-8","x-fb-trace-id": "1234"}', ) self.client = FakeFacebookClient(fake_response=self.fake_response)
def send_batch_request( self, requests, # type: Union[Iterable[FacebookRequest], Mapping[Text, FacebookRequest]] access_token=None, # type: Optional[Text] graph_version=None, # type: Optional[Text] timeout=None, # type: Optional[int] ): # type: (...) -> FacebookBatchResponse access_token = access_token or getattr(self, 'default_access_token', None) graph_version = graph_version or self.default_graph_version batch_request = FacebookBatchRequest( app=self.app, requests=requests, access_token=access_token, graph_version=graph_version, timeout=timeout, ) response = self.client.send_batch_request(batch_request=batch_request) return response