All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jp.fkmsoft.libs.kiilib.apis.impl.KiiBucketAPI Maven / Gradle / Ivy

There is a newer version: 4.1.0
Show newest version
package jp.fkmsoft.libs.kiilib.apis.impl;

import jp.fkmsoft.libs.kiilib.apis.BucketAPI;
import jp.fkmsoft.libs.kiilib.entities.KiiBaseBucket;
import jp.fkmsoft.libs.kiilib.entities.KiiBaseObject;
import jp.fkmsoft.libs.kiilib.entities.KiiObjectFactory;
import jp.fkmsoft.libs.kiilib.entities.QueryParams;
import jp.fkmsoft.libs.kiilib.http.KiiHTTPClient.Method;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

class KiiBucketAPI> implements BucketAPI {

    private final KiiAppAPI api;
    private final KiiObjectFactory mObjectFactory;
    
    KiiBucketAPI(KiiAppAPI api, KiiObjectFactory objectFactory) {
        this.api = api;
        this.mObjectFactory = objectFactory;
    }

    @Override
    public void query(final BUCKET bucket, QueryParams condition, final QueryCallback callback) {
        String url = api.baseUrl + "/apps/" + api.appId + bucket.getResourcePath() + "/query";
        
        api.getHttpClient().sendJsonRequest(Method.POST, url, api.accessToken, 
                "application/vnd.kii.QueryRequest+json", null, condition.toJson(), new KiiResponseHandler>(callback) {
            @Override
            protected void onSuccess(JSONObject response, String etag, QueryCallback callback) {
                try {
                    JSONArray array = response.getJSONArray("results");
                    
                    KiiObjectQueryResult result = new KiiObjectQueryResult(array.length());
                    for (int i = 0 ; i < array.length() ; ++i) {
                        result.add(mObjectFactory.create(bucket, array.getJSONObject(i)));
                    }
                    
                    callback.onSuccess(result);
                } catch (JSONException e) {
                    callback.onError(e);
                }
            }
        });
    }

    @Override
    public void delete(final BUCKET bucket, BucketCallback callback) {
        String url = api.baseUrl + "/apps/" + api.appId + bucket.getResourcePath();
        
        api.getHttpClient().sendJsonRequest(Method.DELETE, url, api.accessToken, 
                null, null, null, new KiiResponseHandler>(callback) {
            @Override
            protected void onSuccess(JSONObject response, String etag, BucketCallback callback) {
                callback.onSuccess(bucket);
            }
        });
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy