Skip to content

Commit

Permalink
Split create client (Azure#27769)
Browse files Browse the repository at this point in the history
### Packages impacted by this PR


### Issues associated with this PR


### Describe the problem that is addressed by this PR


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

---------

Co-authored-by: Marc Ma <[email protected]>
  • Loading branch information
marche0133 and marcma123 authored Nov 13, 2023
1 parent 687c4a7 commit c304867
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,10 @@ export interface ConditionalWorkerSelectorAttachmentOutput extends WorkerSelecto
}

// @public
function createClient(connectionStringOrUrl: string, credentialOrOptions?: KeyCredential | TokenCredential, options?: ClientOptions): AzureCommunicationRoutingServiceClient;
function createClient(connectionStringOrUrl: string, options: ClientOptions): AzureCommunicationRoutingServiceClient;

// @public
function createClient(endpoint: string, credentialOrOptions?: KeyCredential | TokenCredential, options?: ClientOptions): AzureCommunicationRoutingServiceClient;
export default createClient;

// @public (undocumented)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,8 @@ async function quickStart(): Promise<void> {

// Completing a job
// Once the worker is done with the job, the worker has to mark the job as `completed`.
const completeJob = await routerClient.path("/routing/jobs/{jobId}:complete", jobId).post({
const completeJob = await routerClient.path("/routing/jobs/{jobId}/assignments/{assignmentId}:complete", jobId, acceptJobOfferResult.assignmentId).post({
body: {
assignmentId: acceptJobOfferResult.assignmentId,
note: `Job has been completed by ${workerId} at ${new Date()}`
}
})
Expand All @@ -146,9 +145,8 @@ async function quickStart(): Promise<void> {
// Closing a job
// After a job has been completed, the worker can perform wrap up actions to the job before closing the job and finally
// releasing its capacity to accept more incoming jobs
const closeJob = await routerClient.path("/routing/jobs/{jobId}:close", jobId).post({
const closeJob = await routerClient.path("/routing/jobs/{jobId}/assignments/{assignmentId}:close", jobId, acceptJobOfferResult.assignmentId).post({
body: {
assignmentId: acceptJobOfferResult.assignmentId,
note: `Job has been closed by ${workerId} at ${new Date()}`
}
})
Expand All @@ -157,10 +155,9 @@ async function quickStart(): Promise<void> {
// Optionally, a job can also be set up to be marked as closed in the future.
const afterTwoSeconds = new Date();
afterTwoSeconds.setSeconds(afterTwoSeconds.getSeconds() + 2);
const closeJobInFuture = await routerClient.path("/routing/jobs/{jobId}:close", jobId).post({
const closeJobInFuture = await routerClient.path("/routing/jobs/{jobId}/assignments/{assignmentId}:close", jobId, acceptJobOfferResult.assignmentId).post({
body: {
closeAt: afterTwoSeconds,
assignmentId: acceptJobOfferResult.assignmentId,
note: `Job has been marked to close in the future by ${workerId} at ${afterTwoSeconds}`
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,36 +132,51 @@ async function quickStart() {

// Completing a job
// Once the worker is done with the job, the worker has to mark the job as `completed`.
const completeJob = await routerClient.path("/routing/jobs/{jobId}:complete", jobId).post({
body: {
assignmentId: acceptJobOfferResult.assignmentId,
note: `Job has been completed by ${workerId} at ${new Date()}`,
},
});
const completeJob = await routerClient
.path(
"/routing/jobs/{jobId}/assignments/{assignmentId}:complete",
jobId,
acceptJobOfferResult.assignmentId
)
.post({
body: {
note: `Job has been completed by ${workerId} at ${new Date()}`,
},
});

console.log(`Job has been successfully completed: ${completeJob}`);

// Closing a job
// After a job has been completed, the worker can perform wrap up actions to the job before closing the job and finally
// releasing its capacity to accept more incoming jobs
const closeJob = await routerClient.path("/routing/jobs/{jobId}:close", jobId).post({
body: {
assignmentId: acceptJobOfferResult.assignmentId,
note: `Job has been closed by ${workerId} at ${new Date()}`,
},
});
const closeJob = await routerClient
.path(
"/routing/jobs/{jobId}/assignments/{assignmentId}:close",
jobId,
acceptJobOfferResult.assignmentId
)
.post({
body: {
note: `Job has been closed by ${workerId} at ${new Date()}`,
},
});
console.log(`Job has been successfully closed: ${closeJob}`);

// Optionally, a job can also be set up to be marked as closed in the future.
const afterTwoSeconds = new Date();
afterTwoSeconds.setSeconds(afterTwoSeconds.getSeconds() + 2);
const closeJobInFuture = await routerClient.path("/routing/jobs/{jobId}:close", jobId).post({
body: {
closeAt: afterTwoSeconds,
assignmentId: acceptJobOfferResult.assignmentId,
note: `Job has been marked to close in the future by ${workerId} at ${afterTwoSeconds}`,
},
});
const closeJobInFuture = await routerClient
.path(
"/routing/jobs/{jobId}/assignments/{assignmentId}:close",
jobId,
acceptJobOfferResult.assignmentId
)
.post({
body: {
closeAt: afterTwoSeconds,
note: `Job has been marked to close in the future by ${workerId} at ${afterTwoSeconds}`,
},
});
console.log(`Job has been marked to close: ${closeJobInFuture}`); // You'll received a 202 in that case

await delay(2000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,8 @@ async function quickStart(): Promise<void> {

// Completing a job
// Once the worker is done with the job, the worker has to mark the job as `completed`.
const completeJob = await routerClient.path("/routing/jobs/{jobId}:complete", jobId).post({
const completeJob = await routerClient.path("/routing/jobs/{jobId}/assignments/{assignmentId}:complete", jobId, acceptJobOfferResult.assignmentId).post({
body: {
assignmentId: acceptJobOfferResult.assignmentId,
note: `Job has been completed by ${workerId} at ${new Date()}`
}
})
Expand All @@ -146,9 +145,8 @@ async function quickStart(): Promise<void> {
// Closing a job
// After a job has been completed, the worker can perform wrap up actions to the job before closing the job and finally
// releasing its capacity to accept more incoming jobs
const closeJob = await routerClient.path("/routing/jobs/{jobId}:close", jobId).post({
const closeJob = await routerClient.path("/routing/jobs/{jobId}/assignments/{assignmentId}:close", jobId, acceptJobOfferResult.assignmentId).post({
body: {
assignmentId: acceptJobOfferResult.assignmentId,
note: `Job has been closed by ${workerId} at ${new Date()}`
}
})
Expand All @@ -157,10 +155,9 @@ async function quickStart(): Promise<void> {
// Optionally, a job can also be set up to be marked as closed in the future.
const afterTwoSeconds = new Date();
afterTwoSeconds.setSeconds(afterTwoSeconds.getSeconds() + 2);
const closeJobInFuture = await routerClient.path("/routing/jobs/{jobId}:close", jobId).post({
const closeJobInFuture = await routerClient.path("/routing/jobs/{jobId}/assignments/{assignmentId}:close", jobId, acceptJobOfferResult.assignmentId).post({
body: {
closeAt: afterTwoSeconds,
assignmentId: acceptJobOfferResult.assignmentId,
note: `Job has been marked to close in the future by ${workerId} at ${afterTwoSeconds}`
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,75 @@
// Licensed under the MIT license.

import { getClient, ClientOptions } from "@azure-rest/core-client";
import { KeyCredential, TokenCredential } from "@azure/core-auth";
import { isTokenCredential, KeyCredential, TokenCredential } from "@azure/core-auth";
import { logger } from "./logger";
import { AzureCommunicationRoutingServiceClient } from "./clientDefinitions";
import { createCommunicationAuthPolicy, parseClientArguments } from "@azure/communication-common";
import {
createCommunicationAuthPolicy,
isKeyCredential,
parseClientArguments,
} from "@azure/communication-common";

/**
* Initialize a new instance of `AzureCommunicationRoutingServiceClient`
* @param connectionStringOrUrl - The connectionString or url of the Communication Services resource.
* @param credentialOrOptions The key or token credential.
* @param options - the parameter for all optional parameters
*/
export default function createClient(
connectionStringOrUrl: string,
options: ClientOptions
): AzureCommunicationRoutingServiceClient;

/**
* Initialize a new instance of `AzureCommunicationRoutingServiceClient`
* @param endpoint - The endpoint of the Communication Services resource.
* @param credentialOrOptions The key or token credential.
* @param options - the parameter for all optional parameters
*/
export default function createClient(
endpoint: string,
credentialOrOptions?: KeyCredential | TokenCredential,
options: ClientOptions = {}
options?: ClientOptions
): AzureCommunicationRoutingServiceClient;

// Implementation
export default function createClient(
arg1: string,
arg2?: ClientOptions | (KeyCredential | TokenCredential),
arg3?: ClientOptions
): AzureCommunicationRoutingServiceClient {
let credentialOrOptions: KeyCredential | TokenCredential | undefined;
let options: ClientOptions | undefined;
const connectionStringOrUrl = arg1;

// Determine which constructor is being called based on the types of the arguments
if (isTokenCredential(arg2) || isKeyCredential(arg2)) {
credentialOrOptions = arg2 as KeyCredential | TokenCredential;
options = arg3 as ClientOptions;
} else {
options = arg2 as ClientOptions;
}
if (options === undefined) {
options = {};
}

// Rest of the function remains the same, using connectionStringOrUrl or endpoint as needed
const { url, credential } = parseClientArguments(connectionStringOrUrl, credentialOrOptions);
const baseUrl = options.baseUrl ?? `${url}`;
options.apiVersion = options.apiVersion ?? "2023-11-01";
const baseUrl = options?.baseUrl ?? `${url}`;
options.apiVersion = options?.apiVersion ?? "2023-11-01";

const userAgentInfo = `azsdk-js-communication-job-router-rest/1.0.0-beta.1`;
const userAgentInfo = "azsdk-js-communication-job-router-rest/1.0.0-beta.1";
const userAgentPrefix =
options.userAgentOptions && options.userAgentOptions.userAgentPrefix
? `${options.userAgentOptions.userAgentPrefix} ${userAgentInfo}`
options?.userAgentOptions && options?.userAgentOptions.userAgentPrefix
? `${options?.userAgentOptions.userAgentPrefix} ${userAgentInfo}`
: `${userAgentInfo}`;
options = {
...options,
userAgentOptions: {
userAgentPrefix,
},
loggingOptions: {
logger: options.loggingOptions?.logger ?? logger.info,
logger: options?.loggingOptions?.logger ?? logger.info,
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export async function createRecordedRouterClientWithConnectionString(
return {
routerClient: JobRouter(
env.COMMUNICATION_CONNECTION_STRING as string,
undefined,
recorder.configureClientOptions({}) as ClientOptions
),
recorder,
Expand Down

0 comments on commit c304867

Please sign in to comment.