Skip to content

Commit

Permalink
Switch parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramya Raja committed Jan 14, 2020
1 parent 33ce753 commit a749abd
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 41 deletions.
2 changes: 1 addition & 1 deletion sdk/servicebus/service-bus/review/service-bus.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ export interface ServiceBusAtomManagementClientOptions {
// @public
export class ServiceBusClient {
constructor(connectionString: string, options?: ServiceBusClientOptions);
constructor(credential: TokenCredential, host: string, options?: ServiceBusClientOptions);
constructor(host: string, credential: TokenCredential, options?: ServiceBusClientOptions);
close(): Promise<any>;
createQueueClient(queueName: string): QueueClient;
createSubscriptionClient(topicName: string, subscriptionName: string): SubscriptionClient;
Expand Down
46 changes: 17 additions & 29 deletions sdk/servicebus/service-bus/src/serviceBusClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,37 +72,27 @@ export class ServiceBusClient {
* Instantiates a ServiceBusClient to interact with a Service Bus Namespace.
*
* @constructor
* @param credential - credential that implements the TokenCredential interface.
* @param host - The host name for the Service Bus namespace. This is likely to be similar to
* <yournamespace>.servicebus.windows.net
* @param credential - credential that implements the TokenCredential interface.
* @param options - Options to control ways to interact with the Service Bus
* Namespace.
*/
constructor(credential: TokenCredential, host: string, options?: ServiceBusClientOptions);
constructor(host: string, credential: TokenCredential, options?: ServiceBusClientOptions);

constructor(
connectionStringOrCredential: string | TokenCredential,
hostOrServiceBusClientOptions?: string | ServiceBusClientOptions,
hostOrConnectionString: string,
credentialOrServiceBusClientOptions?: TokenCredential | ServiceBusClientOptions,
options?: ServiceBusClientOptions
) {
let config;
let credential;
let connectionString;

if (connectionStringOrCredential == undefined) {
throw new Error("Input parameter 'connectionString' or 'credentials' must be defined.");
}

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

if (typeof connectionStringOrCredential == "string") {
if (!isTokenCredential(credentialOrServiceBusClientOptions)) {
// connectionString and options based constructor was invoked
connectionString = connectionStringOrCredential;
config = ConnectionConfig.create(connectionString);
config = ConnectionConfig.create(hostOrConnectionString);

options = hostOrServiceBusClientOptions as ServiceBusClientOptions;
options = credentialOrServiceBusClientOptions as ServiceBusClientOptions;
config.webSocket = options && options.webSocket;
config.webSocketEndpointPath = "$servicebus/websocket";
config.webSocketConstructorOptions = options && options.webSocketConstructorOptions;
Expand All @@ -112,23 +102,21 @@ export class ServiceBusClient {

ConnectionConfig.validate(config);
} else {
// credential, host and options based constructor was invoked
if (!isTokenCredential(connectionStringOrCredential)) {
throw new Error(
"'credentials' is a required parameter and must be an implementation of TokenCredential when using host based constructor overload."
);
}
credential = connectionStringOrCredential as TokenCredential;
// host, credential and options based constructor was invoked
credential = credentialOrServiceBusClientOptions as TokenCredential;

hostOrServiceBusClientOptions = String(hostOrServiceBusClientOptions);

if (!hostOrServiceBusClientOptions.endsWith("/")) {
hostOrServiceBusClientOptions += "/";
hostOrConnectionString = String(hostOrConnectionString);
if (!hostOrConnectionString.endsWith("/")) {
hostOrConnectionString += "/";
}
connectionString = `Endpoint=sb://${hostOrServiceBusClientOptions};SharedAccessKeyName=defaultKeyName;SharedAccessKey=defaultKeyValue;`;
const connectionString = `Endpoint=sb://${hostOrConnectionString};SharedAccessKeyName=defaultKeyName;SharedAccessKey=defaultKeyValue;`;
config = ConnectionConfig.create(connectionString);
}

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

this.name = config.endpoint;
this._context = ConnectionContext.create(config, credential, options);
}
Expand Down
18 changes: 9 additions & 9 deletions sdk/servicebus/service-bus/test/serviceBusClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,28 +333,28 @@ describe("Test ServiceBusClient creation #RunInBrowser", function(): void {

it("throws error for invalid tokenCredentials", async function(): Promise<void> {
try {
new ServiceBusClient([] as any, serviceBusEndpoint);
new ServiceBusClient(serviceBusEndpoint, [] as any);
} catch (err) {
errorWasThrown = true;
should.equal(
err.message,
"'credentials' is a required parameter and must be an implementation of TokenCredential when using host based constructor overload.",
"Connection string malformed: each part of the connection string must have an `=` assignment.",
// "'credentials' is a required parameter and must be an implementation of TokenCredential when using host based constructor overload.",
"ErrorMessage is different than expected"
);
}
should.equal(errorWasThrown, true, "Error thrown flag must be true");
});

it("throws error for undefined tokenCredentials / connectionString", async function(): Promise<
void
> {
it("throws error for undefined tokenCredentials", async function(): Promise<void> {
try {
new ServiceBusClient(undefined as any, serviceBusEndpoint);
new ServiceBusClient(serviceBusEndpoint, undefined as any);
} catch (err) {
errorWasThrown = true;
should.equal(
err.message,
"Input parameter 'connectionString' or 'credentials' must be defined.",
"Connection string malformed: each part of the connection string must have an `=` assignment.",
// "'credentials' is a required parameter and must be an implementation of TokenCredential when using host based constructor overload.",
"ErrorMessage is different than expected"
);
}
Expand All @@ -366,13 +366,13 @@ describe("Test ServiceBusClient creation #RunInBrowser", function(): void {
void
> {
const tokenCreds = getDefaultTokenCredential();
sbClient = new ServiceBusClient(tokenCreds, 123 as any);
sbClient = new ServiceBusClient(123 as any, tokenCreds);
should.equal(sbClient.name, "sb://123/", "Name of the namespace is different than expected");
});

it("sends a message to the ServiceBus entity", async function(): Promise<void> {
const tokenCreds = getDefaultTokenCredential();
const sbClient = new ServiceBusClient(tokenCreds, serviceBusEndpoint);
const sbClient = new ServiceBusClient(serviceBusEndpoint, tokenCreds);

sbClient.should.be.an.instanceof(ServiceBusClient);
const clients = await getSenderReceiverClients(
Expand Down
4 changes: 2 additions & 2 deletions sdk/servicebus/service-bus/test/streamingReceiver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
import { StreamingReceiver } from "../src/core/streamingReceiver";

import { AccessToken, parseConnectionString, TokenCredential } from "@azure/core-amqp";
import { getEnvVars, EnvVarNames } from "./utils/envVarUtils";
import { getEnvVars, EnvVarNames } from "./utils/envVarUtils";
import { EnvironmentCredential } from "./utils/aadUtils";

const should = chai.should();
Expand Down Expand Up @@ -910,7 +910,7 @@ describe("Streaming - Failed init should not cache recevier", function(): void {
SharedAccessKey: string;
} = parseConnectionString(env[EnvVarNames.SERVICEBUS_CONNECTION_STRING]);
const tokenProvider = new TestTokenCredential();
sbClient = new ServiceBusClient(tokenProvider, connectionObject.Endpoint.substr(5));
sbClient = new ServiceBusClient(connectionObject.Endpoint.substr(5), tokenProvider);
clients = await getSenderReceiverClients(
sbClient,
TestClientType.UnpartitionedQueue,
Expand Down

0 comments on commit a749abd

Please sign in to comment.