Skip to content

Commit 814b725

Browse files
authored
[agrifood] Update snippets generation (#32459)
### Packages impacted by this PR - @azure-rest/agrifood-farming - @azure/arm-agrifood ### Issues associated with this PR - #32416 ### Describe the problem that is addressed by this PR Updates the agrifood projects to use snippet extraction. ### What are the possible designs available to address the problem? If there are more than one possible design, why was the one in this PR chosen? ### Are there test cases added in this PR? _(If not, why?)_ ### Provide a list of related PRs _(if any)_ ### Command used to generate this PR:**_(Applicable only to SDK release request PRs)_ ### Checklists - [ ] Added impacted package name to the issue description - [ ] Does this PR needs any fixes in the SDK Generator?** _(If so, create an Issue in the [Autorest/typescript](https://github.com/Azure/autorest.typescript) repository and link it here)_ - [ ] Added a changelog (if necessary)
1 parent f322179 commit 814b725

File tree

11 files changed

+153
-167
lines changed

11 files changed

+153
-167
lines changed

sdk/advisor/arm-advisor/test/snippets.spec.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,22 @@ import { AdvisorManagementClient } from "@azure/arm-advisor";
66
import { setLogLevel } from "@azure/logger";
77
import { describe, it } from "vitest";
88

9-
describe("snippets", function () {
10-
it("AdvisorManagementClientAuth_Node", async function () {
9+
describe("snippets", () => {
10+
it("AdvisorManagementClientAuth_Node", async () => {
1111
const subscriptionId = "00000000-0000-0000-0000-000000000000";
1212
const client = new AdvisorManagementClient(new DefaultAzureCredential(), subscriptionId);
1313
});
1414

15-
it("AdvisorManagementClientAuth_Browser", async function () {
15+
it("AdvisorManagementClientAuth_Browser", async () => {
16+
const subscriptionId = "00000000-0000-0000-0000-000000000000";
1617
const credential = new InteractiveBrowserCredential({
1718
tenantId: "<YOUR_TENANT_ID>",
1819
clientId: "<YOUR_CLIENT_ID>",
1920
});
2021
const client = new AdvisorManagementClient(credential, subscriptionId);
2122
});
2223

23-
it("setLogLevel", async function () {
24+
it("setLogLevel", async () => {
2425
setLogLevel("info");
2526
});
2627
});

sdk/agrifood/agrifood-farming-rest/README.md

+13-13
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET
5353

5454
Use the returned token credential to authenticate the client:
5555

56-
```typescript
57-
import FarmBeats from "@azure-rest/agrifood-farming";
56+
```ts snippet:CreateFarmBeatsClient
57+
import { FarmBeats } from "@azure-rest/agrifood-farming";
5858
import { DefaultAzureCredential } from "@azure/identity";
5959

6060
const client = FarmBeats(
6161
"https://<farmbeats resource name>.farmbeats.azure.net",
62-
new DefaultAzureCredential()
62+
new DefaultAzureCredential(),
6363
);
6464
```
6565

@@ -95,17 +95,17 @@ Fam operations includes details pertaining to tilling, planting, application of
9595
Once you have authenticated and created the client object as shown in the [Authenticate the client](#create-and-authenticate-a-farmbeats-rest-client)
9696
section, you can create a party within the Data Manager for Agriculture resource like this:
9797

98-
```typescript
99-
import FarmBeats, { isUnexpected } from "@azure-rest/agrifood-farming";
98+
```ts snippet:CreateParty
99+
import { FarmBeats, isUnexpected } from "@azure-rest/agrifood-farming";
100100
import { DefaultAzureCredential } from "@azure/identity";
101101

102102
const client = FarmBeats(
103103
"https://<farmbeats resource name>.farmbeats.azure.net",
104-
new DefaultAzureCredential()
104+
new DefaultAzureCredential(),
105105
);
106106

107107
const partyId = "test_party";
108-
const result = await farmbeatsClient.path("/parties/{partyId}", partyId).patch({
108+
const result = await client.path("/parties/{partyId}", partyId).patch({
109109
body: {
110110
name: "Contoso Party",
111111
description: "Your custom party description here",
@@ -126,22 +126,22 @@ console.log(`Created Party: ${party.name}`);
126126

127127
### List Parties
128128

129-
```typescript
130-
import FarmBeats, { isUnexpected } from "@azure-rest/agrifood-farming";
129+
```ts snippet:ListParties
130+
import { FarmBeats, isUnexpected, paginate } from "@azure-rest/agrifood-farming";
131131
import { DefaultAzureCredential } from "@azure/identity";
132132

133133
const client = FarmBeats(
134134
"https://<farmbeats resource name>.farmbeats.azure.net",
135-
new DefaultAzureCredential()
135+
new DefaultAzureCredential(),
136136
);
137137

138-
const response = await farmbeatsClient.path("/parties").get();
138+
const response = await client.path("/parties").get();
139139

140140
if (isUnexpected(response)) {
141141
throw response.body.error;
142142
}
143143

144-
const parties = paginate(farmbeatsClient, response);
144+
const parties = paginate(client, response);
145145

146146
// Log each party id
147147
for await (const party of parties) {
@@ -160,7 +160,7 @@ For additional samples, please refer to the [samples folder][samples_folder]
160160

161161
Enabling logging may help uncover useful information about failures. In order to see a log of HTTP requests and responses, set the `AZURE_LOG_LEVEL` environment variable to `info`. Alternatively, logging can be enabled at runtime by calling `setLogLevel` in the `@azure/logger`:
162162

163-
```javascript
163+
```ts snippet:SetLogLevel
164164
import { setLogLevel } from "@azure/logger";
165165

166166
setLogLevel("info");

sdk/agrifood/agrifood-farming-rest/karma.conf.js

-133
This file was deleted.

sdk/agrifood/agrifood-farming-rest/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
"unit-test": "dev-tool run vendored cross-env TEST_MODE=playback && npm run unit-test:node && npm run unit-test:browser",
7474
"unit-test:browser": "npm run clean && dev-tool run build-package && dev-tool run build-test && dev-tool run test:vitest --browser",
7575
"unit-test:node": "dev-tool run test:vitest",
76-
"update-snippets": "echo skipped"
76+
"update-snippets": "dev-tool run update-snippets"
7777
},
7878
"sideEffects": false,
7979
"autoPublish": false,

sdk/agrifood/agrifood-farming-rest/test/public/smoke.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
3+
34
import type { FarmBeatsClient, Party, PartiesListParameters } from "../../src/index.js";
45
import { paginate } from "../../src/index.js";
56
import type { Recorder } from "@azure-tools/test-recorder";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
import { describe, it } from "vitest";
5+
import { FarmBeats, isUnexpected, paginate } from "@azure-rest/agrifood-farming";
6+
import { DefaultAzureCredential } from "@azure/identity";
7+
import { setLogLevel } from "@azure/logger";
8+
9+
describe("snippets", async () => {
10+
it("CreateFarmBeatsClient", async () => {
11+
const client = FarmBeats(
12+
"https://<farmbeats resource name>.farmbeats.azure.net",
13+
new DefaultAzureCredential(),
14+
);
15+
});
16+
17+
it("CreateParty", async () => {
18+
const client = FarmBeats(
19+
"https://<farmbeats resource name>.farmbeats.azure.net",
20+
new DefaultAzureCredential(),
21+
);
22+
// @ts-preserve-whitespace
23+
const partyId = "test_party";
24+
const result = await client.path("/parties/{partyId}", partyId).patch({
25+
body: {
26+
name: "Contoso Party",
27+
description: "Your custom party description here",
28+
status: "Active",
29+
properties: { foo: "bar", "numeric one": 1, "1": "numeric key" },
30+
},
31+
// Set the content-type of the request
32+
contentType: "application/merge-patch+json",
33+
});
34+
// @ts-preserve-whitespace
35+
if (isUnexpected(result)) {
36+
throw result.body.error;
37+
}
38+
// @ts-preserve-whitespace
39+
const party = result.body;
40+
console.log(`Created Party: ${party.name}`);
41+
});
42+
43+
it("ListParties", async () => {
44+
const client = FarmBeats(
45+
"https://<farmbeats resource name>.farmbeats.azure.net",
46+
new DefaultAzureCredential(),
47+
);
48+
// @ts-preserve-whitespace
49+
const response = await client.path("/parties").get();
50+
// @ts-preserve-whitespace
51+
if (isUnexpected(response)) {
52+
throw response.body.error;
53+
}
54+
// @ts-preserve-whitespace
55+
const parties = paginate(client, response);
56+
// @ts-preserve-whitespace
57+
// Log each party id
58+
for await (const party of parties) {
59+
const partyOutput = party;
60+
console.log(partyOutput.id);
61+
}
62+
});
63+
64+
it("SetLogLevel", async () => {
65+
setLogLevel("info");
66+
});
67+
});
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
{
2-
"extends": ["./tsconfig.src.json", "../../../tsconfig.test.base.json"]
2+
"extends": ["./tsconfig.src.json", "../../../tsconfig.test.base.json"],
3+
"compilerOptions": {
4+
"paths": {
5+
"@azure-rest/agrifood-farming": ["./src/index.ts"]
6+
}
7+
}
38
}

sdk/agrifood/arm-agrifood/README.md

+22-12
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,29 @@ Set the values of the client ID, tenant ID, and client secret of the AAD applica
4848

4949
For more information about how to create an Azure AD Application check out [this guide](https://learn.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal).
5050

51-
```javascript
52-
const { AgriFoodMgmtClient } = require("@azure/arm-agrifood");
53-
const { DefaultAzureCredential } = require("@azure/identity");
54-
// For client-side applications running in the browser, use InteractiveBrowserCredential instead of DefaultAzureCredential. See https://aka.ms/azsdk/js/identity/examples for more details.
51+
Using Node.js and Node-like environments, you can use the `DefaultAzureCredential` class to authenticate the client.
52+
53+
```ts snippet:CreateAgriFoodMgmtClient_Node
54+
import { AgriFoodMgmtClient } from "@azure/arm-agrifood";
55+
import { DefaultAzureCredential } from "@azure/identity";
5556

5657
const subscriptionId = "00000000-0000-0000-0000-000000000000";
5758
const client = new AgriFoodMgmtClient(new DefaultAzureCredential(), subscriptionId);
59+
```
60+
61+
For browser environments, use the `InteractiveBrowserCredential` from the `@azure/identity` package to authenticate.
62+
63+
```ts snippet:CreateAgriFoodMgmtClient_Browser
64+
import { InteractiveBrowserCredential } from "@azure/identity";
65+
import { AgriFoodMgmtClient } from "@azure/arm-agrifood";
5866

59-
// For client-side applications running in the browser, use this code instead:
60-
// const credential = new InteractiveBrowserCredential({
61-
// tenantId: "<YOUR_TENANT_ID>",
62-
// clientId: "<YOUR_CLIENT_ID>"
63-
// });
64-
// const client = new AgriFoodMgmtClient(credential, subscriptionId);
67+
const subscriptionId = "00000000-0000-0000-0000-000000000000";
68+
const credential = new InteractiveBrowserCredential({
69+
tenantId: "<YOUR_TENANT_ID>",
70+
clientId: "<YOUR_CLIENT_ID>",
71+
});
72+
73+
const client = new AgriFoodMgmtClient(credential, subscriptionId);
6574
```
6675

6776
### JavaScript Bundle
@@ -80,8 +89,9 @@ To use this client library in the browser, first you need to use a bundler. For
8089

8190
Enabling logging may help uncover useful information about failures. In order to see a log of HTTP requests and responses, set the `AZURE_LOG_LEVEL` environment variable to `info`. Alternatively, logging can be enabled at runtime by calling `setLogLevel` in the `@azure/logger`:
8291

83-
```javascript
84-
const { setLogLevel } = require("@azure/logger");
92+
```ts snippet:SetLogLevel
93+
import { setLogLevel } from "@azure/logger";
94+
8595
setLogLevel("info");
8696
```
8797

sdk/agrifood/arm-agrifood/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
"unit-test": "npm run unit-test:node && npm run unit-test:browser",
7676
"unit-test:browser": "echo skipped",
7777
"unit-test:node": "dev-tool run test:vitest",
78-
"update-snippets": "echo skipped"
78+
"update-snippets": "dev-tool run update-snippets"
7979
},
8080
"sideEffects": false,
8181
"//metadata": {

0 commit comments

Comments
 (0)