Search in sources :

Example 1 with LbryRequestException

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;
}
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 LbryRequestException

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;
}
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 LbryRequestException

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

Example 4 with LbryRequestException

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

Example 5 with LbryRequestException

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;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ApiCallException(com.odysee.app.exceptions.ApiCallException) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) LbryResponseException(com.odysee.app.exceptions.LbryResponseException) LbryRequestException(com.odysee.app.exceptions.LbryRequestException) ClaimCacheKey(com.odysee.app.model.ClaimCacheKey) JSONObject(org.json.JSONObject) LbryFile(com.odysee.app.model.LbryFile) JSONObject(org.json.JSONObject)

Aggregations

LbryRequestException (com.odysee.app.exceptions.LbryRequestException)11 LbryResponseException (com.odysee.app.exceptions.LbryResponseException)10 JSONObject (org.json.JSONObject)9 JSONException (org.json.JSONException)8 ApiCallException (com.odysee.app.exceptions.ApiCallException)7 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 LinkedHashMap (java.util.LinkedHashMap)6 IOException (java.io.IOException)5 OkHttpClient (okhttp3.OkHttpClient)5 Request (okhttp3.Request)5 JSONArray (org.json.JSONArray)5 Response (okhttp3.Response)4 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