def test_should_raise_error_on_all_request_errors(): with patch('requests.get') as mock_get, \ pytest.raises(consumer.PyEmbedConsumerError): response = Mock() response.ok = False response.text = 'hello, world' mock_get.return_value = response consumer.get_first_oembed_response(['http://example.com/oembed1', 'http://example.com/oembed2'])
def test_should_raise_error_on_all_request_errors(): with patch('requests.get') as mock_get, \ pytest.raises(consumer.PyEmbedConsumerError): response = Mock() response.ok = False response.text = 'hello, world' mock_get.return_value = response consumer.get_first_oembed_response( ['http://example.com/oembed1', 'http://example.com/oembed2'])
def oembed_discover(url): oembed_data = {} if not url: return oembed_data, None try: oembed_urls = CustomDiscoverer().get_oembed_urls(url) oembed_response = consumer.get_first_oembed_response(oembed_urls) except (PyEmbedDiscoveryError, PyEmbedError), ex: return {}, unicode(ex)
def test_should_discover_and_get_oembed_url_for_xml(): with patch('requests.get') as mock_get, \ patch('pyembed.core.parse.parse_oembed') as mock_parse: result = __set_up_mocks(mock_get, mock_parse, 'text/xml') assert consumer.get_first_oembed_response( ['http://example.com/oembed']) == result mock_get.assert_called_with('http://example.com/oembed') mock_parse.assert_called_with('hello, world', 'text/xml')
def test_should_add_max_height(): with patch('requests.get') as mock_get, \ patch('pyembed.core.parse.parse_oembed') as mock_parse: result = __set_up_mocks(mock_get, mock_parse) assert consumer.get_first_oembed_response( ['http://example.com/oembed'], max_height=200) == result mock_get.assert_called_with('http://example.com/oembed?maxheight=200') mock_parse.assert_called_with('hello, world', 'application/json')
def test_should_add_max_width_and_height_when_query_string_present(): with patch('requests.get') as mock_get, \ patch('pyembed.core.parse.parse_oembed') as mock_parse: result = __set_up_mocks(mock_get, mock_parse) assert consumer.get_first_oembed_response( ['http://example.com/oembed?format=json'], max_width=100, max_height=200) == result mock_get.assert_called_with( 'http://example.com/oembed?format=json&maxwidth=100&maxheight=200') mock_parse.assert_called_with('hello, world', 'application/json')
def test_should_add_max_width_when_query_string_present(): with patch('requests.get') as mock_get, \ patch('pyembed.core.parse.parse_oembed') as mock_parse: result = __set_up_mocks(mock_get, mock_parse) assert consumer.get_first_oembed_response( ['http://example.com/oembed?format=json'], max_width=100) == result mock_get.assert_called_with( 'http://example.com/oembed?format=json&maxwidth=100') mock_parse.assert_called_with('hello, world', 'application/json')
def test_should_discover_and_get_oembed_url_with_charset(): with patch('requests.get') as mock_get, \ patch('pyembed.core.parse.parse_oembed') as mock_parse: result = __set_up_mocks( mock_get, mock_parse, 'application/json;charset=utf-8') assert consumer.get_first_oembed_response( ['http://example.com/oembed']) == result mock_get.assert_called_with('http://example.com/oembed') mock_parse.assert_called_with('hello, world', 'application/json')
def test_should_discover_and_get_oembed_url_with_charset(): with patch('requests.get') as mock_get, \ patch('pyembed.core.parse.parse_oembed') as mock_parse: result = __set_up_mocks(mock_get, mock_parse, 'application/json;charset=utf-8') assert consumer.get_first_oembed_response( ['http://example.com/oembed']) == result mock_get.assert_called_with('http://example.com/oembed') mock_parse.assert_called_with('hello, world', 'application/json')
def test_should_try_next_on_request_error(): with patch('requests.get') as mock_get, \ patch('pyembed.core.parse.parse_oembed') as mock_parse: mock_get.side_effect = __one_bad_two_ok parsed = Mock() mock_parse.return_value = parsed assert consumer.get_first_oembed_response( ['http://example.com/oembed1', 'http://example.com/oembed2']) == parsed mock_get.assert_has_calls([call('http://example.com/oembed1'), call('http://example.com/oembed2')]) mock_parse.assert_called_with('hello, world', 'application/json')
def embed(self, url, max_width=None, max_height=None): """Returns an HTML representation of a resource, given a URL. This can be directly embedded in a web page. :param url: the content URL. :param max_width: (optional) the maximum width of the embedded resource. :param max_height: (optional) the maximum height of the embedded resource. :returns: an HTML representation of the resource. :raises PyEmbedError: if there is an error fetching the response. """ try: oembed_urls = self.discoverer.get_oembed_urls(url) response = consumer.get_first_oembed_response(oembed_urls, max_width=max_width, max_height=max_height) except requests.exceptions.RequestException as e: raise PyEmbedError(e) return self.renderer.render(url, response)
def test_should_try_next_on_request_error(): with patch('requests.get') as mock_get, \ patch('pyembed.core.parse.parse_oembed') as mock_parse: mock_get.side_effect = __one_bad_two_ok parsed = Mock() mock_parse.return_value = parsed assert consumer.get_first_oembed_response( ['http://example.com/oembed1', 'http://example.com/oembed2']) == parsed mock_get.assert_has_calls([ call('http://example.com/oembed1'), call('http://example.com/oembed2') ]) mock_parse.assert_called_with('hello, world', 'application/json')
def embed(self, url, **parameters): """Returns an HTML representation of a resource, given a URL. This can be directly embedded in a web page. :param url: the content URL. :param parameters: (optional) parameters of the embedded resource :returns: an HTML representation of the resource. :raises PyEmbedError: if there is an error fetching the response. """ try: oembed_urls = self.discoverer.get_oembed_urls(url) response = consumer.get_first_oembed_response( oembed_urls, **parameters) except requests.exceptions.RequestException as e: raise PyEmbedError(e) return self.renderer.render(url, response)
def embed(self, url, max_width=None, max_height=None): """Returns an HTML representation of a resource, given a URL. This can be directly embedded in a web page. :param url: the content URL. :param max_width: (optional) the maximum width of the embedded resource. :param max_height: (optional) the maximum height of the embedded resource. :returns: an HTML representation of the resource. :raises PyEmbedError: if there is an error fetching the response. """ try: oembed_urls = self.discoverer.get_oembed_urls(url) response = consumer.get_first_oembed_response( oembed_urls, max_width=max_width, max_height=max_height) except requests.exceptions.RequestException as e: raise PyEmbedError(e) return self.renderer.render(url, response)
def resource_change(self): error_message = None if self.resource_url: # Discover embed code using oEmbed oembed = PyEmbed() embed_code = None thumbnail_url = None try: oembed_urls = oembed.discoverer.get_oembed_urls(self.resource_url) if oembed_urls: oembed_response = oembed_consumer.get_first_oembed_response( oembed_urls, max_width=800, max_height=600 ) embed_code = oembed.renderer.render(self.resource_url, oembed_response) thumbnail_url = getattr(oembed_response, 'thumbnail_url', '') if embed_code and thumbnail_url: self.embed_code = embed_code self.thumbnail_url = thumbnail_url else: error_message = u'''Nie udało się pobrać kodu osadzenia (embed code) tego zasobu!''' except RequestException: error_message = u'''Wystąpił problem podczas łączenia z adresem "%s". Sprawdź czy jest on poprawny.''' % self.resource_url if error_message: self.resource_url = '' self.embed_code = '' self.thumbnail_url = '' return { 'warning': { 'title': u'Problem z pobraniem informacji o zasobie!', 'message': error_message, } }
def resource_change(self): error_message = None if self.resource_url: # Discover embed code using oEmbed oembed = PyEmbed() embed_code = None thumbnail_url = None try: oembed_urls = oembed.discoverer.get_oembed_urls( self.resource_url) if oembed_urls: oembed_response = oembed_consumer.get_first_oembed_response( oembed_urls, max_width=800, max_height=600) embed_code = oembed.renderer.render( self.resource_url, oembed_response) thumbnail_url = getattr(oembed_response, 'thumbnail_url', '') if embed_code and thumbnail_url: self.embed_code = embed_code self.thumbnail_url = thumbnail_url else: error_message = u'''Nie udało się pobrać kodu osadzenia (embed code) tego zasobu!''' except RequestException: error_message = u'''Wystąpił problem podczas łączenia z adresem "%s". Sprawdź czy jest on poprawny.''' % self.resource_url if error_message: self.resource_url = '' self.embed_code = '' self.thumbnail_url = '' return { 'warning': { 'title': u'Problem z pobraniem informacji o zasobie!', 'message': error_message, } }