Search in sources :

Example 1 with BaseRequest

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

the class RestController method property.

@Override
public <E extends Element> void property(PropertyQuery<E> uniQuery) {
    E element = uniQuery.getElement();
    Set<? extends RestSchema<E>> schemas = getSchemas(element.getClass());
    for (RestSchema<E> schema : schemas) {
        try {
            BaseRequest baseRequest = schema.addElement(element);
            if (baseRequest == null)
                return;
            baseRequest.asJson();
        } catch (UnirestException e) {
            e.printStackTrace();
        } catch (NoSuchElementException e) {
            continue;
        }
    }
}
Also used : BaseRequest(com.mashape.unirest.request.BaseRequest) UnirestException(com.mashape.unirest.http.exceptions.UnirestException)

Example 2 with BaseRequest

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

the class RestController method addEdge.

@Override
public Edge addEdge(AddEdgeQuery uniQuery) {
    UniEdge edge = new UniEdge(uniQuery.getProperties(), uniQuery.getOutVertex(), uniQuery.getInVertex(), graph);
    for (RestEdgeSchema edgeSchema : edgeSchemas) {
        try {
            BaseRequest baseRequest = edgeSchema.addElement(edge);
            if (baseRequest == null)
                return edge;
            baseRequest.asJson();
        } catch (UnirestException e) {
            e.printStackTrace();
        } catch (NoSuchElementException e) {
            continue;
        }
    }
    return edge;
}
Also used : UniEdge(org.unipop.structure.UniEdge) BaseRequest(com.mashape.unirest.request.BaseRequest) UnirestException(com.mashape.unirest.http.exceptions.UnirestException)

Example 3 with BaseRequest

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

the class RestController method addVertex.

@Override
public Vertex addVertex(AddVertexQuery uniQuery) {
    UniVertex vertex = new UniVertex(uniQuery.getProperties(), graph);
    for (RestVertexSchema vertexSchema : vertexSchemas) {
        try {
            BaseRequest baseRequest = vertexSchema.addElement(vertex);
            if (baseRequest == null)
                return vertex;
            baseRequest.asJson();
        } catch (UnirestException e) {
            e.printStackTrace();
        } catch (NoSuchElementException e) {
            continue;
        }
    }
    return vertex;
}
Also used : BaseRequest(com.mashape.unirest.request.BaseRequest) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) UniVertex(org.unipop.structure.UniVertex)

Example 4 with BaseRequest

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

the class RESTQueue method handle.

