public createOrJoin()

in src/common.speech/Transcription/ConversationManager.ts [43:144]


    public createOrJoin(args: PropertyCollection, conversationCode: string, cb?: any, err?: any): void {

        try {

            Contracts.throwIfNullOrUndefined(args, "args");

            const languageCode: string = args.getProperty(PropertyId.SpeechServiceConnection_RecoLanguage, ConversationConnectionConfig.defaultLanguageCode);
            const nickname: string = args.getProperty(PropertyId.ConversationTranslator_Name, "conversation_host");
            const endpointHost: string = args.getProperty(PropertyId.ConversationTranslator_Host, this.privHost);
            const correlationId: string = args.getProperty(PropertyId.ConversationTranslator_CorrelationId);
            const subscriptionKey: string = args.getProperty(PropertyId.SpeechServiceConnection_Key);
            const subscriptionRegion: string = args.getProperty(PropertyId.SpeechServiceConnection_Region);
            const authToken: string = args.getProperty(PropertyId.SpeechServiceAuthorization_Token);

            Contracts.throwIfNullOrWhitespace(languageCode, "languageCode");
            Contracts.throwIfNullOrWhitespace(nickname, "nickname");
            Contracts.throwIfNullOrWhitespace(endpointHost, "endpointHost");

            const queryParams: IStringDictionary<string> = {};
            queryParams[this.privRequestParams.apiVersion] = this.privApiVersion;
            queryParams[this.privRequestParams.languageCode] = languageCode;
            queryParams[this.privRequestParams.nickname] = nickname;

            const headers: IStringDictionary<string> = {};
            if (correlationId) {
                headers[this.privRequestParams.correlationId] = correlationId;
            }
            headers[this.privRequestParams.clientAppId] = ConversationConnectionConfig.clientAppId;

            if (conversationCode !== undefined) {
                queryParams[this.privRequestParams.roomId] = conversationCode;
            } else {
                Contracts.throwIfNullOrUndefined(subscriptionRegion, this.privErrors.authInvalidSubscriptionRegion);
                headers[this.privRequestParams.subscriptionRegion] = subscriptionRegion;
                if (subscriptionKey) {
                    headers[this.privRequestParams.subscriptionKey] = subscriptionKey;
                } else if (authToken) {
                    headers[this.privRequestParams.authorization] = `Bearer ${authToken}`;
                } else {
                    Contracts.throwIfNullOrUndefined(subscriptionKey, this.privErrors.authInvalidSubscriptionKey);
                }
            }

            const config: IRequestOptions = {};
            config.headers = headers;
            this.privRestAdapter.options = config;

            const endpoint: string = `https://${endpointHost}${this.privRestPath}`;

            // TODO: support a proxy and certificate validation
            this.privRestAdapter.request(RestRequestType.Post, endpoint, queryParams, null).then((response: IRestResponse) => {

                const requestId: string = RestMessageAdapter.extractHeaderValue(this.privRequestParams.requestId, response.headers);

                if (!response.ok) {
                    if (!!err) {
                        // get the error
                        let errorMessage: string = this.privErrors.invalidCreateJoinConversationResponse.replace("{status}", response.status.toString());
                        let errMessageRaw: IConversationResponseError;
                        try {
                            errMessageRaw = JSON.parse(response.data) as IConversationResponseError;
                            errorMessage += ` [${errMessageRaw.error.code}: ${errMessageRaw.error.message}]`;
                        } catch (e) {
                            errorMessage += ` [${response.data}]`;
                        }
                        if (requestId) {
                            errorMessage += ` ${requestId}`;
                        }

                        err(errorMessage);
                    }
                    return;
                }
                const conversation: IInternalConversation = JSON.parse(response.data) as IInternalConversation;
                if (conversation) {
                    conversation.requestId = requestId;
                }
                if (!!cb) {
                    try {
                        cb(conversation);
                    } catch (e) {
                        if (!!err) {
                            err(e);
                        }
                    }
                    cb = undefined;
                }
            /* tslint:disable:no-empty */
            }).catch( (e: any) => {});

        } catch (error) {
            if (!!err) {
                if (error instanceof Error) {
                    const typedError: Error = error as Error;
                    err(typedError.name + ": " + typedError.message);

                } else {
                    err(error);
                }
            }
        }
    }