Search in sources :

Example 6 with BaseRequest

use of com.mashape.unirest.request.BaseRequest in project unipop by unipop-graph.

the class AbstractRestSchema method createSearch.

protected BaseRequest createSearch(PredicatesHolder predicatesHolder, int limit) {
    if (limit == -1)
        limit = maxResultSize;
    else
        limit = Math.min(maxResultSize, limit);
    Map<String, Object> predicates = PredicatesTranslator.translate(predicatesHolder, opTranslator, complexTranslator, valuesToString, limit);
    Map<String, Object> urlMap = new HashMap<>();
    urlMap.put("resource", resource);
    BaseRequest request = templateHolder.getSearch().execute(baseUrl, urlMap, predicates);
    try {
        if (bulk.size() > 0)
            runBulk();
        if (templateHolder.isCommit())
            templateHolder.getCommit().execute(baseUrl, urlMap, predicates).asJson();
    } catch (UnirestException e) {
        e.printStackTrace();
    }
    return request;
}
Also used : BaseRequest(com.mashape.unirest.request.BaseRequest) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) JSONObject(org.json.JSONObject)

Example 7 with BaseRequest

use of com.mashape.unirest.request.BaseRequest in project unipop by unipop-graph.

the class AbstractRestSchema method runBulk.

private void runBulk() {
    BaseRequest bulk = templateHolder.getBulk().execute(baseUrl, Collections.singletonMap("resource", this.resource), Collections.singletonMap("bulk", this.bulk));
    try {
        bulk.asJson();
        this.bulk.clear();
    } catch (UnirestException e) {
        e.printStackTrace();
    }
}
Also used : BaseRequest(com.mashape.unirest.request.BaseRequest) UnirestException(com.mashape.unirest.http.exceptions.UnirestException)

Example 8 with BaseRequest

use of com.mashape.unirest.request.BaseRequest in project DiscLoader by R3alCl0ud.

the class APIRequest method createRequest.

public BaseRequest createRequest() throws IOException {
    BaseRequest request = null;
    switch(this.method) {
        case 0:
            request = Unirest.get(this.route);
            break;
        case 1:
            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 3:
            request = Unirest.patch(this.route);
            ((HttpRequestWithBody) request).body(gson.toJson(data));
            break;
        case 2:
            request = Unirest.delete(this.route);
            break;
        case 4:
            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 9 with BaseRequest

use of com.mashape.unirest.request.BaseRequest in project DiscLoader by R3alCl0ud.

the class Route method createRequest.

public BaseRequest createRequest(Request<T> request) {
    BaseRequest base = null;
    String endpoint = this.endpoint;
    if (request.getOptions() != null && request.getOptions().getParameters() != null) {
        for (int i = 0; i < request.getOptions().getParameters().length; i++) {
            endpoint += String.format(i == 0 ? "?%s=%s" : "&%s=%s", request.getOptions().getParameters()[i].getName(), request.getOptions().getParameters()[i].getValue());
        }
    }
    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());
    }
    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)

Example 10 with BaseRequest

use of com.mashape.unirest.request.BaseRequest in project DiscLoader by R3alCl0ud.

the class Route method handle.

public void handle() {
    if (waiting || rateLimiter.isLimited() || queue.isEmpty())
        return;
    waiting = true;
    Request<T> apiRequest = queue.remove(0);
    BaseRequest request = createRequest(apiRequest);
    request.asStringAsync(new Callback<String>() {

        @Override
        public void completed(HttpResponse<String> response) {
            try {
                rateLimiter.readHeaders(response.getHeaders());
                rest.loader.emit(new RawEvent(rest.loader, response));
                int code = response.getStatus();
                waiting = false;
                if (code == 429) {
                    queue.add(0, apiRequest);
                    rateLimiter.limitRoute(rest.isGlobally(), rateLimiter.retryIn());
                    // return early so handle() at the end of handle() doesn't get called
                    return;
                } else if (code >= 500 && code < 600) {
                    queue.add(0, apiRequest);
                } else if (code >= 400 && code < 500) {
                    apiRequest.getFuture().completeExceptionally(new DiscordException(gson.fromJson(response.getBody(), ExceptionJSON.class)));
                } else if (code == 204) {
                    apiRequest.getFuture().complete(null);
                } else {
                    T res = gson.fromJson(response.getBody(), cls);
                    apiRequest.getFuture().complete(res);
                }
                handle();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void failed(UnirestException ex) {
            ex.printStackTrace();
            queue.add(0, apiRequest);
            handle();
        }

        @Override
        public void cancelled() {
            queue.add(0, apiRequest);
            handle();
        }
    });
}
Also used : RawEvent(io.discloader.discloader.common.event.RawEvent) ExceptionJSON(io.discloader.discloader.network.json.ExceptionJSON) BaseRequest(com.mashape.unirest.request.BaseRequest) DiscordException(io.discloader.discloader.common.exceptions.DiscordException) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) DiscordException(io.discloader.discloader.common.exceptions.DiscordException)

Aggregations

BaseRequest (com.mashape.unirest.request.BaseRequest)10 UnirestException (com.mashape.unirest.http.exceptions.UnirestException)9 DiscordException (io.discloader.discloader.common.exceptions.DiscordException)3 HttpResponse (com.mashape.unirest.http.HttpResponse)2 HttpRequest (com.mashape.unirest.request.HttpRequest)2 HttpRequestWithBody (com.mashape.unirest.request.HttpRequestWithBody)2 MultipartBody (com.mashape.unirest.request.body.MultipartBody)2 Resource (io.discloader.discloader.client.render.util.Resource)2 RawEvent (io.discloader.discloader.common.event.RawEvent)2 SendableMessage (io.discloader.discloader.entity.sendable.SendableMessage)2 ExceptionJSON (io.discloader.discloader.network.json.ExceptionJSON)2 File (java.io.File)2 JSONObject (org.json.JSONObject)2 UniEdge (org.unipop.structure.UniEdge)2 UniVertex (org.unipop.structure.UniVertex)2 JsonNode (com.mashape.unirest.http.JsonNode)1 Callback (com.mashape.unirest.http.async.Callback)1 DiscLoader (io.discloader.discloader.common.DiscLoader)1 AccountTypeException (io.discloader.discloader.common.exceptions.AccountTypeException)1 PermissionsException (io.discloader.discloader.common.exceptions.PermissionsException)1