use of com.odysee.app.exceptions.LbryRequestException in project odysee-android by OdyseeTeam.
the class Lighthouse method search.
public static List<Claim> search(String rawQuery, int size, int from, boolean nsfw, String relatedTo) throws LbryRequestException, LbryResponseException {
Uri.Builder uriBuilder = Uri.parse(String.format("%s/search", CONNECTION_STRING)).buildUpon().appendQueryParameter("s", rawQuery).appendQueryParameter("resolve", "true").appendQueryParameter("size", String.valueOf(size)).appendQueryParameter("from", String.valueOf(from));
if (!nsfw) {
uriBuilder.appendQueryParameter("nsfw", String.valueOf(nsfw).toLowerCase());
}
if (!Helper.isNullOrEmpty(relatedTo)) {
uriBuilder.appendQueryParameter("related_to", relatedTo);
}
Map<String, Object> cacheKey = buildSearchOptionsKey(rawQuery, size, from, nsfw, relatedTo);
if (searchCache.containsKey(cacheKey)) {
return searchCache.get(cacheKey);
}
List<Claim> results = new ArrayList<>();
Request request = new Request.Builder().url(uriBuilder.toString()).build();
OkHttpClient client = new OkHttpClient();
ResponseBody responseBody = null;
Response response = null;
try {
response = client.newCall(request).execute();
if (response.code() == 200) {
responseBody = response.body();
if (responseBody != null) {
JSONArray array = new JSONArray(responseBody.string());
for (int i = 0; i < array.length(); i++) {
Claim claim = Claim.fromSearchJSONObject(array.getJSONObject(i));
results.add(claim);
}
}
searchCache.put(cacheKey, results);
} else {
throw new LbryResponseException(response.message());
}
} catch (IOException ex) {
throw new LbryRequestException(String.format("search request for '%s' failed", rawQuery), ex);
} catch (JSONException ex) {
throw new LbryResponseException(String.format("the search response for '%s' could not be parsed", rawQuery), ex);
} finally {
if (responseBody != null) {
response.close();
}
}
return results;
}
use of com.odysee.app.exceptions.LbryRequestException in project odysee-android by OdyseeTeam.
the class Lighthouse method autocomplete.
public static List<UrlSuggestion> autocomplete(String text) throws LbryRequestException, LbryResponseException {
if (autocompleteCache.containsKey(text)) {
return autocompleteCache.get(text);
}
List<UrlSuggestion> suggestions = new ArrayList<>();
Uri.Builder uriBuilder = Uri.parse(String.format("%s/autocomplete", CONNECTION_STRING)).buildUpon().appendQueryParameter("s", text);
Request request = new Request.Builder().url(uriBuilder.toString()).build();
OkHttpClient client = new OkHttpClient();
Response response = null;
ResponseBody responseBody = null;
try {
response = client.newCall(request).execute();
if (response.code() == 200) {
responseBody = response.body();
if (responseBody != null) {
JSONArray array = new JSONArray(responseBody.string());
for (int i = 0; i < array.length(); i++) {
String item = array.getString(i);
boolean isChannel = item.startsWith("@");
LbryUri uri = new LbryUri();
if (isChannel) {
uri.setChannelName(item);
} else {
uri.setStreamName(item);
}
UrlSuggestion suggestion = new UrlSuggestion(isChannel ? UrlSuggestion.TYPE_CHANNEL : UrlSuggestion.TYPE_FILE, item);
suggestion.setUri(uri);
suggestions.add(suggestion);
}
}
autocompleteCache.put(text, suggestions);
} else {
throw new LbryResponseException(response.message());
}
} catch (IOException ex) {
throw new LbryRequestException(String.format("autocomplete request for '%s' failed", text), ex);
} catch (JSONException ex) {
throw new LbryResponseException(String.format("the autocomplete response for '%s' could not be parsed", text), ex);
} finally {
if (responseBody != null) {
response.close();
}
}
return suggestions;
}
use of com.odysee.app.exceptions.LbryRequestException in project odysee-android by OdyseeTeam.
the class Lbry method apiCall.
public static Response apiCall(String method, Map<String, Object> params, String connectionString, String authToken) throws LbryRequestException {
long counter = new Double(System.currentTimeMillis() / 1000.0).longValue();
if (Helper.isNullOrEmpty(authToken) && params != null && params.containsKey("auth_token") && params.get("auth_token") != null) {
authToken = params.get("auth_token").toString();
params.remove("auth_token");
}
JSONObject requestParams = buildJsonParams(params);
JSONObject requestBody = new JSONObject();
try {
requestBody.put("jsonrpc", "2.0");
requestBody.put("method", method);
requestBody.put("params", requestParams);
requestBody.put("counter", counter);
} catch (JSONException ex) {
throw new LbryRequestException("Could not build the JSON request body.", ex);
}
RequestBody body = RequestBody.create(requestBody.toString(), Helper.JSON_MEDIA_TYPE);
Request.Builder requestBuilder = new Request.Builder().url(connectionString).post(body);
if (!Helper.isNullOrEmpty(authToken)) {
requestBuilder.addHeader("X-Lbry-Auth-Token", authToken);
}
Request request = requestBuilder.build();
OkHttpClient client = new OkHttpClient.Builder().writeTimeout(300, TimeUnit.SECONDS).readTimeout(300, TimeUnit.SECONDS).build();
try {
return client.newCall(request).execute();
} catch (IOException ex) {
throw new LbryRequestException(String.format("\"%s\" method to %s failed", method, connectionString), ex);
}
}
use of com.odysee.app.exceptions.LbryRequestException in project odysee-android by OdyseeTeam.
the class Lbry method directApiCall.
/**
* @deprecated Use authenticatedGenericApiCall(String, Map, String) instead
* @param method
* @param p
* @param authToken
* @return
* @throws ApiCallException
*/
@Deprecated
public static Object directApiCall(String method, Map<String, Object> p, String authToken) throws ApiCallException {
p.put("auth_token", authToken);
Object response = null;
try {
response = parseResponse(apiCall(method, p, API_CONNECTION_STRING));
} catch (LbryRequestException | LbryResponseException ex) {
throw new ApiCallException(String.format("Could not execute %s call: %s", method, ex.getMessage()), ex);
}
return response;
}
use of com.odysee.app.exceptions.LbryRequestException in project odysee-android by OdyseeTeam.
the class Lbry method fileList.
public static List<LbryFile> fileList(String claimId, boolean downloads, int page, int pageSize) throws ApiCallException {
List<LbryFile> files = new ArrayList<>();
Map<String, Object> params = new HashMap<>();
if (!Helper.isNullOrEmpty(claimId)) {
params.put("claim_id", claimId);
}
if (downloads) {
params.put("download_path", null);
params.put("comparison", "ne");
}
if (page > 0) {
params.put("page", page);
}
if (pageSize > 0) {
params.put("page_size", pageSize);
}
try {
JSONObject result = (JSONObject) parseResponse(apiCall(METHOD_FILE_LIST, params));
JSONArray items = result.getJSONArray("items");
for (int i = 0; i < items.length(); i++) {
JSONObject fileObject = items.getJSONObject(i);
LbryFile file = LbryFile.fromJSONObject(fileObject);
files.add(file);
String fileClaimId = file.getClaimId();
if (!Helper.isNullOrEmpty(fileClaimId)) {
ClaimCacheKey key = new ClaimCacheKey();
key.setClaimId(fileClaimId);
if (claimCache.containsKey(key)) {
claimCache.get(key).setFile(file);
}
}
}
} catch (LbryRequestException | LbryResponseException | JSONException ex) {
throw new ApiCallException("Could not execute resolve call", ex);
}
return files;
}
Aggregations