Azure / azure-sdk-for-python

This repository is for active development of the Azure SDK for Python. For consumers of the SDK we recommend visiting our public developer docs at https://learn.microsoft.com/python/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-python.
MIT License
4.54k stars 2.77k forks source link

[Autosuggest] Autosuggest method path is only '/Suggestions'; should be '/bing/v7.0/Suggestions' #6472

Closed v-jaswel closed 4 years ago

v-jaswel commented 5 years ago

Describe the bug The path for the autosuggest method is '/Suggestions' when it should be '/bing/v7.0/Suggestions'. As a result, requests to the service return 404 unless the user appends '/bing/v7.0' to their endpoint.

Exception or Stack Trace

Traceback (most recent call last):
  File "auto.py", line 27, in <module>
    result = client.auto_suggest('xb')
  File "C:\Program Files (x86)\Python\Lib\site-packages\azure\cognitiveservices\search\autosuggest\auto_suggest_search_api.py", line 316, in auto_suggest
    raise models.ErrorResponseException(self._deserialize, response)
azure.cognitiveservices.search.autosuggest.models.error_response_py3.ErrorResponseException: Operation returned an invalid status code 'Resource Not Found'

The target URL is https://westus.api.cognitive.microsoft.com/Suggestions?mkt=en-us&q=xb, as verified by a debug statement inserted at auto_suggest_search_api.py line 309:

# Construct and send request
request = self._client.get(url, query_parameters)
print(request.url)
response = self._client.send(request, header_parameters, stream=False, **operation_config)

To Reproduce

  1. Get a Bing Autosuggest subscription key.
  2. Set the environment variable AUTOSUGGEST_SUBSCRIPTION_KEY to your subscription key.
  3. Set the environment variable AUTOSUGGEST_ENDPOINT to the region for your subscription key (example: westus).
  4. Install Python 3.
  5. Install the package azure-cognitiveservices-search_autosuggest.
  6. Run the following code.

Code Snippet

from azure.cognitiveservices.search.autosuggest import AutoSuggestSearchAPI
from msrest.authentication import CognitiveServicesCredentials

import json, os, sys

key_var_name = 'AUTOSUGGEST_SUBSCRIPTION_KEY'
if not key_var_name in os.environ:
    raise Exception('Please set/export the environment variable: {}'.format(key_var_name))
subscription_key = os.environ[key_var_name]

endpoint_var_name = 'AUTOSUGGEST_ENDPOINT'
if not endpoint_var_name in os.environ:
    raise Exception('Please set/export the environment variable: {}'.format(endpoint_var_name))
endpoint = os.environ[endpoint_var_name]

# Instantiate a Bing Autosuggest client
#endpoint = endpoint + "/bing/v7.0"
client = AutoSuggestSearchAPI(CognitiveServicesCredentials(subscription_key), endpoint)

result = client.auto_suggest('xb')
print(result.suggestion_groups[0].search_suggestions[0].query)
print(result.suggestion_groups[0].search_suggestions[0].display_text)

Expected behavior Expected output:

https://westus.api.cognitive.microsoft.com/bing/v7.0/Suggestions?mkt=en-us&q=xb
xbox
xbox

To fix the error, uncomment the following line in the code snippet:

endpoint = endpoint + "/bing/v7.0"

Additional context None

maggiepint commented 5 years ago

Thanks for the feedback. Routing to appropriate team for follow-up

wiazur commented 5 years ago

Hi @maggiepint any progress on this issue? Thanks much.

wiazur commented 4 years ago

I was looking into this bug again and it appears the problem is in the auto_suggest_search_api.py file. Redoing the very last line, fixes this issue (even though it sounds a different problem than what v-jaswel described). Here is the modification (replace the region with user region or custom domain):

#auto_suggest.metadata = {'url': '/suggestions/'}
auto_suggest.metadata = { 'url': 'https://westus.cognitiveservices.azure.com/bing/v7.0/suggestions/'}

I included the old line to show the diff. This is hardcoded though, so a better fix is needed. But just to illustrate where the bug is.

Using Autosuggest in Python is not useable until this gets fixed. Thanks.

ghost commented 4 years ago

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @jaggerbodas-ms, @arwong.

v-jaswel commented 4 years ago

Closing as fixed. The following updated code snippet runs as expected.

from azure.cognitiveservices.search.autosuggest import AutoSuggestClient
from msrest.authentication import CognitiveServicesCredentials

import json, os, sys

key_var_name = 'AUTOSUGGEST_SUBSCRIPTION_KEY'
if not key_var_name in os.environ:
    raise Exception('Please set/export the environment variable: {}'.format(key_var_name))
subscription_key = os.environ[key_var_name]

endpoint_var_name = 'AUTOSUGGEST_ENDPOINT'
if not endpoint_var_name in os.environ:
    raise Exception('Please set/export the environment variable: {}'.format(endpoint_var_name))
endpoint = os.environ[endpoint_var_name]

# Instantiate a Bing Autosuggest client
#endpoint = endpoint + "/bing/v7.0"
client = AutoSuggestClient(endpoint, CognitiveServicesCredentials(subscription_key))

result = client.auto_suggest('xb')
print(result.suggestion_groups[0].search_suggestions[0].query)
print(result.suggestion_groups[0].search_suggestions[0].display_text)

Output

xbox
xbox