Search in sources :

Example 81 with Builder

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();
}
Also used : Request(okhttp3.Request) ApiRequest(com.yandex.money.api.net.ApiRequest) ApiRequest(com.yandex.money.api.net.ApiRequest) Map(java.util.Map) RequestBody(okhttp3.RequestBody)

Example 82 with Builder

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);
    }
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Builder(okhttp3.Request.Builder) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException)

Example 83 with Builder

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");
}
Also used : Response(okhttp3.Response) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Builder(okhttp3.Request.Builder)

Example 84 with Builder

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");
    }
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Builder(okhttp3.Request.Builder) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException)

Example 85 with Builder

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();
}
Also used : Builder(okhttp3.OkHttpClient.Builder) Request(okhttp3.Request) AsyncClientHttpRequest(org.springframework.http.client.AsyncClientHttpRequest) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) List(java.util.List) Map(java.util.Map) RequestBody(okhttp3.RequestBody)

Aggregations

Request (okhttp3.Request)206 Response (okhttp3.Response)148 OkHttpClient (okhttp3.OkHttpClient)142 IOException (java.io.IOException)111 RequestBody (okhttp3.RequestBody)81 Test (org.junit.Test)75 HttpUrl (okhttp3.HttpUrl)47 File (java.io.File)42 MockResponse (okhttp3.mockwebserver.MockResponse)42 MultipartBody (okhttp3.MultipartBody)40 Map (java.util.Map)39 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)31 Call (okhttp3.Call)29 Interceptor (okhttp3.Interceptor)29 Retrofit (retrofit2.Retrofit)29 Builder (okhttp3.OkHttpClient.Builder)26 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)25 ResponseBody (okhttp3.ResponseBody)24 HashMap (java.util.HashMap)22 FormBody (okhttp3.FormBody)21