Azure / azure-sdk-for-js

This repository is for active development of the Azure SDK for JavaScript (NodeJS & Browser). For consumers of the SDK we recommend visiting our public developer docs at https://docs.microsoft.com/javascript/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-js.
MIT License
2.04k stars 1.19k forks source link

[Custom Search] Client context has incorrect base URI, so requests fail #4220

Closed v-jaswel closed 5 years ago

v-jaswel commented 5 years ago

Describe the bug CustomSearchClientContext (https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/cognitiveservices/cognitiveservices-customsearch/src/customSearchClientContext.ts) does not set the base URI correctly (see line 42), so calls to the service fail.

To Reproduce Steps to reproduce the behavior:

  1. Set environment variables CUSTOM_SEARCH_SUBSCRIPTION_KEY and CUSTOM_SEARCH_INSTANCE_ID to a valid Custom Search subscription key and Custom Search instance ID, respectively.
  2. Install @azure/cognitiveservices-customsearch and @azure/ms-rest-js.
  3. Run the following code with Node.
    
    'use strict';

const search = require('@azure/cognitiveservices-customsearch'); const auth = require("@azure/ms-rest-js");

if (!process.env.CUSTOM_SEARCH_SUBSCRIPTION_KEY) { throw 'Please set/export the following environment variable: CUSTOM_SEARCH_SUBSCRIPTION_KEY'; } let subscription_key = process.env.CUSTOM_SEARCH_SUBSCRIPTION_KEY;

/* For more information, see:

let query = 'xbox';

let credentials = new auth.ApiKeyCredentials({ inHeader: { 'Ocp-Apim-Subscription-Key': subscription_key } }); let client = new search.CustomSearchClient(credentials);

client.customInstance.search(instance_ID, query, function (err, result, request, response) { if (err) { console.log(err); } else { console.log(result.queryContext.originalQuery); console.log(result.webPages.value); } });


Output:

{ Error: Resource not found at new RestError (C:\Users\\node_modules\@azure\cognitiveservices-customsearch\node_modules\@azure\ms-rest-js\dist\msRest.node.js:1399:28) at C:\Users\\node_modules\@azure\cognitiveservices-customsearch\node_modules\@azure\ms-rest-js\dist\msRest.node.js:2494:37 at process._tickCallback (internal/process/next_tick.js:68:7) code: '404', statusCode: 404, request: WebResource { streamResponseBody: false, url: 'https://api.cognitive.microsoft.com/search?customConfig=3b906310-44ac-40c8-b4d6-37194e2f8119&mkt=en-us&q=xbox', method: 'GET', headers: HttpHeaders { _headersMap: [Object] }, body: undefined, query: undefined, formData: undefined, withCredentials: false, abortSignal: undefined, timeout: 0, onUploadProgress: undefined, onDownloadProgress: undefined, proxySettings: undefined, operationSpec: { httpMethod: 'GET', path: 'search', urlParameters: [Array], queryParameters: [Array], headerParameters: [Array], responses: [Object], serializer: [Serializer] } }, response: { body: '{"error":{"code":"404","message": "Resource not found"}}', headers: HttpHeaders { _headersMap: [Object] }, status: 404 }, body: { _type: 'ErrorResponse', error: { code: '404', message: 'Resource not found' } } }


**Expected behavior**
Output:

[ { _type: 'WebPage', id: 'https://api.cognitive.microsoft.com/api/v7/#WebPages.0', name: 'Xbox Gaming Consoles: Xbox One X, Xbox One S, Gaming ...', url: 'https://www.microsoft.com/en-us/store/b/xbox', displayUrl: 'https://www.microsoft.com/en-us/store/b/xbox', snippet: ...


**Additional context**
CustomSearchClientContext sets the base URI as follows:

_this.baseUri = "{Endpoint}";

Instead, it should set it as follows (for example):

_this.baseUri = "{Endpoint}/bingcustomsearch/v7.0;



By making this change to my local copy of C:\Users\<user>\node_modules\@azure\cognitiveservices-customsearch\dist\cognitiveservices-customsearch.js, my code works as expected.

A debug statement placed in my local copy of C:\Users\<user>\node_modules\@azure\cognitiveservices-customsearch\node_modules\@azure\ms-rest-js\dist\msRest.node.js shows that before the fix, the URL used for the list request is:
https://api.cognitive.microsoft.com/search?customConfig=3b906310-44ac-40c8-b4d6-37194e2f8119&mkt=en-us&q=xbox
which is wrong. After the fix, the request is:
https://api.cognitive.microsoft.com/bingcustomsearch/v7.0/search?customConfig=3b906310-44ac-40c8-b4d6-37194e2f8119&mkt=en-us&q=xbox
which is correct.
maggiepint commented 5 years ago

Thanks for the bug report. I have routed to the appropriate team for follow-up.

ramya-rao-a commented 5 years ago

The library needs be re-generated using the latest version of the code generator which should fix the problem with the baseuri

ramya-rao-a commented 5 years ago

The library has been re-generated as part of #4955 and a new version has been published where this issue has been fixed

ghost commented 5 years ago

Thanks for working with Microsoft on GitHub! Tell us how you feel about your experience using the reactions on this comment.