use of com.ibm.cloud.sdk.core.security.Authenticator in project java-sdk by watson-developer-cloud.
the class DiscoveryQueryExample method main.
public static void main(String[] args) {
Authenticator authenticator = new IamAuthenticator("<iam_api_key>");
Discovery discovery = new Discovery("2019-04-30", authenticator);
String environmentId = null;
String configurationId = null;
String collectionId = null;
String documentId = null;
// See if an environment already exists
System.out.println("Check if environment exists");
ListEnvironmentsOptions listOptions = new ListEnvironmentsOptions.Builder().build();
ListEnvironmentsResponse listResponse = discovery.listEnvironments(listOptions).execute().getResult();
for (Environment environment : listResponse.getEnvironments()) {
// look for an existing environment that isn't read only
if (!environment.isReadOnly()) {
environmentId = environment.getEnvironmentId();
System.out.println("Found existing environment ID: " + environmentId);
break;
}
}
if (environmentId == null) {
System.out.println("No environment found, creating new one...");
// no environment found, create a new one (assuming we are a FREE plan)
String environmentName = "watson_developer_cloud_test_environment";
CreateEnvironmentOptions createOptions = new CreateEnvironmentOptions.Builder().name(environmentName).size(String.valueOf(0L)).build();
Environment createResponse = discovery.createEnvironment(createOptions).execute().getResult();
environmentId = createResponse.getEnvironmentId();
System.out.println("Created new environment ID: " + environmentId);
// wait for environment to be ready
System.out.println("Waiting for environment to be ready...");
boolean environmentReady = false;
while (!environmentReady) {
GetEnvironmentOptions getEnvironmentOptions = new GetEnvironmentOptions.Builder(environmentId).build();
Environment getEnvironmentResponse = discovery.getEnvironment(getEnvironmentOptions).execute().getResult();
environmentReady = getEnvironmentResponse.getStatus().equals(Environment.Status.ACTIVE);
try {
if (!environmentReady) {
Thread.sleep(500);
}
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted", e);
}
}
System.out.println("Environment Ready!");
}
// find the default configuration
System.out.println("Finding the default configuration");
ListConfigurationsOptions listConfigsOptions = new ListConfigurationsOptions.Builder(environmentId).build();
ListConfigurationsResponse listConfigsResponse = discovery.listConfigurations(listConfigsOptions).execute().getResult();
for (Configuration configuration : listConfigsResponse.getConfigurations()) {
if (configuration.name().equals(DEFAULT_CONFIG_NAME)) {
configurationId = configuration.configurationId();
System.out.println("Found default configuration ID: " + configurationId);
break;
}
}
// create a new collection
System.out.println("Creating a new collection...");
String collectionName = "my_watson_developer_cloud_collection";
CreateCollectionOptions createCollectionOptions = new CreateCollectionOptions.Builder(environmentId, collectionName).configurationId(configurationId).build();
Collection collection = discovery.createCollection(createCollectionOptions).execute().getResult();
collectionId = collection.getCollectionId();
System.out.println("Created a collection ID: " + collectionId);
// wait for the collection to be "available"
System.out.println("Waiting for collection to be ready...");
boolean collectionReady = false;
while (!collectionReady) {
GetCollectionOptions getCollectionOptions = new GetCollectionOptions.Builder(environmentId, collectionId).build();
Collection getCollectionResponse = discovery.getCollection(getCollectionOptions).execute().getResult();
collectionReady = getCollectionResponse.getStatus().equals(Collection.Status.ACTIVE);
try {
if (!collectionReady) {
Thread.sleep(500);
}
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted", e);
}
}
System.out.println("Collection Ready!");
// add a document
System.out.println("Creating a new document...");
String documentJson = "{\"field\":\"value\"}";
InputStream documentStream = new ByteArrayInputStream(documentJson.getBytes());
AddDocumentOptions.Builder createDocumentBuilder = new AddDocumentOptions.Builder(environmentId, collectionId);
createDocumentBuilder.file(documentStream).fileContentType(HttpMediaType.APPLICATION_JSON);
DocumentAccepted createDocumentResponse = discovery.addDocument(createDocumentBuilder.build()).execute().getResult();
documentId = createDocumentResponse.getDocumentId();
System.out.println("Created a document ID: " + documentId);
// wait for document to be ready
System.out.println("Waiting for document to be ready...");
boolean documentReady = false;
while (!documentReady) {
GetDocumentStatusOptions getDocumentStatusOptions = new GetDocumentStatusOptions.Builder(environmentId, collectionId, documentId).build();
DocumentStatus getDocumentResponse = discovery.getDocumentStatus(getDocumentStatusOptions).execute().getResult();
documentReady = !getDocumentResponse.getStatus().equals(DocumentStatus.Status.PROCESSING);
try {
if (!documentReady) {
Thread.sleep(500);
}
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted");
}
}
System.out.println("Document Ready!");
// query document
System.out.println("Querying the collection...");
QueryOptions.Builder queryBuilder = new QueryOptions.Builder(environmentId, collectionId);
queryBuilder.query("field:value");
QueryResponse queryResponse = discovery.query(queryBuilder.build()).execute().getResult();
// print out the results
System.out.println("Query Results:");
System.out.println(queryResponse);
// cleanup the collection created
System.out.println("Deleting the collection...");
DeleteCollectionOptions deleteOptions = new DeleteCollectionOptions.Builder(environmentId, collectionId).build();
discovery.deleteCollection(deleteOptions).execute();
System.out.println("Collection deleted!");
System.out.println("Discovery example finished");
}
use of com.ibm.cloud.sdk.core.security.Authenticator in project java-sdk by watson-developer-cloud.
the class LanguageTranslatorExample method main.
public static void main(String[] args) {
Authenticator authenticator = new IamAuthenticator("<iam_api_key>");
LanguageTranslator service = new LanguageTranslator("2018-05-01", authenticator);
TranslateOptions translateOptions = new TranslateOptions.Builder().addText("text").modelId("en-es").build();
TranslationResult translationResult = service.translate(translateOptions).execute().getResult();
System.out.println(translationResult);
}
use of com.ibm.cloud.sdk.core.security.Authenticator in project java-sdk by watson-developer-cloud.
the class LanguageTranslatorIT method setUp.
/**
* Sets up the tests.
*
* @throws Exception the exception
*/
/*
* (non-Javadoc)
* @see com.ibm.watson.developercloud.WatsonServiceTest#setUp()
*/
@Override
@Before
public void setUp() throws Exception {
super.setUp();
String iamApiKey = System.getenv("LANGUAGE_TRANSLATOR_APIKEY");
String serviceUrl = System.getenv("LANGUAGE_TRANSLATOR_URL");
if (iamApiKey == null) {
iamApiKey = getProperty("language_translator.apikey");
serviceUrl = getProperty("language_translator.url");
}
assertNotNull("LANGUAGE_TRANSLATOR_APIKEY is not defined and config.properties doesn't have valid credentials.", iamApiKey);
Authenticator authenticator = new IamAuthenticator(iamApiKey);
service = new LanguageTranslator("2018-05-01", authenticator);
service.setServiceUrl(serviceUrl);
// issue currently where document translation fails with learning opt-out
Map<String, String> headers = new HashMap<>();
headers.put(WatsonHttpHeaders.X_WATSON_TEST, "1");
service.setDefaultHeaders(headers);
}
use of com.ibm.cloud.sdk.core.security.Authenticator in project java-sdk by watson-developer-cloud.
the class LanguageTranslatorTest method constructClientService.
// Constructs an instance of the service to be used by the tests
public void constructClientService() {
final String serviceName = "testService";
// set mock values for global params
String version = "2018-05-01";
final Authenticator authenticator = new NoAuthAuthenticator();
languageTranslatorService = new LanguageTranslator(version, serviceName, authenticator);
String url = server.url("/").toString();
languageTranslatorService.setServiceUrl(url);
}
use of com.ibm.cloud.sdk.core.security.Authenticator in project java-sdk by watson-developer-cloud.
the class NaturalLanguageUnderstandingTest method constructClientService.
// Constructs an instance of the service to be used by the tests
public void constructClientService() {
final String serviceName = "testService";
// set mock values for global params
String version = "testString";
final Authenticator authenticator = new NoAuthAuthenticator();
naturalLanguageUnderstandingService = new NaturalLanguageUnderstanding(version, serviceName, authenticator);
String url = server.url("/").toString();
naturalLanguageUnderstandingService.setServiceUrl(url);
}
Aggregations