use of okhttp3.HttpUrl.Builder in project yandex-money-sdk-java by yandex-money.
the class DefaultApiClient method prepareRequest.
private Request prepareRequest(ApiRequest<?> request) {
checkNotNull(request, "request");
Request.Builder builder = new Request.Builder().cacheControl(cacheControl).url(request.requestUrl(getHostsProvider())).addHeader(HttpHeaders.USER_AGENT, getUserAgent().getName()).addHeader(HttpHeaders.ACCEPT_LANGUAGE, getLanguage().iso6391Code);
if (isAuthorized()) {
builder.addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken);
}
for (Map.Entry<String, String> entry : request.getHeaders().entrySet()) {
String value = entry.getValue();
if (value != null) {
builder.addHeader(entry.getKey(), value);
}
}
ApiRequest.Method method = request.getMethod();
if (method != ApiRequest.Method.GET) {
RequestBody body = RequestBody.create(MediaType.parse(request.getContentType()), request.getBody());
switch(method) {
case POST:
builder.post(body);
break;
case PUT:
builder.put(body);
break;
case DELETE:
builder.delete();
break;
}
}
return builder.build();
}
use of okhttp3.HttpUrl.Builder in project nzbhydra2 by theotherp.
the class ReleaseMojo method setReleaseEffective.
private void setReleaseEffective(org.nzbhydra.github.mavenreleaseplugin.ReleaseRequest releaseRequest, org.nzbhydra.github.mavenreleaseplugin.Release release) throws IOException, MojoExecutionException {
getLog().info("Setting release effective");
releaseRequest.setDraft(false);
String url = release.getUrl() + "?access_token=" + githubToken;
Call call = client.newCall(new Builder().url(url).patch(RequestBody.create(MediaType.parse("application/json"), objectMapper.writeValueAsString(releaseRequest))).build());
Response response = call.execute();
if (!response.isSuccessful()) {
throw new MojoExecutionException("When trying to set release effective Github returned code " + response.code() + " and message: " + response.message());
}
String body = response.body().string();
response.body().close();
try {
org.nzbhydra.github.mavenreleaseplugin.Release effectiveRelease = objectMapper.readValue(body, org.nzbhydra.github.mavenreleaseplugin.Release.class);
if (effectiveRelease.isDraft()) {
getLog().error("Release is still in state 'draft'");
} else {
getLog().info("Successfully set release effective");
}
} catch (Exception e) {
throw new MojoExecutionException("Unable to parse GitHub's release edit response: " + body, e);
}
}
use of okhttp3.HttpUrl.Builder in project nzbhydra2 by theotherp.
the class ReleaseMojo method uploadAssets.
private void uploadAssets(org.nzbhydra.github.mavenreleaseplugin.Release release) throws IOException, MojoExecutionException {
String uploadUrl = release.getUploadUrl();
uploadUrl = uploadUrl.replace("{?name,label}", "");
String name = windowsAsset.getName();
getLog().info("Uploading windows asset to " + uploadUrl);
Response response = client.newCall(new Builder().header("Content-Length", String.valueOf(windowsAsset.length())).url(uploadUrl + "?name=" + name + "&access_token=" + githubToken).post(RequestBody.create(MediaType.parse("application/zip"), windowsAsset)).build()).execute();
if (!response.isSuccessful()) {
throw new MojoExecutionException("When trying to upload windows asset Github returned code " + response.code() + " and message: " + response.message());
}
getLog().info("Successfully uploaded windows asset");
getLog().info("Uploading linux asset to " + uploadUrl);
name = linuxAsset.getName();
response = client.newCall(new Builder().header("Content-Length", String.valueOf(linuxAsset.length())).url(uploadUrl + "?name=" + name + "&access_token=" + githubToken).post(RequestBody.create(MediaType.parse("application/gzip"), linuxAsset)).build()).execute();
if (!response.isSuccessful()) {
throw new MojoExecutionException("When trying to upload linux asset Github returned code " + response.code() + " and message: " + response.message());
}
getLog().info("Successfully uploaded linux asset");
}
use of okhttp3.HttpUrl.Builder in project nzbhydra2 by theotherp.
the class ReleaseMojo method createRelease.
private org.nzbhydra.github.mavenreleaseplugin.Release createRelease(org.nzbhydra.github.mavenreleaseplugin.ReleaseRequest releaseRequest) throws IOException, MojoExecutionException {
getLog().info("Creating release in draft mode using base URL " + githubReleasesUrl);
String url = githubReleasesUrl + "?access_token=" + githubToken;
String requestBody = objectMapper.writeValueAsString(releaseRequest);
Call call = client.newCall(new Builder().url(url).post(RequestBody.create(MediaType.parse("application/json"), requestBody)).build());
Response response = call.execute();
if (!response.isSuccessful() || response.body() == null) {
throw new MojoExecutionException("When trying to create release with URL " + url + " Github returned code " + response.code() + " and message: " + response.message());
}
String body = response.body().string();
response.body().close();
try {
return objectMapper.readValue(body, org.nzbhydra.github.mavenreleaseplugin.Release.class);
} catch (Exception e) {
throw new MojoExecutionException("Unable to parse GitHub's release response: " + body, e);
} finally {
getLog().info("Successfully created release");
}
}
use of okhttp3.HttpUrl.Builder in project nzbhydra2 by theotherp.
the class HydraOkHttp3ClientHttpRequestFactory method buildRequest.
static Request buildRequest(HttpHeaders headers, byte[] content, URI uri, HttpMethod method) throws MalformedURLException {
okhttp3.MediaType contentType = getContentType(headers);
RequestBody body = (content.length > 0 || okhttp3.internal.http.HttpMethod.requiresRequestBody(method.name()) ? RequestBody.create(contentType, content) : null);
Request.Builder builder = new Request.Builder().url(uri.toURL()).method(method.name(), body);
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
String headerName = entry.getKey();
for (String headerValue : entry.getValue()) {
builder.addHeader(headerName, headerValue);
}
}
return builder.build();
}
Aggregations