本文整理了Java中com.ibm.watson.developer_cloud.http.RequestBuilder.body
方法的一些代码示例,展示了RequestBuilder.body
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。RequestBuilder.body
方法的具体详情如下:
包路径:com.ibm.watson.developer_cloud.http.RequestBuilder
类名称:RequestBuilder
方法名:body
[英]Sets the body.
[中]设置身体。
代码示例来源:origin: watson-developer-cloud/java-sdk
/**
* Sets the string content to the request (used with POST/PUT). This will encapsulate the string into a
* {@link RequestBody} encoded with UTF-8
*
* @param content the content to POST/PUT
* @param contentType the HTTP contentType to use.
*
* @return this
*/
public RequestBuilder bodyContent(String content, String contentType) {
return body(RequestBody.create(MediaType.parse(contentType), content));
}
代码示例来源:origin: com.ibm.watson.developer_cloud/core
/**
* Sets the string content to the request (used with POST/PUT). This will encapsulate the string into a
* {@link RequestBody} encoded with UTF-8
*
* @param content the content to POST/PUT
* @param contentType the HTTP contentType to use.
*
* @return this
*/
public RequestBuilder bodyContent(String content, String contentType) {
return body(RequestBody.create(MediaType.parse(contentType), content));
}
代码示例来源:origin: watson-developer-cloud/java-sdk
/**
* Sets the file content (InputStream) to the request (used with POST/PUT).
*
* @param stream the InputStream to read the request body content from
* @param contentType the contentType associated with the data read from the InputStream
* @return this
*/
public RequestBuilder bodyContent(InputStream stream, String contentType) {
return body(InputStreamRequestBody.create(MediaType.parse(contentType), stream));
}
代码示例来源:origin: com.ibm.watson.developer_cloud/core
/**
* Sets the file content (InputStream) to the request (used with POST/PUT).
*
* @param stream the InputStream to read the request body content from
* @param contentType the contentType associated with the data read from the InputStream
* @return this
*/
public RequestBuilder bodyContent(InputStream stream, String contentType) {
return body(InputStreamRequestBody.create(MediaType.parse(contentType), stream));
}
代码示例来源:origin: com.ibm.watson.developer_cloud/retrieve-and-rank
private RequestBuilder createUploadSolrConfigurationRequest(String solrClusterId, String configName,
File zippedConfig) {
final String configPath = createConfigPath(solrClusterId, configName);
final RequestBuilder requestBuilder = RequestBuilder.post(configPath);
requestBuilder.body(RequestBody.create(MediaType.parse(HttpMediaType.APPLICATION_ZIP), zippedConfig));
return requestBuilder;
}
代码示例来源:origin: com.ibm.watson.developer_cloud/dialog
/**
* Updates an existing {@link Dialog}.
*
* @param dialogId The dialog identifier
* @param dialogFile The dialog file
* @return the service call
* @see Dialog
*/
public ServiceCall<Void> updateDialog(final String dialogId, final File dialogFile) {
Validator.isTrue((dialogId != null) && !dialogId.isEmpty(), "dialogId cannot be null or empty");
Validator.isTrue((dialogFile != null) && dialogFile.exists(), "dialogFile cannot be null or inexistent");
final RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
.addFormDataPart(FILE, dialogFile.getName(), RequestBody.create(HttpMediaType.BINARY_FILE, dialogFile)).build();
final Request request = RequestBuilder.put(String.format(PATH_DIALOG, dialogId)).body(body).build();
return createServiceCall(request, ResponseConverterUtils.getVoid());
}
代码示例来源:origin: com.ibm.watson.developer_cloud/retrieve-and-rank
/**
* Sends data to create and train a ranker, and returns information about the new ranker. The status has the value of
* `Training` when the operation is successful, and might remain at this status for a while.
*
* @param name Name of the ranker
* @param training The file with the training data i.e., the set of (qid, feature values, and rank) tuples
* @return the ranker object
* @see Ranker
*/
public ServiceCall<Ranker> createRanker(final String name, final File training) {
Validator.notNull(training, "training file cannot be null");
Validator.isTrue(training.exists(), "training file: " + training.getAbsolutePath() + " not found");
final JsonObject contentJson = new JsonObject();
if ((name != null) && !name.isEmpty()) {
contentJson.addProperty(NAME, name);
}
final RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
.addPart(Headers.of(HttpHeaders.CONTENT_DISPOSITION, "form-data; name=\"training_data\""),
RequestBody.create(HttpMediaType.BINARY_FILE, training))
.addPart(Headers.of(HttpHeaders.CONTENT_DISPOSITION, "form-data; name=\"training_metadata\""),
RequestBody.create(HttpMediaType.TEXT, contentJson.toString()))
.build();
final Request request = RequestBuilder.post(PATH_CREATE_RANKER).body(body).build();
return createServiceCall(request, ResponseConverterUtils.getObject(Ranker.class));
}
代码示例来源:origin: com.ibm.watson.developer_cloud/dialog
/**
* Creates a dialog.
*
* @param name The dialog name
* @param dialogFile The dialog script file
* @return The created dialog
* @see Dialog
*/
public ServiceCall<Dialog> createDialog(final String name, final File dialogFile) {
Validator.isTrue((name != null) && !name.isEmpty(), "name cannot be null or empty");
Validator.isTrue((dialogFile != null) && dialogFile.exists(), "dialogFile cannot be null or inexistent");
final RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
.addFormDataPart(FILE, dialogFile.getName(), RequestBody.create(HttpMediaType.BINARY_FILE, dialogFile))
.addFormDataPart(NAME, name).build();
final Request request = RequestBuilder.post(PATH_DIALOGS).body(body).build();
return createServiceCall(request, ResponseConverterUtils.getObject(Dialog.class));
}
代码示例来源:origin: com.ibm.watson.developer_cloud/retrieve-and-rank
/**
* Gets and returns the ranked answers.
*
* @param rankerID The ranker ID
* @param answers The CSV file that contains the search results that you want to rank.
* @param topAnswers The number of top answers needed, default is 10
* @return the ranking of the answers
*/
public ServiceCall<Ranking> rank(final String rankerID, final File answers, Integer topAnswers) {
Validator.isTrue((rankerID != null) && !rankerID.isEmpty(), "rankerID cannot be null or empty");
Validator.notNull(answers, "answers file cannot be null");
Validator.isTrue(answers.exists(), "answers file: " + answers.getAbsolutePath() + " not found");
final okhttp3.MultipartBody.Builder builder = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addPart(Headers.of(
HttpHeaders.CONTENT_DISPOSITION, "form-data; name=\"answer_data\""),
RequestBody.create(HttpMediaType.BINARY_FILE, answers));
if (topAnswers != null) {
builder.addFormDataPart(ANSWERS, topAnswers.toString());
}
final String path = String.format(PATH_RANK, rankerID);
final Request request = RequestBuilder.post(path).body(builder.build()).build();
return createServiceCall(request, ResponseConverterUtils.getObject(Ranking.class));
}
代码示例来源:origin: watson-developer-cloud/java-sdk
/**
* Test with body.
*
* @throws IOException Signals that an I/O exception has occurred.
*/
@Test
public void testWithBody() throws IOException {
final File test = new File("src/test/resources/car.png");
final Request request =
RequestBuilder.post(HttpUrl.parse(urlWithQuery))
.body(RequestBody.create(HttpMediaType.BINARY_FILE, test))
.build();
final RequestBody requestedBody = request.body();
assertEquals(test.length(), requestedBody.contentLength());
assertEquals(HttpMediaType.BINARY_FILE, requestedBody.contentType());
}
代码示例来源:origin: com.ibm.watson.developer_cloud/core
/**
* Refresh an IAM token using a refresh token. Also updates internal managed IAM token information.
*
* @return the new access token
*/
private String refreshToken() {
RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(url, new String[0]));
builder.header(HttpHeaders.CONTENT_TYPE, HttpMediaType.APPLICATION_FORM_URLENCODED);
builder.header(HttpHeaders.AUTHORIZATION, DEFAULT_AUTHORIZATION);
FormBody formBody = new FormBody.Builder()
.add(GRANT_TYPE, REFRESH_GRANT_TYPE)
.add(REFRESH_TOKEN, tokenData.getRefreshToken())
.build();
builder.body(formBody);
tokenData = callIamApi(builder.build());
return tokenData.getAccessToken();
}
代码示例来源:origin: watson-developer-cloud/java-sdk
/**
* Refresh an IAM token using a refresh token. Also updates internal managed IAM token information.
*
* @return the new access token
*/
private String refreshToken() {
RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(url, new String[0]));
builder.header(HttpHeaders.CONTENT_TYPE, HttpMediaType.APPLICATION_FORM_URLENCODED);
builder.header(HttpHeaders.AUTHORIZATION, DEFAULT_AUTHORIZATION);
FormBody formBody = new FormBody.Builder()
.add(GRANT_TYPE, REFRESH_GRANT_TYPE)
.add(REFRESH_TOKEN, tokenData.getRefreshToken())
.build();
builder.body(formBody);
tokenData = callIamApi(builder.build());
return tokenData.getAccessToken();
}
代码示例来源:origin: watson-developer-cloud/java-sdk
/**
* Request an IAM token using an API key. Also updates internal managed IAM token information.
*
* @return the new access token
*/
private String requestToken() {
RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(url, new String[0]));
builder.header(HttpHeaders.CONTENT_TYPE, HttpMediaType.APPLICATION_FORM_URLENCODED);
builder.header(HttpHeaders.AUTHORIZATION, DEFAULT_AUTHORIZATION);
FormBody formBody = new FormBody.Builder()
.add(GRANT_TYPE, REQUEST_GRANT_TYPE)
.add(API_KEY, apiKey)
.add(RESPONSE_TYPE, CLOUD_IAM)
.build();
builder.body(formBody);
tokenData = callIamApi(builder.build());
return tokenData.getAccessToken();
}
代码示例来源:origin: com.ibm.watson.developer_cloud/core
/**
* Request an IAM token using an API key. Also updates internal managed IAM token information.
*
* @return the new access token
*/
private String requestToken() {
RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(url, new String[0]));
builder.header(HttpHeaders.CONTENT_TYPE, HttpMediaType.APPLICATION_FORM_URLENCODED);
builder.header(HttpHeaders.AUTHORIZATION, DEFAULT_AUTHORIZATION);
FormBody formBody = new FormBody.Builder()
.add(GRANT_TYPE, REQUEST_GRANT_TYPE)
.add(API_KEY, apiKey)
.add(RESPONSE_TYPE, CLOUD_IAM)
.build();
builder.body(formBody);
tokenData = callIamApi(builder.build());
return tokenData.getAccessToken();
}
代码示例来源:origin: watson-developer-cloud/java-sdk
/**
* Create stopword list.
*
* Upload a custom stopword list to use with the specified collection.
*
* @param createStopwordListOptions the {@link CreateStopwordListOptions} containing the options for the call
* @return a {@link ServiceCall} with a response type of {@link TokenDictStatusResponse}
*/
public ServiceCall<TokenDictStatusResponse> createStopwordList(CreateStopwordListOptions createStopwordListOptions) {
Validator.notNull(createStopwordListOptions, "createStopwordListOptions cannot be null");
String[] pathSegments = { "v1/environments", "collections", "word_lists/stopwords" };
String[] pathParameters = { createStopwordListOptions.environmentId(), createStopwordListOptions.collectionId() };
RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments,
pathParameters));
builder.query(VERSION, versionDate);
MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
multipartBuilder.setType(MultipartBody.FORM);
RequestBody stopwordFileBody = RequestUtils.inputStreamBody(createStopwordListOptions.stopwordFile(),
"application/octet-stream");
multipartBuilder.addFormDataPart("stopword_file", createStopwordListOptions.stopwordFilename(), stopwordFileBody);
builder.body(multipartBuilder.build());
return createServiceCall(builder.build(), ResponseConverterUtils.getObject(TokenDictStatusResponse.class));
}
代码示例来源:origin: watson-developer-cloud/java-sdk
/**
* Create classifier.
*
* Sends data to create and train a classifier and returns information about the new classifier.
*
* @param createClassifierOptions the {@link CreateClassifierOptions} containing the options for the call
* @return a {@link ServiceCall} with a response type of {@link Classifier}
*/
public ServiceCall<Classifier> createClassifier(CreateClassifierOptions createClassifierOptions) {
Validator.notNull(createClassifierOptions, "createClassifierOptions cannot be null");
String[] pathSegments = { "v1/classifiers" };
RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments));
MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
multipartBuilder.setType(MultipartBody.FORM);
RequestBody trainingMetadataBody = RequestUtils.inputStreamBody(createClassifierOptions.metadata(),
"application/json");
multipartBuilder.addFormDataPart("training_metadata", createClassifierOptions.metadataFilename(),
trainingMetadataBody);
RequestBody trainingDataBody = RequestUtils.inputStreamBody(createClassifierOptions.trainingData(), "text/csv");
multipartBuilder.addFormDataPart("training_data", createClassifierOptions.trainingDataFilename(), trainingDataBody);
builder.body(multipartBuilder.build());
return createServiceCall(builder.build(), ResponseConverterUtils.getObject(Classifier.class));
}
代码示例来源:origin: watson-developer-cloud/java-sdk
/**
* Classify the elements of a document.
*
* Uploads a file. The response includes an analysis of the document's structural and semantic elements.
*
* @param classifyElementsOptions the {@link ClassifyElementsOptions} containing the options for the call
* @return a {@link ServiceCall} with a response type of {@link ClassifyReturn}
*/
public ServiceCall<ClassifyReturn> classifyElements(ClassifyElementsOptions classifyElementsOptions) {
Validator.notNull(classifyElementsOptions, "classifyElementsOptions cannot be null");
String[] pathSegments = { "v1/element_classification" };
RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments));
builder.query(VERSION, versionDate);
if (classifyElementsOptions.modelId() != null) {
builder.query("model_id", classifyElementsOptions.modelId());
}
MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
multipartBuilder.setType(MultipartBody.FORM);
RequestBody fileBody = RequestUtils.inputStreamBody(classifyElementsOptions.file(), classifyElementsOptions
.fileContentType());
multipartBuilder.addFormDataPart("file", classifyElementsOptions.filename(), fileBody);
builder.body(multipartBuilder.build());
return createServiceCall(builder.build(), ResponseConverterUtils.getObject(ClassifyReturn.class));
}
代码示例来源:origin: watson-developer-cloud/java-sdk
/**
* Convert file to HTML.
*
* Uploads an input file. The response includes an HTML version of the document.
*
* @param convertToHtmlOptions the {@link ConvertToHtmlOptions} containing the options for the call
* @return a {@link ServiceCall} with a response type of {@link HTMLReturn}
*/
public ServiceCall<HTMLReturn> convertToHtml(ConvertToHtmlOptions convertToHtmlOptions) {
Validator.notNull(convertToHtmlOptions, "convertToHtmlOptions cannot be null");
String[] pathSegments = { "v1/html_conversion" };
RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments));
builder.query(VERSION, versionDate);
if (convertToHtmlOptions.modelId() != null) {
builder.query("model_id", convertToHtmlOptions.modelId());
}
MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
multipartBuilder.setType(MultipartBody.FORM);
RequestBody fileBody = RequestUtils.inputStreamBody(convertToHtmlOptions.file(), convertToHtmlOptions
.fileContentType());
multipartBuilder.addFormDataPart("file", convertToHtmlOptions.filename(), fileBody);
builder.body(multipartBuilder.build());
return createServiceCall(builder.build(), ResponseConverterUtils.getObject(HTMLReturn.class));
}
代码示例来源:origin: watson-developer-cloud/java-sdk
/**
* Extract a document's tables.
*
* Uploads a file. The response includes an analysis of the document's tables.
*
* @param extractTablesOptions the {@link ExtractTablesOptions} containing the options for the call
* @return a {@link ServiceCall} with a response type of {@link TableReturn}
*/
public ServiceCall<TableReturn> extractTables(ExtractTablesOptions extractTablesOptions) {
Validator.notNull(extractTablesOptions, "extractTablesOptions cannot be null");
String[] pathSegments = { "v1/tables" };
RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments));
builder.query(VERSION, versionDate);
if (extractTablesOptions.modelId() != null) {
builder.query("model_id", extractTablesOptions.modelId());
}
MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
multipartBuilder.setType(MultipartBody.FORM);
RequestBody fileBody = RequestUtils.inputStreamBody(extractTablesOptions.file(), extractTablesOptions
.fileContentType());
multipartBuilder.addFormDataPart("file", extractTablesOptions.filename(), fileBody);
builder.body(multipartBuilder.build());
return createServiceCall(builder.build(), ResponseConverterUtils.getObject(TableReturn.class));
}
代码示例来源:origin: com.ibm.watson.developer_cloud/natural-language-classifier
/**
* Create classifier.
*
* Sends data to create and train a classifier and returns information about the new classifier.
*
* @param createClassifierOptions the {@link CreateClassifierOptions} containing the options for the call
* @return a {@link ServiceCall} with a response type of {@link Classifier}
*/
public ServiceCall<Classifier> createClassifier(CreateClassifierOptions createClassifierOptions) {
Validator.notNull(createClassifierOptions, "createClassifierOptions cannot be null");
String[] pathSegments = { "v1/classifiers" };
RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments));
builder.header("X-IBMCloud-SDK-Analytics",
"service_name=natural_language_classifier;service_version=v1;operation_id=createClassifier");
MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
multipartBuilder.setType(MultipartBody.FORM);
RequestBody trainingMetadataBody = RequestUtils.inputStreamBody(createClassifierOptions.metadata(),
"application/json");
multipartBuilder.addFormDataPart("training_metadata", createClassifierOptions.metadataFilename(),
trainingMetadataBody);
RequestBody trainingDataBody = RequestUtils.inputStreamBody(createClassifierOptions.trainingData(), "text/csv");
multipartBuilder.addFormDataPart("training_data", createClassifierOptions.trainingDataFilename(), trainingDataBody);
builder.body(multipartBuilder.build());
return createServiceCall(builder.build(), ResponseConverterUtils.getObject(Classifier.class));
}
内容来源于网络,如有侵权,请联系作者删除!