Skip to content

Instantly share code, notes, and snippets.

@Sunil02kumar
Last active June 18, 2018 15:46
Show Gist options
  • Save Sunil02kumar/12516b895671fcdc43e5a76af17ef842 to your computer and use it in GitHub Desktop.
Save Sunil02kumar/12516b895671fcdc43e5a76af17ef842 to your computer and use it in GitHub Desktop.
//this class is created to parse response from box while searching any content on box
public class BoxSearchContentResponseParser {
public Integer total_count {get;set;}
public List<Entries> entries {get;set;}
public Integer limit_Z {get;set;} // in json: limit
public Integer offset {get;set;}
public class Entries {
public String type_Z {get;set;} // in json: type
public String id {get;set;}
public String etag {get;set;}
public String name {get;set;}
public Parent parent {get;set;}
}
public class Parent {
public String type_Z {get;set;} // in json: type
public String id {get;set;}
public Object sequence_id {get;set;}
public Object etag {get;set;}
public String name {get;set;}
}
}
public class BoxUtility{
public static BoxSearchContentResponseParser SearchContentOnBox(string searchstr,string searchType,string folderidForSearch,string access_token){
BoxSearchContentResponseParser searchResults = new BoxSearchContentResponseParser ();
//searchType specify if you want to search on file or folder.
//for folder search specify searchType as 'folder' and for searching files specify searchType as 'file'
//searchstr is text which you want to search
string endPointUrl='https://api.box.com/2.0/search?query='+searchstr+'&type='+searchType;
//specify folderidForSearch if you want to search in specific folder
//if you want to search accross all folder then donot specify ancestor_folder_ids parameters
if(folderidForSearch !='' && folderidForSearch!=null && folderidForSearch!='0'){
endPointUrl = endPointUrl + '&ancestor_folder_ids='+folderidForSearch;
}
endPointUrl = endPointUrl + '&fields=name,parent,type,id&limit=200';
system.debug('***endPointUrl:'+endPointUrl);
Httprequest req = new HttpRequest();
req.setEndpoint(endPointUrl);
req.setMethod('GET');
req.setHeader('Authorization', 'Bearer '+access_token);
req.setHeader('Content-Type','application/json');
HttpResponse response = new HttpResponse();
try{
Http h1 = new Http();
response= h1.send(req);
system.debug('response *** '+response.getBody());
system.debug('***response.getStatusCode():'+response.getStatusCode());
//200 will be returned with total count for match
if(response.getStatusCode() == 200){
string resJSON =response.getBody();
resJSON = resJSON.replaceAll('type','type_Z');
searchResults = (BoxSearchContentResponseParser )System.JSON.deserialize(resJSON,BoxSearchContentResponseParser.class);
system.debug('***searchResults:'+searchResults);
}
}catch(exception ex){
system.debug('****some error occured in searchContentOnBox method:'+ex.getMessage());
}
return searchResults;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment