List of usage examples for com.squareup.okhttp RequestBody create
public static RequestBody create(final MediaType contentType, final File file)
From source file:com.ibm.watson.developer_cloud.dialog.v1.DialogService.java
License:Open Source License
/** * Creates a dialog./*from www. java 2 s .c om*/ * * @param name The dialog name * @param dialogFile The dialog file created by using the Dialog service Applet. * @return The created dialog * @see Dialog */ public Dialog createDialog(final String name, final File dialogFile) { if (name == null || name.isEmpty()) throw new IllegalArgumentException("name cannot be null or empty"); if (dialogFile == null || !dialogFile.exists()) throw new IllegalArgumentException("dialogFile cannot be null or empty"); final RequestBody body = new MultipartBuilder().type(MultipartBuilder.FORM) .addFormDataPart(FILE, dialogFile.getName(), RequestBody.create(HttpMediaType.BINARY_FILE, dialogFile)) .addFormDataPart(NAME, name).build(); final Request request = RequestBuilder.post(PATH_DIALOGS).withBody(body).build(); return executeRequest(request, Dialog.class); }
From source file:com.ibm.watson.developer_cloud.dialog.v1.DialogService.java
License:Open Source License
/** * Updates a dialog.//ww w.jav a2 s . co m * * @param dialogId The dialog identifier * @param dialogFile The dialog file * @return the created dialog * @see Dialog */ public Dialog updateDialog(final String dialogId, final File dialogFile) { if (dialogId == null || dialogId.isEmpty()) throw new IllegalArgumentException("dialogId cannot be null or empty"); if (dialogFile == null || !dialogFile.exists()) throw new IllegalArgumentException("dialogFile cannot be null or empty"); final RequestBody body = new MultipartBuilder().type(MultipartBuilder.FORM).addFormDataPart(FILE, dialogFile.getName(), RequestBody.create(HttpMediaType.BINARY_FILE, dialogFile)).build(); final Request request = RequestBuilder.put(String.format(PATH_DIALOG, dialogId)).withBody(body).build(); executeWithoutResponse(request); final Dialog dialog = new Dialog().withDialogId(dialogId); return dialog; }
From source file:com.ibm.watson.developer_cloud.document_conversion.v1.DocumentConversion.java
License:Open Source License
/** * Converts a document and returns an {@link InputStream}. * /* w ww.j a va2s . co m*/ * @param document The file to convert * @param mediaType Internet media type of the file * @param conversionTarget The conversion target to use * @param customConfig The additional config params to use * @return Converted document in the specified format * @see {@link HttpMediaType} for available media types */ private InputStream convertDocument(final File document, final String mediaType, final ConversionTarget conversionTarget, final JsonObject customConfig) { if (document == null || !document.exists()) throw new IllegalArgumentException("document cannot be null and must exist"); if (customConfig == null) throw new NullPointerException("custom config must not be null"); final String type = mediaType != null ? mediaType : ConversionUtils.getMediaTypeFromFile(document); if (type == null) { throw new RuntimeException("mediaType cannot be null or empty"); } else if (!ConversionUtils.isValidMediaType(type)) { throw new IllegalArgumentException("file with the given media type is not supported"); } final JsonObject configJson = new JsonObject(); // Do this since we shouldn't mutate customConfig for (Map.Entry<String, JsonElement> entry : customConfig.entrySet()) { configJson.add(entry.getKey(), entry.getValue()); } // Add or override the conversion target configJson.addProperty(CONVERSION_TARGET, conversionTarget.toString()); final MediaType mType = MediaType.parse(type); final RequestBody body = new MultipartBuilder().type(MultipartBuilder.FORM) .addPart(Headers.of(HttpHeaders.CONTENT_DISPOSITION, "form-data; name=\"config\""), RequestBody.create(HttpMediaType.JSON, configJson.toString())) .addPart(Headers.of(HttpHeaders.CONTENT_DISPOSITION, "form-data; name=\"file\""), RequestBody.create(mType, document)) .build(); final Request request = RequestBuilder.post(CONVERT_DOCUMENT_PATH).withQuery(VERSION, versionDate) .withBody(body).build(); final Response response = execute(request); return ResponseUtil.getInputStream(response); }
From source file:com.ibm.watson.developer_cloud.http.RequestBuilder.java
License:Open Source License
/** * Builds a request with the given set of parameters and files. * /*from w w w . jav a 2 s. c o m*/ * * @return HTTP request, prepared to be executed */ public Request build() { final Builder builder = new Request.Builder(); // URL builder.url(toUrl()); // POST/PUT require a body so send an empty body if the actual is null RequestBody requestBody = body; if (body == null) requestBody = RequestBody.create(null, new byte[0]); if (!formParams.isEmpty()) { final FormEncodingBuilder formBody = new FormEncodingBuilder(); for (final NameValue param : formParams) { final String value = param.getValue() != null ? param.getValue() : ""; formBody.add(param.getName(), value); } requestBody = formBody.build(); } // accept application/json by default builder.addHeader(HttpHeaders.ACCEPT, HttpMediaType.APPLICATION_JSON); if (!headers.isEmpty()) { for (final NameValue header : headers) { builder.addHeader(header.getName(), header.getValue()); } } switch (method) { case GET: builder.get(); break; case POST: builder.post(requestBody); break; case PUT: builder.put(requestBody); break; case DELETE: builder.delete(requestBody); break; } return builder.build(); }
From source file:com.ibm.watson.developer_cloud.http.RequestBuilder.java
License:Open Source License
/** * Adds string content to the request (used with POST/PUT). This will encapsulate the string into * a {@link RequestBody} encoded with UTF-8 * //from w ww. j ava2s.c o m * @param content the content to POST/PUT * @param contentType the HTTP contentType to use. * * @return this */ public RequestBuilder withBodyContent(String content, String contentType) { body = RequestBody.create(MediaType.parse(contentType), content); return this; }
From source file:com.ibm.watson.developer_cloud.natural_language_classifier.v1.NaturalLanguageClassifier.java
License:Open Source License
/** * Sends data to create and train a classifier, and returns information about the new classifier. * The status has the value of `Training` when the operation is successful, and might remain at * this status for a while.// w ww . jav a2 s. com * * @param name the classifier name * @param language IETF primary language for the classifier. for example: 'en' * @param trainingData The set of questions and their "keys" used to adapt a system to a domain * (the ground truth) * @return the classifier * @see Classifier */ public Classifier createClassifier(final String name, final String language, final File trainingData) { Validate.isTrue(trainingData != null && trainingData.exists(), "trainingData cannot be null or not be found"); Validate.isTrue(language != null && !language.isEmpty(), "language cannot be null or empty"); final JsonObject contentJson = new JsonObject(); contentJson.addProperty(LANGUAGE, language); if (name != null && !name.isEmpty()) { contentJson.addProperty(NAME, name); } final RequestBody body = new MultipartBuilder().type(MultipartBuilder.FORM) .addPart(Headers.of(HttpHeaders.CONTENT_DISPOSITION, FORM_DATA_TRAINING_DATA), RequestBody.create(HttpMediaType.BINARY_FILE, trainingData)) .addFormDataPart(TRAINING_METADATA, contentJson.toString()).build(); final Request request = RequestBuilder.post(PATH_CLASSIFIERS).withBody(body).build(); final Response response = execute(request); return ResponseUtil.getObject(response, Classifier.class); }
From source file:com.ibm.watson.developer_cloud.retrieve_and_rank.v1.RetrieveAndRank.java
License:Open Source License
/** * 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./*from w w w . ja va 2s .c om*/ * * @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 Ranker createRanker(final String name, final File training) { Validate.notNull(training, "training file cannot be null"); Validate.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 MultipartBuilder().type(MultipartBuilder.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).withBody(body).build(); return executeRequest(request, Ranker.class); }
From source file:com.ibm.watson.developer_cloud.retrieve_and_rank.v1.RetrieveAndRank.java
License:Open Source License
/** * Gets and returns the ranked answers./* w w w . j av a 2 s. c o m*/ * * @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 Ranking rank(final String rankerID, final File answers, Integer topAnswers) { Validate.isTrue(rankerID != null && !rankerID.isEmpty(), "rankerID cannot be null or empty"); Validate.notNull(answers, "answers file cannot be null"); Validate.isTrue(answers.exists(), "answers file: " + answers.getAbsolutePath() + " not found"); final JsonObject contentJson = new JsonObject(); contentJson.addProperty(ANSWERS, (topAnswers != null && topAnswers > 0) ? topAnswers : 10); final RequestBody body = new MultipartBuilder().type(MultipartBuilder.FORM) .addPart(Headers.of(HttpHeaders.CONTENT_DISPOSITION, "form-data; name=\"answer_data\""), RequestBody.create(HttpMediaType.BINARY_FILE, answers)) .addPart(Headers.of(HttpHeaders.CONTENT_DISPOSITION, "form-data; name=\"answer_metadata\""), RequestBody.create(HttpMediaType.TEXT, contentJson.toString())) .build(); final String path = String.format(PATH_RANK, rankerID); final Request request = RequestBuilder.post(path).withBody(body).build(); return executeRequest(request, Ranking.class); }
From source file:com.ibm.watson.developer_cloud.retrieve_and_rank.v1.RetrieveAndRank.java
License:Open Source License
@Override public void uploadSolrClusterConfigurationZip(String solrClusterId, String configName, File zippedConfig) { final String configPath = createConfigPath(solrClusterId, configName); final RequestBuilder requestBuilder = RequestBuilder.post(configPath); requestBuilder.withBody(RequestBody.create(MediaType.parse(HttpMediaType.APPLICATION_ZIP), zippedConfig)); executeWithoutResponse(requestBuilder.build()); }
From source file:com.ibm.watson.developer_cloud.service.RequestBuilderTest.java
License:Open Source License
/** * Test with body./*from w ww .ja va2 s . co m*/ * * @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(urlWithQuery) .withBody(RequestBody.create(HttpMediaType.BINARY_FILE, test)).build(); final RequestBody requestedBody = request.body(); assertEquals(test.length(), requestedBody.contentLength()); assertEquals(HttpMediaType.BINARY_FILE, requestedBody.contentType()); }