Skip to content

Commit

Permalink
[agrifood] Update snippets generation (#32459)
Browse files Browse the repository at this point in the history
### 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)
  • Loading branch information
mpodwysocki authored Jan 8, 2025
1 parent f322179 commit 814b725
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 167 deletions.
9 changes: 5 additions & 4 deletions sdk/advisor/arm-advisor/test/snippets.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,22 @@ import { AdvisorManagementClient } from "@azure/arm-advisor";
import { setLogLevel } from "@azure/logger";
import { describe, it } from "vitest";

describe("snippets", function () {
it("AdvisorManagementClientAuth_Node", async function () {
describe("snippets", () => {
it("AdvisorManagementClientAuth_Node", async () => {
const subscriptionId = "00000000-0000-0000-0000-000000000000";
const client = new AdvisorManagementClient(new DefaultAzureCredential(), subscriptionId);
});

it("AdvisorManagementClientAuth_Browser", async function () {
it("AdvisorManagementClientAuth_Browser", async () => {
const subscriptionId = "00000000-0000-0000-0000-000000000000";
const credential = new InteractiveBrowserCredential({
tenantId: "<YOUR_TENANT_ID>",
clientId: "<YOUR_CLIENT_ID>",
});
const client = new AdvisorManagementClient(credential, subscriptionId);
});

it("setLogLevel", async function () {
it("setLogLevel", async () => {
setLogLevel("info");
});
});
26 changes: 13 additions & 13 deletions sdk/agrifood/agrifood-farming-rest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET

Use the returned token credential to authenticate the client:

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

const client = FarmBeats(
"https://<farmbeats resource name>.farmbeats.azure.net",
new DefaultAzureCredential()
new DefaultAzureCredential(),
);
```

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

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

const client = FarmBeats(
"https://<farmbeats resource name>.farmbeats.azure.net",
new DefaultAzureCredential()
new DefaultAzureCredential(),
);

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

### List Parties

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

const client = FarmBeats(
"https://<farmbeats resource name>.farmbeats.azure.net",
new DefaultAzureCredential()
new DefaultAzureCredential(),
);

const response = await farmbeatsClient.path("/parties").get();
const response = await client.path("/parties").get();

if (isUnexpected(response)) {
throw response.body.error;
}

const parties = paginate(farmbeatsClient, response);
const parties = paginate(client, response);

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

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`:

```javascript
```ts snippet:SetLogLevel
import { setLogLevel } from "@azure/logger";

setLogLevel("info");
Expand Down
133 changes: 0 additions & 133 deletions sdk/agrifood/agrifood-farming-rest/karma.conf.js

This file was deleted.

2 changes: 1 addition & 1 deletion sdk/agrifood/agrifood-farming-rest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"unit-test": "dev-tool run vendored cross-env TEST_MODE=playback && npm run unit-test:node && npm run unit-test:browser",
"unit-test:browser": "npm run clean && dev-tool run build-package && dev-tool run build-test && dev-tool run test:vitest --browser",
"unit-test:node": "dev-tool run test:vitest",
"update-snippets": "echo skipped"
"update-snippets": "dev-tool run update-snippets"
},
"sideEffects": false,
"autoPublish": false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import type { FarmBeatsClient, Party, PartiesListParameters } from "../../src/index.js";
import { paginate } from "../../src/index.js";
import type { Recorder } from "@azure-tools/test-recorder";
Expand Down
67 changes: 67 additions & 0 deletions sdk/agrifood/agrifood-farming-rest/test/snippets.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { describe, it } from "vitest";
import { FarmBeats, isUnexpected, paginate } from "@azure-rest/agrifood-farming";
import { DefaultAzureCredential } from "@azure/identity";
import { setLogLevel } from "@azure/logger";

describe("snippets", async () => {
it("CreateFarmBeatsClient", async () => {
const client = FarmBeats(
"https://<farmbeats resource name>.farmbeats.azure.net",
new DefaultAzureCredential(),
);
});

it("CreateParty", async () => {
const client = FarmBeats(
"https://<farmbeats resource name>.farmbeats.azure.net",
new DefaultAzureCredential(),
);
// @ts-preserve-whitespace
const partyId = "test_party";
const result = await client.path("/parties/{partyId}", partyId).patch({
body: {
name: "Contoso Party",
description: "Your custom party description here",
status: "Active",
properties: { foo: "bar", "numeric one": 1, "1": "numeric key" },
},
// Set the content-type of the request
contentType: "application/merge-patch+json",
});
// @ts-preserve-whitespace
if (isUnexpected(result)) {
throw result.body.error;
}
// @ts-preserve-whitespace
const party = result.body;
console.log(`Created Party: ${party.name}`);
});

it("ListParties", async () => {
const client = FarmBeats(
"https://<farmbeats resource name>.farmbeats.azure.net",
new DefaultAzureCredential(),
);
// @ts-preserve-whitespace
const response = await client.path("/parties").get();
// @ts-preserve-whitespace
if (isUnexpected(response)) {
throw response.body.error;
}
// @ts-preserve-whitespace
const parties = paginate(client, response);
// @ts-preserve-whitespace
// Log each party id
for await (const party of parties) {
const partyOutput = party;
console.log(partyOutput.id);
}
});

it("SetLogLevel", async () => {
setLogLevel("info");
});
});
7 changes: 6 additions & 1 deletion sdk/agrifood/agrifood-farming-rest/tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
{
"extends": ["./tsconfig.src.json", "../../../tsconfig.test.base.json"]
"extends": ["./tsconfig.src.json", "../../../tsconfig.test.base.json"],
"compilerOptions": {
"paths": {
"@azure-rest/agrifood-farming": ["./src/index.ts"]
}
}
}
34 changes: 22 additions & 12 deletions sdk/agrifood/arm-agrifood/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,29 @@ Set the values of the client ID, tenant ID, and client secret of the AAD applica

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).

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

```ts snippet:CreateAgriFoodMgmtClient_Node
import { AgriFoodMgmtClient } from "@azure/arm-agrifood";
import { DefaultAzureCredential } from "@azure/identity";

const subscriptionId = "00000000-0000-0000-0000-000000000000";
const client = new AgriFoodMgmtClient(new DefaultAzureCredential(), subscriptionId);
```

For browser environments, use the `InteractiveBrowserCredential` from the `@azure/identity` package to authenticate.

```ts snippet:CreateAgriFoodMgmtClient_Browser
import { InteractiveBrowserCredential } from "@azure/identity";
import { AgriFoodMgmtClient } from "@azure/arm-agrifood";

// For client-side applications running in the browser, use this code instead:
// const credential = new InteractiveBrowserCredential({
// tenantId: "<YOUR_TENANT_ID>",
// clientId: "<YOUR_CLIENT_ID>"
// });
// const client = new AgriFoodMgmtClient(credential, subscriptionId);
const subscriptionId = "00000000-0000-0000-0000-000000000000";
const credential = new InteractiveBrowserCredential({
tenantId: "<YOUR_TENANT_ID>",
clientId: "<YOUR_CLIENT_ID>",
});

const client = new AgriFoodMgmtClient(credential, subscriptionId);
```

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

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`:

```javascript
const { setLogLevel } = require("@azure/logger");
```ts snippet:SetLogLevel
import { setLogLevel } from "@azure/logger";

setLogLevel("info");
```

Expand Down
2 changes: 1 addition & 1 deletion sdk/agrifood/arm-agrifood/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"unit-test": "npm run unit-test:node && npm run unit-test:browser",
"unit-test:browser": "echo skipped",
"unit-test:node": "dev-tool run test:vitest",
"update-snippets": "echo skipped"
"update-snippets": "dev-tool run update-snippets"
},
"sideEffects": false,
"//metadata": {
Expand Down
Loading

0 comments on commit 814b725

Please sign in to comment.