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