public void handle() {
    try {
        if (waiting || queue.size() == 0 || globalLimit) {
            return;
        }
        waiting = true;
        final APIRequest apiRequest = queue.get(0);
        BaseRequest request = apiRequest.createRequest();
        request = addHeaders(request, apiRequest.auth, apiRequest.multi);
        request.asStringAsync(new Callback<String>() {

            @Override
            public void cancelled() {
                apiRequest.future.completeExceptionally(new Throwable());
            }

            @Override
            public void completed(HttpResponse<String> response) {
                Map<String, List<String>> headers = response.getHeaders();
                headers.forEach((name, value) -> {
                    switch(name) {
                        case "X-RateLimit-Limit":
                            rateLimit = Integer.parseInt(value.get(0), 10);
                            break;
                        case "X-RateLimit-Remaining":
                            remaining = Integer.parseInt(value.get(0), 10);
                            break;
                        case "x-ratelimit-reset":
                        case "X-RateLimit-Reset":
                            resetTime = (Long.parseLong(value.get(0), 10) * 1000L);
                            break;
                        case "X-RateLimit-Global":
                            globalLimit = Boolean.parseBoolean(value.get(0));
                            break;
                    }
                });
                RawEvent event = new RawEvent(loader, response);
                DateFormat df = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");
                try {
                    timeDifference = Date.from(Instant.now()).getTime() - df.parse(headers.get("Date").get(0)).getTime();
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                int code = response.getStatus();
                if (code == 429) {
                    Thread wait = new Thread("Ratelimit resetting - " + apiRequest.url) {

                        @Override
                        public void run() {
                            try {
                                Thread.sleep(Integer.parseInt(headers.get("Retry-After").get(0), 10) + 500);
                            } catch (NumberFormatException e) {
                                e.printStackTrace();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            waiting = false;
                            globalLimit = false;
                            handle();
                        }
                    };
                    wait.setPriority((Thread.NORM_PRIORITY + Thread.MAX_PRIORITY) / 2);
                    wait.setDaemon(true);
                    wait.start();
                    return;
                } else if (code != 200 && code != 201 && code != 204 && code != 304) {
                    queue.remove(apiRequest);
                    loader.emit(event);
                    loader.emit("RawPacket", event);
                    ExceptionJSON data = gson.fromJson(response.getBody(), ExceptionJSON.class);
                    switch(code) {
                        case 401:
                            apiRequest.future.completeExceptionally(new UnauthorizedException(response.getBody()));
                            break;
                        case 403:
                            switch(data.code) {
                                case 20002:
                                    apiRequest.future.completeExceptionally(new AccountTypeException(data));
                                    break;
                                case 50013:
                                    apiRequest.future.completeExceptionally(new PermissionsException(data));
                                    break;
                                default:
                                    apiRequest.future.completeExceptionally(new UnauthorizedException(response.getBody()));
                                    break;
                            }
                            break;
                        default:
                            apiRequest.future.completeExceptionally(new DiscordException(data));
                            break;
                    }
                } else {
                    queue.remove(apiRequest);
                    loader.emit(event);
                    loader.emit("RawPacket", event);
                    apiRequest.future.complete(response.getBody());
                }
                globalLimit = false;
                long waitTime = ((resetTime - System.currentTimeMillis()) + timeDifference + 500);
                if (remaining == 0 && waitTime > 0) {
                    Thread wait = new Thread("REST Waiting - " + apiRequest.url) {

                        @Override
                        public void run() {
                            try {
                                Thread.sleep(waitTime);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            waiting = false;
                            handle();
                        }
                    };
                    wait.setPriority((Thread.NORM_PRIORITY + Thread.MAX_PRIORITY) / 2);
                    wait.setDaemon(true);
                    wait.start();
                } else {
                    waiting = false;
                    handle();
                }
            }

            @Override
            public void failed(UnirestException e) {
                apiRequest.future.completeExceptionally(e);
                handle();
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Date(java.util.Date) SimpleDateFormat(java.text.SimpleDateFormat) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) HttpRequest(com.mashape.unirest.request.HttpRequest) Instant(java.time.Instant) ArrayList(java.util.ArrayList) List(java.util.List) Callback(com.mashape.unirest.http.async.Callback) RawEvent(io.discloader.discloader.common.event.RawEvent) PermissionsException(io.discloader.discloader.common.exceptions.PermissionsException) DLUtil.gson(io.discloader.discloader.util.DLUtil.gson) HttpResponse(com.mashape.unirest.http.HttpResponse) DiscordException(io.discloader.discloader.common.exceptions.DiscordException) Map(java.util.Map) DiscLoader(io.discloader.discloader.common.DiscLoader) UnauthorizedException(io.discloader.discloader.common.exceptions.UnauthorizedException) ExceptionJSON(io.discloader.discloader.network.json.ExceptionJSON) ParseException(java.text.ParseException) BaseRequest(com.mashape.unirest.request.BaseRequest) DateFormat(java.text.DateFormat) AccountTypeException(io.discloader.discloader.common.exceptions.AccountTypeException) RawEvent(io.discloader.discloader.common.event.RawEvent) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) PermissionsException(io.discloader.discloader.common.exceptions.PermissionsException) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) PermissionsException(io.discloader.discloader.common.exceptions.PermissionsException) DiscordException(io.discloader.discloader.common.exceptions.DiscordException) UnauthorizedException(io.discloader.discloader.common.exceptions.UnauthorizedException) ParseException(java.text.ParseException) AccountTypeException(io.discloader.discloader.common.exceptions.AccountTypeException) ExceptionJSON(io.discloader.discloader.network.json.ExceptionJSON) AccountTypeException(io.discloader.discloader.common.exceptions.AccountTypeException) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) BaseRequest(com.mashape.unirest.request.BaseRequest) UnauthorizedException(io.discloader.discloader.common.exceptions.UnauthorizedException) DiscordException(io.discloader.discloader.common.exceptions.DiscordException) ParseException(java.text.ParseException) Map(java.util.Map) SimpleDateFormat(java.text.SimpleDateFormat)

Example 5 with BaseRequest

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

the class RestController method fetchProperties.

@Override
public void fetchProperties(DeferredVertexQuery uniQuery) {
    RestCollector<RestVertexSchema, BaseRequest, Vertex> collector = new RestCollector<>(schema -> schema.getSearch(uniQuery), (schema, result) -> schema.parseResults(result, uniQuery));
    Map<RestVertexSchema, BaseRequest> schemas = vertexSchemas.stream().filter(schema -> this.traversalFilter.filter(schema, uniQuery.getTraversal())).collect(collector);
    Iterator<Vertex> iterator = search(uniQuery, schemas, collector);
    Map<Object, DeferredVertex> vertexMap = uniQuery.getVertices().stream().collect(Collectors.toMap(UniElement::id, Function.identity(), (a, b) -> a));
    iterator.forEachRemaining(newVertex -> {
        DeferredVertex deferredVertex = vertexMap.get(newVertex.id());
        if (deferredVertex != null)
            deferredVertex.loadProperties(newVertex);
    });
}
Also used : java.util(java.util) LoggerFactory(org.slf4j.LoggerFactory) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) SimpleController(org.unipop.query.controller.SimpleController) EmptyIterator(org.apache.tinkerpop.gremlin.util.iterator.EmptyIterator) UniQuery(org.unipop.query.UniQuery) AddEdgeQuery(org.unipop.query.mutation.AddEdgeQuery) RemoveQuery(org.unipop.query.mutation.RemoveQuery) TraversalFilter(org.unipop.structure.traversalfilter.TraversalFilter) ElementSchema(org.unipop.schema.element.ElementSchema) UniEdge(org.unipop.structure.UniEdge) DeferredVertexQuery(org.unipop.query.search.DeferredVertexQuery) Collector(java.util.stream.Collector) JsonNode(com.mashape.unirest.http.JsonNode) BaseRequest(com.mashape.unirest.request.BaseRequest) Edge(org.apache.tinkerpop.gremlin.structure.Edge) DeferredVertex(org.unipop.schema.reference.DeferredVertex) Logger(org.slf4j.Logger) UniElement(org.unipop.structure.UniElement) AddVertexQuery(org.unipop.query.mutation.AddVertexQuery) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Collectors(java.util.stream.Collectors) Element(org.apache.tinkerpop.gremlin.structure.Element) HttpResponse(com.mashape.unirest.http.HttpResponse) UniGraph(org.unipop.structure.UniGraph) PropertyQuery(org.unipop.query.mutation.PropertyQuery) SearchQuery(org.unipop.query.search.SearchQuery) java.util.function(java.util.function) SearchVertexQuery(org.unipop.query.search.SearchVertexQuery) UniVertex(org.unipop.structure.UniVertex) DeferredVertex(org.unipop.schema.reference.DeferredVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) UniVertex(org.unipop.structure.UniVertex) DeferredVertex(org.unipop.schema.reference.DeferredVertex) BaseRequest(com.mashape.unirest.request.BaseRequest)

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