/**
 * Copyright 2014-present Liquid Data Intelligence S.A.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.lqd.sdk;

import io.lqd.sdk.model.LQDataPoint;
import io.lqd.sdk.model.LQNetworkRequest;

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

public class LQRequestFactory {

	protected static final String LIQUID_SERVER_BASE_URL = "https://api.lqd.io/collect/";
	protected static final String LIQUID_DATAPOINT_URL = LIQUID_SERVER_BASE_URL + "data_points";
	protected static final String LIQUID_ALIAS_URL = LIQUID_SERVER_BASE_URL + "users/%s/alias";
	protected static final String LIQUID_LQD_PACKAGE_URL = LIQUID_SERVER_BASE_URL + "users/%s/devices/%s/liquid_package";
	protected static final String LIQUID_VARIABLES_URL = LIQUID_SERVER_BASE_URL + "variables";


	public static LQNetworkRequest createAliasRequest(String oldId, String newId) {
		String url = String.format(LIQUID_ALIAS_URL, oldId);
		String json;
		try {
			json = new JSONObject().put("new_user_id", newId).toString();
			return new LQNetworkRequest(url, "POST", json);
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static LQNetworkRequest createDataPointRequest(LQDataPoint datapoint) {
		return new LQNetworkRequest(LIQUID_DATAPOINT_URL, "POST", datapoint.toJSON().toString());
	}

	public static LQNetworkRequest requestLiquidPackageRequest(String userId, String userDevice) {
		String url = String.format(LIQUID_LQD_PACKAGE_URL, userId, userDevice);
		return new LQNetworkRequest(url, "GET", null);
	}

	public static LQNetworkRequest createVariableRequest(JSONObject variable) {
		return new LQNetworkRequest(LIQUID_VARIABLES_URL, "POST", variable.toString());
	}

}

@@ -1070,22 +1070,15 @@
  + %22
-users/%25s/
 alias
+es
 %22;%0A%09
@@ -1376,146 +1376,130 @@
 %7B%0A%09%09
-String url = String.format(LIQUID_ALIAS_URL, oldId);%0A%09%09String json;%0A%09%09try %7B%0A%09%09%09json = new JSONObject().put(%22new_user_id%22, newId).toString(
+JSONObject json;%0A%09%09try %7B%0A%09%09%09json = new JSONObject();%0A%09%09%09json.put(%22unique_id%22, newId);%0A%09%09%09json.put(%22unique_id_alias%22, oldId
 );%0A%09
@@ -1528,19 +1528,32 @@
 Request(
-url
+LIQUID_ALIAS_URL
 , %22POST%22
@@ -1558,16 +1558,27 @@
 T%22, json
+.toString()
 );%0A%09%09%7D c
@@ -1606,32 +1606,8 @@
 ) %7B%0A
-%09%09%09e.printStackTrace();%0A
 %09%09%09r

Change alias endpoint