Search in sources :

Example 1 with Request

use of me.semx11.autotip.api.request.Request in project Hyperium by HyperiumClient.

the class RequestHandler method getReply.

public static Optional<Reply> getReply(Request request, URI uri) {
    String json = null;
    HttpURLConnection conn = null;
    try {
        conn = (HttpURLConnection) uri.toURL().openConnection();
        conn.setRequestProperty("User-Agent", "Autotip v" + autotip.getVersion());
        InputStream input = conn.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST ? conn.getInputStream() : conn.getErrorStream();
        json = IOUtils.toString(input, StandardCharsets.UTF_8);
        Autotip.LOGGER.info(request.getType() + " JSON: " + json);
        Reply reply = GSON.fromJson(json, (Type) request.getType().getReplyClass());
        return Optional.ofNullable(reply);
    } catch (IOException | JsonParseException e) {
        ErrorReport.reportException(e);
        Autotip.LOGGER.debug(request.getType() + " JSON: " + json);
        return Optional.empty();
    } finally {
        if (conn != null)
            conn.disconnect();
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStream(java.io.InputStream) Reply(me.semx11.autotip.api.reply.Reply) IOException(java.io.IOException) JsonParseException(com.google.gson.JsonParseException)

Example 2 with Request

use of me.semx11.autotip.api.request.Request in project Hyperium by HyperiumClient.

the class KeepAliveRequest method execute.

@Override
public KeepAliveReply execute() {
    HttpUriRequest request = GetBuilder.of(this).addParameter("key", sessionKey).build();
    Optional<Reply> optional = RequestHandler.getReply(this, request.getURI());
    return optional.map(reply -> (KeepAliveReply) reply).orElseGet(() -> new KeepAliveReply(false));
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) KeepAliveReply(me.semx11.autotip.api.reply.impl.KeepAliveReply) RequestHandler(me.semx11.autotip.api.RequestHandler) SessionKey(me.semx11.autotip.api.SessionKey) RequestType(me.semx11.autotip.api.RequestType) GetBuilder(me.semx11.autotip.api.GetBuilder) Reply(me.semx11.autotip.api.reply.Reply) Optional(java.util.Optional) HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) Request(me.semx11.autotip.api.request.Request) KeepAliveReply(me.semx11.autotip.api.reply.impl.KeepAliveReply) Reply(me.semx11.autotip.api.reply.Reply) KeepAliveReply(me.semx11.autotip.api.reply.impl.KeepAliveReply)

Example 3 with Request

use of me.semx11.autotip.api.request.Request in project Hyperium by HyperiumClient.

the class SettingsRequest method execute.

@Override
public SettingsReply execute() {
    HttpUriRequest request = GetBuilder.of(this).addParameter("v", version.get()).build();
    Optional<Reply> optional = RequestHandler.getReply(this, request.getURI());
    return optional.map(reply -> (SettingsReply) reply).orElseGet(() -> new SettingsReply(false));
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) Autotip(me.semx11.autotip.Autotip) RequestHandler(me.semx11.autotip.api.RequestHandler) SettingsReply(me.semx11.autotip.api.reply.impl.SettingsReply) RequestType(me.semx11.autotip.api.RequestType) GetBuilder(me.semx11.autotip.api.GetBuilder) Reply(me.semx11.autotip.api.reply.Reply) Version(me.semx11.autotip.util.Version) Optional(java.util.Optional) HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) Request(me.semx11.autotip.api.request.Request) SettingsReply(me.semx11.autotip.api.reply.impl.SettingsReply) Reply(me.semx11.autotip.api.reply.Reply) SettingsReply(me.semx11.autotip.api.reply.impl.SettingsReply)

Example 4 with Request

use of me.semx11.autotip.api.request.Request in project Hyperium by HyperiumClient.

the class LocaleRequest method execute.

@Override
public LocaleReply execute() {
    HttpUriRequest request = GetBuilder.of(this).addParameter("lang", locale.toLanguageTag()).addParameter("v", version.get()).build();
    Optional<Reply> optional = RequestHandler.getReply(this, request.getURI());
    return optional.map(reply -> (LocaleReply) reply).orElseGet(() -> new LocaleReply(false));
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) Autotip(me.semx11.autotip.Autotip) RequestHandler(me.semx11.autotip.api.RequestHandler) LocaleReply(me.semx11.autotip.api.reply.impl.LocaleReply) Locale(java.util.Locale) RequestType(me.semx11.autotip.api.RequestType) GetBuilder(me.semx11.autotip.api.GetBuilder) Reply(me.semx11.autotip.api.reply.Reply) Version(me.semx11.autotip.util.Version) Optional(java.util.Optional) HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) Request(me.semx11.autotip.api.request.Request) LocaleReply(me.semx11.autotip.api.reply.impl.LocaleReply) LocaleReply(me.semx11.autotip.api.reply.impl.LocaleReply) Reply(me.semx11.autotip.api.reply.Reply)

Example 5 with Request

use of me.semx11.autotip.api.request.Request in project Hyperium by HyperiumClient.

the class LogoutRequest method execute.

@Override
public LogoutReply execute() {
    HttpUriRequest request = GetBuilder.of(this).addParameter("key", sessionKey).build();
    Optional<Reply> optional = RequestHandler.getReply(this, request.getURI());
    return optional.map(reply -> (LogoutReply) reply).orElseGet(() -> new LogoutReply(false));
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) LogoutReply(me.semx11.autotip.api.reply.impl.LogoutReply) RequestHandler(me.semx11.autotip.api.RequestHandler) SessionKey(me.semx11.autotip.api.SessionKey) RequestType(me.semx11.autotip.api.RequestType) GetBuilder(me.semx11.autotip.api.GetBuilder) Reply(me.semx11.autotip.api.reply.Reply) Optional(java.util.Optional) HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) Request(me.semx11.autotip.api.request.Request) LogoutReply(me.semx11.autotip.api.reply.impl.LogoutReply) LogoutReply(me.semx11.autotip.api.reply.impl.LogoutReply) Reply(me.semx11.autotip.api.reply.Reply)

Aggregations

Reply (me.semx11.autotip.api.reply.Reply)7 Optional (java.util.Optional)6 GetBuilder (me.semx11.autotip.api.GetBuilder)6 RequestHandler (me.semx11.autotip.api.RequestHandler)6 RequestType (me.semx11.autotip.api.RequestType)6 Request (me.semx11.autotip.api.request.Request)6 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)6 Autotip (me.semx11.autotip.Autotip)3 SessionKey (me.semx11.autotip.api.SessionKey)3 GameProfile (com.mojang.authlib.GameProfile)2 Version (me.semx11.autotip.util.Version)2 JsonParseException (com.google.gson.JsonParseException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 Locale (java.util.Locale)1 KeepAliveReply (me.semx11.autotip.api.reply.impl.KeepAliveReply)1 LocaleReply (me.semx11.autotip.api.reply.impl.LocaleReply)1 LoginReply (me.semx11.autotip.api.reply.impl.LoginReply)1 LogoutReply (me.semx11.autotip.api.reply.impl.LogoutReply)1