Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Service Bus] Improve Namespace creation experience #1422

Merged
merged 5 commits into from
Mar 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/@azure/servicebus/data-plane/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { AmqpError } from "rhea-promise";

/**
* @internal
* Interface for Queue/Topic/Subscription clients
*/
export interface Client {
/**
Expand Down
11 changes: 8 additions & 3 deletions packages/@azure/servicebus/data-plane/lib/connectionContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
ConnectionContextBase,
CreateConnectionContextBaseParameters,
Dictionary,
delay
delay,
TokenProvider
} from "@azure/amqp-common";
import { NamespaceOptions } from "./namespace";
import { Client } from "./client";
Expand Down Expand Up @@ -42,11 +43,15 @@ export namespace ConnectionContext {
packageJsonInfo.version
} (NODE-VERSION ${process.version}; ${os.type()} ${os.release()})`;

export function create(config: ConnectionConfig, options?: NamespaceOptions): ConnectionContext {
export function create(
config: ConnectionConfig,
tokenProvider: TokenProvider,
options?: NamespaceOptions
): ConnectionContext {
if (!options) options = {};
const parameters: CreateConnectionContextBaseParameters = {
config: config,
tokenProvider: options.tokenProvider,
tokenProvider: tokenProvider,
dataTransformer: options.dataTransformer,
isEntityPathRequired: false,
connectionProperties: {
Expand Down
2 changes: 1 addition & 1 deletion packages/@azure/servicebus/data-plane/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

/// <reference lib="es2015" />

export { Namespace, NamespaceOptions, NamespaceOptionsBase } from "./namespace";
export { Namespace, NamespaceOptions } from "./namespace";
export {
TokenInfo,
TokenType,
Expand Down
57 changes: 28 additions & 29 deletions packages/@azure/servicebus/data-plane/lib/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ import {
ConnectionConfig,
DataTransformer,
TokenProvider,
AadTokenProvider
AadTokenProvider,
SasTokenProvider
} from "@azure/amqp-common";
import { SubscriptionClient } from "./subscriptionClient";

/**
* Describes the base namespace options.
* @interface NamespaceOptionsBase
* Describes the options that can be provided while creating the Namespace.
* @interface NamespaceOptions
*/
export interface NamespaceOptionsBase {
export interface NamespaceOptions {
/**
* @property {DataTransformer} [dataTransformer] The data transformer that will be used to encode
* and decode the sent and received messages respectively. If not provided then we will use the
Expand All @@ -34,20 +35,8 @@ export interface NamespaceOptionsBase {
}

/**
* Describes the options that can be provided while creating the Namespace.
* @interface NamespaceOptions
*/
export interface NamespaceOptions extends NamespaceOptionsBase {
/**
* @property {TokenProvider} [tokenProvider] - The token provider that provides the token
* for authentication. Default value: SasTokenProvider.
*/
tokenProvider?: TokenProvider;
}

/**
* Describes the Service Bus Namespace and is the entry point for using Queues, Topics and
* Subscriptions.
* Holds the AMQP connection to the Service Bus Namespace and is the entry point for using Queues,
* Topics and Subscriptions.
*/
export class Namespace {
/**
Expand All @@ -61,17 +50,22 @@ export class Namespace {
private _context: ConnectionContext;

/**
* Instantiates a client pointing to the ServiceBus Queue given by this configuration.
* Instantiates a client pointing to the Service Bus Namespace.
*
* @constructor
* @param {ConnectionConfig} config - The connection configuration to create the Namespace.
* @param {TokenProvider} [tokenProvider] - The token provider that provides the token for
* authentication. Default value: `SasTokenProvider`.
* authentication.
* @param {NamespaceOptions} - Options to create the Namespace.
*/
private constructor(config: ConnectionConfig, options?: NamespaceOptions) {
private constructor(
config: ConnectionConfig,
tokenProvider: TokenProvider,
ramya-rao-a marked this conversation as resolved.
Show resolved Hide resolved
options?: NamespaceOptions
) {
if (!options) options = {};
this.name = config.endpoint;
this._context = ConnectionContext.create(config, options);
this._context = ConnectionContext.create(config, tokenProvider, options);
}

/**
Expand Down Expand Up @@ -182,21 +176,27 @@ export class Namespace {
throw new Error("'connectionString' is a required parameter and must be of type: 'string'.");
}
const config = ConnectionConfig.create(connectionString);
return new Namespace(config, options);
ConnectionConfig.validate(config);
const tokenProvider = new SasTokenProvider(
config.endpoint,
config.sharedAccessKeyName,
config.sharedAccessKey
);
return new Namespace(config, tokenProvider, options);
}

/**
* Creates a Namespace from a generic token provider.
* @param {string} host - Fully qualified domain name for Servicebus. Most likely,
* `<yournamespace>.servicebus.windows.net`.
* @param {TokenProvider} tokenProvider - Your token provider that implements the TokenProvider interface.
* @param {NamespaceOptionsBase} options - The options that can be provided during namespace creation.
* @param {NamespaceOptions} options - The options that can be provided during namespace creation.
* @returns {Namespace} An instance of the Namespace.
*/
static createFromTokenProvider(
host: string,
tokenProvider: TokenProvider,
options?: NamespaceOptionsBase
options?: NamespaceOptions
): Namespace {
if (!host || (host && typeof host !== "string")) {
throw new Error("'host' is a required parameter and must be of type: 'string'.");
Expand All @@ -208,10 +208,9 @@ export class Namespace {
const connectionString =
`Endpoint=sb://${host};SharedAccessKeyName=defaultKeyName;` +
`SharedAccessKey=defaultKeyValue`;
if (!options) options = {};
const nsOptions: NamespaceOptions = options;
nsOptions.tokenProvider = tokenProvider;
return Namespace.createFromConnectionString(connectionString, nsOptions);
const config = ConnectionConfig.create(connectionString);
ConnectionConfig.validate(config);
return new Namespace(config, tokenProvider, options);
}

/**
Expand Down