-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathautosuggest_samples.py
74 lines (56 loc) · 2.42 KB
/
autosuggest_samples.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
from autosuggest_client import AutoSuggestClient
from autosuggest_client.models import (
Suggestions,
SuggestionsSuggestionGroup,
SearchAction,
ErrorResponse,
)
from azure.core.credentials import AzureKeyCredential
SUBSCRIPTION_KEY = None
ENDPOINT = "https://api.bing.microsoft.com" + "/v7.0/"
def autosuggest_lookup(subscription_key):
"""AutoSuggestLookup.
This will look up a single query (Xbox) and print out name and url for first web result.
"""
client = AutoSuggestClient(
endpoint=ENDPOINT, credential=AzureKeyCredential(SUBSCRIPTION_KEY)
)
try:
suggestions = client.auto_suggest(query="Satya Nadella") # type: Suggestions
if suggestions.suggestion_groups:
print('Searched for "Satya Nadella" and found suggestions:')
suggestion_group = suggestions.suggestion_groups[
0
] # type: SuggestionsSuggestionGroup
for suggestion in suggestion_group.search_suggestions: # type: SearchAction
print("....................................")
print(suggestion.query)
print(suggestion.display_text)
print(suggestion.url)
print(suggestion.search_kind)
else:
print("Didn't see any suggestion..")
except Exception as err:
print("Encountered exception. {}".format(err))
def error(subscription_key):
"""Error.
This triggers a bad request and shows how to read the error response.
"""
# Breaking the subscription key on purpose
client = AutoSuggestClient(
credential=AzureKeyCredential(SUBSCRIPTION_KEY + "1"), endpoint=ENDPOINT
)
try:
suggestions = client.auto_suggest(query="Satya Nadella", market="no-ty")
except Exception as err:
# The status code of the error should be a good indication of what occurred. However, if you'd like more details, you can dig into the response.
# Please note that depending on the type of error, the response schema might be different, so you aren't guaranteed a specific error response schema.
print("Exception occurred, with reason {}.\n".format(err))
if __name__ == "__main__":
import sys
import os.path
sys.path.append(os.path.abspath(os.path.join(__file__, "..", "..")))
autosuggest_lookup(SUBSCRIPTION_KEY)
error(SUBSCRIPTION_KEY)