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;
}
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();
}
}
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;
}
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;
}
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();
}
});
}
Aggregations