List of usage examples for com.squareup.okhttp MultipartBuilder FORM
MediaType FORM
To view the source code for com.squareup.okhttp MultipartBuilder FORM.
Click Source Link
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.//from w w w .j a v a2s . c om * * @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. j a va2 s .c o m*/ * * @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.//ww w .ja v 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.visual_insights.v1.VisualInsights.java
License:Open Source License
/** * Upload a set of images as a ZIP file for visual insight extraction. * //from ww w .ja va 2s . c om * <br> * Here is the code to get the {@link Summary} of a collection of images: * * <pre> * VisualInsights service = new VisualInsights(); * service.setUsernameAndPassword("<username>", "<password>"); * * File images = new File("images.zip"); * Summary summary = service.getSummary(images); * * System.out.println(summary); * </pre> * * @param imagesFile the images File * @return the {@link Summary} of the collection's visual attributes */ public Summary getSummary(final File imagesFile) { if (imagesFile == null || !imagesFile.exists()) throw new IllegalArgumentException("imagesFile cannot be null or empty"); final MediaType mediaType = HttpMediaType.BINARY_FILE; final RequestBody body = new MultipartBuilder().type(MultipartBuilder.FORM) .addFormDataPart(IMAGES_FILE, imagesFile.getName(), RequestBody.create(mediaType, imagesFile)) .build(); final RequestBuilder requestBuilder = RequestBuilder.post(SUMMARY_PATH).withBody(body); return executeRequest(requestBuilder.build(), Summary.class); }
From source file:com.ibm.watson.developer_cloud.visual_recognition.v1.VisualRecognition.java
License:Open Source License
public VisualRecognitionImages recognize(final String imageName, final InputStream image, final LabelSet labelSet) { if (image == null) throw new IllegalArgumentException("image cannot be null"); final RequestBuilder requestBuilder = RequestBuilder.post("/v1/tag/recognize"); final MultipartBuilder bodyBuilder = new MultipartBuilder().type(MultipartBuilder.FORM); bodyBuilder.addFormDataPart(IMG_FILE, imageName, InputStreamRequestBody.create(HttpMediaType.BINARY_FILE, image)); if (labelSet != null) bodyBuilder.addFormDataPart(LABELS_TO_CHECK, GsonSingleton.getGson().toJson(labelSet)); requestBuilder.withBody(bodyBuilder.build()); return executeRequest(requestBuilder.build(), VisualRecognitionImages.class); }
From source file:com.ibm.watson.developer_cloud.visual_recognition.v2.VisualRecognition.java
License:Open Source License
/** * Classifies the image/s against the Classifiers. The response includes a score for a classifier * only if the score meets the minimum threshold of 0.5. If no score meets the threshold for an * image, no classifiers are returned./*w ww. j a v a 2 s. co m*/ * * <br> * <br> * Here is an example of how to classify an InputStream image with an existing classifier: * * <pre> * VisualRecognition service = new VisualRecognition("2015-12-02"); * service.setUsernameAndPassword("<username>", "<password>"); * * FileInputStream image = new FileInputStream("car.png"); * VisualClassifier car = new VisualClassifier("Car"); * * VisualClassification result = service.classify("foo.png", image, car); * System.out.println(result); * </pre> * * @param filename The file name * @param imagesInputStream the image/s input stream to classify * @param classifiers the classifiers * @return the {@link Classification} */ public VisualClassification classify(final String filename, final InputStream imagesInputStream, final VisualClassifier... classifiers) { Validate.notNull(imagesInputStream, "image cannot be null"); Validate.notNull(filename, "filename cannot be null"); MultipartBuilder bodyBuilder = new MultipartBuilder().type(MultipartBuilder.FORM); bodyBuilder.addFormDataPart(IMAGES_FILE, filename, InputStreamRequestBody.create(HttpMediaType.BINARY_FILE, imagesInputStream)); if (classifiers != null && classifiers.length > 0 && classifiers[0] != null) { JsonObject classifierIds = getClassifierIdsAsJson(classifiers); bodyBuilder.addFormDataPart(CLASSIFIER_IDS, classifierIds.toString()); } RequestBuilder requestBuilder = RequestBuilder.post(PATH_CLASSIFY).withQuery(VERSION, versionDate) .withBody(bodyBuilder.build()); return executeRequest(requestBuilder.build(), VisualClassification.class); }
From source file:com.ibm.watson.developer_cloud.visual_recognition.v2.VisualRecognition.java
License:Open Source License
/** * Train a new classifier on the uploaded image data. Upload a compressed (.zip) file of images * (.jpg, .png, or .gif) with positive examples that show your classifier and another compressed * file with negative examples that are similar to but do NOT show your classifier. <br> * <br>//from w ww. j a v a2 s. c om * Here is an example of how to create a classifier with positive and negatives images: * * <pre> * VisualRecognition service = new VisualRecognition("2015-12-02"); * service.setUsernameAndPassword("<username>", "<password>"); * * File positiveImages = new File("positive.zip"); // zip file with at least 10 images * File negativeImages = new File("negative.zip"); * * VisualClassifier foo = service.createClassifier("foo", positiveImages, negativeImages); * System.out.println(foo); * </pre> * * * @param name the classifier name * @param positiveImages A compressed (.zip) file of images which prominently depict the visual * subject for a new classifier. Must contain a minimum of 10 images. * @param negativeImages A compressed (.zip) file of images which do not depict the subject for a * new classifier. Must contain a minimum of 10 images. * @return the created {@link VisualClassifier} * @see VisualClassifier */ public VisualClassifier createClassifier(final String name, final File positiveImages, final File negativeImages) { Validate.isTrue(positiveImages != null && positiveImages.exists(), "positiveImages cannot be null or not be found"); Validate.isTrue(negativeImages != null && negativeImages.exists(), "negativeImages cannot be null or not be found"); Validate.isTrue(name != null && !name.isEmpty(), "name cannot be null or empty"); // POST body RequestBody body = new MultipartBuilder().type(MultipartBuilder.FORM) .addFormDataPart(POSITIVE_EXAMPLES, positiveImages.getName(), RequestBody.create(HttpMediaType.BINARY_FILE, positiveImages)) .addFormDataPart(NEGATIVE_EXAMPLES, negativeImages.getName(), RequestBody.create(HttpMediaType.BINARY_FILE, negativeImages)) .addFormDataPart(NAME, name).build(); Request request = RequestBuilder.post(PATH_CLASSIFIERS).withQuery(VERSION, versionDate).withBody(body) .build(); return executeRequest(request, VisualClassifier.class); }
From source file:com.intuit.tank.okhttpclient.TankOkHttpClient.java
License:Open Source License
private RequestBody buildParts(BaseRequest request) { MultipartBuilder multipartBuilder = new MultipartBuilder().type(MultipartBuilder.FORM); for (PartHolder h : TankHttpUtil.getPartsFromBody(request)) { if (h.getFileName() == null) { if (h.isContentTypeSet()) { multipartBuilder.addFormDataPart(h.getPartName(), null, RequestBody.create(MediaType.parse(request.getContentType()), h.getBody())); } else { multipartBuilder.addFormDataPart(h.getPartName(), h.getBodyAsString()); }//from w ww .j a v a2 s. co m } else { if (h.isContentTypeSet()) { multipartBuilder.addFormDataPart(h.getPartName(), h.getFileName(), RequestBody.create(MediaType.parse(request.getContentType()), h.getBody())); } else { multipartBuilder.addFormDataPart(h.getPartName(), h.getFileName(), RequestBody.create(null, h.getBody())); } } } return multipartBuilder.build(); }
From source file:com.kkbox.toolkit.internal.api.APIRequest.java
License:Apache License
public void addMultiPartPostParam(String key, String fileName, RequestBody requestBody) { if (multipartBuilder == null) { multipartBuilder = new MultipartBuilder().type(MultipartBuilder.FORM); }/*from w ww . ja v a2 s . c o m*/ multipartBuilder.addFormDataPart(key, fileName, requestBody); }
From source file:com.kkbox.toolkit.internal.api.APIRequest.java
License:Apache License
public void addMultiPartPostParam(String key, String value) { if (multipartBuilder == null) { multipartBuilder = new MultipartBuilder().type(MultipartBuilder.FORM); }/* www . j a v a 2 s . c o m*/ multipartBuilder.addFormDataPart(key, value); }