Android Open Source - meets-android Api Method






From Project

Back to project page meets-android.

License

The source code is released under:

MIT License

If you think the Android project meets-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.theagilemonkeys.meets;
/*w  w w. j a  va  2  s.c o m*/
import com.octo.android.robospice.exception.RequestCancelledException;
import com.octo.android.robospice.persistence.DurationInMillis;
import com.octo.android.robospice.persistence.exception.KeySanitationExcepion;
import com.octo.android.robospice.persistence.exception.SpiceException;
import com.octo.android.robospice.persistence.keysanitation.DefaultKeySanitizer;
import com.octo.android.robospice.request.googlehttpclient.GoogleHttpClientSpiceRequest;
import com.octo.android.robospice.request.listener.RequestListener;
import com.theagilemonkeys.meets.utils.StringUtils;

import org.jdeferred.Deferred;
import org.jdeferred.impl.DeferredObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;

/**
 * Android Meets SDK
 * Original work Copyright (c) 2014 [TheAgileMonkeys]
 *
 * @author ??lvaro Lpez Espinosa
 */
public abstract class ApiMethod<RESULT> extends GoogleHttpClientSpiceRequest<RESULT> implements RequestListener<RESULT> {

    /////// Static global configuration. These values are used for all methods by default //////

    /////// CACHE ////////
    public static final AtomicLong globalCacheDuration = new AtomicLong(DurationInMillis.ONE_SECOND);
    public static final AtomicBoolean globalAlwaysGetFromCacheFirst = new AtomicBoolean(true);

    /////// FIXED PARAMS //////
    public static final Map<String,Object> fixedParams = Collections.synchronizedMap(new HashMap<String, Object>());
    public static final List<String> fixedUrlExtraSegments = Collections.synchronizedList(new ArrayList<String>());

    /////// BASIC AUTHORIZATION ///////
    private static String basicAuthName;
    private static String basicAuthPass;

    public static synchronized void setBasicAuth(String name, String pass){
        basicAuthName = name;
        basicAuthPass = pass;
    }
    public static synchronized String getBasicAuthName() {
        return basicAuthName;
    }
    public static synchronized String getBasicAuthPass() {
        return basicAuthPass;
    }

    //////// Instance attributes and methods ///////
    protected List<String> urlExtraSegments;
    protected Map <String, Object> params;
    protected final Class modelClass;
    protected Deferred runDeferred;
    protected long cacheDuration = globalCacheDuration.get();
    protected boolean alwaysGetFromCacheFirst = globalAlwaysGetFromCacheFirst.get();

    public ApiMethod(Class<RESULT> magentoModelClass) {
        super(magentoModelClass);
        modelClass = magentoModelClass;
    }

    /**
     * Set the cache duration for this method. This method will override default cache duration
     * for this method.
     * @param milliseconds The cache duration in milliseconds. If < 0, no cache will be used. If == 0,
     *                     the cache never expires. You will prefer use DurationInMillis constants.
     * @return This method
     */
    public ApiMethod<RESULT> setCacheDuration(long milliseconds) {
        cacheDuration = milliseconds;
        return this;
    }

    /**
     * If you pass true to this method and call "run", it will try to get the data from cache and,
     * regardless of the result, the request is always performed to update cache data (and return it
     * if it was not found before).
     * Note that if data in cache is found and is expired, the response listener will be called twice:
     * once with the dirty data from cache and another with the updated data when the request finish.
     * @param val
     * @return
     */
    public ApiMethod<RESULT> setAlwaysGetFromCacheFirst(boolean val) {
        alwaysGetFromCacheFirst = val;
        return this;
    }

    public ApiMethod<RESULT> clearCache() {
        Meets.spiceManager.removeAllDataFromCache();
        return this;
    }

    public Deferred run(String... urlExtraSegments) {
        return run(null, urlExtraSegments);
    }

    public Deferred run(Map<String, Object> params, String... urlExtraSegments){
        return run(params, Arrays.asList(urlExtraSegments));
    }

