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

com.starkinfra.utils.Rest Maven / Gradle / Ivy

Go to download

Welcome to the Stark Infra Java SDK! This tool is made for Java developers who want to easily integrate with our API. This SDK version is compatible with the Stark Infra API v2.

There is a newer version: 0.11.2
Show newest version
package com.starkinfra.utils;

import com.google.gson.*;
import com.starkinfra.User;

import java.util.Map;
import java.util.List;
import java.util.HashMap;
import java.util.ArrayList;
import java.io.InputStream;
import java.lang.reflect.Type;


public final class Rest {
    public static  T getId(Resource.ClassData resource, String id, User user) throws Exception {
        return getId(resource, id, null, user);
    }

    public static  T getId(Resource.ClassData resource, String id, Map query, User user) throws Exception {
        String content = Response.fetch(Api.endpoint(resource, id), "GET", null, query, user).content();
        Gson gson = GsonEvent.getInstance();
        JsonObject contentJson = gson.fromJson(content, JsonObject.class);
        JsonObject jsonObject = contentJson.get(Api.getLastName(resource)).getAsJsonObject();
        return gson.fromJson(jsonObject, (Type) resource.cls);
    }

    public static  List post(SubResource.ClassData resource, List entities, User user) throws Exception {
        return post(resource, entities, null, user);
    }

    public static  List post(SubResource.ClassData resource, List entities, Map data, User user) throws Exception {
        JsonObject payload = new JsonObject();
        payload.add(Api.getLastNamePlural(resource), new Gson().toJsonTree(entities).getAsJsonArray());
        String content = Response.fetch(Api.endpoint(resource), "POST", payload, data, user).content();
        JsonObject contentJson = new Gson().fromJson(content, JsonObject.class);
        List postEntities = new ArrayList<>();
        JsonArray jsonArray = contentJson.get(Api.getLastNamePlural(resource)).getAsJsonArray();
        for (JsonElement resourceElement : jsonArray) {
            JsonObject jsonObject = resourceElement.getAsJsonObject();
            postEntities.add(GsonEvent.getInstance().fromJson(jsonObject, (Type) resource.cls));
        }
        return postEntities;
    }

    public static  T patch(Resource.ClassData resource, String id, Map data, User user) throws Exception {
        JsonObject payload = new Gson().fromJson(new Gson().toJson(data), JsonObject.class);
        String content = Response.fetch(Api.endpoint(resource, id), "PATCH", payload, null, user).content();
        Gson gson = GsonEvent.getInstance();
        JsonObject contentJson = gson.fromJson(content, JsonObject.class);
        JsonObject jsonObject = contentJson.get(Api.getLastName(resource)).getAsJsonObject();
        return gson.fromJson(jsonObject, (Type) resource.cls);
    }

    public static Page getPage(Resource.ClassData resource, Map params, User user) throws Exception {
        String content = Response.fetch(Api.endpoint(resource), "GET", null, params, user).content();
        Gson gson = GsonEvent.getInstance();
        JsonObject contentJson = gson.fromJson(content, JsonObject.class);
        JsonElement cursorJson = contentJson.get("cursor");
        String cursor = cursorJson.isJsonNull() ? null : cursorJson.getAsString();

        List entities = new ArrayList<>();
        JsonArray jsonArray = contentJson.get(Api.getLastNamePlural(resource)).getAsJsonArray();
        for (JsonElement resourceElement : jsonArray) {
            JsonObject jsonObject = resourceElement.getAsJsonObject();
            entities.add(GsonEvent.getInstance().fromJson(jsonObject, (Type) resource.cls));
        };

        return new Page(entities, cursor);
    }

