Search in sources :

Example 1 with LbryResponseException

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;
}
Also used : OkHttpClient(okhttp3.OkHttpClient) ArrayList(java.util.ArrayList) Request(okhttp3.Request) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) LbryResponseException(com.odysee.app.exceptions.LbryResponseException) IOException(java.io.IOException) Uri(android.net.Uri) LbryRequestException(com.odysee.app.exceptions.LbryRequestException) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) Claim(com.odysee.app.model.Claim)

Example 2 with LbryResponseException

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;
}
Also used : OkHttpClient(okhttp3.OkHttpClient) ArrayList(java.util.ArrayList) Request(okhttp3.Request) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) LbryResponseException(com.odysee.app.exceptions.LbryResponseException) IOException(java.io.IOException) Uri(android.net.Uri) LbryRequestException(com.odysee.app.exceptions.LbryRequestException) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) UrlSuggestion(com.odysee.app.model.UrlSuggestion)

Example 3 with LbryResponseException

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);
    }
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) LbryResponseException(com.odysee.app.exceptions.LbryResponseException)

Example 4 with LbryResponseException

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;
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) LbryResponseException(com.odysee.app.exceptions.LbryResponseException) IOException(java.io.IOException)

Example 5 with LbryResponseException

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;
}
Also used : ApiCallException(com.odysee.app.exceptions.ApiCallException) JSONObject(org.json.JSONObject) LbryResponseException(com.odysee.app.exceptions.LbryResponseException) LbryRequestException(com.odysee.app.exceptions.LbryRequestException)

Aggregations

LbryResponseException (com.odysee.app.exceptions.LbryResponseException)17 JSONObject (org.json.JSONObject)15 JSONException (org.json.JSONException)12 LbryRequestException (com.odysee.app.exceptions.LbryRequestException)10 IOException (java.io.IOException)8 HashMap (java.util.HashMap)8 ApiCallException (com.odysee.app.exceptions.ApiCallException)7 ArrayList (java.util.ArrayList)7 LinkedHashMap (java.util.LinkedHashMap)6 JSONArray (org.json.JSONArray)6 OkHttpClient (okhttp3.OkHttpClient)5 Request (okhttp3.Request)5 Response (okhttp3.Response)5 Claim (com.odysee.app.model.Claim)3 Uri (android.net.Uri)2 ClaimCacheKey (com.odysee.app.model.ClaimCacheKey)2 LbryFile (com.odysee.app.model.LbryFile)2 ResponseBody (okhttp3.ResponseBody)2 Handler (android.os.Handler)1 WebResourceRequest (android.webkit.WebResourceRequest)1