Exemple #1
0
        public DetailedResponse <QueryResponse> Query(string query, string filter, int count, int offset)
        {
            string apiKey        = _appConfiguration["WatsonAPIs:Discovery:Key"];
            string version       = _appConfiguration["WatsonAPIs:Discovery:Version"];
            string instanceURL   = _appConfiguration["WatsonAPIs:Discovery:InstanceURL"];
            string environmentId = _appConfiguration["WatsonAPIs:Discovery:EnvironmentId"];
            string collectionId  = _appConfiguration["WatsonAPIs:Discovery:CollectionId"];

            //Check settings are OK
            if (String.IsNullOrEmpty(apiKey) || String.IsNullOrEmpty(version) || String.IsNullOrEmpty(instanceURL))
            {
                throw new Exception("Invalid Discovery API configuration. Please check appsettings.json.");
            }

            IamAuthenticator authenticator = new IamAuthenticator(apikey: apiKey);
            DiscoveryService discovery     = new DiscoveryService(version, authenticator);

            discovery.SetServiceUrl(instanceURL);
            discovery.DisableSslVerification(true);

            return(discovery.Query(
                       environmentId: environmentId,
                       collectionId: collectionId,
                       filter: string.IsNullOrEmpty(filter) ? null : filter,
                       query: query,
                       passages: false,
                       count: count,
                       offset: offset
                       ));
        }
        public object REST(string[] parameters)
        {
            string urlCfg         = ((NameValueCollection)ConfigurationManager.GetSection("Watson/Discovery"))["URL"];
            string apikeyCfg      = ((NameValueCollection)ConfigurationManager.GetSection("Watson/Discovery"))["APIKey"];
            string versionCfg     = ((NameValueCollection)ConfigurationManager.GetSection("Watson/Discovery"))["Version"];
            string collectionCfg  = ((NameValueCollection)ConfigurationManager.GetSection("Watson/Discovery"))["CollectionID"];
            string configCfg      = ((NameValueCollection)ConfigurationManager.GetSection("Watson/Discovery"))["ConfigurationID"];
            string environmentCfg = ((NameValueCollection)ConfigurationManager.GetSection("Watson/Discovery"))["environmentID"];
            string docPathCfg     = ((NameValueCollection)ConfigurationManager.GetSection("Watson/Discovery"))["documentPath"];
            string docIdCFG       = ((NameValueCollection)ConfigurationManager.GetSection("Watson/Discovery"))["documentID"];
            string ibmUserCFG     = ((NameValueCollection)ConfigurationManager.GetSection("Watson/Discovery"))["userIBM"];
            string fileNameCFG    = ((NameValueCollection)ConfigurationManager.GetSection("Watson/Discovery"))["documentName"];

            //Headers + Auth
            //IamAuthenticator authenticator = new IamAuthenticator(apikey: "D_nQRIAE4URpHN53bhMvgPQKfXPmOmAJhIbNQ86ZQdUV");
            IamAuthenticator authenticator = new IamAuthenticator(apikey: apikeyCfg);

            DiscoveryService discovery = new DiscoveryService(versionCfg, authenticator);

            discovery.SetServiceUrl("https://api.us-east.discovery.watson.cloud.ibm.com");

            discovery.DisableSslVerification(true);

            discovery.WithHeader("X-Watson-Learning-Opt-Out", "true");

            try
            {
                //Update Collection
                //Paso 1. Leer Base de datos. Paso 2. Leer collections. Paso 3. Si es necesario, actualizar.
                DetailedResponse <DocumentAccepted> result;

                using (FileStream fs = File.OpenRead(docPathCfg))
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        fs.CopyTo(ms);
                        result = discovery.UpdateDocument(
                            environmentId: environmentCfg,
                            collectionId: collectionCfg,
                            documentId: docIdCFG,
                            file: ms,
                            filename: fileNameCFG,
                            fileContentType: "text/html",
                            metadata: "{ \"Creator\": \"" + ibmUserCFG + "\", \"Subject\": \"Update\" }"
                            );
                    }
                }

                Console.WriteLine("\r\nWatson Discovery: La informacion se cargo exitosamente.");

                return(result.Response);
            }
            catch (Exception e)
            {
                Console.WriteLine("No se pudo realizar la carga en Watson Discovery");
                Console.WriteLine(e);
                return("Cant send request");
            }
        }
Exemple #3
0
        public void Setup()
        {
            Authenticator discoveryAuthenticator = new BearerTokenAuthenticator(bearerToken: bearerToken);

            service = new DiscoveryService(versionDate: versionDate, authenticator: discoveryAuthenticator);
            service.SetServiceUrl(serviceUrl: serviceUrl);
            service.DisableSslVerification(true);
        }
        public void Setup()
        {
            Authenticator discoveryAuthenticator = new BearerTokenAuthenticator(bearerToken: bearerToken);

            service = new DiscoveryService(version: versionDate, authenticator: discoveryAuthenticator);
            service.SetServiceUrl(serviceUrl: serviceUrl);
            service.DisableSslVerification(true);
            //service = new DiscoveryService(versionDate);
            //service.SetServiceUrl(serviceUrl);
            var creds = CredentialUtils.GetServiceProperties("discovery");

            creds.TryGetValue("PROJECT_ID", out projectId);
            creds.TryGetValue("COLLECTION_ID", out collectionId);
        }
Exemple #5
0
        public void Setup()
        {
            BearerTokenAuthenticator authenticator = new BearerTokenAuthenticator(
                bearerToken: bearerToken
                );

            service = new DiscoveryService("2019-10-03", authenticator);
            service.SetServiceUrl(serviceUrl);
            service.DisableSslVerification(true);

            var listEnvironmentsResult = service.ListEnvironments();

            environmentId = listEnvironmentsResult.Result.Environments[0].EnvironmentId;

            var listCollectionsResult = service.ListCollections(environmentId: environmentId);

            collectionId = listCollectionsResult.Result.Collections[0].CollectionId;
        }
        public WatsonBusiness()
        {
            var query     = "Quem descobriu o Brasil?";
            var versaoDia = DateTime.Now.ToString("yyyy-MM-dd");

            try
            {
                IamAuthenticator authenticator = new IamAuthenticator(apikey: $"{apikey}");
                DiscoveryService discovery     = new DiscoveryService($"{versaoDia}", authenticator);
                discovery.SetServiceUrl($"{url}");
                discovery.DisableSslVerification(true);
                ListaCollection(discovery);
                ResponseQueries(discovery, environmentId, collectionId, query);
            }
            catch (ServiceResponseException e)
            {
                Console.WriteLine($"Error: {e.Message}, com status: {e.Status}");
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: " + e.Message);
            }
        }