Skip to content

Instantly share code, notes, and snippets.

@rushabhnagda11
Last active August 28, 2019 07:55
Show Gist options
  • Select an option

  • Save rushabhnagda11/41bbcc07aae4e4d3b44d68b9f6adb618 to your computer and use it in GitHub Desktop.

Select an option

Save rushabhnagda11/41bbcc07aae4e4d3b44d68b9f6adb618 to your computer and use it in GitHub Desktop.
package com.nanonets.app;
import java.io.File;
import java.io.IOException;
import java.util.Base64;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import java.util.concurrent.TimeUnit;;
public class App
{
private static final MediaType MEDIA_TYPE_JPG = MediaType.get("image/jpg");
private static final String URL = "https://app.nanonets.com/api/v2/ImageCategorization/LabelUrls/";
private static final String MODEL_ID = "MODEL_ID";
private static final String API_KEY = "API_KEY";
static String userCredentials = API_KEY + ":";
static String encoded = Base64.getEncoder().encodeToString(userCredentials.getBytes());
private static OkHttpClient client;
public static void getByUrls() throws IOException{
RequestBody requestBody = new okhttp3.MultipartBody.Builder()
.setType(okhttp3.MultipartBody.FORM)
.addFormDataPart("modelId", MODEL_ID)
.addFormDataPart("urls", "https://www.gstatic.com/webp/gallery/1.jpg" )
.addFormDataPart("urls", "https://goo.gl/ICoiHc" )
.addFormDataPart("urls", "https://goo.gl/ICoiHc" )
.addFormDataPart("urls", "https://goo.gl/ICoiHc" )
.addFormDataPart("urls", "https://goo.gl/ICoiHc" )
.addFormDataPart("urls", "https://goo.gl/ICoiHc" )
.addFormDataPart("urls", "https://goo.gl/ICoiHc" )
.addFormDataPart("urls", "https://goo.gl/ICoiHc" )
.addFormDataPart("urls", "https://goo.gl/ICoiHc" )
.addFormDataPart("urls", "https://goo.gl/ICoiHc" )
.addFormDataPart("urls", "https://goo.gl/ICoiHc" )
.addFormDataPart("urls", "https://goo.gl/ICoiHc" )
.addFormDataPart("urls", "https://www.gstatic.com/webp/gallery/1.jpg" )
.addFormDataPart("urls", "https://goo.gl/ICoiHc" )
.addFormDataPart("urls", "https://www.gstatic.com/webp/gallery/1.jpg" )
.build();
Request request = new Request.Builder()
.header("Authorization", "Basic "+encoded)
.url(URL)
.post(requestBody)
.build();
System.out.println(System.currentTimeMillis());
try (okhttp3.Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
System.out.println(response.body().string());
}
System.out.println(System.currentTimeMillis());
}
public static void getByFiles() throws IOException{
RequestBody requestBody = new okhttp3.MultipartBody.Builder()
.setType(okhttp3.MultipartBody.FORM)
.addFormDataPart("modelId",MODEL_ID)
.addFormDataPart("file", "image1.jpg",
RequestBody.create(MEDIA_TYPE_JPG, new File("./images/image1.jpg")))
.addFormDataPart("file", "image2.jpg",
RequestBody.create(MEDIA_TYPE_JPG, new File("./images/image2.jpg")))
.build();
Request request = new Request.Builder()
.header("Authorization", "Basic "+encoded)
.url("https://app.nanonets.com/api/v2/ImageCategorization/LabelFile/")
.post(requestBody)
.build();
try (okhttp3.Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
System.out.println(response.body().string());
}
}
public static void main(String[] args) throws IOException{
client = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build();
getByUrls();
//getByFiles();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment