refactor: rest samples(code+rest) better exception handling
This commit is contained in:
Родитель
c929665fda
Коммит
09651cb281
|
@ -7,19 +7,19 @@ from pprint import pprint
|
|||
|
||||
import dotenv
|
||||
import requests
|
||||
from requests import HTTPError
|
||||
|
||||
|
||||
def auto_suggest_basic(
|
||||
query,
|
||||
subscription_key,
|
||||
auth_header_name="Ocp-Apim-Subscription-Key",
|
||||
mkt="en-us",
|
||||
query, subscription_key, auth_header_name="Ocp-Apim-Subscription-Key", mkt="en-us"
|
||||
):
|
||||
"""Bing Auto Suggest Basic REST call
|
||||
|
||||
This sample uses the Bing Autosuggest API to provide a list of suggested search query strings
|
||||
Documentation: https://docs.microsoft.com/en-us/bing/search-apis/bing-autosuggest/overview
|
||||
|
||||
May throw HTTPError in case of invalid parameters or a server error.
|
||||
|
||||
Args:
|
||||
subscription_key (str): Azure subscription key of Bing Auto Suggest service
|
||||
auth_header_name (str): Name of the authorization header
|
||||
|
@ -37,8 +37,10 @@ def auto_suggest_basic(
|
|||
response = requests.get(endpoint, headers=headers, params=params, timeout=10)
|
||||
response.raise_for_status()
|
||||
return response
|
||||
except Exception as ex:
|
||||
raise Exception(f"Encountered exception: {ex}") from ex
|
||||
except HTTPError as ex:
|
||||
print(ex)
|
||||
print("++The above exception was thrown and handled succesfully++")
|
||||
return response
|
||||
|
||||
|
||||
def main() -> None:
|
||||
|
@ -46,6 +48,7 @@ def main() -> None:
|
|||
# Load the environment variables from .env file
|
||||
env = dotenv.dotenv_values()
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
SUBSCRIPTION_KEY_ENV_VAR_NAME = "BING_SEARCH_V7_AUTO_SUGGEST_SUBSCRIPTION_KEY"
|
||||
|
||||
# Add your Bing Auto Suggest V7 subscription key to your environment variables / .env file
|
||||
|
@ -57,16 +60,12 @@ def main() -> None:
|
|||
)
|
||||
)
|
||||
|
||||
try:
|
||||
response = auto_suggest_basic(query="sail", subscription_key=subscription_key)
|
||||
print("\nResponse Headers:\n")
|
||||
pprint(dict(response.headers))
|
||||
response = auto_suggest_basic(query="sail", subscription_key=subscription_key)
|
||||
print("\nResponse Headers:\n")
|
||||
pprint(dict(response.headers))
|
||||
|
||||
print("\nJSON Response:\n")
|
||||
print(json.dumps(response.json(), indent=4))
|
||||
|
||||
except Exception as ex:
|
||||
print(f"Encountered exception: {ex}")
|
||||
print("\nJSON Response:\n")
|
||||
print(json.dumps(response.json(), indent=4))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
@ -7,6 +7,7 @@ from pprint import pprint
|
|||
|
||||
import dotenv
|
||||
import requests
|
||||
from requests import HTTPError
|
||||
|
||||
|
||||
def custom_search_basic(
|
||||
|
@ -22,6 +23,8 @@ def custom_search_basic(
|
|||
get back user-controlled web page results.
|
||||
Documentation: https://docs.microsoft.com/en-us/bing/search-apis/bing-custom-search/overview
|
||||
|
||||
May throw HTTPError in case of invalid parameters or a server error.
|
||||
|
||||
Args:
|
||||
subscription_key (str): Azure subscription key of Bing Custom Search service
|
||||
custom_config_id (str): Custom Configuation ID obtained from the portal
|
||||
|
@ -39,9 +42,10 @@ def custom_search_basic(
|
|||
response = requests.get(endpoint, headers=headers, params=params, timeout=10)
|
||||
response.raise_for_status()
|
||||
return response
|
||||
|
||||
except Exception as ex:
|
||||
raise Exception(f"Encountered exception: {ex}") from ex
|
||||
except HTTPError as ex:
|
||||
print(ex)
|
||||
print("++The above exception was thrown and handled succesfully++")
|
||||
return response
|
||||
|
||||
|
||||
def main() -> None:
|
||||
|
@ -49,6 +53,7 @@ def main() -> None:
|
|||
# Load the environment variables from .env file
|
||||
env = dotenv.dotenv_values()
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
SUBSCRIPTION_KEY_ENV_VAR_NAME = "BING_SEARCH_V7_CUSTOM_SEARCH_SUBSCRIPTION_KEY"
|
||||
CUSTOM_CONFIG_ID_ENV_VAR_NAME = "BING_CUSTOM_SEARCH_CONFIG"
|
||||
|
||||
|
@ -63,16 +68,12 @@ def main() -> None:
|
|||
)
|
||||
custom_config_id = env.get(CUSTOM_CONFIG_ID_ENV_VAR_NAME, "1")
|
||||
|
||||
try:
|
||||
response = custom_search_basic("Microsoft", subscription_key, custom_config_id)
|
||||
print("\nResponse Headers:\n")
|
||||
pprint(dict(response.headers))
|
||||
response = custom_search_basic("Microsoft", subscription_key, custom_config_id)
|
||||
print("\nResponse Headers:\n")
|
||||
pprint(dict(response.headers))
|
||||
|
||||
print("\nJSON Response:\n")
|
||||
print(json.dumps(response.json(), indent=4))
|
||||
|
||||
except Exception as ex:
|
||||
print(f"Encountered exception: {ex}")
|
||||
print("\nJSON Response:\n")
|
||||
print(json.dumps(response.json(), indent=4))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
@ -7,19 +7,19 @@ from pprint import pprint
|
|||
|
||||
import dotenv
|
||||
import requests
|
||||
from requests import HTTPError
|
||||
|
||||
|
||||
def entity_search_basic(
|
||||
query,
|
||||
subscription_key,
|
||||
auth_header_name="Ocp-Apim-Subscription-Key",
|
||||
mkt="en-us",
|
||||
query, subscription_key, auth_header_name="Ocp-Apim-Subscription-Key", mkt="en-us"
|
||||
):
|
||||
"""Bing Entity Search Basic REST call
|
||||
|
||||
This sample uses the Bing Entity Search API to retreive information about a well-known entity
|
||||
Documentation: https://docs.microsoft.com/en-us/bing/search-apis/bing-entity-search/overview
|
||||
|
||||
May throw HTTPError in case of invalid parameters or a server error.
|
||||
|
||||
Args:
|
||||
subscription_key (str): Azure subscription key of Bing Entity Search service
|
||||
auth_header_name (str): Name of the authorization header
|
||||
|
@ -36,8 +36,10 @@ def entity_search_basic(
|
|||
response = requests.get(endpoint, headers=headers, params=params, timeout=10)
|
||||
response.raise_for_status()
|
||||
return response
|
||||
except Exception as ex:
|
||||
raise Exception(f"Encountered exception: {ex}") from ex
|
||||
except HTTPError as ex:
|
||||
print(ex)
|
||||
print("++The above exception was thrown and handled succesfully++")
|
||||
return response
|
||||
|
||||
|
||||
def main() -> None:
|
||||
|
@ -45,6 +47,7 @@ def main() -> None:
|
|||
# Load the environment variables from .env file
|
||||
env = dotenv.dotenv_values()
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
SUBSCRIPTION_KEY_ENV_VAR_NAME = "BING_SEARCH_V7_ENTITY_SEARCH_SUBSCRIPTION_KEY"
|
||||
|
||||
# Add your Bing Entity Search V7 subscription key to your environment variables / .env file
|
||||
|
@ -56,16 +59,12 @@ def main() -> None:
|
|||
)
|
||||
)
|
||||
|
||||
try:
|
||||
response = entity_search_basic("alija izetbegović", subscription_key)
|
||||
print("\nResponse Headers:\n")
|
||||
pprint(dict(response.headers))
|
||||
response = entity_search_basic("alija izetbegović", subscription_key)
|
||||
print("\nResponse Headers:\n")
|
||||
pprint(dict(response.headers))
|
||||
|
||||
print("\nJSON Response:\n")
|
||||
print(json.dumps(response.json(), indent=4))
|
||||
|
||||
except Exception as ex:
|
||||
print(f"Encountered exception: {ex}")
|
||||
print("\nJSON Response:\n")
|
||||
print(json.dumps(response.json(), indent=4))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
@ -7,13 +7,11 @@ from pprint import pprint
|
|||
|
||||
import dotenv
|
||||
import requests
|
||||
from requests import HTTPError
|
||||
|
||||
|
||||
def image_search_basic(
|
||||
query,
|
||||
subscription_key,
|
||||
auth_header_name="Ocp-Apim-Subscription-Key",
|
||||
mkt="en-us",
|
||||
query, subscription_key, auth_header_name="Ocp-Apim-Subscription-Key", mkt="en-us"
|
||||
):
|
||||
"""Bing Image Search Basic REST call
|
||||
|
||||
|
@ -21,6 +19,8 @@ def image_search_basic(
|
|||
returns relevant images with data.
|
||||
Documentation: https://docs.microsoft.com/en-us/bing/search-apis/bing-image-search/overview
|
||||
|
||||
May throw HTTPError in case of invalid parameters or a server error.
|
||||
|
||||
Args:
|
||||
subscription_key (str): Azure subscription key of Bing Image Search service
|
||||
auth_header_name (str): Name of the authorization header
|
||||
|
@ -37,9 +37,10 @@ def image_search_basic(
|
|||
response = requests.get(endpoint, headers=headers, params=params, timeout=10)
|
||||
response.raise_for_status()
|
||||
return response
|
||||
|
||||
except Exception as ex:
|
||||
raise Exception(f"Encountered exception: {ex}") from ex
|
||||
except HTTPError as ex:
|
||||
print(ex)
|
||||
print("++The above exception was thrown and handled succesfully++")
|
||||
return response
|
||||
|
||||
|
||||
def main() -> None:
|
||||
|
@ -47,6 +48,7 @@ def main() -> None:
|
|||
# Load the environment variables from .env file
|
||||
env = dotenv.dotenv_values()
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
SUBSCRIPTION_KEY_ENV_VAR_NAME = "BING_SEARCH_V7_IMAGE_SEARCH_SUBSCRIPTION_KEY"
|
||||
|
||||
# Add your Bing Image Search V7 subscription key to your environment variables / .env file
|
||||
|
@ -58,16 +60,12 @@ def main() -> None:
|
|||
)
|
||||
)
|
||||
|
||||
try:
|
||||
response = image_search_basic("Arabian horse", subscription_key)
|
||||
print("\nResponse Headers:\n")
|
||||
pprint(dict(response.headers))
|
||||
response = image_search_basic("Arabian horse", subscription_key)
|
||||
print("\nResponse Headers:\n")
|
||||
pprint(dict(response.headers))
|
||||
|
||||
print("\nJSON Response:\n")
|
||||
print(json.dumps(response.json(), indent=4))
|
||||
|
||||
except Exception as ex:
|
||||
print(f"Encountered exception: {ex}")
|
||||
print("\nJSON Response:\n")
|
||||
print(json.dumps(response.json(), indent=4))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
@ -7,13 +7,11 @@ from pprint import pprint
|
|||
|
||||
import dotenv
|
||||
import requests
|
||||
from requests import HTTPError
|
||||
|
||||
|
||||
def news_search_basic(
|
||||
query,
|
||||
subscription_key,
|
||||
auth_header_name="Ocp-Apim-Subscription-Key",
|
||||
mkt="en-us",
|
||||
query, subscription_key, auth_header_name="Ocp-Apim-Subscription-Key", mkt="en-us"
|
||||
):
|
||||
"""Bing News Search Basic REST call
|
||||
|
||||
|
@ -21,6 +19,8 @@ def news_search_basic(
|
|||
returns relevant news webpages.
|
||||
Documentation: https://docs.microsoft.com/en-us/bing/search-apis/bing-news-search/overview
|
||||
|
||||
May throw HTTPError in case of invalid parameters or a server error.
|
||||
|
||||
Args:
|
||||
subscription_key (str): Azure subscription key of Bing News Search service
|
||||
auth_header_name (str): Name of the authorization header
|
||||
|
@ -37,9 +37,10 @@ def news_search_basic(
|
|||
response = requests.get(endpoint, headers=headers, params=params, timeout=10)
|
||||
response.raise_for_status()
|
||||
return response
|
||||
|
||||
except Exception as ex:
|
||||
raise Exception(f"Encountered exception: {ex}") from ex
|
||||
except HTTPError as ex:
|
||||
print(ex)
|
||||
print("++The above exception was thrown and handled succesfully++")
|
||||
return response
|
||||
|
||||
|
||||
def main() -> None:
|
||||
|
@ -47,6 +48,7 @@ def main() -> None:
|
|||
# Load the environment variables from .env file
|
||||
env = dotenv.dotenv_values()
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
SUBSCRIPTION_KEY_ENV_VAR_NAME = "BING_SEARCH_V7_NEWS_SEARCH_SUBSCRIPTION_KEY"
|
||||
|
||||
# Add your Bing News Search V7 subscription key to your environment variables / .env file
|
||||
|
@ -58,16 +60,12 @@ def main() -> None:
|
|||
)
|
||||
)
|
||||
|
||||
try:
|
||||
response = news_search_basic("Microsoft", subscription_key)
|
||||
print("\nResponse Headers:\n")
|
||||
pprint(dict(response.headers))
|
||||
response = news_search_basic("Microsoft", subscription_key)
|
||||
print("\nResponse Headers:\n")
|
||||
pprint(dict(response.headers))
|
||||
|
||||
print("\nJSON Response:\n")
|
||||
print(json.dumps(response.json(), indent=4))
|
||||
|
||||
except Exception as ex:
|
||||
print(f"Encountered exception: {ex}")
|
||||
print("\nJSON Response:\n")
|
||||
print(json.dumps(response.json(), indent=4))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
@ -7,6 +7,7 @@ from pprint import pprint
|
|||
|
||||
import dotenv
|
||||
import requests
|
||||
from requests import HTTPError
|
||||
|
||||
|
||||
def spell_check_basic(
|
||||
|
@ -21,7 +22,9 @@ def spell_check_basic(
|
|||
This sample uses the Bing Spell Check API to perform contextual grammar and spell
|
||||
checking on a text string and then suggests corrections with a scored confidence
|
||||
Bing Spell Check API:
|
||||
https://docs.microsoft.com/en-us/bing/search-apis/bing-spell-check/overview
|
||||
Documentation: https://docs.microsoft.com/en-us/bing/search-apis/bing-spell-check/overview
|
||||
|
||||
May throw HTTPError in case of invalid parameters or a server error.
|
||||
|
||||
Args:
|
||||
subscription_key (str): Azure subscription key of Bing Spell Check service
|
||||
|
@ -48,9 +51,10 @@ def spell_check_basic(
|
|||
)
|
||||
response.raise_for_status()
|
||||
return response
|
||||
|
||||
except Exception as ex:
|
||||
raise Exception(f"Encountered exception: {ex}") from ex
|
||||
except HTTPError as ex:
|
||||
print(ex)
|
||||
print("++The above exception was thrown and handled succesfully++")
|
||||
return response
|
||||
|
||||
|
||||
def main() -> None:
|
||||
|
@ -58,6 +62,7 @@ def main() -> None:
|
|||
# Load the environment variables from .env file
|
||||
env = dotenv.dotenv_values()
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
SUBSCRIPTION_KEY_ENV_VAR_NAME = "BING_SEARCH_V7_SPELL_CHECK_SUBSCRIPTION_KEY"
|
||||
|
||||
# Add your Bing Spell Check V7 subscription key to your environment variables / .env file
|
||||
|
@ -69,20 +74,16 @@ def main() -> None:
|
|||
)
|
||||
)
|
||||
|
||||
try:
|
||||
response = spell_check_basic(
|
||||
"when i went two the houze i heared they'r'e voice and they're srcreams.\
|
||||
I walk their and told: \"helo fren\"",
|
||||
subscription_key,
|
||||
)
|
||||
print("\nResponse Headers:\n")
|
||||
pprint(dict(response.headers))
|
||||
response = spell_check_basic(
|
||||
"when i went two the houze i heared they'r'e voice and they're srcreams.\
|
||||
I walk their and told: \"helo fren\"",
|
||||
subscription_key,
|
||||
)
|
||||
print("\nResponse Headers:\n")
|
||||
pprint(dict(response.headers))
|
||||
|
||||
print("\nJSON Response:\n")
|
||||
print(json.dumps(response.json(), indent=4))
|
||||
|
||||
except Exception as ex:
|
||||
print(f"Encountered exception: {ex}")
|
||||
print("\nJSON Response:\n")
|
||||
print(json.dumps(response.json(), indent=4))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
@ -7,13 +7,11 @@ from pprint import pprint
|
|||
|
||||
import dotenv
|
||||
import requests
|
||||
from requests import HTTPError
|
||||
|
||||
|
||||
def video_search_basic(
|
||||
query,
|
||||
subscription_key,
|
||||
auth_header_name="Ocp-Apim-Subscription-Key",
|
||||
mkt="en-us",
|
||||
query, subscription_key, auth_header_name="Ocp-Apim-Subscription-Key", mkt="en-us"
|
||||
):
|
||||
"""Bing Video Search Basic REST call
|
||||
|
||||
|
@ -22,6 +20,8 @@ def video_search_basic(
|
|||
|
||||
Documentation: https://docs.microsoft.com/en-us/bing/search-apis/bing-video-search/overview
|
||||
|
||||
May throw HTTPError in case of invalid parameters or a server error.
|
||||
|
||||
Args:
|
||||
subscription_key (str): Azure subscription key of Bing Video Search service
|
||||
auth_header_name (str): Name of the authorization header
|
||||
|
@ -38,9 +38,10 @@ def video_search_basic(
|
|||
response = requests.get(endpoint, headers=headers, params=params, timeout=10)
|
||||
response.raise_for_status()
|
||||
return response
|
||||
|
||||
except Exception as ex:
|
||||
raise Exception(f"Encountered exception: {ex}") from ex
|
||||
except HTTPError as ex:
|
||||
print(ex)
|
||||
print("++The above exception was thrown and handled succesfully++")
|
||||
return response
|
||||
|
||||
|
||||
def main() -> None:
|
||||
|
@ -48,6 +49,7 @@ def main() -> None:
|
|||
# Load the environment variables from .env file
|
||||
env = dotenv.dotenv_values()
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
SUBSCRIPTION_KEY_ENV_VAR_NAME = "BING_SEARCH_V7_VIDEO_SEARCH_SUBSCRIPTION_KEY"
|
||||
|
||||
# Add your Bing Video Search V7 subscription key to your environment variables / .env file
|
||||
|
@ -59,16 +61,12 @@ def main() -> None:
|
|||
)
|
||||
)
|
||||
|
||||
try:
|
||||
response = video_search_basic("kentucky derby", subscription_key)
|
||||
print("\nResponse Headers:\n")
|
||||
pprint(dict(response.headers))
|
||||
response = video_search_basic("kentucky derby", subscription_key)
|
||||
print("\nResponse Headers:\n")
|
||||
pprint(dict(response.headers))
|
||||
|
||||
print("\nJSON Response:\n")
|
||||
print(json.dumps(response.json(), indent=4))
|
||||
|
||||
except Exception as ex:
|
||||
print(f"Encountered exception: {ex}")
|
||||
print("\nJSON Response:\n")
|
||||
print(json.dumps(response.json(), indent=4))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
@ -7,6 +7,7 @@ from pprint import pprint
|
|||
|
||||
import dotenv
|
||||
import requests
|
||||
from requests import HTTPError
|
||||
|
||||
|
||||
def visual_search_basic(
|
||||
|
@ -22,6 +23,8 @@ def visual_search_basic(
|
|||
|
||||
Documentation: https://docs.microsoft.com/en-us/bing/search-apis/bing-visual-search/overview
|
||||
|
||||
May throw HTTPError in case of invalid parameters or a server error.
|
||||
|
||||
Args:
|
||||
subscription_key (str): Azure subscription key of Bing Visual Search service
|
||||
auth_header_name (str): Name of the authorization header
|
||||
|
@ -46,9 +49,10 @@ def visual_search_basic(
|
|||
)
|
||||
response.raise_for_status()
|
||||
return response
|
||||
|
||||
except Exception as ex:
|
||||
raise Exception(f"Encountered exception: {ex}") from ex
|
||||
except HTTPError as ex:
|
||||
print(ex)
|
||||
print("++The above exception was thrown and handled succesfully++")
|
||||
return response
|
||||
|
||||
|
||||
def main() -> None:
|
||||
|
@ -56,6 +60,7 @@ def main() -> None:
|
|||
# Load the environment variables from .env file
|
||||
env = dotenv.dotenv_values()
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
SUBSCRIPTION_KEY_ENV_VAR_NAME = "BING_SEARCH_V7_VISUAL_SEARCH_SUBSCRIPTION_KEY"
|
||||
|
||||
# Add your Bing Visual Search V7 subscription key to your environment variables / .env file
|
||||
|
@ -67,16 +72,12 @@ def main() -> None:
|
|||
)
|
||||
)
|
||||
|
||||
try:
|
||||
response = visual_search_basic("./my_image.jpg", subscription_key)
|
||||
print("\nResponse Headers:\n")
|
||||
pprint(dict(response.headers))
|
||||
response = visual_search_basic("./my_image.jpg", subscription_key)
|
||||
print("\nResponse Headers:\n")
|
||||
pprint(dict(response.headers))
|
||||
|
||||
print("\nJSON Response:\n")
|
||||
print(json.dumps(response.json(), indent=4))
|
||||
|
||||
except Exception as ex:
|
||||
print(f"Encountered exception: {ex}")
|
||||
print("\nJSON Response:\n")
|
||||
print(json.dumps(response.json(), indent=4))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
@ -3,23 +3,23 @@
|
|||
"""Python example showcasing the usage of Bing Web Search API using REST calls"""
|
||||
|
||||
import json
|
||||
import os
|
||||
from pprint import pprint
|
||||
|
||||
import dotenv
|
||||
import requests
|
||||
from requests import HTTPError
|
||||
|
||||
|
||||
def web_search_basic(
|
||||
query,
|
||||
subscription_key,
|
||||
auth_header_name="Ocp-Apim-Subscription-Key",
|
||||
mkt="en-us",
|
||||
query, subscription_key, auth_header_name="Ocp-Apim-Subscription-Key", mkt="en-us"
|
||||
):
|
||||
"""Bing Web Search Basic REST call
|
||||
|
||||
This sample makes a call to the Bing Web Search API with a text query and returns relevant pages
|
||||
Documentation: https://docs.microsoft.com/en-us/bing/search-apis/bing-web-search/overview
|
||||
|
||||
May throw HTTPError in case of invalid parameters or a server error.
|
||||
|
||||
Args:
|
||||
subscription_key (str): Azure subscription key of Bing Web Search service
|
||||
auth_header_name (str): Name of the authorization header
|
||||
|
@ -36,9 +36,10 @@ def web_search_basic(
|
|||
response = requests.get(endpoint, headers=headers, params=params, timeout=10)
|
||||
response.raise_for_status()
|
||||
return response
|
||||
|
||||
except Exception as ex:
|
||||
raise Exception(f"Encountered exception: {ex}") from ex
|
||||
except HTTPError as ex:
|
||||
print(ex)
|
||||
print("++The above exception was thrown and handled succesfully++")
|
||||
return response
|
||||
|
||||
|
||||
def main() -> None:
|
||||
|
@ -46,6 +47,7 @@ def main() -> None:
|
|||
# Load the environment variables from .env file
|
||||
env = dotenv.dotenv_values()
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
SUBSCRIPTION_KEY_ENV_VAR_NAME = "BING_SEARCH_V7_WEB_SEARCH_SUBSCRIPTION_KEY"
|
||||
|
||||
# Add your Bing Web Search V7 subscription key to your environment variables / .env file
|
||||
|
@ -57,16 +59,12 @@ def main() -> None:
|
|||
)
|
||||
)
|
||||
|
||||
try:
|
||||
response = web_search_basic("Microsoft", subscription_key)
|
||||
print("\nResponse Headers:\n")
|
||||
pprint(dict(response.headers))
|
||||
response = web_search_basic("Microsoft", subscription_key)
|
||||
print("\nResponse Headers:\n")
|
||||
pprint(dict(response.headers))
|
||||
|
||||
print("\nJSON Response:\n")
|
||||
print(json.dumps(response.json(), indent=4))
|
||||
|
||||
except Exception as ex:
|
||||
print(f"Encountered exception: {ex}")
|
||||
print("\nJSON Response:\n")
|
||||
print(json.dumps(response.json(), indent=4))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
import unittest
|
||||
|
||||
import dotenv
|
||||
from requests import HTTPError, JSONDecodeError
|
||||
from requests import JSONDecodeError
|
||||
|
||||
from samples.rest.bing_auto_suggest_v7 import auto_suggest_basic
|
||||
|
||||
|
@ -16,25 +16,23 @@ class AutoSuggestRESTSamplesTest(unittest.TestCase):
|
|||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.env = dotenv.dotenv_values()
|
||||
cls.subscription_key = cls.env.get(
|
||||
"BING_SEARCH_V7_AUTO_SUGGEST_SUBSCRIPTION_KEY"
|
||||
)
|
||||
|
||||
def test_auto_suggest_subscription_key_not_empty(self):
|
||||
"""Test that the subscription key is defined in the environment"""
|
||||
self.assertIsNotNone(self.subscription_key)
|
||||
self.assertNotEqual(self.subscription_key, "")
|
||||
|
||||
def test_auto_suggest_basic(self):
|
||||
"""Test the basic REST call to Auto Suggest API"""
|
||||
response = auto_suggest_basic(
|
||||
"sail",
|
||||
subscription_key=self.env.get(
|
||||
"BING_SEARCH_V7_AUTO_SUGGEST_SUBSCRIPTION_KEY"
|
||||
),
|
||||
)
|
||||
response = auto_suggest_basic("sail", subscription_key=self.subscription_key)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
def test_auto_suggest_response_is_json(self):
|
||||
"""Test that Auto Suggest API returns responses in JSON format"""
|
||||
response = auto_suggest_basic(
|
||||
"vim",
|
||||
subscription_key=self.env.get(
|
||||
"BING_SEARCH_V7_AUTO_SUGGEST_SUBSCRIPTION_KEY"
|
||||
),
|
||||
)
|
||||
response = auto_suggest_basic("vim", subscription_key=self.subscription_key)
|
||||
try:
|
||||
response.json()
|
||||
except JSONDecodeError:
|
||||
|
@ -44,18 +42,12 @@ class AutoSuggestRESTSamplesTest(unittest.TestCase):
|
|||
|
||||
def test_auto_suggest_no_auth(self):
|
||||
"""Test that Auto Suggest API returns 401 if authorization fails"""
|
||||
with self.assertRaises(Exception) as ex:
|
||||
response = auto_suggest_basic("power", subscription_key="")
|
||||
self.assertEqual(type(ex.exception.__cause__), HTTPError)
|
||||
response = auto_suggest_basic("power", subscription_key="")
|
||||
self.assertEqual(response.status_code, 401)
|
||||
|
||||
def test_auto_suggest_response_object_type(self):
|
||||
"""Test that Auto Suggest API returns the correct type hint"""
|
||||
response = auto_suggest_basic(
|
||||
"cute",
|
||||
subscription_key=self.env.get(
|
||||
"BING_SEARCH_V7_AUTO_SUGGEST_SUBSCRIPTION_KEY"
|
||||
),
|
||||
)
|
||||
response = auto_suggest_basic("cute", subscription_key=self.subscription_key)
|
||||
try:
|
||||
self.assertEqual(response.json()["_type"], "Suggestions")
|
||||
except KeyError:
|
||||
|
@ -64,10 +56,7 @@ class AutoSuggestRESTSamplesTest(unittest.TestCase):
|
|||
def test_auto_suggest_response_object_structure(self):
|
||||
"""Test that Auto Suggest API responses follow the correct structure"""
|
||||
response = auto_suggest_basic(
|
||||
"photography",
|
||||
subscription_key=self.env.get(
|
||||
"BING_SEARCH_V7_AUTO_SUGGEST_SUBSCRIPTION_KEY"
|
||||
),
|
||||
"photography", subscription_key=self.subscription_key
|
||||
)
|
||||
response_json = response.json()
|
||||
self.assertIn("suggestionGroups", response_json)
|
||||
|
@ -76,14 +65,18 @@ class AutoSuggestRESTSamplesTest(unittest.TestCase):
|
|||
len(response_json["suggestionGroups"][0]["searchSuggestions"]), 0
|
||||
)
|
||||
|
||||
# https://learn.microsoft.com/en-us/bing/search-apis/bing-autosuggest/reference/response-objects#errorresponse
|
||||
def test_auto_suggest_error_response_object_structure(self):
|
||||
"""Test the structure of the Error Response"""
|
||||
response = auto_suggest_basic("", subscription_key="")
|
||||
try:
|
||||
self.assertEqual(response.json()["_type"], "ErrorResponse")
|
||||
except KeyError:
|
||||
self.fail("The response object type hint is missing")
|
||||
|
||||
def test_auto_suggest_empty_query_trending(self):
|
||||
"""Test that Auto Suggest API returns trending searches when the query is empty"""
|
||||
response = auto_suggest_basic(
|
||||
"",
|
||||
subscription_key=self.env.get(
|
||||
"BING_SEARCH_V7_AUTO_SUGGEST_SUBSCRIPTION_KEY"
|
||||
),
|
||||
)
|
||||
response = auto_suggest_basic("", subscription_key=self.subscription_key)
|
||||
response_json = response.json()
|
||||
self.assertIn("suggestionGroups", response_json)
|
||||
# Non-empty array of suggestions
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
import unittest
|
||||
|
||||
import dotenv
|
||||
from requests import HTTPError, JSONDecodeError
|
||||
from requests import JSONDecodeError
|
||||
|
||||
from samples.rest.bing_custom_search_v7 import custom_search_basic
|
||||
|
||||
|
@ -16,15 +16,22 @@ class CustomSearchRESTSamplesTest(unittest.TestCase):
|
|||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.env = dotenv.dotenv_values()
|
||||
cls.subscription_key = cls.env.get(
|
||||
"BING_SEARCH_V7_CUSTOM_SEARCH_SUBSCRIPTION_KEY"
|
||||
)
|
||||
cls.config_id = cls.env.get("BING_CUSTOM_SEARCH_CONFIG")
|
||||
|
||||
def test_custom_search_subscription_key_not_empty(self):
|
||||
"""Test that the subscription key is defined in the environment"""
|
||||
self.assertIsNotNone(self.subscription_key)
|
||||
self.assertNotEqual(self.subscription_key, "")
|
||||
|
||||
def test_custom_search_basic(self):
|
||||
"""Test the basic REST call to Custom Search API"""
|
||||
response = custom_search_basic(
|
||||
"microsoft",
|
||||
subscription_key=self.env.get(
|
||||
"BING_SEARCH_V7_CUSTOM_SEARCH_SUBSCRIPTION_KEY"
|
||||
),
|
||||
custom_config_id=self.env.get("BING_CUSTOM_SEARCH_CONFIG"),
|
||||
subscription_key=self.subscription_key,
|
||||
custom_config_id=self.config_id,
|
||||
)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
|
@ -32,10 +39,8 @@ class CustomSearchRESTSamplesTest(unittest.TestCase):
|
|||
"""Test that Custom Search API returns responses in JSON format"""
|
||||
response = custom_search_basic(
|
||||
"engine",
|
||||
subscription_key=self.env.get(
|
||||
"BING_SEARCH_V7_CUSTOM_SEARCH_SUBSCRIPTION_KEY"
|
||||
),
|
||||
custom_config_id=self.env.get("BING_CUSTOM_SEARCH_CONFIG"),
|
||||
subscription_key=self.subscription_key,
|
||||
custom_config_id=self.config_id,
|
||||
)
|
||||
try:
|
||||
response.json()
|
||||
|
@ -46,22 +51,17 @@ class CustomSearchRESTSamplesTest(unittest.TestCase):
|
|||
|
||||
def test_custom_search_no_auth(self):
|
||||
"""Test that Custom Search API returns 401 if authorization fails"""
|
||||
with self.assertRaises(Exception) as ex:
|
||||
response = custom_search_basic(
|
||||
"power",
|
||||
subscription_key="",
|
||||
custom_config_id=self.env.get("BING_CUSTOM_SEARCH_CONFIG"),
|
||||
)
|
||||
self.assertEqual(type(ex.exception.__cause__), HTTPError)
|
||||
response = custom_search_basic(
|
||||
"power", subscription_key="", custom_config_id=self.config_id
|
||||
)
|
||||
self.assertEqual(response.status_code, 401)
|
||||
|
||||
def test_custom_search_response_object_type(self):
|
||||
"""Test that Custom Search API returns the correct type hint"""
|
||||
response = custom_search_basic(
|
||||
"new",
|
||||
subscription_key=self.env.get(
|
||||
"BING_SEARCH_V7_CUSTOM_SEARCH_SUBSCRIPTION_KEY"
|
||||
),
|
||||
custom_config_id=self.env.get("BING_CUSTOM_SEARCH_CONFIG"),
|
||||
subscription_key=self.subscription_key,
|
||||
custom_config_id=self.config_id,
|
||||
)
|
||||
try:
|
||||
self.assertEqual(response.json()["_type"], "SearchResponse")
|
||||
|
@ -72,10 +72,8 @@ class CustomSearchRESTSamplesTest(unittest.TestCase):
|
|||
"""Test that Custom Search API responses follow the correct structure"""
|
||||
response = custom_search_basic(
|
||||
"news",
|
||||
subscription_key=self.env.get(
|
||||
"BING_SEARCH_V7_CUSTOM_SEARCH_SUBSCRIPTION_KEY"
|
||||
),
|
||||
custom_config_id=self.env.get("BING_CUSTOM_SEARCH_CONFIG"),
|
||||
subscription_key=self.subscription_key,
|
||||
custom_config_id=self.config_id,
|
||||
)
|
||||
response_json = response.json()
|
||||
self.assertIn("webPages", response_json)
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
import unittest
|
||||
|
||||
import dotenv
|
||||
from requests import HTTPError, JSONDecodeError
|
||||
from requests import JSONDecodeError
|
||||
|
||||
from samples.rest.bing_entity_search_v7 import entity_search_basic
|
||||
|
||||
|
@ -16,24 +16,26 @@ class EntitySearchRESTSamplesTest(unittest.TestCase):
|
|||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.env = dotenv.dotenv_values()
|
||||
cls.subscription_key = cls.env.get(
|
||||
"BING_SEARCH_V7_ENTITY_SEARCH_SUBSCRIPTION_KEY"
|
||||
)
|
||||
|
||||
def test_entity_search_subscription_key_not_empty(self):
|
||||
"""Test that the subscription key is defined in the environment"""
|
||||
self.assertIsNotNone(self.subscription_key)
|
||||
self.assertNotEqual(self.subscription_key, "")
|
||||
|
||||
def test_entity_search_basic(self):
|
||||
"""Test the basic REST call to Entity Search API"""
|
||||
response = entity_search_basic(
|
||||
"alija izetbegović",
|
||||
subscription_key=self.env.get(
|
||||
"BING_SEARCH_V7_ENTITY_SEARCH_SUBSCRIPTION_KEY"
|
||||
),
|
||||
"alija izetbegović", subscription_key=self.subscription_key
|
||||
)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
def test_entity_search_response_is_json(self):
|
||||
"""Test that Entity Search API returns responses in JSON format"""
|
||||
response = entity_search_basic(
|
||||
"Abu Hayyan",
|
||||
subscription_key=self.env.get(
|
||||
"BING_SEARCH_V7_ENTITY_SEARCH_SUBSCRIPTION_KEY"
|
||||
),
|
||||
"Abu Hayyan", subscription_key=self.subscription_key
|
||||
)
|
||||
try:
|
||||
response.json()
|
||||
|
@ -44,17 +46,13 @@ class EntitySearchRESTSamplesTest(unittest.TestCase):
|
|||
|
||||
def test_entity_search_no_auth(self):
|
||||
"""Test that Entity Search API returns 401 if authorization fails"""
|
||||
with self.assertRaises(Exception) as ex:
|
||||
response = entity_search_basic("Egypt", subscription_key="")
|
||||
self.assertEqual(type(ex.exception.__cause__), HTTPError)
|
||||
response = entity_search_basic("Egypt", subscription_key="")
|
||||
self.assertEqual(response.status_code, 401)
|
||||
|
||||
def test_entity_search_response_object_type(self):
|
||||
"""Test that Entity Search API returns the correct type hint"""
|
||||
response = entity_search_basic(
|
||||
"Ghiyath al-Din Muhammad",
|
||||
subscription_key=self.env.get(
|
||||
"BING_SEARCH_V7_ENTITY_SEARCH_SUBSCRIPTION_KEY"
|
||||
),
|
||||
"Ghiyath al-Din Muhammad", subscription_key=self.subscription_key
|
||||
)
|
||||
try:
|
||||
self.assertEqual(response.json()["_type"], "SearchResponse")
|
||||
|
@ -63,12 +61,7 @@ class EntitySearchRESTSamplesTest(unittest.TestCase):
|
|||
|
||||
def test_entity_search_response_object_structure(self):
|
||||
"""Test that Entity Search API responses follow the correct structure"""
|
||||
response = entity_search_basic(
|
||||
"Arab",
|
||||
subscription_key=self.env.get(
|
||||
"BING_SEARCH_V7_ENTITY_SEARCH_SUBSCRIPTION_KEY"
|
||||
),
|
||||
)
|
||||
response = entity_search_basic("Arab", subscription_key=self.subscription_key)
|
||||
response_json = response.json()
|
||||
self.assertIn("entities", response_json)
|
||||
self.assertIn("rankingResponse", response_json)
|
||||
|
@ -77,20 +70,20 @@ class EntitySearchRESTSamplesTest(unittest.TestCase):
|
|||
except KeyError:
|
||||
self.fail("The response object doesn't include any entity results")
|
||||
|
||||
# https://learn.microsoft.com/en-us/bing/search-apis/bing-entity-search/reference/query-parameters
|
||||
def test_entity_search_required_parameter_query(self):
|
||||
"""Test that Entity Search API returns an error if required parameter is empty"""
|
||||
response = entity_search_basic(
|
||||
"",
|
||||
subscription_key=self.env.get(
|
||||
"BING_SEARCH_V7_ENTITY_SEARCH_SUBSCRIPTION_KEY"
|
||||
),
|
||||
)
|
||||
"""Test that Entity Search API returns an error if a required parameter is missing"""
|
||||
response = entity_search_basic(subscription_key=self.subscription_key, query="")
|
||||
self.assertEqual(response.status_code, 400)
|
||||
|
||||
# https://learn.microsoft.com/en-us/bing/search-apis/bing-entity-search/reference/response-objects#errorresponse
|
||||
def test_entity_search_error_response_object_structure(self):
|
||||
"""Test the structure of the Error Response"""
|
||||
response = entity_search_basic("", "")
|
||||
try:
|
||||
self.assertEqual(response.json()["_type"], "ErrorResponse")
|
||||
except KeyError:
|
||||
self.fail(
|
||||
"The response object is not of type 'ErrorResponse' when the query is empty"
|
||||
)
|
||||
self.fail("The response object is not of type 'ErrorResponse'")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
import unittest
|
||||
|
||||
import dotenv
|
||||
from requests import HTTPError, JSONDecodeError
|
||||
from requests import JSONDecodeError
|
||||
|
||||
from samples.rest.bing_image_search_v7 import image_search_basic
|
||||
|
||||
|
@ -16,25 +16,23 @@ class ImageSearchRESTSamplesTest(unittest.TestCase):
|
|||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.env = dotenv.dotenv_values()
|
||||
cls.subscription_key = cls.env.get(
|
||||
"BING_SEARCH_V7_IMAGE_SEARCH_SUBSCRIPTION_KEY"
|
||||
)
|
||||
|
||||
def test_image_search_subscription_key_not_empty(self):
|
||||
"""Test that the subscription key is defined in the environment"""
|
||||
self.assertIsNotNone(self.subscription_key)
|
||||
self.assertNotEqual(self.subscription_key, "")
|
||||
|
||||
def test_image_search_basic(self):
|
||||
"""Test the basic REST call to Image Search API"""
|
||||
response = image_search_basic(
|
||||
"flowers",
|
||||
subscription_key=self.env.get(
|
||||
"BING_SEARCH_V7_IMAGE_SEARCH_SUBSCRIPTION_KEY"
|
||||
),
|
||||
)
|
||||
response = image_search_basic("flowers", subscription_key=self.subscription_key)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
def test_image_search_response_is_json(self):
|
||||
"""Test that Image Search API returns responses in JSON format"""
|
||||
response = image_search_basic(
|
||||
"nature",
|
||||
subscription_key=self.env.get(
|
||||
"BING_SEARCH_V7_IMAGE_SEARCH_SUBSCRIPTION_KEY"
|
||||
),
|
||||
)
|
||||
response = image_search_basic("nature", subscription_key=self.subscription_key)
|
||||
try:
|
||||
response.json()
|
||||
except JSONDecodeError:
|
||||
|
@ -44,18 +42,12 @@ class ImageSearchRESTSamplesTest(unittest.TestCase):
|
|||
|
||||
def test_image_search_no_auth(self):
|
||||
"""Test that Image Search API returns 401 if authorization fails"""
|
||||
with self.assertRaises(Exception) as ex:
|
||||
response = image_search_basic("mountain", subscription_key="")
|
||||
self.assertEqual(type(ex.exception.__cause__), HTTPError)
|
||||
response = image_search_basic("mountain", subscription_key="")
|
||||
self.assertEqual(response.status_code, 401)
|
||||
|
||||
def test_image_search_response_object_type(self):
|
||||
"""Test that Image Search API returns the correct type hint"""
|
||||
response = image_search_basic(
|
||||
"river",
|
||||
subscription_key=self.env.get(
|
||||
"BING_SEARCH_V7_IMAGE_SEARCH_SUBSCRIPTION_KEY"
|
||||
),
|
||||
)
|
||||
response = image_search_basic("river", subscription_key=self.subscription_key)
|
||||
try:
|
||||
self.assertEqual(response.json()["_type"], "Images")
|
||||
except KeyError:
|
||||
|
@ -64,10 +56,7 @@ class ImageSearchRESTSamplesTest(unittest.TestCase):
|
|||
def test_image_search_response_object_structure(self):
|
||||
"""Test that Image Search API responses follow the correct structure"""
|
||||
response = image_search_basic(
|
||||
"rainbow ray",
|
||||
subscription_key=self.env.get(
|
||||
"BING_SEARCH_V7_IMAGE_SEARCH_SUBSCRIPTION_KEY"
|
||||
),
|
||||
"rainbow ray", subscription_key=self.subscription_key
|
||||
)
|
||||
response_json = response.json()
|
||||
self.assertGreater(response_json["totalEstimatedMatches"], 0)
|
||||
|
@ -76,20 +65,15 @@ class ImageSearchRESTSamplesTest(unittest.TestCase):
|
|||
except KeyError:
|
||||
self.fail("The response object doesn't include any image results")
|
||||
|
||||
def test_image_search_required_parameter_query(self):
|
||||
"""Test that Image Search API returns an error if required parameter is empty"""
|
||||
response = image_search_basic(
|
||||
subscription_key=self.env.get(
|
||||
"BING_SEARCH_V7_IMAGE_SEARCH_SUBSCRIPTION_KEY"
|
||||
),
|
||||
query="",
|
||||
)
|
||||
try:
|
||||
self.assertEqual(response.json()["_type"], "ErrorResponse")
|
||||
except KeyError:
|
||||
self.fail(
|
||||
"The response object is not of type 'ErrorResponse' when the query is empty"
|
||||
)
|
||||
def test_image_search_missing_required_parameter(self):
|
||||
"""Test that Image Search API returns an error with the right structure
|
||||
if a required parameter is missing/empty"""
|
||||
response = image_search_basic(subscription_key=self.subscription_key, query="")
|
||||
# Status Code
|
||||
self.assertEqual(response.status_code, 400)
|
||||
# Structure
|
||||
self.assertIn("_type", response.json())
|
||||
self.assertEqual(response.json()["_type"], "ErrorResponse")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
import unittest
|
||||
|
||||
import dotenv
|
||||
from requests import HTTPError, JSONDecodeError
|
||||
from requests import JSONDecodeError
|
||||
|
||||
from samples.rest.bing_news_search_v7 import news_search_basic
|
||||
|
||||
|
@ -16,25 +16,25 @@ class NewsSearchRESTSamplesTest(unittest.TestCase):
|
|||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.env = dotenv.dotenv_values()
|
||||
cls.subscription_key = cls.env.get(
|
||||
"BING_SEARCH_V7_NEWS_SEARCH_SUBSCRIPTION_KEY"
|
||||
)
|
||||
|
||||
def test_news_search_subscription_key_not_empty(self):
|
||||
"""Test that the subscription key is defined in the environment"""
|
||||
self.assertIsNotNone(self.subscription_key)
|
||||
self.assertNotEqual(self.subscription_key, "")
|
||||
|
||||
def test_news_search_basic(self):
|
||||
"""Test the basic REST call to News Search API"""
|
||||
response = news_search_basic(
|
||||
"microsoft",
|
||||
subscription_key=self.env.get(
|
||||
"BING_SEARCH_V7_NEWS_SEARCH_SUBSCRIPTION_KEY"
|
||||
),
|
||||
"microsoft", subscription_key=self.subscription_key
|
||||
)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
def test_news_search_response_is_json(self):
|
||||
"""Test that News Search API returns responses in JSON format"""
|
||||
response = news_search_basic(
|
||||
"ai",
|
||||
subscription_key=self.env.get(
|
||||
"BING_SEARCH_V7_NEWS_SEARCH_SUBSCRIPTION_KEY"
|
||||
),
|
||||
)
|
||||
response = news_search_basic("ai", subscription_key=self.subscription_key)
|
||||
try:
|
||||
response.json()
|
||||
except JSONDecodeError:
|
||||
|
@ -44,18 +44,12 @@ class NewsSearchRESTSamplesTest(unittest.TestCase):
|
|||
|
||||
def test_news_search_no_auth(self):
|
||||
"""Test that News Search API returns 401 if authorization fails"""
|
||||
with self.assertRaises(Exception) as ex:
|
||||
response = news_search_basic("copilot", subscription_key="")
|
||||
self.assertEqual(type(ex.exception.__cause__), HTTPError)
|
||||
response = news_search_basic("copilot", subscription_key="")
|
||||
self.assertEqual(response.status_code, 401)
|
||||
|
||||
def test_news_search_response_object_type(self):
|
||||
"""Test that News Search API returns the correct type hint"""
|
||||
response = news_search_basic(
|
||||
"azure",
|
||||
subscription_key=self.env.get(
|
||||
"BING_SEARCH_V7_NEWS_SEARCH_SUBSCRIPTION_KEY"
|
||||
),
|
||||
)
|
||||
response = news_search_basic("azure", subscription_key=self.subscription_key)
|
||||
try:
|
||||
self.assertEqual(response.json()["_type"], "News")
|
||||
except KeyError:
|
||||
|
@ -63,25 +57,22 @@ class NewsSearchRESTSamplesTest(unittest.TestCase):
|
|||
|
||||
def test_news_search_response_object_structure(self):
|
||||
"""Test that News Search API responses follow the correct structure"""
|
||||
response = news_search_basic(
|
||||
"vim",
|
||||
subscription_key=self.env.get(
|
||||
"BING_SEARCH_V7_NEWS_SEARCH_SUBSCRIPTION_KEY"
|
||||
),
|
||||
)
|
||||
response = news_search_basic("vim", subscription_key=self.subscription_key)
|
||||
response_json = response.json()
|
||||
self.assertTrue(response_json["totalEstimatedMatches"] > 0)
|
||||
self.assertGreater(len(response_json["value"]), 0)
|
||||
|
||||
# https://learn.microsoft.com/en-us/bing/search-apis/bing-news-search/reference/query-parameters
|
||||
def test_news_search_trending_using_empty_query(self):
|
||||
"""Test that News Search API returns trending stories if the query is empty"""
|
||||
response = news_search_basic(
|
||||
subscription_key=self.env.get(
|
||||
"BING_SEARCH_V7_NEWS_SEARCH_SUBSCRIPTION_KEY"
|
||||
),
|
||||
query="",
|
||||
)
|
||||
self.assertGreater(len(response.json()["value"]), 0)
|
||||
response = news_search_basic(query="", subscription_key=self.subscription_key)
|
||||
self.assertTrue(response.ok)
|
||||
try:
|
||||
self.assertGreater(len(response.json()["value"]), 0)
|
||||
except JSONDecodeError:
|
||||
self.fail("The response is not JSON")
|
||||
except (KeyError, TypeError):
|
||||
self.fail("The response doesn't have the correct structure")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
import unittest
|
||||
|
||||
import dotenv
|
||||
from requests import HTTPError, JSONDecodeError
|
||||
from requests import JSONDecodeError
|
||||
|
||||
from samples.rest.bing_spell_check_v7 import spell_check_basic
|
||||
|
||||
|
@ -16,24 +16,26 @@ class SpellCheckRESTSamplesTest(unittest.TestCase):
|
|||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.env = dotenv.dotenv_values()
|
||||
cls.subscription_key = cls.env.get(
|
||||
"BING_SEARCH_V7_SPELL_CHECK_SUBSCRIPTION_KEY"
|
||||
)
|
||||
|
||||
def test_spell_check_subscription_key_not_empty(self):
|
||||
"""Test that the subscription key is defined in the environment"""
|
||||
self.assertIsNotNone(self.subscription_key)
|
||||
self.assertNotEqual(self.subscription_key, "")
|
||||
|
||||
def test_spell_check_basic(self):
|
||||
"""Test the basic REST call to Spell Check API"""
|
||||
response = spell_check_basic(
|
||||
"helo wordl",
|
||||
subscription_key=self.env.get(
|
||||
"BING_SEARCH_V7_SPELL_CHECK_SUBSCRIPTION_KEY"
|
||||
),
|
||||
"helo wordl", subscription_key=self.subscription_key
|
||||
)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
def test_spell_check_response_is_json(self):
|
||||
"""Test that Spell Check API returns responses in JSON format"""
|
||||
response = spell_check_basic(
|
||||
"mine pizza",
|
||||
subscription_key=self.env.get(
|
||||
"BING_SEARCH_V7_SPELL_CHECK_SUBSCRIPTION_KEY"
|
||||
),
|
||||
"mine pizza", subscription_key=self.subscription_key
|
||||
)
|
||||
try:
|
||||
response.json()
|
||||
|
@ -44,17 +46,13 @@ class SpellCheckRESTSamplesTest(unittest.TestCase):
|
|||
|
||||
def test_spell_check_no_auth(self):
|
||||
"""Test that Spell Check API returns 401 if authorization fails"""
|
||||
with self.assertRaises(Exception) as ex:
|
||||
response = spell_check_basic("give me this this", subscription_key="")
|
||||
self.assertEqual(type(ex.exception.__cause__), HTTPError)
|
||||
response = spell_check_basic("give me this this", subscription_key="")
|
||||
self.assertEqual(response.status_code, 401)
|
||||
|
||||
def test_spell_check_response_object_type(self):
|
||||
"""Test that Spell Check API returns the correct type hint"""
|
||||
response = spell_check_basic(
|
||||
"best top laptop",
|
||||
subscription_key=self.env.get(
|
||||
"BING_SEARCH_V7_SPELL_CHECK_SUBSCRIPTION_KEY"
|
||||
),
|
||||
"best top laptop", subscription_key=self.subscription_key
|
||||
)
|
||||
try:
|
||||
self.assertEqual(response.json()["_type"], "SpellCheck")
|
||||
|
@ -64,24 +62,24 @@ class SpellCheckRESTSamplesTest(unittest.TestCase):
|
|||
def test_spell_check_response_object_structure(self):
|
||||
"""Test that Spell Check API responses follow the correct structure"""
|
||||
response = spell_check_basic(
|
||||
"widnows lapyop",
|
||||
subscription_key=self.env.get(
|
||||
"BING_SEARCH_V7_SPELL_CHECK_SUBSCRIPTION_KEY"
|
||||
),
|
||||
"widnows lapyop", subscription_key=self.subscription_key
|
||||
)
|
||||
response_json = response.json()
|
||||
self.assertIn("flaggedTokens", response_json)
|
||||
|
||||
def test_spell_check_required_parameter_query(self):
|
||||
"""Test that Entity Search API returns an error if required parameter is empty"""
|
||||
with self.assertRaises(Exception) as ex:
|
||||
response = spell_check_basic(
|
||||
subscription_key=self.env.get(
|
||||
"BING_SEARCH_V7_SPELL_CHECK_SUBSCRIPTION_KEY"
|
||||
),
|
||||
query="",
|
||||
)
|
||||
self.assertEqual(type(ex.exception.__cause__), HTTPError)
|
||||
"""Test that Spell Check API returns an error if a required parameter is missing"""
|
||||
response = spell_check_basic(query="", subscription_key=self.subscription_key)
|
||||
self.assertEqual(response.status_code, 400)
|
||||
|
||||
# https://learn.microsoft.com/en-us/bing/search-apis/bing-spell-check/reference/response-objects#errorresponse
|
||||
def test_spell_check_error_response_object_structure(self):
|
||||
"""Test the structure of the Error Response"""
|
||||
response = spell_check_basic("", "")
|
||||
try:
|
||||
self.assertEqual(response.json()["_type"], "ErrorResponse")
|
||||
except KeyError:
|
||||
self.fail("The response object type hint is missing")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
import unittest
|
||||
|
||||
import dotenv
|
||||
from requests import HTTPError, JSONDecodeError
|
||||
from requests import JSONDecodeError
|
||||
|
||||
from samples.rest.bing_video_search_v7 import video_search_basic
|
||||
|
||||
|
@ -16,24 +16,24 @@ class VideoSearchRESTSamplesTest(unittest.TestCase):
|
|||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.env = dotenv.dotenv_values()
|
||||
cls.subscription_key = cls.env.get(
|
||||
"BING_SEARCH_V7_VIDEO_SEARCH_SUBSCRIPTION_KEY"
|
||||
)
|
||||
|
||||
def test_video_search_subscription_key_not_empty(self):
|
||||
"""Test that the subscription key is defined in the environment"""
|
||||
self.assertIsNotNone(self.subscription_key)
|
||||
self.assertNotEqual(self.subscription_key, "")
|
||||
|
||||
def test_video_search_basic(self):
|
||||
"""Test the basic REST call to Video Search API"""
|
||||
response = video_search_basic(
|
||||
"nature",
|
||||
subscription_key=self.env.get(
|
||||
"BING_SEARCH_V7_VIDEO_SEARCH_SUBSCRIPTION_KEY"
|
||||
),
|
||||
)
|
||||
response = video_search_basic("nature", subscription_key=self.subscription_key)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
def test_video_search_response_is_json(self):
|
||||
"""Test that Video Search API returns responses in JSON format"""
|
||||
response = video_search_basic(
|
||||
"vim showcase",
|
||||
subscription_key=self.env.get(
|
||||
"BING_SEARCH_V7_VIDEO_SEARCH_SUBSCRIPTION_KEY"
|
||||
),
|
||||
"vim showcase", subscription_key=self.subscription_key
|
||||
)
|
||||
try:
|
||||
response.json()
|
||||
|
@ -44,17 +44,13 @@ class VideoSearchRESTSamplesTest(unittest.TestCase):
|
|||
|
||||
def test_video_search_no_auth(self):
|
||||
"""Test that Video Search API returns 401 if authorization fails"""
|
||||
with self.assertRaises(Exception) as ex:
|
||||
response = video_search_basic("kentucky derby", subscription_key="")
|
||||
self.assertEqual(type(ex.exception.__cause__), HTTPError)
|
||||
response = video_search_basic("kentucky derby", subscription_key="")
|
||||
self.assertEqual(response.status_code, 401)
|
||||
|
||||
def test_video_search_response_object_type(self):
|
||||
"""Test that Video Search API returns the correct type hint"""
|
||||
response = video_search_basic(
|
||||
"car relaxing drive",
|
||||
subscription_key=self.env.get(
|
||||
"BING_SEARCH_V7_VIDEO_SEARCH_SUBSCRIPTION_KEY"
|
||||
),
|
||||
"car relaxing drive", subscription_key=self.subscription_key
|
||||
)
|
||||
try:
|
||||
self.assertEqual(response.json()["_type"], "Videos")
|
||||
|
@ -64,10 +60,7 @@ class VideoSearchRESTSamplesTest(unittest.TestCase):
|
|||
def test_video_search_response_object_structure(self):
|
||||
"""Test that Video Search API responses follow the correct structure"""
|
||||
response = video_search_basic(
|
||||
"how to download Teams",
|
||||
subscription_key=self.env.get(
|
||||
"BING_SEARCH_V7_VIDEO_SEARCH_SUBSCRIPTION_KEY"
|
||||
),
|
||||
"how to download Teams", subscription_key=self.subscription_key
|
||||
)
|
||||
response_json = response.json()
|
||||
self.assertGreater(response_json["totalEstimatedMatches"], 0)
|
||||
|
@ -77,19 +70,19 @@ class VideoSearchRESTSamplesTest(unittest.TestCase):
|
|||
self.fail("The response object doesn't include any video results")
|
||||
|
||||
def test_video_search_required_parameter_query(self):
|
||||
"""Test that Video Search API returns an error if required parameter is empty"""
|
||||
response = video_search_basic(
|
||||
subscription_key=self.env.get(
|
||||
"BING_SEARCH_V7_VIDEO_SEARCH_SUBSCRIPTION_KEY"
|
||||
),
|
||||
query="",
|
||||
)
|
||||
"""Test that Video Search API returns an error if a required parameter is missing"""
|
||||
response = video_search_basic(subscription_key=self.subscription_key, query="")
|
||||
self.assertEqual(response.status_code, 400)
|
||||
|
||||
# pylint: disable=line-too-long
|
||||
# learn.microsoft.com/en-us/bing/search-apis/bing-video-search/reference/response-objects#errorresponse
|
||||
def test_video_search_error_response_object_structure(self):
|
||||
"""Test the structure of the Error Response"""
|
||||
response = video_search_basic("", "")
|
||||
try:
|
||||
self.assertEqual(response.json()["_type"], "ErrorResponse")
|
||||
except KeyError:
|
||||
self.fail(
|
||||
"The response object is not of type 'ErrorResponse' when the query is empty"
|
||||
)
|
||||
self.fail("The response object type hint is missing")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
import unittest
|
||||
|
||||
import dotenv
|
||||
from requests import HTTPError, JSONDecodeError
|
||||
from requests import JSONDecodeError
|
||||
|
||||
from samples.rest.bing_visual_search_v7 import visual_search_basic
|
||||
|
||||
|
@ -16,24 +16,26 @@ class VisualSearchRESTSamplesTest(unittest.TestCase):
|
|||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.env = dotenv.dotenv_values()
|
||||
cls.subscription_key = cls.env.get(
|
||||
"BING_SEARCH_V7_VISUAL_SEARCH_SUBSCRIPTION_KEY"
|
||||
)
|
||||
|
||||
def test_visual_search_subscription_key_not_empty(self):
|
||||
"""Test that the subscription key is defined in the environment"""
|
||||
self.assertIsNotNone(self.subscription_key)
|
||||
self.assertNotEqual(self.subscription_key, "")
|
||||
|
||||
def test_visual_search_basic(self):
|
||||
"""Test the basic REST call to Visual Search API"""
|
||||
response = visual_search_basic(
|
||||
"./my_image.jpg",
|
||||
subscription_key=self.env.get(
|
||||
"BING_SEARCH_V7_VISUAL_SEARCH_SUBSCRIPTION_KEY"
|
||||
),
|
||||
"./my_image.jpg", subscription_key=self.subscription_key
|
||||
)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
def test_visual_search_response_is_json(self):
|
||||
"""Test that Visual Search API returns responses in JSON format"""
|
||||
response = visual_search_basic(
|
||||
"./my_image.jpg",
|
||||
subscription_key=self.env.get(
|
||||
"BING_SEARCH_V7_VISUAL_SEARCH_SUBSCRIPTION_KEY"
|
||||
),
|
||||
"./my_image.jpg", subscription_key=self.subscription_key
|
||||
)
|
||||
try:
|
||||
response.json()
|
||||
|
@ -44,17 +46,13 @@ class VisualSearchRESTSamplesTest(unittest.TestCase):
|
|||
|
||||
def test_visual_search_no_auth(self):
|
||||
"""Test that Visual Search API returns 401 if authorization fails"""
|
||||
with self.assertRaises(Exception) as ex:
|
||||
response = visual_search_basic("./my_image.jpg", subscription_key="")
|
||||
self.assertEqual(type(ex.exception.__cause__), HTTPError)
|
||||
response = visual_search_basic("./my_image.jpg", subscription_key="")
|
||||
self.assertEqual(response.status_code, 401)
|
||||
|
||||
def test_visual_search_response_object_type(self):
|
||||
"""Test that Visual Search API returns the correct type hint"""
|
||||
response = visual_search_basic(
|
||||
"./my_image.jpg",
|
||||
subscription_key=self.env.get(
|
||||
"BING_SEARCH_V7_VISUAL_SEARCH_SUBSCRIPTION_KEY"
|
||||
),
|
||||
"./my_image.jpg", subscription_key=self.subscription_key
|
||||
)
|
||||
try:
|
||||
self.assertEqual(response.json()["_type"], "ImageKnowledge")
|
||||
|
@ -64,10 +62,7 @@ class VisualSearchRESTSamplesTest(unittest.TestCase):
|
|||
def test_visual_search_response_object_structure(self):
|
||||
"""Test that Visual Search API responses follow the correct structure"""
|
||||
response = visual_search_basic(
|
||||
"./my_image.jpg",
|
||||
subscription_key=self.env.get(
|
||||
"BING_SEARCH_V7_VISUAL_SEARCH_SUBSCRIPTION_KEY"
|
||||
),
|
||||
"./my_image.jpg", subscription_key=self.subscription_key
|
||||
)
|
||||
response_json = response.json()
|
||||
self.assertIn("image", response_json)
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
import unittest
|
||||
|
||||
import dotenv
|
||||
from requests import HTTPError, JSONDecodeError
|
||||
from requests import JSONDecodeError
|
||||
|
||||
from samples.rest.bing_web_search_v7 import web_search_basic
|
||||
|
||||
|
@ -16,20 +16,22 @@ class WebSearchRESTSamplesTest(unittest.TestCase):
|
|||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.env = dotenv.dotenv_values()
|
||||
cls.subscription_key = cls.env.get("BING_SEARCH_V7_WEB_SEARCH_SUBSCRIPTION_KEY")
|
||||
|
||||
def test_web_search_subscription_key_not_empty(self):
|
||||
"""Test that the subscription key is defined in the environment"""
|
||||
self.assertIsNotNone(self.subscription_key)
|
||||
self.assertNotEqual(self.subscription_key, "")
|
||||
|
||||
def test_web_search_basic(self):
|
||||
"""Test the basic REST call to Web Search API"""
|
||||
response = web_search_basic(
|
||||
"vim",
|
||||
subscription_key=self.env.get("BING_SEARCH_V7_WEB_SEARCH_SUBSCRIPTION_KEY"),
|
||||
)
|
||||
response = web_search_basic("vim", subscription_key=self.subscription_key)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
def test_web_search_response_is_json(self):
|
||||
"""Test that Web Search API returns responses in JSON format"""
|
||||
response = web_search_basic(
|
||||
"copilot news",
|
||||
subscription_key=self.env.get("BING_SEARCH_V7_WEB_SEARCH_SUBSCRIPTION_KEY"),
|
||||
"copilot news", subscription_key=self.subscription_key
|
||||
)
|
||||
try:
|
||||
response.json()
|
||||
|
@ -40,15 +42,13 @@ class WebSearchRESTSamplesTest(unittest.TestCase):
|
|||
|
||||
def test_web_search_no_auth(self):
|
||||
"""Test that Web Search API returns 401 if authorization fails"""
|
||||
with self.assertRaises(Exception) as ex:
|
||||
response = web_search_basic("python crash course", subscription_key="")
|
||||
self.assertEqual(type(ex.exception.__cause__), HTTPError)
|
||||
response = web_search_basic("python crash course", subscription_key="")
|
||||
self.assertEqual(response.status_code, 401)
|
||||
|
||||
def test_web_search_response_object_type(self):
|
||||
"""Test that Web Search API returns the correct type hint"""
|
||||
response = web_search_basic(
|
||||
"root of pi",
|
||||
subscription_key=self.env.get("BING_SEARCH_V7_WEB_SEARCH_SUBSCRIPTION_KEY"),
|
||||
"root of pi", subscription_key=self.subscription_key
|
||||
)
|
||||
try:
|
||||
self.assertEqual(response.json()["_type"], "SearchResponse")
|
||||
|
@ -58,8 +58,7 @@ class WebSearchRESTSamplesTest(unittest.TestCase):
|
|||
def test_web_search_response_object_structure(self):
|
||||
"""Test that Web Search API responses follow the correct structure"""
|
||||
response = web_search_basic(
|
||||
"fhd license-free wallpaper",
|
||||
subscription_key=self.env.get("BING_SEARCH_V7_WEB_SEARCH_SUBSCRIPTION_KEY"),
|
||||
"fhd license-free wallpaper", subscription_key=self.subscription_key
|
||||
)
|
||||
response_json = response.json()
|
||||
self.assertIn("webPages", response_json)
|
||||
|
@ -70,8 +69,7 @@ class WebSearchRESTSamplesTest(unittest.TestCase):
|
|||
def test_web_search_computation(self):
|
||||
"""Test that Web Search API returns relevant Computation results"""
|
||||
response = web_search_basic(
|
||||
subscription_key=self.env.get("BING_SEARCH_V7_WEB_SEARCH_SUBSCRIPTION_KEY"),
|
||||
query="289*12 - 492/41",
|
||||
subscription_key=self.subscription_key, query="289*12 - 492/41"
|
||||
)
|
||||
self.assertIn("computation", response.json())
|
||||
self.assertEqual(3456, int(float(response.json()["computation"]["value"])))
|
||||
|
|
Загрузка…
Ссылка в новой задаче