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.
Setup
All API calls should be inside the "using" statement
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://www.vizseek.com/api/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/json"));
}
Example of getting access tokens and using them to make a basic API call
private static string getAccessToken(HttpClient client, bool requiresUserToken)
{
string accessToken = null;
DateTime tokenExpiration = requiresUserToken ? UserTokenExpiration : AppTokenExpiration;
if (tokenExpiration <= DateTime.UtcNow)
{
var formContent;
if (requiresUserToken)
{
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]")
});
}
else
{
formContent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("grant_type", "client_credentials"),
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);
accessToken = jObject.GetValue("access_token").ToString();
DateTime expiration = DateTime.UtcNow.AddSeconds((double)Int32.Parse(jObject.GetValue("expires_in")));
if (requiresUserToken)
{
UserTokenExpiration = expiration;
}
else
{
AppTokenExpiration = expiration;
}
}
}
else
{
accessToken = requiresUserToken ? UserAccessToken : AppAccessToken;
}
return accessToken;
}
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + getAccessToken(client, false));
response = client.GetAsync("Version").Result;
if (response.IsSuccessStatusCode)
{
string version = response.Content.ReadAsAsync<string>().Result;
}
Example of getting your company ID (or "CompanyVizSpaceID")
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + getAccessToken(client, false));
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
string fileContent = Convert.ToBase64String([byte array of the file]);
string attributes = HttpUtility.UrlEncode("color=red&size=large");
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + getAccessToken(client, true));
HttpResponseMessage response = client.PutAsJsonAsync("File?file=exampleFile.png&isSearchInput=false&attributes=" + attributes, JsonConvert.SerializeObject(fileContent)).Result;
HttpResponseMessage response = client.PutAsJsonAsync("File?file=.png", JsonConvert.SerializeObject(fileContent)).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");
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + getAccessToken(client, true));
HttpResponseMessage response = client.GetAsync("Search?searchTypeStr=file&filterStr=" + filterStr).Result;
string[] byteArrays = new string[1];
byteArrays[0] = [byte array of the file];
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;
Download the complete example code here:
VizSeekAPI.java
Example of getting access tokens and using them to make a basic API call
private static String getAccessToken(boolean requiresUserToken) {
String accessToken = null;
long tokenExpirationMillis = requiresUserToken ? UserTokenExpiration : AppTokenExpiration;
if (tokenExpirationMillis <= System.currentTimeMillis()) {
String postString;
if (requiresUserToken) {
postString = String.format("grant_type=password&username=%s&password=%s&client_id=%s", [user name], [password], [your client id]);
} else {
postString = String.format("grant_type=client_credentials&client_id=%s", [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);
accessToken = reader.getString("access_token");
String expirationStr = reader.getString("expires_in");
tokenExpirationMillis = Long.parseLong(expirationStr, 10) * 1000;
if (requiresUserToken)
{
UserTokenExpiration = tokenExpirationMillis;
}
else
{
AppTokenExpiration = tokenExpirationMillis;
}
Log.i(TAG,access_token);
}
catch (JSONException e){
e.printStackTrace();
}
} else {
accessToken = requiresUserToken ? UserAccessToken : AppAccessToken; //use stored application-level variable from a previous request
}
return accessToken;
}
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 " + getAccessToken(true));
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 " + getAccessToken(true));
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 " + getAccessToken(true));
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);