Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AutoPR @azure/cognitiveservices-visualsearch] Fix json format by prettier #47

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 59 additions & 76 deletions sdk/cognitiveservices/cognitiveservices-visualsearch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,112 +17,95 @@ npm install @azure/cognitiveservices-visualsearch

#### nodejs - Authentication, client creation and visualSearch images as an example written in TypeScript.

##### Install @azure/ms-rest-azure-js
##### Install @azure/ms-rest-nodeauth

- Please install minimum version of `"@azure/ms-rest-nodeauth": "^3.0.0"`.
```bash
npm install @azure/ms-rest-azure-js
npm install @azure/ms-rest-nodeauth@"^3.0.0"
```

##### Sample code
The following sample performs a visual search, i.e. perform a search with a image. To know more, refer to the [Azure Documentation on Bing Visual Search](https://docs.microsoft.com/en-us/azure/cognitive-services/bing-visual-search/).

```typescript
import {
VisualSearchClient,
VisualSearchModels
} from "@azure/cognitiveservices-visualsearch";
import { CognitiveServicesCredentials } from "@azure/ms-rest-azure-js";

async function main(): Promise<void> {
const visualSearchKey = process.env["visualSearchKey"] || "<visualSearchKey>";
const visualSearchEndPoint =
process.env["visualSearchEndPoint"] || "<visualSearchEndPoint>";
const cognitiveServiceCredentials = new CognitiveServicesCredentials(
visualSearchKey
);
const client = new VisualSearchClient(cognitiveServiceCredentials, {
endpoint: visualSearchEndPoint
import * as msRest from "@azure/ms-rest-js";
import * as msRestNodeAuth from "@azure/ms-rest-nodeauth";
import { VisualSearchClient, VisualSearchModels, VisualSearchMappers } from "@azure/cognitiveservices-visualsearch";
const subscriptionId = process.env["AZURE_SUBSCRIPTION_ID"];

msRestNodeAuth.interactiveLogin().then((creds) => {
const client = new VisualSearchClient(creds, subscriptionId);
const acceptLanguage = "testacceptLanguage";
const contentType = "testcontentType";
const userAgent = "testuserAgent";
const clientId = "testclientId";
const clientIp = "testclientIp";
const location = "westus";
const market = "testmarket";
const safeSearch = "Off";
const setLang = "testsetLang";
const knowledgeRequest = "testknowledgeRequest";
const image = new require("stream").Readable();
client.images.visualSearch(acceptLanguage, contentType, userAgent, clientId, clientIp, location, market, safeSearch, setLang, knowledgeRequest, image).then((result) => {
console.log("The result is:");
console.log(result);
});
}).catch((err) => {
console.error(err);
});
```

const insightsToken =
process.env["insights_token"] || "<insights_token>";;
#### browser - Authentication, client creation and visualSearch images as an example written in JavaScript.

const knowledgeRequest = JSON.stringify({
imageInfo: {
imageInsightsToken: insightsToken
}
});
##### Install @azure/ms-rest-browserauth

const options: VisualSearchModels.ImagesVisualSearchOptionalParams = {
acceptLanguage: "en-US",
knowledgeRequest: knowledgeRequest
};

client.images
.visualSearch(options)
.then(result => {
console.log("The result is: ");
console.log(result);
})
.catch(err => {
console.log("An error occurred:");
console.error(err);
});
}

main();
```bash
npm install @azure/ms-rest-browserauth
```

#### browser - Authentication, client creation and visualSearch images as an example written in JavaScript.

##### Sample code

See https://github.com/Azure/ms-rest-browserauth to learn how to authenticate to Azure in the browser.

- index.html
```html
<!DOCTYPE html>
<html lang="en">
<head>
<title>@azure/cognitiveservices-visualsearch sample</title>
<script src="node_modules/@azure/ms-rest-js/dist/msRest.browser.js"></script>
<script src="node_modules/@azure/ms-rest-browserauth/dist/msAuth.js"></script>
<script src="node_modules/@azure/cognitiveservices-visualsearch/dist/cognitiveservices-visualsearch.js"></script>
<script type="text/javascript">
const visualSearchKey = "<YOUR_VISUAL_SEARCH_KEY>";
const visualSearchEndPoint = "<YOUR_VISUAL_SEARCH_ENDPOINT>";
const cognitiveServiceCredentials = new msRest.ApiKeyCredentials({
inHeader: {
"Ocp-Apim-Subscription-Key": visualSearchKey
}
const subscriptionId = "<Subscription_Id>";
const authManager = new msAuth.AuthManager({
clientId: "<client id for your Azure AD app>",
tenant: "<optional tenant for your organization>"
});
const client = new Azure.CognitiveservicesVisualsearch.VisualSearchClient(
cognitiveServiceCredentials,
{
endpoint: visualSearchEndPoint
authManager.finalizeLogin().then((res) => {
if (!res.isLoggedIn) {
// may cause redirects
authManager.login();
}
);

const insightsToken = "<YOUR_INSIGHTS_TOKEN>";

const knowledgeRequest = JSON.stringify({
imageInfo: {
imageInsightsToken: insightsToken
}
});

const options = {
acceptLanguage: "en-US",
knowledgeRequest: knowledgeRequest
};

client.images
.visualSearch(options)
.then(result => {
console.log("The result is: ");
const client = new Azure.CognitiveservicesVisualsearch.VisualSearchClient(res.creds, subscriptionId);
const acceptLanguage = "testacceptLanguage";
const contentType = "testcontentType";
const userAgent = "testuserAgent";
const clientId = "testclientId";
const clientIp = "testclientIp";
const location = "westus";
const market = "testmarket";
const safeSearch = "Off";
const setLang = "testsetLang";
const knowledgeRequest = "testknowledgeRequest";
const image = new ReadableStream();
client.images.visualSearch(acceptLanguage, contentType, userAgent, clientId, clientIp, location, market, safeSearch, setLang, knowledgeRequest, image).then((result) => {
console.log("The result is:");
console.log(result);
})
.catch(err => {
}).catch((err) => {
console.log("An error occurred:");
console.error(err);
});
});
</script>
</head>
<body></body>
Expand Down
3 changes: 2 additions & 1 deletion swagger_to_sdk_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"typescript": "",
"license-header": "MICROSOFT_MIT_NO_VERSION",
"sdkrel:typescript-sdks-folder": ".",
"use": "@microsoft.azure/autorest.typescript@4.1.1"
"typescript.clear-output-folder": "",
"use": "@microsoft.azure/autorest.typescript@4.2.2"
},
"advanced_options": {
"clone_dir": "./azure-sdk-for-js",
Expand Down