package com.cloudmine.api.rest;

import com.android.volley.NetworkResponse;
import com.android.volley.Response;
import com.cloudmine.api.CMSessionToken;
import com.cloudmine.api.rest.response.CMResponse;
import me.cloudmine.annotations.Expand;
import me.cloudmine.annotations.Optional;

import java.util.Map;

/**
 * <br>Copyright CloudMine LLC. All rights reserved
 * <br> See LICENSE file included with SDK for details.
 */
public class BaseSnippetRequest extends CloudMineRequest<CMResponse> {
    public static final int REQUEST_TYPE = 414;

    private static final String BASE_ENDPOINT = "/run";

    private static String getSnippetUrl(String snippetName, Map<String, String> extraParams) {
        StringBuilder urlBuilder = new StringBuilder(BASE_ENDPOINT).append("/").append(CMURLBuilder.encode(snippetName));
        if(extraParams != null && !extraParams.isEmpty()) {
            String separator ="?";
            for(Map.Entry<String, String> extraParam : extraParams.entrySet()) {
                urlBuilder.append(separator).append(CMURLBuilder.encode(extraParam.getKey())).append("=").append(CMURLBuilder.encode(extraParam.getValue()));
                separator = "&";
            }
        }
        return urlBuilder.toString();
    }

    @Expand
    public BaseSnippetRequest(String snippetName, @Optional Map< String, String> extraParams, @Optional CMSessionToken sessionToken, @Optional Response.Listener<CMResponse> successListener, @Optional Response.ErrorListener errorListener) {
        super(Method.GET, getSnippetUrl(snippetName, extraParams), null, sessionToken, null, successListener, errorListener);
    }

    @Override
    protected Response<CMResponse> parseNetworkResponse(NetworkResponse networkResponse) {
        return Response.success(new CMResponse(new String(networkResponse.data), networkResponse.statusCode), getCacheEntry(networkResponse));
    }

    @Override
    public int getRequestType() {
        return REQUEST_TYPE;
    }
}

@@ -307,16 +307,203 @@
 p;%0A%0A/**%0A
+ * A Request for calling a server side code snippet. For detailed information about our%0A * server side code environment, view the documentation here: https://cloudmine.me/docs/servercode%0A
  * %3Cbr%3EC
@@ -1432,24 +1432,417 @@
 g();%0A    %7D%0A%0A
+    /**%0A     * Create a new SnippetRequest to run the Java or JavaScript code with the given snippetName%0A     * @param snippetName the name of the snippet to run%0A     * @param extraParams optional parameters that will be passed into the snippet%0A     * @param sessionToken optional, will be passed into the snippet if specified%0A     * @param successListener%0A     * @param errorListener%0A     */%0A
     @Expand%0A

Comment SnippetRequest