    public static  Generator getStream(Resource.ClassData resource, Map params, User user) {
        return new Generator() {
            public void run() throws Exception {
                Map paramsCopy = new HashMap<>();
                for (Map.Entry entry: params.entrySet()) {
                    paramsCopy.put(entry.getKey(), entry.getValue());
                }
                Integer limit = (Integer) paramsCopy.get("limit");
                String cursor = null;
                do {
                    paramsCopy.put("cursor", cursor);
                    if (limit != null) {
                        paramsCopy.put("limit", limit > 100 ? "100" : limit.toString());
                        limit -= 100;
                    }
                    String content = Response.fetch(Api.endpoint(resource), "GET", null, paramsCopy, user).content();
                    Gson gson = GsonEvent.getInstance();
                    JsonObject contentJson = gson.fromJson(content, JsonObject.class);
                    JsonElement cursorJson = contentJson.get("cursor");
                    cursor = cursorJson != null ? (cursorJson.isJsonNull() ? "" : cursorJson.getAsString()) : null;
                    JsonArray jsonArray = contentJson.get(Api.getLastNamePlural(resource)).getAsJsonArray();
                    for (JsonElement resourceElement : jsonArray) {
                        JsonObject jsonObject = resourceElement.getAsJsonObject();
                        T element = gson.fromJson(jsonObject, (Type) resource.cls);
                        if(element == null)
                            break;
                        this.yield(element);
                    }
                } while (cursor != null && !cursor.isEmpty() && (limit == null || limit > 0));
            }
        };
    }

    public static  Generator getSimpleList(Resource.ClassData resource, Map params, User user) {
        return new Generator() {
            public void run() throws Exception {
                Map paramsCopy = new HashMap<>();
                for (Map.Entry entry: params.entrySet()) {
                    paramsCopy.put(entry.getKey(), entry.getValue());
                }
                String content = Response.fetch(Api.endpoint(resource), "GET", null, paramsCopy, user).content();
                Gson gson = GsonEvent.getInstance();
                JsonObject contentJson = gson.fromJson(content, JsonObject.class);
                JsonArray jsonArray = contentJson.get(Api.getLastNamePlural(resource)).getAsJsonArray();
                for (JsonElement resourceElement : jsonArray) {
                    JsonObject jsonObject = resourceElement.getAsJsonObject();
                    T element = gson.fromJson(jsonObject, (Type) resource.cls);
                    if(element == null)
                        break;
                    this.yield(element);
                }
            }
        };
    }

    public static InputStream getContent(Resource.ClassData resource, String id, String subResourceName ,User user, Map options) throws Exception {
        return Response.fetch(Api.endpoint(resource, id) + "/" + subResourceName, "GET", null, options, user).stream;
    }

    public static  T getSubResource(Resource.ClassData resource, String id, SubResource.ClassData subResource, User user, Map options) throws Exception {
        String content = Response.fetch(Api.endpoint(resource, id) + "/" + Api.endpoint(subResource), "GET", null, options, user).content();
        JsonObject contentJson = new Gson().fromJson(content, JsonObject.class);
        JsonObject jsonObject = contentJson.get(Api.getLastName(subResource)).getAsJsonObject();
        Gson gson = GsonEvent.getInstance();
        return gson.fromJson(jsonObject, (Type) subResource.cls);
    }

    public static  List getSubResources(Resource.ClassData resource, String id, SubResource.ClassData subResource, User user, Map options) throws Exception {
        String content = Response.fetch(Api.endpoint(resource, id) + "/" + Api.endpoint(subResource), "GET", null, options, user).content();
        JsonObject contentJson = new Gson().fromJson(content, JsonObject.class);
        JsonArray jsonArray = contentJson.get(Api.getLastNamePlural(subResource)).getAsJsonArray();
        List entities = new ArrayList<>();
        for (JsonElement resourceElement : jsonArray) {
            JsonObject jsonObject = resourceElement.getAsJsonObject();
            entities.add(GsonEvent.getInstance().fromJson(jsonObject, (Type) subResource.cls));
        }
        return entities;
    }

    public static  T delete(Resource.ClassData resource, String id, User user) throws Exception {
        String content = Response.fetch(Api.endpoint(resource, id), "DELETE", null, null, user).content();
        Gson gson = GsonEvent.getInstance();
        JsonObject contentJson = gson.fromJson(content, JsonObject.class);
        JsonObject jsonObject = contentJson.get(Api.getLastName(resource)).getAsJsonObject();
        return gson.fromJson(jsonObject, (Type) resource.cls);
    }

    public static  T postSingle(SubResource.ClassData resource, SubResource entity, User user) throws Exception {
        JsonObject payload = (JsonObject) new Gson().toJsonTree((entity));
        String content = Response.fetch(Api.endpoint(resource), "POST", payload, null, user).content();
        Gson gson = GsonEvent.getInstance();
        JsonObject contentJson = gson.fromJson(content, JsonObject.class);
        JsonObject jsonObject = contentJson.get(Api.getLastName(resource)).getAsJsonObject();
        return gson.fromJson(jsonObject, (Type) resource.cls);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy