Download the complete example code here.
Contact us to get the local indexer installer.
Download the file used to start the local indexer process here.
Example of getting access tokens and using them to make a basic API call
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://www.vizseek.com/api/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var formContent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("username", "[user name]"),
new KeyValuePair<string, string>("password", "[password]"),
new KeyValuePair<string, string>("client_id", "[your client id]")
});
HttpResponseMessage response = client.PostAsync("token", formContent).Result;
if (response.IsSuccessStatusCode)
{
var responseJson = response.Content.ReadAsStringAsync().Result;
var jObject = JObject.Parse(responseJson);
string access_token = jObject.GetValue("access_token").ToString();
string refreshToken = jObject.GetValue("refresh_token").ToString();
DateTime userTokenExpiration = DateTime.UtcNow.AddSeconds((double)Int32.Parse(jObject.GetValue("expires_in").ToString()));
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + access_token);
response = client.GetAsync("Version").Result;
if (response.IsSuccessStatusCode)
{
string version = response.Content.ReadAsAsync<string>().Result;
}
}
}
Example of getting your company ID (or "CompanyVizSpaceID")
HttpResponseMessage response = client.GetAsync("User?emailID=" + userEmail + "&password=" + userPassword).Result;
UserProfile user = response.Content.ReadAsAsync<UserProfile>().Result;
string myCompanyId = user.CompanyVizSpaceID;
Example of adding a file
byte[] exampleByteArray = new byte[100];
string attributes = HttpUtility.UrlEncode("color=red&size=large");
HttpResponseMessage response = client.PutAsJsonAsync("File?file=exampleFile.png&isSearchInput=false&attributes=" + attributes, JsonConvert.SerializeObject(exampleByteArray)).Result;
HttpResponseMessage response = client.PutAsJsonAsync("File?file=.png", JsonConvert.SerializeObject(exampleByteArray)).Result;
string fileUID = response.Content.ReadAsAsync<string>().Result;
Example of searching with a file
string filterStr = HttpUtility.UrlEncode("uid=[your company ID]&fileUID=" + fileUID + "&fileType=image");
HttpResponseMessage response = client.GetAsync("Search?searchTypeStr=file&filterStr=" + filterStr).Result;
string[] byteArrays = new string[1];
byteArrays[0] = new byte[100];
string filterStr = HttpUtility.UrlEncode("uid=[your company id]&fileType=image");
HttpResponseMessage response = client.PutAsJsonAsync("Search?searchTypeStr=file&fileExtension=.png&filterStr=" + filterStr, JsonConvert.SerializeObject(byteArrays)).Result;
SearchResultSummary search = response.Content.ReadAsAsync<SearchResultSummary>().Result;
Example of using the refresh token
When an access token expires, use the refresh token to get a new access token:
if (userTokenExpiration <= DateTime.UtcNow)
{
formContent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("grant_type", "refresh_token"),
new KeyValuePair<string, string>("refresh_token", refreshToken),
new KeyValuePair<string, string>("client_id", "[your client id]")
});
HttpResponseMessage response = client.PostAsync("token", formContent).Result;
if (response.IsSuccessStatusCode)
{
access_token = jObject.GetValue("access_token").ToString();
refreshToken = jObject.GetValue("refresh_token").ToString();
userTokenExpiration = DateTime.UtcNow.AddSeconds((double)Int32.Parse(jObject.GetValue("expires_in").ToString()));
}
}
Download the complete example code here:
VizSeekAPI.java,
Server.java
Example of getting access tokens and using them to make a basic API call
String postString = String.format("grant_type=password&username=%s&password=%s&client_id=%s", [user name], [password], [your client id]);
String RequestURL = "https://www.vizseek.com/api/token";
StringBuilder sb = new StringBuilder();
try {
URL url = new URL(RequestURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(timeout);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Accept", "text/json");
conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
byte[] outputInBytes = postString.getBytes("UTF-8");
OutputStream os = conn.getOutputStream();
os.write(outputInBytes);
os.close();
BufferedReader br;
if (200 <= conn.getResponseCode() && conn.getResponseCode() <= 299) {
br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
} else {
br = new BufferedReader(new InputStreamReader((conn.getErrorStream())));
}
String output;
while ((output = br.readLine()) != null)
sb.append(output);
} catch (MalformedURLException e) {
e.printStackTrace();
Log.i(TAG, "MalformedURLException: " + e);
} catch (ProtocolException e) {
e.printStackTrace();
Log.i(TAG, "ProtocolException: " + e);
} catch (IOException e) {
e.printStackTrace();
Log.i(TAG, "IOException: " + e);
}
String response = sb.toString();
try {
JSONObject reader = new JSONObject(response);
String access_token = reader.getString("access_token");
String refreshToken = reader.getString("refresh_token");
Log.i(TAG,access_token);
}
catch (JSONException e){
e.printStackTrace();
}
Example of adding a file
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArrayImage = stream.toByteArray();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArrayImage = stream.toByteArray();
String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
String bodyStr = "\"\\\"" + encodedImage + "\\\"\"";
String RequestURL = "File?file=exampleFile.png&isSearchInput=false&attributes=" + attributes;
String RequestURL = "https://www.vizseek.com/api/File?file=.jpg";
StringBuilder sb = new StringBuilder();
try {
URL url = new URL(RequestURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(timeout);
conn.setRequestMethod("PUT");
conn.setRequestProperty("Accept", "text/json");
conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
conn.addRequestProperty("Authorization", "Bearer " + token);
byte[] outputInBytes = bodyStr.getBytes("UTF-8");
OutputStream os = conn.getOutputStream();
os.write(outputInBytes);
os.close();
BufferedReader br;
if (200 <= conn.getResponseCode() && conn.getResponseCode() <= 299) {
br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
} else {
br = new BufferedReader(new InputStreamReader((conn.getErrorStream())));
}
String output;
while ((output = br.readLine()) != null)
sb.append(output);
} catch (MalformedURLException e) {
e.printStackTrace();
Log.i(TAG, "MalformedURLException: " + e);
} catch (ProtocolException e) {
e.printStackTrace();
Log.i(TAG, "ProtocolException: " + e);
} catch (IOException e) {
e.printStackTrace();
Log.i(TAG, "IOException: " + e);
}
String response = sb.toString();
Log.i(TAG,response);
Example of searching with a file
String filterStr = "uid="+ [your company id] + "&fileUID=" + fileUID + "&fileType=image";
try {
encodedfilterStr = URLEncoder.encode(filterStr, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String URI = URI = "searchTypeStr=" + [your company type (file or prod)] + "&filterstr=" + encodedfilterStr;
String RequestURL = "https://www.vizseek.com/api/Search";
try {
URL url = new URL(RequestURL + "?" + URI);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(timeout);
conn.setRequestMethod("PUT");
conn.setRequestProperty("Accept", "text/json");
conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
conn.addRequestProperty("Authorization", "Bearer " + token);
BufferedReader br;
if (200 <= conn.getResponseCode() && conn.getResponseCode() <= 299) {
br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
} else {
br = new BufferedReader(new InputStreamReader((conn.getErrorStream())));
}
String output;
while ((output = br.readLine()) != null)
sb.append(output);
} catch (MalformedURLException e) {
e.printStackTrace();
Log.i(TAG, "MalformedURLException: " + e);
} catch (ProtocolException e) {
e.printStackTrace();
Log.i(TAG, "ProtocolException: " + e);
} catch (IOException e) {
e.printStackTrace();
Log.i(TAG, "IOException: " + e);
}
String response = sb.toString();
Log.i(TAG,response);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArrayImage = stream.toByteArray();
String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
ArrayList<String> inputImageArrayList= new ArrayList<String>;
inputImageArrayList.add(encodedImage)
String bodyStrl;
if (inputImageArrayList.size() > 1) {
for (int i = 1; i < inputImageArrayList.size(); i++) {
str += "\\\",\\\"" + inputImageArrayList.get(i);
}
bodyStr = "\"[\\\"" + str + "\\\"]\"";
} else {
Log.i("VizSeek", "single image!");
bodyStr = "\"[\\\"" + inputImageArrayList.get(0) + "\\\"]\"";
}
String filterStr = "uid="+ [your company id] + "&fileExtension=.jpg&isGZipCompressed=false&fileType=image";
String encodedfilterStr = "";
try {
encodedfilterStr = URLEncoder.encode(filterStr, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String URI = URI = "searchTypeStr=" + [your company type (file or prod)] + "&filterstr=" + encodedfilterStr + "&fileExtension=.jpg&isGZipCompressed=false";
String RequestURL = "https://www.vizseek.com/api/Search";
StringBuilder sb = new StringBuilder();
try {
URL url = new URL(RequestURL + "?" + URI);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(timeout);
conn.setRequestMethod("PUT");
conn.setRequestProperty("Accept", "text/json");
conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
conn.addRequestProperty("Authorization", "Bearer " + token);
byte[] outputInBytes = bodyStr.getBytes("UTF-8");
OutputStream os = conn.getOutputStream();
os.write(outputInBytes);
os.close();
BufferedReader br;
if (200 <= conn.getResponseCode() && conn.getResponseCode() <= 299) {
br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
} else {
br = new BufferedReader(new InputStreamReader((conn.getErrorStream())));
}
String output;
while ((output = br.readLine()) != null)
sb.append(output);
} catch (MalformedURLException e) {
e.printStackTrace();
Log.i(TAG, "MalformedURLException: " + e);
} catch (ProtocolException e) {
e.printStackTrace();
Log.i(TAG, "ProtocolException: " + e);
} catch (IOException e) {
e.printStackTrace();
Log.i(TAG, "IOException: " + e);
}
String response = sb.toString();
Log.i(TAG,response);
Example of using the refresh token
When an access token expires, use the refresh token to get a new access token:
if(userTokenExpiration <= System.currentTimeMillis() )
{
String postString = String.format("grant_type=refresh_token&refresh_token=%s&client_id=%s", refreshToken, [your client id]);
String RequestURL = "https://www.vizseek.com/api/token";
}