csharp/Azure/ConferenceBuddy/ConferenceBuddy/ConferenceBuddy.Core/Services/QnAMakerService.cs

QnAMakerService.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

using Microsoft.ConferenceBuddy.Common.Models;
using Microsoft.ConferenceBuddy.Common.Utilities;

namespace Microsoft.ConferenceBuddy.Core.Services
{
    public clast QnAMakerService : ServiceBase
    {
        /// 
        /// The constructor of the QnA Maker service
        /// 
        public QnAMakerService(string serviceUrl, string subscriptionKey)
        {
            if (string.IsNullOrEmpty(serviceUrl) == true)
            {
                throw new ArgumentNullException("Service url is not initialized.");
            }

            if (string.IsNullOrEmpty(subscriptionKey) == true)
            {
                throw new ArgumentNullException("Subscription key is not initialized.");
            }

            this.BaseServiceUrl = serviceUrl;
            this.RequestHeaders = new Dictionary()
            {
                {  this.HEADER_SUB_KEY, subscriptionKey }
            };
        }

        /// 
        /// Generate answer from QnA Maker service
        /// 
        public async Task GenerateAnswerAsync(string query, string knowledgebaseId)
        {
            if (string.IsNullOrEmpty(query) == true)
            {
                return default(QnAMakerSearchResult);
            }

            // Get request uri
            string requestUrl = Path.Combine(this.BaseServiceUrl, "knowledgebases", knowledgebaseId, "generateAnswer");
            Uri requestUri = new Uri(requestUrl);

            // Create content
            QnAMakerSearchRequest content = new QnAMakerSearchRequest()
            {
                Question = query
            };

            // Post request
            QnAMakerSearchResult result = await HttpClientUtility.PostAsJsonAsync(requestUri, this.RequestHeaders, content);
            return result;
        }

    }
}