Search in sources :

Example 91 with HttpUrl

use of okhttp3.HttpUrl in project runelite by runelite.

the class SessionClient method open.

public UUID open() throws IOException {
    HttpUrl url = RuneLiteAPI.getApiBase().newBuilder().addPathSegment("session").build();
    Request request = new Request.Builder().url(url).build();
    try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute()) {
        ResponseBody body = response.body();
        InputStream in = body.byteStream();
        return RuneLiteAPI.GSON.fromJson(new InputStreamReader(in), UUID.class);
    } catch (// UUID.fromString can throw IllegalArgumentException
    JsonParseException | IllegalArgumentException ex) {
        throw new IOException(ex);
    }
}
Also used : Response(okhttp3.Response) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Request(okhttp3.Request) IOException(java.io.IOException) JsonParseException(com.google.gson.JsonParseException) HttpUrl(okhttp3.HttpUrl) ResponseBody(okhttp3.ResponseBody)

Example 92 with HttpUrl

use of okhttp3.HttpUrl in project runelite by runelite.

the class AccountClient method sesssionCheck.

public boolean sesssionCheck() {
    HttpUrl url = RuneLiteAPI.getApiBase().newBuilder().addPathSegment("account").addPathSegment("session-check").build();
    logger.debug("Built URI: {}", url);
    Request request = new Request.Builder().header(RuneLiteAPI.RUNELITE_AUTH, uuid.toString()).url(url).build();
    try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute()) {
        return response.isSuccessful();
    } catch (IOException ex) {
        logger.debug("Unable to verify session", ex);
        // assume it is still valid if the server is unreachable
        return true;
    }
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) IOException(java.io.IOException) HttpUrl(okhttp3.HttpUrl)

Example 93 with HttpUrl

use of okhttp3.HttpUrl in project runelite by runelite.

the class AccountClient method logout.

public void logout() throws IOException {
    HttpUrl url = RuneLiteAPI.getApiBase().newBuilder().addPathSegment("account").addPathSegment("logout").build();
    logger.debug("Built URI: {}", url);
    Request request = new Request.Builder().header(RuneLiteAPI.RUNELITE_AUTH, uuid.toString()).url(url).build();
    try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute()) {
        logger.debug("Sent logout request");
    }
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) HttpUrl(okhttp3.HttpUrl)

Example 94 with HttpUrl

use of okhttp3.HttpUrl in project TicktockMusic by Lauzy.

the class BaseUrlInterceptor method intercept.

@Override
public Response intercept(@NonNull Chain chain) throws IOException {
    Request originalRequest = chain.request();
    String urlHead = originalRequest.headers().get(NetConstants.Header.BASE_URL_HEAD);
    HttpUrl oldHttpUrl = originalRequest.url();
    // Request.Builder builder = originalRequest.newBuilder();
    if (urlHead != null && !urlHead.isEmpty()) {
        HttpUrl newBaseUrl;
        Request.Builder builder;
        // builder.removeHeader(NetConstants.Header.BASE_URL_HEAD);
        if (NetConstants.Header.BAIDU_HEAD_CONTENT.equals(urlHead)) {
            newBaseUrl = HttpUrl.parse(NetConstants.BASE_URL);
            builder = originalRequest.newBuilder().addHeader(NetConstants.Header.USER_AGENT, NetConstants.Header.USER_AGENT_CONTENT);
        } else if (NetConstants.Header.BANDSINTOWN_HEAD_CONTENT.equals(urlHead)) {
            newBaseUrl = HttpUrl.parse(NetConstants.BASE_ARTIST_URL);
            builder = originalRequest.newBuilder();
        } else if (NetConstants.Header.GECIMI_HEAD_CONTENT.equals(urlHead)) {
            newBaseUrl = HttpUrl.parse(NetConstants.BASE_LRC_URL);
            builder = originalRequest.newBuilder();
        } else {
            newBaseUrl = oldHttpUrl;
            builder = originalRequest.newBuilder();
        }
        if (null == newBaseUrl) {
            return null;
        }
        HttpUrl newFullUrl = oldHttpUrl.newBuilder().scheme(newBaseUrl.scheme()).host(newBaseUrl.host()).port(newBaseUrl.port()).build();
        return chain.proceed(builder.url(newFullUrl).build());
    } else {
        return chain.proceed(originalRequest);
    }
}
Also used : Request(okhttp3.Request) HttpUrl(okhttp3.HttpUrl)

Example 95 with HttpUrl

use of okhttp3.HttpUrl in project runelite by runelite.

the class TwitterService method getTweets.

private List<FeedItem> getTweets(boolean hasRetried) throws IOException {
    if (token == null) {
        updateToken();
    }
    HttpUrl url = LIST_STATUSES_URL.newBuilder().addQueryParameter("list_id", listId).addQueryParameter("count", "15").addQueryParameter("include_entities", "false").build();
    Request request = new Request.Builder().url(url).header("Authorization", "Bearer " + token).build();
    try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute()) {
        if (!response.isSuccessful()) {
            switch(HttpStatus.valueOf(response.code())) {
                case BAD_REQUEST:
                case UNAUTHORIZED:
                    updateToken();
                    if (!hasRetried) {
                        return getTweets(true);
                    }
                    throw new InternalServerErrorException("Could not auth to Twitter after trying once: " + response.message());
                default:
                    throw new IOException("Error getting Twitter list: " + response.message());
            }
        }
        InputStream in = response.body().byteStream();
        Type listType = new TypeToken<List<TwitterStatusesResponseItem>>() {
        }.getType();
        List<TwitterStatusesResponseItem> statusesResponse = RuneLiteAPI.GSON.fromJson(new InputStreamReader(in), listType);
        List<FeedItem> items = new ArrayList<>();
        for (TwitterStatusesResponseItem i : statusesResponse) {
            items.add(new FeedItem(FeedItemType.TWEET, i.getUser().getProfileImageUrl(), i.getUser().getScreenName(), i.getText().replace("\n\n", " ").replaceAll("\n", " "), "https://twitter.com/statuses/" + i.getId(), getTimestampFromSnowflake(i.getId())));
        }
        return items;
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Request(okhttp3.Request) ArrayList(java.util.ArrayList) IOException(java.io.IOException) HttpUrl(okhttp3.HttpUrl) Response(okhttp3.Response) FeedItemType(net.runelite.http.api.feed.FeedItemType) Type(java.lang.reflect.Type) FeedItem(net.runelite.http.api.feed.FeedItem) InternalServerErrorException(net.runelite.http.service.util.exception.InternalServerErrorException) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

HttpUrl (okhttp3.HttpUrl)302 Request (okhttp3.Request)130 Test (org.junit.Test)86 Response (okhttp3.Response)69 IOException (java.io.IOException)61 MockResponse (okhttp3.mockwebserver.MockResponse)40 Cookie (okhttp3.Cookie)35 ArrayList (java.util.ArrayList)28 OkHttpClient (okhttp3.OkHttpClient)27 MockWebServer (okhttp3.mockwebserver.MockWebServer)26 InputStream (java.io.InputStream)21 InputStreamReader (java.io.InputStreamReader)20 HashMap (java.util.HashMap)19 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)19 RequestBody (okhttp3.RequestBody)18 JsonParseException (com.google.gson.JsonParseException)13 File (java.io.File)12 OAuthRequest (com.github.scribejava.core.model.OAuthRequest)10 List (java.util.List)10 Test (org.junit.jupiter.api.Test)10