    public Deferred run(Map<String, Object> params, List<String> urlExtraSegments){
        runDeferred = new DeferredObject();

        this.params = new HashMap<String, Object>(fixedParams);
        if (params != null)
            this.params.putAll(params);

        this.urlExtraSegments = new ArrayList<String>(fixedUrlExtraSegments);
        if (urlExtraSegments != null)
            this.urlExtraSegments.addAll(urlExtraSegments);

        makeRequest();

        return runDeferred;
    }

    private void makeRequest() {
        if (cacheDuration >= 0){
            if (alwaysGetFromCacheFirst)
                Meets.spiceManager.getFromCacheAndLoadFromNetworkIfExpired(this, getCacheKey(), cacheDuration, this);
            else
                Meets.spiceManager.execute(this, getCacheKey(), cacheDuration, this);
        }
        else {
            Meets.spiceManager.execute(this, this);
        }
    }

    @Override
    public void onRequestFailure(SpiceException e) {
        if(runDeferred.isPending()) {
            runDeferred.reject(e);
            Meets.globalListener.onFail(null, e);
            Meets.globalListener.onAlways(null, e);
        }
    }

    @Override
    public void onRequestSuccess(RESULT response) {
        if(runDeferred.isPending()) {
            runDeferred.resolve(response);
            Meets.globalListener.onDone(null);
            Meets.globalListener.onAlways(null, null);
        }
    }

    public String getCacheKey(){
        String key = null;
        try {
            key = (String) new DefaultKeySanitizer().sanitizeKey(generateUrl());
        } catch (KeySanitationExcepion e) {
            e.printStackTrace();
        }
        return key;
    }

    protected abstract String getBaseUrl();

    protected String generateUrl(){
        return getBaseUrl() + getMethodName() + "/" + generateUrlExtraSegments() + "?" + generateUrlParams();
    }

    protected String getMethodName() {
        return StringUtils.toLowerCaseFirst(getClass().getSimpleName());
    }
    protected String generateUrlExtraSegments(){
        String res = "";
        if ( urlExtraSegments != null ){
            for ( String segment : urlExtraSegments ){
                res += segment + "/";
            }
        }
        return res;
    }

    protected String generateUrlParams(){
        String res = "";
        if ( params != null){
            for (Map.Entry<String, ?> entry : params.entrySet()){
                String key = entry.getKey();
                Object val = entry.getValue();
                if ( val instanceof List){
                    res += generateUrlListParamForKey(key, (List) val);
                }
                else{
                    res += key + "=" + String.valueOf(val) + "&";
                }
            }
        }
        return res;
    }

    protected String generateUrlListParamForKey(String key, List list) {
        String res = "";
        int listIndex = 0;
        for( Object elem : list ){
            String indexedKey = key + "[" + listIndex++ + "]";
            if ( elem instanceof Map){
                for( Map.Entry entry : ((Map<Object,Object>) elem).entrySet()){
                    res += indexedKey + "[" + String.valueOf(entry.getKey()) + "]="
                                      + String.valueOf(entry.getValue()) + "&";
                }
            }
            else{
                res += indexedKey + "=" + String.valueOf(elem) + "&";
            }
        }
        return res;
    }
}




Java Source Code List

