use of com.odysee.app.exceptions.LbryResponseException 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.LbryResponseException 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.LbryResponseException in project odysee-android by OdyseeTeam.
the class Lbry method parseSdkResponse.
public static JSONObject parseSdkResponse(String responseString) throws LbryResponseException {
try {
JSONObject json = new JSONObject(responseString);
if (json.has("error")) {
String errorMessage = null;
Object jsonError = json.get("error");
if (jsonError instanceof String) {
errorMessage = jsonError.toString();
} else {
errorMessage = ((JSONObject) jsonError).getString("message");
}
throw new LbryResponseException(json.getString("error"));
}
return json;
} catch (JSONException ex) {
throw new LbryResponseException(String.format("Could not parse response: %s", responseString), ex);
}
}
use of com.odysee.app.exceptions.LbryResponseException in project odysee-android by OdyseeTeam.
the class Lbry method parseResponse.
public static Object parseResponse(Response response) throws LbryResponseException {
String responseString = null;
try {
responseString = response.body().string();
JSONObject json = new JSONObject(responseString);
if (response.code() >= 200 && response.code() < 300) {
if (json.has("result")) {
if (json.isNull("result")) {
return null;
}
return json.get("result");
} else {
processErrorJson(json);
}
}
processErrorJson(json);
} catch (JSONException | IOException ex) {
throw new LbryResponseException(String.format("Could not parse response: %s", responseString), ex);
}
return null;
}
use of com.odysee.app.exceptions.LbryResponseException 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;
}
Aggregations