Skip to content

Commit

Permalink
add client initialization docs (#1945)
Browse files Browse the repository at this point in the history
Co-authored-by: iscai-msft <[email protected]>
Co-authored-by: Weidong Xu <[email protected]>
  • Loading branch information
3 people authored Dec 6, 2024
1 parent 1139adf commit 00dcd6b
Showing 1 changed file with 75 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Client hierarchy
title: Clients
---

import { ClientTabs, ClientTabItem } from "@components/client-tabs";
Expand Down Expand Up @@ -555,3 +555,77 @@ petActionClient.pet();
```

</ClientTabs>

### Adding Client Initialization Parameters

By default, we only generate our clients with initialization parameters for `endpoint`, `credential`, and `apiVersion`, whenever any of these are applicable. There are cases where spec authors would like their clients to have additional input parameters. This is where the `@clientInitialization` decorator comes in.

With `@clientInitialization`, you can pass in additional parameters you would like your client to have, by passing in an input model. All properties of the input model will be appended to the current default list of client initialization parameters. Additionally, these client parameters will no longer appear on service methods that previously had them as part of the method signature. The generated code will automatically pass in the inputted value from the client init to the service.

<ClientTabs>

```typespec title="main.tsp"
@service
namespace Storage;
op upload(@path blobName: string): void;
op download(@path blobName: string): void;
```

```typespec title="client.tsp"
import "./main.tsp";
import "@azure-tools/typespec-client-generator-core";
using Azure.ClientGenerator.Core;
namespace Customizations;
model StorageClientOptions {
blobName: string;
}
@@clientInitialization(Storage, StorageClientOptions);
```

```python
from storage import StorageClient

client = StorageClient(endpoint="<my-endpoint>", blob_name="myBlobName", ...)

client.upload()
client.download()
```

```csharp
// TODO: how do you pass in the options model
using Storage;

StorageClient client = new StorageClient();

client.Upload();
client.Download();
```

```typescript
// TODO: how do you pass in the options model
import { StorageClient } from "@azure/package-name";

const client = new StorageClient();

client.upload();
client.download();
```

```java
package storage;

StorageClient client = new StorageClient()
.endpoint("<my-endpoint>")
.blobName("myBlobName")
.buildClient();

client.upload()
client.download()
```

</ClientTabs>

0 comments on commit 00dcd6b

Please sign in to comment.