package com.duesk.api; import okhttp3.*; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.reflect.TypeToken; import com.duesk.api.models.*; import java.io.IOException; import java.lang.reflect.Type; import java.util.List; import java.util.concurrent.TimeUnit; public class ApiClient { private static final String BASE_URL = "https://paul-koop.org/api/"; private static final OkHttpClient client = new OkHttpClient.Builder() .connectTimeout(30, TimeUnit.SECONDS) .readTimeout(30, TimeUnit.SECONDS) .build(); private static final Gson gson = new GsonBuilder().create(); public static LoginResponse login(String username, String password) throws IOException { String json = "{\"username\":\"" + username + "\",\"password\":\"" + password + "\"}"; RequestBody body = RequestBody.create(json, MediaType.parse("application/json")); Request request = new Request.Builder() .url(BASE_URL + "api_login.php") .post(body) .build(); try (Response response = client.newCall(request).execute()) { String responseBody = response.body().string(); return gson.fromJson(responseBody, LoginResponse.class); } } public static List getProfiles() throws IOException { if (!SessionManager.getInstance().isLoggedIn()) return null; Request request = new Request.Builder() .url(BASE_URL + "api_profiles.php") .addHeader("X-User-ID", SessionManager.getInstance().getUserId()) .addHeader("X-Session", SessionManager.getInstance().getSession()) .build(); try (Response response = client.newCall(request).execute()) { String responseBody = response.body().string(); Type listType = new TypeToken>(){}.getType(); return gson.fromJson(responseBody, listType); } } public static Profile getProfile(String profileId) throws IOException { if (!SessionManager.getInstance().isLoggedIn()) return null; Request request = new Request.Builder() .url(BASE_URL + "api_profiles.php?id=" + profileId) .addHeader("X-User-ID", SessionManager.getInstance().getUserId()) .addHeader("X-Session", SessionManager.getInstance().getSession()) .build(); try (Response response = client.newCall(request).execute()) { String responseBody = response.body().string(); return gson.fromJson(responseBody, Profile.class); } } public static boolean createProfile(Profile profile) throws IOException { if (!SessionManager.getInstance().isLoggedIn()) return false; String json = gson.toJson(profile); RequestBody body = RequestBody.create(json, MediaType.parse("application/json")); Request request = new Request.Builder() .url(BASE_URL + "api_profiles.php") .post(body) .addHeader("X-User-ID", SessionManager.getInstance().getUserId()) .addHeader("X-Session", SessionManager.getInstance().getSession()) .build(); try (Response response = client.newCall(request).execute()) { return response.isSuccessful(); } } public static boolean updateProfile(Profile profile) throws IOException { if (!SessionManager.getInstance().isLoggedIn()) return false; String json = gson.toJson(profile); RequestBody body = RequestBody.create(json, MediaType.parse("application/json")); Request request = new Request.Builder() .url(BASE_URL + "api_profiles.php") .put(body) .addHeader("X-User-ID", SessionManager.getInstance().getUserId()) .addHeader("X-Session", SessionManager.getInstance().getSession()) .build(); try (Response response = client.newCall(request).execute()) { return response.isSuccessful(); } } public static boolean deleteProfile(String profileId) throws IOException { if (!SessionManager.getInstance().isLoggedIn()) return false; Request request = new Request.Builder() .url(BASE_URL + "api_profiles.php?id=" + profileId) .delete() .addHeader("X-User-ID", SessionManager.getInstance().getUserId()) .addHeader("X-Session", SessionManager.getInstance().getSession()) .build(); try (Response response = client.newCall(request).execute()) { return response.isSuccessful(); } } public static List getGroups() throws IOException { if (!SessionManager.getInstance().isLoggedIn()) return null; Request request = new Request.Builder() .url(BASE_URL + "api_groups.php") .addHeader("X-User-ID", SessionManager.getInstance().getUserId()) .addHeader("X-Session", SessionManager.getInstance().getSession()) .build(); try (Response response = client.newCall(request).execute()) { String responseBody = response.body().string(); Type listType = new TypeToken>(){}.getType(); return gson.fromJson(responseBody, listType); } } public static boolean createGroup(String groupName) throws IOException { if (!SessionManager.getInstance().isLoggedIn()) return false; String json = "{\"name\":\"" + groupName + "\"}"; RequestBody body = RequestBody.create(json, MediaType.parse("application/json")); Request request = new Request.Builder() .url(BASE_URL + "api_groups.php") .post(body) .addHeader("X-User-ID", SessionManager.getInstance().getUserId()) .addHeader("X-Session", SessionManager.getInstance().getSession()) .build(); try (Response response = client.newCall(request).execute()) { return response.isSuccessful(); } } public static boolean deleteGroup(int groupId) throws IOException { if (!SessionManager.getInstance().isLoggedIn()) return false; Request request = new Request.Builder() .url(BASE_URL + "api_groups.php?id=" + groupId) .delete() .addHeader("X-User-ID", SessionManager.getInstance().getUserId()) .addHeader("X-Session", SessionManager.getInstance().getSession()) .build(); try (Response response = client.newCall(request).execute()) { return response.isSuccessful(); } } }