com.theagilemonkeys.meets.ApiMethodModelHelperInterface.java
com.theagilemonkeys.meets.ApiMethodModelHelper.java
com.theagilemonkeys.meets.ApiMethod.java
com.theagilemonkeys.meets.MeetsSpiceService.java
com.theagilemonkeys.meets.Meets.java
com.theagilemonkeys.meets.magento.RestApiMethod.java
com.theagilemonkeys.meets.magento.SoapApiMethod.java
com.theagilemonkeys.meets.magento.methods.CatalogCategoryInfo.java
com.theagilemonkeys.meets.magento.methods.CatalogCategoryLevel.java
com.theagilemonkeys.meets.magento.methods.CatalogCategoryTree.java
com.theagilemonkeys.meets.magento.methods.CatalogInventoryStockItemList.java
com.theagilemonkeys.meets.magento.methods.CatalogProductAttributeOptions.java
com.theagilemonkeys.meets.magento.methods.CatalogProductInfo.java
com.theagilemonkeys.meets.magento.methods.CatalogProductList.java
com.theagilemonkeys.meets.magento.methods.CustomerAddressCreate.java
com.theagilemonkeys.meets.magento.methods.CustomerAddressDelete.java
com.theagilemonkeys.meets.magento.methods.CustomerAddressInfo.java
com.theagilemonkeys.meets.magento.methods.CustomerAddressList.java
com.theagilemonkeys.meets.magento.methods.CustomerAddressUpdate.java
com.theagilemonkeys.meets.magento.methods.CustomerCustomerCreate.java
com.theagilemonkeys.meets.magento.methods.CustomerCustomerInfo.java
com.theagilemonkeys.meets.magento.methods.CustomerCustomerList.java
com.theagilemonkeys.meets.magento.methods.CustomerCustomerUpdate.java
com.theagilemonkeys.meets.magento.methods.Products.java
com.theagilemonkeys.meets.magento.methods.ShoppingCartCreate.java
com.theagilemonkeys.meets.magento.methods.ShoppingCartCustomerAddresses.java
com.theagilemonkeys.meets.magento.methods.ShoppingCartCustomerSet.java
com.theagilemonkeys.meets.magento.methods.ShoppingCartInfo.java
com.theagilemonkeys.meets.magento.methods.ShoppingCartOrder.java
com.theagilemonkeys.meets.magento.methods.ShoppingCartPaymentList.java
com.theagilemonkeys.meets.magento.methods.ShoppingCartPaymentMethod.java
com.theagilemonkeys.meets.magento.methods.ShoppingCartProductAdd.java
com.theagilemonkeys.meets.magento.methods.ShoppingCartProductRemove.java
com.theagilemonkeys.meets.magento.methods.ShoppingCartShippingList.java
com.theagilemonkeys.meets.magento.methods.ShoppingCartShippingMethod.java
com.theagilemonkeys.meets.magento.models.MageMeetsAddress.java
com.theagilemonkeys.meets.magento.models.MageMeetsCartItem.java
com.theagilemonkeys.meets.magento.models.MageMeetsCartPayment.java
com.theagilemonkeys.meets.magento.models.MageMeetsCartShipping.java
com.theagilemonkeys.meets.magento.models.MageMeetsCart.java
com.theagilemonkeys.meets.magento.models.MageMeetsCategory.java
com.theagilemonkeys.meets.magento.models.MageMeetsCustomer.java
com.theagilemonkeys.meets.magento.models.MageMeetsProduct.java
com.theagilemonkeys.meets.magento.models.MageStockInfoList.java
com.theagilemonkeys.meets.magento.models.MageStockItem.java
com.theagilemonkeys.meets.magento.models.base.MageMeetsCollectionPojos.java
com.theagilemonkeys.meets.magento.models.base.MageMeetsCollection.java
com.theagilemonkeys.meets.magento.models.base.MageMeetsFactory.java
com.theagilemonkeys.meets.magento.models.base.MageMeetsModel.java
com.theagilemonkeys.meets.models.MeetsAddress.java
com.theagilemonkeys.meets.models.MeetsCart.java
com.theagilemonkeys.meets.models.MeetsCategory.java
com.theagilemonkeys.meets.models.MeetsCustomer.java
com.theagilemonkeys.meets.models.MeetsProduct.java
com.theagilemonkeys.meets.models.MeetsStock.java
com.theagilemonkeys.meets.models.base.MeetsCollectionPojos.java
com.theagilemonkeys.meets.models.base.MeetsCollection.java
com.theagilemonkeys.meets.models.base.MeetsFactory.java
com.theagilemonkeys.meets.models.base.MeetsListener.java
com.theagilemonkeys.meets.models.base.MeetsModel.java
com.theagilemonkeys.meets.utils.Copier.java
com.theagilemonkeys.meets.utils.StringUtils.java
com.theagilemonkeys.meets.utils.soap.Serializable.java
com.theagilemonkeys.meets.utils.soap.SoapParser.java