Created
May 21, 2023 20:52
-
-
Save Bfaschat/ab476aa578c8207aef52ec7e0b93ede4 to your computer and use it in GitHub Desktop.
class APIClient
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Source code recreated from a .class file by IntelliJ IDEA | |
// (powered by FernFlower decompiler) | |
// | |
package com.sendbird.desk.android; | |
import android.os.Build.VERSION; | |
import com.sendbird.android.SendBirdException; | |
import com.sendbird.android.shadow.com.google.gson.Gson; | |
import com.sendbird.android.shadow.com.google.gson.JsonElement; | |
import com.sendbird.android.shadow.com.google.gson.JsonNull; | |
import com.sendbird.android.shadow.com.google.gson.JsonObject; | |
import com.sendbird.android.shadow.com.google.gson.JsonParser; | |
import com.sendbird.android.shadow.okhttp3.Call; | |
import com.sendbird.android.shadow.okhttp3.Callback; | |
import com.sendbird.android.shadow.okhttp3.MediaType; | |
import com.sendbird.android.shadow.okhttp3.OkHttpClient; | |
import com.sendbird.android.shadow.okhttp3.Request; | |
import com.sendbird.android.shadow.okhttp3.RequestBody; | |
import com.sendbird.android.shadow.okhttp3.Response; | |
import com.sendbird.android.shadow.okhttp3.Request.Builder; | |
import com.sendbird.desk.android.SendBirdDesk.BuildConfig; | |
import java.io.IOException; | |
import java.io.UnsupportedEncodingException; | |
import java.net.URLEncoder; | |
import java.util.HashMap; | |
import java.util.Iterator; | |
import java.util.Map; | |
import java.util.Map.Entry; | |
class APIClient { | |
private static String mApiHost; | |
private static final String API_CUSTOMERS_AUTH = "/customers/auth/"; | |
private static final String API_CUSTOMERS_CUSTOM_FIELDS = "/customers/custom_fields/"; | |
static final String API_TICKETS = "/tickets/"; | |
static final String API_TICKETS_TICKETID = "/tickets/%s/"; | |
static final String API_TICKETS_TICKETID_EDITMESSAGE = "/tickets/%s/edit_message/"; | |
static final String API_TICKETS_TICKETID_REOPEN = "/tickets/%s/reopen/"; | |
static final String API_TICKETS_TICKETID_CLOSE = "/tickets/%s/close/"; | |
static final String API_TICKETS_COUNT = "/tickets/count/"; | |
static final String API_TICKETS_CUSTOM_FIELDS = "/tickets/%s/custom_fields/"; | |
static final String API_TICKETS_TICKETID_CANCEL = "/tickets/%s/cancel/"; | |
static final String API_TICKETS_SELECT_QUESTION = "/tickets/%s/select_question/"; | |
private static final String ERR_CODE_DESK_AUTH = "desk401100"; | |
private static final Gson sGson; | |
private static final MediaType MIME_JSON; | |
APIClient() { | |
} | |
private static String getAppId() { | |
return SendBirdDesk.getInstance().getAppId(); | |
} | |
private static String getUserId() { | |
return SendBirdDesk.getInstance().getUserId(); | |
} | |
private static String getDeskToken() { | |
return SendBirdDesk.getInstance().getDeskToken(); | |
} | |
private static OkHttpClient getOkHttpClient() { | |
return SendBirdDesk.getInstance().getOkHttpClient(); | |
} | |
private static String getSdkVersion() { | |
return SendBirdDesk.getSdkVersion(); | |
} | |
private static String getOsVersion() { | |
return String.valueOf(VERSION.SDK_INT); | |
} | |
private static void setApiHost(String appId) { | |
if (SendBirdDesk.BUILD_CONFIG == BuildConfig.RELEASE) { | |
mApiHost = "https://desk-api-" + appId + ".sendbird.com/sapi"; | |
} | |
} | |
static void init(String appId) { | |
setApiHost(appId); | |
} | |
static void requestAuth(String accessToken, APIClient.APIClientHandler handler) { | |
if (checkApiRequest(handler)) { | |
String fullUrl = mApiHost + "/customers/auth/"; | |
Logger.d("POST: " + fullUrl); | |
JsonObject obj = new JsonObject(); | |
obj.addProperty("sendbirdAppId", getAppId()); | |
obj.addProperty("sendbirdId", getUserId()); | |
try { | |
String formJson = sGson.toJson(obj); | |
Logger.d("API request: " + formJson); | |
RequestBody body = RequestBody.create(MIME_JSON, formJson); | |
Request request = (new Builder()).header("Accept", "application/json").header("User-Agent", "Dand/" + getSdkVersion()).header("SendBirdDesk", "Android," + getOsVersion() + "," + getSdkVersion() + "," + getAppId()).header("sendbirdAccessToken", accessToken).url(fullUrl).post(body).build(); | |
newCall(request, handler); | |
} catch (Exception var7) { | |
if (handler != null) { | |
handler.onResult((JsonElement)null, new SendBirdException(var7.getMessage(), 800220)); | |
} | |
} | |
} | |
} | |
static void setCustomerCustomFields(Map<String, String> customFields, APIClient.APIClientHandler handler) { | |
if (checkApiRequest(handler)) { | |
JsonObject form = new JsonObject(); | |
JsonObject customFieldObj = new JsonObject(); | |
if (customFields != null && customFields.size() > 0) { | |
Iterator var4 = customFields.entrySet().iterator(); | |
while(var4.hasNext()) { | |
Entry<String, String> entry = (Entry)var4.next(); | |
customFieldObj.addProperty((String)entry.getKey(), (String)entry.getValue()); | |
} | |
} | |
form.addProperty("customFields", customFieldObj.toString()); | |
requestPATCH("/customers/custom_fields/", form, handler); | |
} | |
} | |
static void requestGET(String url, HashMap<String, Object> params, APIClient.APIClientHandler handler) { | |
if (checkApiRequest(handler)) { | |
if (params == null) { | |
params = new HashMap(); | |
} | |
params.put("sendbirdAppId", getAppId()); | |
String query = APIClient.UrlUtil.urlEncodeUTF8((Map)params); | |
String fullUrl = mApiHost + url + "?" + query; | |
Logger.d("GET: " + fullUrl); | |
try { | |
Request request = (new Builder()).header("Accept", "application/json").header("User-Agent", "Dand/" + getSdkVersion()).header("SendBirdDesk", "Android," + getOsVersion() + "," + getSdkVersion() + "," + getAppId()).header("sendbirdDeskToken", getDeskToken() == null ? "" : getDeskToken()).url(fullUrl).build(); | |
newCall(request, handler); | |
} catch (Exception var6) { | |
if (handler != null) { | |
handler.onResult((JsonElement)null, new SendBirdException(var6.getMessage(), 800220)); | |
} | |
} | |
} | |
} | |
static void requestPOST(String url, JsonElement form, APIClient.APIClientHandler handler) { | |
if (checkApiRequest(handler)) { | |
String fullUrl = mApiHost + url; | |
Logger.d("POST: " + fullUrl); | |
if (form == null) { | |
form = new JsonObject(); | |
} | |
JsonObject obj = ((JsonElement)form).getAsJsonObject(); | |
obj.addProperty("sendbirdAppId", getAppId()); | |
try { | |
String formJson = sGson.toJson((JsonElement)form); | |
Logger.d("API request: " + formJson); | |
RequestBody body = RequestBody.create(MIME_JSON, formJson); | |
Request request = (new Builder()).header("Accept", "application/json").header("User-Agent", "Dand/" + getSdkVersion()).header("SendBirdDesk", "Android," + getOsVersion() + "," + getSdkVersion() + "," + getAppId()).header("sendbirdDeskToken", getDeskToken() == null ? "" : getDeskToken()).url(fullUrl).post(body).build(); | |
newCall(request, handler); | |
} catch (Exception var8) { | |
if (handler != null) { | |
handler.onResult((JsonElement)null, new SendBirdException(var8.getMessage(), 800220)); | |
} | |
} | |
} | |
} | |
static void requestPATCH(String url, JsonElement form, APIClient.APIClientHandler handler) { | |
if (checkApiRequest(handler)) { | |
String fullUrl = mApiHost + url; | |
Logger.d("PATCH: " + fullUrl); | |
if (form == null) { | |
form = new JsonObject(); | |
} | |
JsonObject obj = ((JsonElement)form).getAsJsonObject(); | |
obj.addProperty("sendbirdAppId", getAppId()); | |
try { | |
String formJson = sGson.toJson((JsonElement)form); | |
Logger.d("API request: " + formJson); | |
RequestBody body = RequestBody.create(MIME_JSON, formJson); | |
Request request = (new Builder()).header("Accept", "application/json").header("User-Agent", "Dand/" + getSdkVersion()).header("SendBirdDesk", "Android," + getOsVersion() + "," + getSdkVersion() + "," + getAppId()).header("sendbirdDeskToken", getDeskToken() == null ? "" : getDeskToken()).url(fullUrl).patch(body).build(); | |
newCall(request, handler); | |
} catch (Exception var8) { | |
if (handler != null) { | |
handler.onResult((JsonElement)null, new SendBirdException(var8.getMessage(), 800220)); | |
} | |
} | |
} | |
} | |
private static boolean checkApiRequest(final APIClient.APIClientHandler handler) { | |
if (getAppId() == null) { | |
SendBirdDesk.runOnUiThread(new Runnable() { | |
public void run() { | |
if (handler != null) { | |
handler.onResult((JsonElement)null, new SendBirdException("APP ID is not valid.", 400404)); | |
} | |
} | |
}); | |
return false; | |
} else if (getUserId() == null) { | |
SendBirdDesk.runOnUiThread(new Runnable() { | |
public void run() { | |
if (handler != null) { | |
handler.onResult((JsonElement)null, new SendBirdException("Connection is required.", 800101)); | |
} | |
} | |
}); | |
return false; | |
} else { | |
return true; | |
} | |
} | |
private static void newCall(Request request, final APIClient.APIClientHandler handler) { | |
getOkHttpClient().newCall(request).enqueue(new Callback() { | |
public void onFailure(Call call, IOException e) { | |
if (handler != null) { | |
handler.onResult((JsonElement)null, new SendBirdException(e.getMessage(), 800220)); | |
} | |
} | |
public void onResponse(Call call, Response response) throws IOException { | |
try { | |
JsonElement result = APIClient.processResponse(response); | |
if (handler != null) { | |
handler.onResult(result, (SendBirdException)null); | |
} | |
} catch (SendBirdException var4) { | |
Logger.d(var4); | |
if (handler != null) { | |
handler.onResult((JsonElement)null, var4); | |
} | |
} catch (Exception var5) { | |
Logger.d(var5); | |
if (handler != null) { | |
handler.onResult((JsonElement)null, new SendBirdException(var5.getMessage(), 800220)); | |
} | |
} | |
} | |
}); | |
} | |
private static JsonElement processResponse(Response response) throws SendBirdException { | |
String body; | |
try { | |
body = response.body().string(); | |
Logger.d("API response: " + body); | |
} catch (IOException var7) { | |
throw new SendBirdException(var7.getMessage(), 800130); | |
} | |
if (body.length() <= 0) { | |
return JsonNull.INSTANCE; | |
} else { | |
JsonElement json; | |
try { | |
json = (new JsonParser()).parse(body); | |
} catch (Exception var6) { | |
throw new SendBirdException(var6.getMessage(), 800130); | |
} | |
if (!response.isSuccessful()) { | |
String code = null; | |
StringBuilder builder = new StringBuilder(); | |
if (json.isJsonObject()) { | |
if (json.getAsJsonObject().has("code")) { | |
code = json.getAsJsonObject().get("code").getAsString(); | |
builder.append("["); | |
builder.append(code); | |
builder.append("]"); | |
builder.append(" "); | |
} | |
if (json.getAsJsonObject().has("detail")) { | |
builder.append(json.getAsJsonObject().get("detail")); | |
} | |
} | |
int errorCode = code != null && code.equals("desk401100") ? 400108 : 800220; | |
throw new SendBirdException(builder.toString(), errorCode); | |
} else { | |
return json; | |
} | |
} | |
} | |
static String getFilterString(Map<String, String> map) { | |
StringBuilder sb = new StringBuilder(); | |
Entry entry; | |
if (map != null && map.size() > 0) { | |
for(Iterator var2 = map.entrySet().iterator(); var2.hasNext(); sb.append(String.format("%s:%s", entry.getKey(), APIClient.UrlUtil.urlEncodeUTF8((String)entry.getValue())))) { | |
entry = (Entry)var2.next(); | |
if (sb.length() > 0) { | |
sb.append(","); | |
} | |
} | |
} | |
return APIClient.UrlUtil.urlEncodeUTF8(sb.toString()); | |
} | |
static { | |
if (SendBirdDesk.BUILD_CONFIG == BuildConfig.STAGING) { | |
mApiHost = "https://desk-staging.sendbird.com/sapi"; | |
Logger.sLevel = 98765; | |
} | |
sGson = new Gson(); | |
MIME_JSON = MediaType.parse("application/json; charset=utf-8"); | |
} | |
private static class UrlUtil { | |
private UrlUtil() { | |
} | |
static String urlEncodeUTF8(String s) { | |
try { | |
return s == null ? null : URLEncoder.encode(s, "UTF-8"); | |
} catch (UnsupportedEncodingException var2) { | |
throw new UnsupportedOperationException(var2); | |
} | |
} | |
static String urlEncodeUTF8(Map<?, ?> map) { | |
StringBuilder sb = new StringBuilder(); | |
Iterator var2 = map.entrySet().iterator(); | |
while(true) { | |
while(var2.hasNext()) { | |
Entry<?, ?> entry = (Entry)var2.next(); | |
if (sb.length() > 0) { | |
sb.append("&"); | |
} | |
if (entry.getValue() instanceof String) { | |
sb.append(String.format("%s=%s", urlEncodeUTF8(entry.getKey().toString()), entry.getValue().toString())); | |
} else if (entry.getValue() instanceof String[]) { | |
String[] values = (String[])((String[])entry.getValue()); | |
for(int i = 0; i < values.length; ++i) { | |
if (i > 0) { | |
sb.append("&"); | |
} | |
sb.append(String.format("%s=%s", urlEncodeUTF8(entry.getKey().toString()), values[i])); | |
} | |
} | |
} | |
return sb.toString(); | |
} | |
} | |
} | |
interface APIClientHandler { | |
void onResult(JsonElement var1, SendBirdException var2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment