use of com.github.kevinsawicki.http.HttpRequest in project seadroid by haiwen.
the class SeafConnection method createNewDir.
public Pair<String, String> createNewDir(String repoID, String parentDir, String dirName) throws SeafException {
HttpRequest req = null;
try {
String fullPath = Utils.pathJoin(parentDir, dirName);
final String encodeUriComponent = encodeUriComponent(fullPath).replaceAll("\\+", "%20");
Map<String, Object> params = Maps.newHashMap();
params.put("p", encodeUriComponent);
params.put("reloaddir", "true");
req = prepareApiPostRequest("api2/repos/" + repoID + "/dir/", true, params, false);
req.form("operation", "mkdir");
checkRequestResponseStatus(req, HttpURLConnection.HTTP_OK);
String newDirID = req.header("oid");
if (newDirID == null) {
return null;
}
String content = new String(req.bytes(), "UTF-8");
if (content.length() == 0) {
return null;
}
return new Pair<String, String>(newDirID, content);
} catch (SeafException e) {
throw e;
} catch (UnsupportedEncodingException e) {
throw SeafException.encodingException;
} catch (HttpRequestException e) {
throw getSeafExceptionFromHttpRequestException(e);
}
}
use of com.github.kevinsawicki.http.HttpRequest in project seadroid by haiwen.
the class SeafConnection method getServerInfo.
public String getServerInfo() throws SeafException {
String result;
try {
HttpRequest req = prepareApiGetRequest("api2/server-info/");
checkRequestResponseStatus(req, HttpURLConnection.HTTP_OK);
result = new String(req.bytes(), "UTF-8");
} catch (SeafException e) {
throw e;
} catch (HttpRequestException e) {
throw getSeafExceptionFromHttpRequestException(e);
} catch (IOException e) {
throw SeafException.networkException;
}
return result;
}
use of com.github.kevinsawicki.http.HttpRequest in project seadroid by haiwen.
the class SeafConnection method searchLibraries.
public String searchLibraries(String query, int page) throws SeafException {
try {
Map<String, Object> params = Maps.newHashMap();
params.put("q", encodeUriComponent(query));
if (page > 0)
params.put("per_page", page);
HttpRequest req = prepareApiGetRequest("api2/search/", params);
checkRequestResponseStatus(req, HttpURLConnection.HTTP_OK);
String result = new String(req.bytes(), "UTF-8");
return result;
} catch (SeafException e) {
throw e;
} catch (HttpRequestException e) {
throw getSeafExceptionFromHttpRequestException(e);
} catch (IOException e) {
throw SeafException.networkException;
}
}
use of com.github.kevinsawicki.http.HttpRequest in project seadroid by haiwen.
the class SeafConnection method getDirents.
/**
* Get the contents of a directory.
* @param repoID
* @param path
* @param cachedDirID The local cached dirID.
* @return A non-null Pair of (dirID, content). If the local cache is up to date, the "content" is null.
* @throws SeafException
*/
public Pair<String, String> getDirents(String repoID, String path, String cachedDirID) throws SeafException {
try {
String apiPath = String.format("api2/repos/%s/dir/", repoID);
Map<String, Object> params = Maps.newHashMap();
params.put("p", encodeUriComponent(path));
if (cachedDirID != null) {
params.put("oid", cachedDirID);
}
HttpRequest req = prepareApiGetRequest(apiPath, params);
checkRequestResponseStatus(req, HttpURLConnection.HTTP_OK);
String dirID = req.header("oid");
String content;
if (dirID == null) {
throw SeafException.unknownException;
}
if (dirID.equals(cachedDirID)) {
// local cache is valid
// Log.d(DEBUG_TAG, String.format("dir %s is cached", path));
content = null;
} else {
/*Log.d(DEBUG_TAG,
String.format("dir %s will be downloaded from server, latest %s, local cache %s",
path, dirID, cachedDirID != null ? cachedDirID : "null"));*/
byte[] rawBytes = req.bytes();
if (rawBytes == null) {
throw SeafException.unknownException;
}
content = new String(rawBytes, "UTF-8");
}
return new Pair<String, String>(dirID, content);
} catch (SeafException e) {
throw e;
} catch (UnsupportedEncodingException e) {
throw SeafException.encodingException;
} catch (HttpRequestException e) {
throw getSeafExceptionFromHttpRequestException(e);
} catch (IOException e) {
throw SeafException.networkException;
}
}
use of com.github.kevinsawicki.http.HttpRequest in project seadroid by haiwen.
the class SeafConnection method star.
public void star(String repoID, String path) throws SeafException {
try {
HttpRequest req = prepareApiPostRequest("api2/starredfiles/", true, null);
req.form("repo_id", repoID);
req.form("p", path);
checkRequestResponseStatus(req, HttpURLConnection.HTTP_CREATED);
} catch (SeafException e) {
throw e;
} catch (HttpRequestException e) {
throw getSeafExceptionFromHttpRequestException(e);
}
}
Aggregations