def test_tag_url(self): api_key = "test" tag = theguardian_tag.Tag(api_key) tag_request_content = tag.get_request_response() self.assertEqual(tag.base_url, tag_request_content.url.split("?")[0])
def test_tag_get_result_without_exception(self): api_key = "test" tag = theguardian_tag.Tag(api_key) tag_content = tag.get_content_response() results = tag.get_results(tag_content) self.assertIs(type(results), list)
def test_tag_get_indirect_content(self): api_key = "test" tag = theguardian_tag.Tag(api_key) res = tag.get_request_response() tag_content = tag.get_content_response() self.assertEqual(res.status_code, 200) self.assertIn("response", tag_content.keys())
def test_tag_get_indirect_content_invalid_credentials(self): api_key = "tests" tag = theguardian_tag.Tag(api_key) res = tag.get_request_response() tag_content = tag.get_content_response() self.assertEqual(res.status_code, 403) self.assertIn("message", tag_content.keys()) self.assertEqual("Invalid authentication credentials", tag_content["message"])
def test_section_get_references_incorrect_pages(self): api_key = "test" tag = theguardian_tag.Tag(api_key, **{ "q": "apple", "section": "technology", }) head = tag.response_headers() self.assertRaises(ValueError, tag.get_references_in_page, page_number=head["pages"] + 1)
def test_section_get_references_correct_pages(self): api_key = "test" tag = theguardian_tag.Tag(api_key, **{ "q": "apple", "section": "technology", }) refs = tag.get_references_in_page(page_number=1) refs2 = tag.get_references_in_page() self.assertIs(type(refs), list) self.assertIs(type(refs2), list)
""" This example deals with returning content of specific tags. """ from theguardian import theguardian_tag from theguardian import theguardian_content # get the apple tags headers = { "q": "apple", "section": "technology", "show-references": "all", } tag = theguardian_tag.Tag(api='test', **headers) # get the results tag_content = tag.get_content_response() results = tag.get_results(tag_content) # get results for specific tag first_tag_apiUrl = results[0]["apiUrl"] # use this api url to content content = theguardian_content.Content(api='test', url=first_tag_apiUrl) # get content response content_response = content.get_content_response() print(content_response)
""" 1. Get total number of available tags. 2. print first 15 web title for tags of type 'contributor' """ from theguardian import theguardian_tag # 1. tag = theguardian_tag.Tag(api="test") tag_response_headers = tag.response_headers() print("Total number of tags: {tags_count}.".format( tags_count=tag_response_headers["total"])) # 2. headers = { "type": "contributor", "order-by": "newest", "page-size": 15, } tag_contributor = theguardian_tag.Tag(api="test", **headers) tag_contributor_data = tag_contributor.get_content_response() results = tag_contributor.get_results(tag_contributor_data) webTitles = [result["webUrl"] for result in results] print("Web Tiles {title}.".format(title=webTitles))
def test_tag_get_result_with_exception(self): api_key = "test" tag = theguardian_tag.Tag(api_key) self.assertRaises(TypeError, tag.get_results, "some random text")
def test_tag_get_direct_content(self): api_key = "test" res = theguardian_tag.Tag(api_key).get_content_response() self.assertIn("response", res.keys())
def test_tag_response_failure_incorrect_api_key(self): api_key = "tests" res = theguardian_tag.Tag(api_key).get_request_response() self.assertEqual(res.status_code, 403)
def test_tag_response_success_correct_details(self): api_key = "test" res = theguardian_tag.Tag(api_key).get_request_response() self.assertEqual(res.status_code, 200)
""" This example deals with returning references of specific tags. """ from theguardian import theguardian_tag header = { "q": "apple", "section": "technology", } t = theguardian_tag.Tag("test", **header) print(t.get_references_in_page(1))