private Request buildRequest(String hostname, int type) { Request.Builder requestBuilder = new Request.Builder().header("Accept", DNS_MESSAGE.toString()); ByteString query = DnsRecordCodec.encodeQuery(hostname, type); if (post) { requestBuilder = requestBuilder.url(url).post(RequestBody.create(DNS_MESSAGE, query)); } else { String encoded = query.base64Url().replace("=", ""); HttpUrl requestUrl = url.newBuilder().addQueryParameter("dns", encoded).build(); requestBuilder = requestBuilder.url(requestUrl); } return requestBuilder.build(); }
/** See https://api.slack.com/methods/rtm.start. */ public RtmStartResponse rtmStart(String accessToken) throws IOException { HttpUrl url = baseUrl.newBuilder("rtm.start") .addQueryParameter("token", accessToken) .build(); Request request = new Request.Builder() .url(url) .build(); Call call = httpClient.newCall(request); try (Response response = call.execute()) { JsonAdapter<RtmStartResponse> jsonAdapter = moshi.adapter(RtmStartResponse.class); return jsonAdapter.fromJson(response.body().source()); } }
private Request injectParamsIntoUrl(HttpUrl.Builder httpUrlBuilder, Request.Builder requestBuilder, Map<String, String> paramsMap) { if (paramsMap.size() > 0) { Iterator iterator = paramsMap.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry entry = (Map.Entry) iterator.next(); httpUrlBuilder.addQueryParameter((String) entry.getKey(), (String) entry.getValue()); } requestBuilder.url(httpUrlBuilder.build()); return requestBuilder.build(); } return null; }
@Override @SneakyThrows public InvocationBuilder request() { String resource = StringUtils.join(path, "/"); if (!resource.startsWith("/")) { resource = "/" + resource; } HttpUrl.Builder baseUrlBuilder = baseUrl.newBuilder() .encodedPath(resource); for (Map.Entry<String, Collection<String>> queryParamEntry : queryParams.asMap().entrySet()) { String key = queryParamEntry.getKey(); for (String paramValue : queryParamEntry.getValue()) { baseUrlBuilder.addQueryParameter(key, paramValue); } } return new OkHttpInvocationBuilder( MAPPER, okHttpClient, baseUrlBuilder.build() ); }
public int getQp(String username) throws IOException { HttpUrl url = RuneLiteAPI.getApiBase().newBuilder() .addPathSegment("chat") .addPathSegment("qp") .addQueryParameter("name", username) .build(); Request request = new Request.Builder() .url(url) .build(); try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute()) { if (!response.isSuccessful()) { throw new IOException("Unable to look up quest points!"); } return Integer.parseInt(response.body().string()); } }
public String getUrl() { String tempUrl = mUrl; for (HashMap.Entry<String, String> entry : mPathParameterMap.entrySet()) { tempUrl = tempUrl.replace("{" + entry.getKey() + "}", String.valueOf(entry.getValue())); } HttpUrl.Builder urlBuilder = HttpUrl.parse(tempUrl).newBuilder(); if (mQueryParameterMap != null) { Set<Map.Entry<String, List<String>>> entries = mQueryParameterMap.entrySet(); for (Map.Entry<String, List<String>> entry : entries) { String name = entry.getKey(); List<String> list = entry.getValue(); if (list != null) { for (String value : list) { urlBuilder.addQueryParameter(name, value); } } } } return urlBuilder.build().toString(); }
void ping(UUID uuid) throws IOException { HttpUrl url = RuneLiteAPI.getApiRoot().newBuilder() .addPathSegment("session") .addPathSegment("ping") .addQueryParameter("session", uuid.toString()) .build(); Request request = new Request.Builder() .url(url) .build(); try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute()) { if (!response.isSuccessful()) { throw new IOException("Unsuccessful ping"); } } }
public boolean submitKc(String username, String boss, int kc) throws IOException { HttpUrl url = RuneLiteAPI.getApiBase().newBuilder() .addPathSegment("chat") .addPathSegment("kc") .addQueryParameter("name", username) .addQueryParameter("boss", boss) .addQueryParameter("kc", Integer.toString(kc)) .build(); Request request = new Request.Builder() .post(RequestBody.create(null, new byte[0])) .url(url) .build(); try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute()) { return response.isSuccessful(); } }
void addQueryParam(String name, @Nullable String value, boolean encoded) { if (relativeUrl != null) { // Do a one-time combination of the built relative URL and the base URL. urlBuilder = baseUrl.newBuilder(relativeUrl); if (urlBuilder == null) { throw new IllegalArgumentException( "Malformed URL. Base: " + baseUrl + ", Relative: " + relativeUrl); } relativeUrl = null; } if (encoded) { //noinspection ConstantConditions Checked to be non-null by above 'if' block. urlBuilder.addEncodedQueryParameter(name, value); } else { //noinspection ConstantConditions Checked to be non-null by above 'if' block. urlBuilder.addQueryParameter(name, value); } }
public boolean submitPb(String username, String boss, int pb) throws IOException { HttpUrl url = RuneLiteAPI.getApiBase().newBuilder() .addPathSegment("chat") .addPathSegment("pb") .addQueryParameter("name", username) .addQueryParameter("boss", boss) .addQueryParameter("pb", Integer.toString(pb)) .build(); Request request = new Request.Builder() .post(RequestBody.create(null, new byte[0])) .url(url) .build(); try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute()) { return response.isSuccessful(); } }
public boolean submitQp(String username, int qp) throws IOException { HttpUrl url = RuneLiteAPI.getApiBase().newBuilder() .addPathSegment("chat") .addPathSegment("qp") .addQueryParameter("name", username) .addQueryParameter("qp", Integer.toString(qp)) .build(); Request request = new Request.Builder() .post(RequestBody.create(null, new byte[0])) .url(url) .build(); try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute()) { return response.isSuccessful(); } }
@Override public String get(String url, Map<String, String> queryParas) { okhttp3.HttpUrl.Builder urlBuilder = okhttp3.HttpUrl.parse(url).newBuilder(); for (Entry<String, String> entry : queryParas.entrySet()) { urlBuilder.addQueryParameter(entry.getKey(), entry.getValue()); } okhttp3.HttpUrl httpUrl = urlBuilder.build(); okhttp3.Request request = new okhttp3.Request.Builder().url(httpUrl).get().build(); return exec(request); }
void delete(UUID uuid) throws IOException { HttpUrl url = RuneLiteAPI.getApiRoot().newBuilder() .addPathSegment("session") .addQueryParameter("session", uuid.toString()) .build(); Request request = new Request.Builder() .delete() .url(url) .build(); RuneLiteAPI.CLIENT.newCall(request).execute().close(); } }
public Builder setQueryParameter(String name, @Nullable String value) { removeAllQueryParameters(name); addQueryParameter(name, value); return this; }
private void openLoginInBrowser() { String initialScope = "user,public_repo,repo,delete_repo,notifications,gist"; HttpUrl.Builder url = new HttpUrl.Builder() .scheme("https") .host(OAUTH_HOST) .addPathSegment("login") .addPathSegment("oauth") .addPathSegment("authorize") .addQueryParameter("client_id", getString(R.string.github_client)) .addQueryParameter("scope", initialScope); Intent intent = new Intent(this, LoginWebViewActivity.class); intent.putExtra(INTENT_EXTRA_URL, url.toString()); startActivityForResult(intent, WEBVIEW_REQUEST_CODE); }
private static void completeUrlQueryParameters(BaseRequest<?> request, HttpUrl.Builder urlBuilder) { request.getParameters().getKeys() .forEach(key -> request.getParameters().getValues(key) .forEach(value -> urlBuilder.addQueryParameter(key, value))); }
void addQueryParam(String name, @Nullable String value, boolean encoded) { if (relativeUrl != null) { // Do a one-time combination of the built relative URL and the base URL. urlBuilder = baseUrl.newBuilder(relativeUrl); if (urlBuilder == null) { throw new IllegalArgumentException( "Malformed URL. Base: " + baseUrl + ", Relative: " + relativeUrl); } relativeUrl = null; } if (encoded) { //noinspection ConstantConditions Checked to be non-null by above 'if' block. urlBuilder.addEncodedQueryParameter(name, value); } else { //noinspection ConstantConditions Checked to be non-null by above 'if' block. urlBuilder.addQueryParameter(name, value); } }
/** See https://api.slack.com/methods/oauth.access. */ public OAuthSession exchangeCode(String code, HttpUrl redirectUrl) throws IOException { HttpUrl url = baseUrl.newBuilder("oauth.access") .addQueryParameter("client_id", clientId) .addQueryParameter("client_secret", clientSecret) .addQueryParameter("code", code) .addQueryParameter("redirect_uri", redirectUrl.toString()) .build(); Request request = new Request.Builder() .url(url) .build(); Call call = httpClient.newCall(request); try (Response response = call.execute()) { JsonAdapter<OAuthSession> jsonAdapter = moshi.adapter(OAuthSession.class); return jsonAdapter.fromJson(response.body().source()); } }
public Builder setQueryParameter(String name, @Nullable String value) { removeAllQueryParameters(name); addQueryParameter(name, value); return this; }
/** See https://api.slack.com/docs/oauth. */ public HttpUrl authorizeUrl(String scopes, HttpUrl redirectUrl, ByteString state, String team) { HttpUrl.Builder builder = baseUrl.newBuilder("/oauth/authorize") .addQueryParameter("client_id", clientId) .addQueryParameter("scope", scopes) .addQueryParameter("redirect_uri", redirectUrl.toString()) .addQueryParameter("state", state.base64()); if (team != null) { builder.addQueryParameter("team", team); } return builder.build(); }