Search in sources :

Example 1 with Resource

use of io.discloader.discloader.client.render.util.Resource in project DiscLoader by R3alCl0ud.

the class ModContainer method handleAssets.

private void handleAssets() {
    ZipFile zip = null;
    try {
        try {
            zip = mod.getZipFile();
        } catch (ZipException e) {
            e.printStackTrace();
        }
        if (zip != null) {
            for (ZipEntry e : readEntries(zip.entries())) {
                if (!e.getName().startsWith("assets/" + modInfo.modid()))
                    continue;
                String name = e.getName().substring(("assets/" + modInfo.modid()).length() + 1);
                Resource r = new Resource(modInfo.modid(), name);
                ResourceManager.instance.addResource(r);
            }
        }
    } catch (Exception e1) {
        e1.printStackTrace();
    }
}
Also used : ZipFile(java.util.zip.ZipFile) ZipEntry(java.util.zip.ZipEntry) Resource(io.discloader.discloader.client.render.util.Resource) ZipException(java.util.zip.ZipException) ZipException(java.util.zip.ZipException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with Resource

use of io.discloader.discloader.client.render.util.Resource in project DiscLoader by R3alCl0ud.

the class APIRequest method createRequest.

public BaseRequest createRequest() throws IOException {
    BaseRequest request = null;
    switch(this.method) {
        case Methods.GET:
            request = Unirest.get(this.route);
            break;
        case Methods.POST:
            request = Unirest.post(this.route);
            if (multi) {
                // try {
                SendableMessage sdata = (SendableMessage) this.data;
                File file = sdata.file;
                Resource resource = sdata.resource;
                byte[] bytes = new byte[0];
                if (file != null)
                    bytes = DLUtil.readAllBytes(file);
                if (resource != null)
                    bytes = DLUtil.readAllBytes(resource);
                MultipartBody body = ((HttpRequestWithBody) request).fields(null);
                String loc = "";
                if (file != null)
                    loc = file.getName();
                if (resource != null)
                    loc = resource.getFileName();
                body.field("Content-type", "multipart/form-data").field("file", bytes, loc).field("payload_json", gson.toJson(sdata));
            // } catch (IOException ex) {
            // ex.printStackTrace();
            // }
            } else {
                ((HttpRequestWithBody) request).body(gson.toJson(data));
            }
            break;
        case Methods.PATCH:
            request = Unirest.patch(this.route);
            ((HttpRequestWithBody) request).body(gson.toJson(data));
            break;
        case Methods.DELETE:
            request = Unirest.delete(this.route);
            break;
        case DLUtil.Methods.PUT:
            request = Unirest.put(this.route);
            ((HttpRequestWithBody) request).body(gson.toJson(this.data));
            break;
        default:
            request = Unirest.get(this.route);
            break;
    }
    return request;
}
Also used : SendableMessage(io.discloader.discloader.entity.sendable.SendableMessage) MultipartBody(com.mashape.unirest.request.body.MultipartBody) HttpRequestWithBody(com.mashape.unirest.request.HttpRequestWithBody) BaseRequest(com.mashape.unirest.request.BaseRequest) Resource(io.discloader.discloader.client.render.util.Resource) File(java.io.File)

Example 3 with Resource

use of io.discloader.discloader.client.render.util.Resource in project DiscLoader by R3alCl0ud.

the class Route method createRequest.

public BaseRequest createRequest(Request<T> request) {
    BaseRequest base = null;
    switch(method) {
        case GET:
            base = Unirest.get(endpoint);
            break;
        case POST:
            base = Unirest.post(endpoint);
            if (request.getData() instanceof SendableMessage) {
                MultipartBody body = ((HttpRequestWithBody) base).fields(null);
                body.field("Content-Type", "multipart/form-data");
                SendableMessage sdata = (SendableMessage) request.getData();
                if (sdata.file != null || sdata.resource != null) {
                    try {
                        File file = sdata.file;
                        Resource resource = sdata.resource;
                        byte[] bytes = new byte[0];
                        if (file != null)
                            bytes = DLUtil.readAllBytes(file);
                        if (resource != null && resource.getResourceAsStream() != null)
                            bytes = DLUtil.readAllBytes(resource);
                        String loc = "";
                        if (file != null)
                            loc = file.getName();
                        if (resource != null)
                            loc = resource.getFileName();
                        body.field("file", bytes, loc);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
                try {
                    if (request.getData() instanceof JSONObject) {
                        body.field("payload_json", request.getData().toString());
                    } else if (request.getData() instanceof String) {
                        body.field("payload_json", request.getData());
                    } else {
                        body.field("payload_json", gson.toJson(request.getData()));
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            } else {
                base.getHttpRequest().header("Content-Type", "application/json");
                if (request.getData() instanceof JSONObject) {
                    ((HttpRequestWithBody) base).body(request.getData().toString());
                } else if (request.getData() instanceof String) {
                    ((HttpRequestWithBody) base).body(request.getData());
                } else {
                    ((HttpRequestWithBody) base).body(gson.toJson(request.getData()));
                }
            }
            break;
        case DELETE:
            base = Unirest.delete(endpoint);
            break;
        case PATCH:
            base = Unirest.patch(endpoint);
            base.getHttpRequest().header("Content-Type", "application/json");
            if (request.getData() instanceof JSONObject) {
                ((HttpRequestWithBody) base).body(request.getData().toString());
            } else if (request.getData() instanceof String) {
                ((HttpRequestWithBody) base).body(request.getData().toString());
            } else {
                ((HttpRequestWithBody) base).body(gson.toJson(request.getData()));
            }
            break;
        case PUT:
            base = Unirest.put(endpoint);
            base.getHttpRequest().header("Content-Type", "application/json");
            if (request.getData() instanceof JSONObject) {
                ((HttpRequestWithBody) base).body(request.getData().toString());
            } else if (request.getData() instanceof String) {
                ((HttpRequestWithBody) base).body(request.getData());
            } else {
                ((HttpRequestWithBody) base).body(gson.toJson(request.getData()));
            }
            break;
        default:
            base = Unirest.get(endpoint);
            break;
    }
    // body.
    HttpRequest httprequest = base.getHttpRequest();
    if (auth && rest.loader.token != null)
        httprequest.header("Authorization", rest.loader.token);
    if (request.getOptions() != null && request.getOptions().getReason() != null) {
        httprequest.header("X-Audit-Log-Reason", request.getOptions().getReason());
        System.out.println("is this thing working?");
    }
    httprequest.header("user-agent", "DiscordBot (http://discloader.io, v0.2.0)");
    httprequest.header("Accept-Encoding", "gzip");
    return base;
}
Also used : HttpRequest(com.mashape.unirest.request.HttpRequest) JSONObject(org.json.JSONObject) SendableMessage(io.discloader.discloader.entity.sendable.SendableMessage) MultipartBody(com.mashape.unirest.request.body.MultipartBody) HttpRequestWithBody(com.mashape.unirest.request.HttpRequestWithBody) BaseRequest(com.mashape.unirest.request.BaseRequest) Resource(io.discloader.discloader.client.render.util.Resource) File(java.io.File) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) DiscordException(io.discloader.discloader.common.exceptions.DiscordException)

Aggregations

Resource (io.discloader.discloader.client.render.util.Resource)3 BaseRequest (com.mashape.unirest.request.BaseRequest)2 HttpRequestWithBody (com.mashape.unirest.request.HttpRequestWithBody)2 MultipartBody (com.mashape.unirest.request.body.MultipartBody)2 SendableMessage (io.discloader.discloader.entity.sendable.SendableMessage)2 File (java.io.File)2 UnirestException (com.mashape.unirest.http.exceptions.UnirestException)1 HttpRequest (com.mashape.unirest.request.HttpRequest)1 DiscordException (io.discloader.discloader.common.exceptions.DiscordException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ZipEntry (java.util.zip.ZipEntry)1 ZipException (java.util.zip.ZipException)1 ZipFile (java.util.zip.ZipFile)1 JSONObject (org.json.JSONObject)1