com.github.automately.sdk.Automately.java Source code

Java tutorial

Introduction

Here is the source code for com.github.automately.sdk.Automately.java

Source

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package com.github.automately.sdk;

import com.squareup.okhttp.*;
import io.jsync.json.DecodeException;
import io.jsync.json.JsonObject;

import java.io.IOException;

public class Automately extends com.github.automately.java.Automately {

    public static String USER_API_ENDPOINT = "https://dashboard.automate.ly/userApi";
    private static String REGISTRY_ENDPOINT = "https://registry.automate.ly";

    public static JsonObject getAutomatelyCloudPlan() {
        try {

            JsonObject requestData = new JsonObject();

            requestData.putString("username", getApiUsername());
            requestData.putString("apiKey", getApiKey());

            Request request = new Request.Builder()
                    .url(USER_API_ENDPOINT + "/getPlan").post(RequestBody
                            .create(MediaType.parse("application/json"), requestData.toString().getBytes()))
                    .build();

            Response response = httpClient.newCall(request).execute();

            if (response != null) {
                String responseEntity = response.body().string();
                try {
                    return new JsonObject(responseEntity);
                } catch (DecodeException ignored) {
                    getFormattedError("Decode Exception", "There was an issue decoding the response.");
                }
            }
        } catch (IOException ignored) {
        }
        return null;
    }

    public static JsonObject getAutomatelyCloudPlans() {
        try {

            Request request = new Request.Builder().url(USER_API_ENDPOINT + "/getPlans").get().build();

            Response response = httpClient.newCall(request).execute();

            if (response != null) {
                String responseEntity = response.body().string();
                try {
                    return new JsonObject(responseEntity);
                } catch (DecodeException ignored) {
                    getFormattedError("Decode Exception", "There was an issue decoding the response.");
                }
            }
        } catch (IOException ignored) {
        }
        return null;
    }

    public static JsonObject setAutomatelyCloudPlan(String planId) {
        try {
            JsonObject requestData = new JsonObject();

            requestData.putString("planId", planId);
            requestData.putString("username", getApiUsername());
            requestData.putString("apiKey", getApiKey());

            Request request = new Request.Builder()
                    .url(USER_API_ENDPOINT + "/changePlan").post(RequestBody
                            .create(MediaType.parse("application/json"), requestData.toString().getBytes()))
                    .build();

            Response response = httpClient.newCall(request).execute();

            if (response != null) {
                String responseEntity = response.body().string();
                try {
                    return new JsonObject(responseEntity);
                } catch (DecodeException ignored) {
                    getFormattedError("Decode Exception", "There was an issue decoding the response.");
                }
            }
        } catch (IOException ignored) {
        }
        return null;
    }

    public static JsonObject registerModule(JsonObject manifest) {
        try {

            JsonObject requestData = new JsonObject();

            // The module register requires authentication
            requestData.putObject("manifest", manifest);
            requestData.putString("username", getApiUsername());
            requestData.putString("apiKey", getApiKey());

            Request request = new Request.Builder().url(REGISTRY_ENDPOINT + "/submit").post(
                    RequestBody.create(MediaType.parse("application/json"), requestData.toString().getBytes()))
                    .build();

            Response response = httpClient.newCall(request).execute();

            if (response != null) {
                String responseEntity = response.body().string();
                try {
                    checkAuthorized(responseEntity);
                    return new JsonObject(responseEntity);
                } catch (DecodeException ignored) {
                    getFormattedError("Decode Exception", "There was an issue decoding the response.");
                }
            }
        } catch (IOException ignored) {
        }
        return null;
    }

    public static JsonObject findModule(String moduleName) {
        try {

            // TODO Include credentials to allow for the searching of private user modules
            Request request = new Request.Builder().url(REGISTRY_ENDPOINT + "/retrieve?name=" + moduleName).get()
                    .build();

            Response response = httpClient.newCall(request).execute();

            if (response != null) {
                String responseEntity = response.body().string();
                try {
                    checkAuthorized(responseEntity);
                    return new JsonObject(responseEntity);
                } catch (DecodeException ignored) {
                    getFormattedError("Decode Exception", "There was an issue decoding the response.");
                }
            }
        } catch (IOException ignored) {
        }
        return null;
    }

}