Skip to content

Commit

Permalink
Chat Core2 Migration (#17045)
Browse files Browse the repository at this point in the history
* migrate to core client

* add tests recordings

* revert rollup change

* remove commented code

* fix dependency

* fix rush lint

* format files

* update swagger version

* update sdk version

* update pacakge version

* update the constants path

* revert recording files
  • Loading branch information
LuChen-Microsoft authored Aug 25, 2021
1 parent 0596e3a commit c44618b
Show file tree
Hide file tree
Showing 28 changed files with 615 additions and 546 deletions.
4 changes: 3 additions & 1 deletion common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 2 additions & 6 deletions sdk/communication/communication-chat/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
# Release History

## 1.1.0-beta.2 (Unreleased)

### Features Added
## 1.1.0 (Unreleased)

### Breaking Changes

### Bugs Fixed

### Other Changes
- Migrated from using `@azure/core-http` to `@azure/core-rest-pipeline` for the handling of HTTP requests. See [Azure Core v1 vs v2](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/core/core-rest-pipeline/documentation/core2.md) for more on the difference and benefits of the move.

## 1.1.0-beta.1 (2021-08-10)

Expand Down
18 changes: 9 additions & 9 deletions sdk/communication/communication-chat/package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "@azure/communication-chat",
"version": "1.1.0-beta.2",
"version": "1.1.0",
"description": "Azure client library for Azure Communication Chat services",
"sdk-type": "client",
"main": "dist/index.js",
"module": "dist-esm/src/index.js",
"types": "types/communication-chat.d.ts",
"scripts": {
"audit": "node ../../../common/scripts/rush-audit.js && rimraf node_modules package-lock.json && npm i --package-lock-only 2>&1 && npm audit",
"build:autorest": "autorest ./swagger/README.md --typescript --version=3.0.6267 --v3 && rushx format",
"build:autorest": "autorest ./swagger/README.md --typescript --v3 && rushx format",
"build:browser": "tsc -p . && cross-env ONLY_BROWSER=true rollup -c 2>&1",
"build:node": "tsc -p . && cross-env ONLY_NODE=true rollup -c 2>&1",
"build:samples": "echo Obsolete.",
Expand Down Expand Up @@ -68,14 +68,17 @@
"@azure/communication-common": "^1.1.0",
"@azure/communication-signaling": "1.0.0-beta.7",
"@azure/core-auth": "^1.3.0",
"@azure/core-http": "^2.0.0",
"@azure/core-client": "^1.0.0",
"@azure/core-rest-pipeline": "^1.1.0",
"@azure/core-tracing": "1.0.0-preview.13",
"@azure/logger": "^1.0.0",
"events": "^3.0.0",
"tslib": "^2.2.0",
"@azure/core-paging": "^1.1.1"
"@azure/core-paging": "^1.1.1",
"uuid": "^8.3.0"
},
"devDependencies": {
"@azure/core-util": "^1.0.0-beta.1",
"@azure/communication-identity": "^1.0.0",
"@azure/dev-tool": "^1.0.0",
"@azure/eslint-plugin-azure-sdk": "^3.0.0",
Expand All @@ -90,6 +93,7 @@
"@types/mocha": "^7.0.2",
"@types/node": "^12.0.0",
"@types/sinon": "^9.0.4",
"@types/uuid": "^8.0.0",
"assert": "^1.4.1",
"chai": "^4.2.0",
"cross-env": "^7.0.2",
Expand Down Expand Up @@ -128,11 +132,7 @@
"constantPaths": [
{
"path": "src/generated/src/chatApiClientContext.ts",
"prefix": "packageVersion"
},
{
"path": "src/constants.ts",
"prefix": "SDK_VERSION"
"prefix": "packageDetails"
},
{
"path": "swagger/README.md",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import { ChatMessageReceivedEvent } from '@azure/communication-signaling';
import { ChatThreadCreatedEvent } from '@azure/communication-signaling';
import { ChatThreadDeletedEvent } from '@azure/communication-signaling';
import { ChatThreadPropertiesUpdatedEvent } from '@azure/communication-signaling';
import { CommonClientOptions } from '@azure/core-client';
import { CommunicationIdentifier } from '@azure/communication-common';
import { CommunicationIdentifierKind } from '@azure/communication-common';
import { CommunicationTokenCredential } from '@azure/communication-common';
import * as coreHttp from '@azure/core-http';
import { OperationOptions } from '@azure/core-http';
import * as coreClient from '@azure/core-client';
import { OperationOptions } from '@azure/core-client';
import { PagedAsyncIterableIterator } from '@azure/core-paging';
import { ParticipantsAddedEvent } from '@azure/communication-signaling';
import { ParticipantsRemovedEvent } from '@azure/communication-signaling';
import { PipelineOptions } from '@azure/core-http';
import { ReadReceiptReceivedEvent } from '@azure/communication-signaling';
import { TypingIndicatorReceivedEvent } from '@azure/communication-signaling';

Expand Down Expand Up @@ -67,7 +67,7 @@ export class ChatClient {
}

// @public
export interface ChatClientOptions extends PipelineOptions {
export interface ChatClientOptions extends CommonClientOptions {
}

// @public
Expand Down Expand Up @@ -209,25 +209,25 @@ export type ListReadReceiptsOptions = RestListReadReceiptsOptions;
export type RemoveParticipantOptions = OperationOptions;

// @public
export interface RestListChatThreadsOptions extends coreHttp.OperationOptions {
export interface RestListChatThreadsOptions extends coreClient.OperationOptions {
maxPageSize?: number;
startTime?: Date;
}

// @public
export interface RestListMessagesOptions extends coreHttp.OperationOptions {
export interface RestListMessagesOptions extends coreClient.OperationOptions {
maxPageSize?: number;
startTime?: Date;
}

// @public
export interface RestListParticipantsOptions extends coreHttp.OperationOptions {
export interface RestListParticipantsOptions extends coreClient.OperationOptions {
maxPageSize?: number;
skip?: number;
}

// @public
export interface RestListReadReceiptsOptions extends coreHttp.OperationOptions {
export interface RestListReadReceiptsOptions extends coreClient.OperationOptions {
maxPageSize?: number;
skip?: number;
}
Expand Down
51 changes: 15 additions & 36 deletions sdk/communication/communication-chat/src/chatClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import { logger } from "./models/logger";
import { EventEmitter } from "events";
import { SDK_VERSION } from "./constants";
import { CommunicationTokenCredential } from "@azure/communication-common";
import {
SignalingClient,
Expand All @@ -21,13 +20,6 @@ import {
ParticipantsRemovedEvent
} from "@azure/communication-signaling";
import { getSignalingClient } from "./signaling/signalingClient";
import {
InternalPipelineOptions,
createPipelineFromOptions,
operationOptionsToRequestOptionsBase,
generateUuid
} from "@azure/core-http";
import "@azure/core-paging";
import { PagedAsyncIterableIterator } from "@azure/core-paging";
import { SpanStatusCode } from "@azure/core-tracing";
import { createSpan } from "./tracing";
Expand All @@ -44,17 +36,18 @@ import {
mapToCreateChatThreadResultSdkModel
} from "./models/mappers";
import { ChatThreadItem, CreateChatThreadResult, ListPageSettings } from "./models/models";
import { createCommunicationTokenCredentialPolicy } from "./credential/communicationTokenCredentialPolicy";
import { InternalPipelineOptions } from "@azure/core-rest-pipeline";
import { ChatApiClient } from "./generated/src";
import { CreateChatThreadRequest } from "./models/requests";
import { createCommunicationTokenCredentialPolicy } from "./credential/communicationTokenCredentialPolicy";
import { generateUuid } from "./models/uuid";

/**
* The client to do chat operations
*/
export class ChatClient {
private readonly tokenCredential: CommunicationTokenCredential;
private readonly clientOptions: ChatClientOptions;
// private readonly api: Chat;
private readonly client: ChatApiClient;
private readonly signalingClient: SignalingClient | undefined = undefined;
private readonly emitter = new EventEmitter();
Expand All @@ -75,32 +68,22 @@ export class ChatClient {
this.tokenCredential = credential;
this.clientOptions = { ...options };

const libInfo = `azsdk-js-communication-chat/${SDK_VERSION}`;

if (!options.userAgentOptions) {
options.userAgentOptions = {};
}

const userAgentOptions = { ...options.userAgentOptions };
if (options.userAgentOptions.userAgentPrefix) {
userAgentOptions.userAgentPrefix = `${options.userAgentOptions.userAgentPrefix} ${libInfo}`;
} else {
userAgentOptions.userAgentPrefix = libInfo;
}

const internalPipelineOptions: InternalPipelineOptions = {
...{ ...options, userAgentOptions },
...options,
...{
loggingOptions: {
logger: logger.info
}
}
};

const authPolicy = createCommunicationTokenCredentialPolicy(this.tokenCredential);
const pipeline = createPipelineFromOptions(internalPipelineOptions, authPolicy);
this.client = new ChatApiClient(this.endpoint, {
endpoint: this.endpoint,
...internalPipelineOptions
});

this.client = new ChatApiClient(this.endpoint, pipeline);
const authPolicy = createCommunicationTokenCredentialPolicy(this.tokenCredential);
this.client.pipeline.addPolicy(authPolicy);

this.signalingClient = getSignalingClient(credential, logger);
}
Expand Down Expand Up @@ -130,14 +113,14 @@ export class ChatClient {
updatedOptions.idempotencyToken = updatedOptions.idempotencyToken ?? generateUuid();
const updatedRestModelOptions = mapToCreateChatThreadOptionsRestModel(updatedOptions);

const { _response, ...result } = await this.client.chat.createChatThread(
const result = await this.client.chat.createChatThread(
{
topic: request.topic,
participants: options.participants?.map((participant) =>
mapToChatParticipantRestModel(participant)
)
},
operationOptionsToRequestOptionsBase(updatedRestModelOptions)
updatedRestModelOptions
);
return mapToCreateChatThreadResultSdkModel(result);
} catch (e) {
Expand All @@ -155,9 +138,8 @@ export class ChatClient {
continuationState: ListPageSettings,
options: ListChatThreadsOptions = {}
): AsyncIterableIterator<ChatThreadItem[]> {
const requestOptions = operationOptionsToRequestOptionsBase(options);
if (!continuationState.continuationToken) {
const currentSetResponse = await this.client.chat.listChatThreads(requestOptions);
const currentSetResponse = await this.client.chat.listChatThreads(options);
continuationState.continuationToken = currentSetResponse.nextLink;
if (currentSetResponse.value) {
yield currentSetResponse.value;
Expand All @@ -167,7 +149,7 @@ export class ChatClient {
while (continuationState.continuationToken) {
const currentSetResponse = await this.client.chat.listChatThreadsNext(
continuationState.continuationToken,
requestOptions
options
);
continuationState.continuationToken = currentSetResponse.nextLink;
if (currentSetResponse.value) {
Expand Down Expand Up @@ -230,10 +212,7 @@ export class ChatClient {
const { span, updatedOptions } = createSpan("ChatClient-DeleteChatThread", options);

try {
await this.client.chat.deleteChatThread(
threadId,
operationOptionsToRequestOptionsBase(updatedOptions)
);
await this.client.chat.deleteChatThread(threadId, updatedOptions);
} catch (e) {
span.setStatus({
code: SpanStatusCode.ERROR,
Expand Down
Loading

0 comments on commit c44618b

Please sign in to comment.