Example usage for com.squareup.okhttp RequestBody create

List of usage examples for com.squareup.okhttp RequestBody create

Introduction

In this page you can find the example usage for com.squareup.okhttp RequestBody create.

Prototype

public static RequestBody create(final MediaType contentType, final File file) 

Source Link

Document

Returns a new request body that transmits the content of file .

Usage

From source file:io.fabric8.docker.client.impl.TagImage.java

License:Apache License

@Override
public Boolean withTagName(String tagName) {
    try {/*from w  ww  . ja  v a 2s .  c  om*/
        StringBuilder sb = new StringBuilder().append(getOperationUrl()).append(Q).append(TAG_OPERATION)
                .append(EQUALS).append(tagName);

        sb.append(A).append(FORCE).append(EQUALS).append(force);

        if (Utils.isNotNullOrEmpty(repository)) {
            sb.append(A).append(REPOSITORY).append(EQUALS).append(repository);
        }

        RequestBody body = RequestBody.create(MEDIA_TYPE_JSON, ByteString.EMPTY);
        Request.Builder requestBuilder = new Request.Builder().post(body).url(new URL(sb.toString()));
        handleResponse(requestBuilder, 201);
        return true;
    } catch (Exception e) {
        throw DockerClientException.launderThrowable(e);
    }
}

From source file:io.fabric8.kubernetes.client.dsl.base.OperationSupport.java

License:Apache License

protected void handleDelete(URL requestUrl, long gracePeriodSeconds)
        throws ExecutionException, InterruptedException, KubernetesClientException, IOException {
    RequestBody requestBody = null;//from w w w .  jav  a  2  s  . c om
    if (gracePeriodSeconds >= 0) {
        DeleteOptions deleteOptions = new DeleteOptions();
        deleteOptions.setGracePeriodSeconds(gracePeriodSeconds);
        requestBody = RequestBody.create(JSON, JSON_MAPPER.writeValueAsString(deleteOptions));
    }

    Request.Builder requestBuilder = new Request.Builder().delete(requestBody).url(requestUrl);
    handleResponse(requestBuilder, 200, null);
}

From source file:io.fabric8.kubernetes.client.dsl.base.OperationSupport.java

License:Apache License

protected <T, I> T handleCreate(I resource, Class<T> outputType)
        throws ExecutionException, InterruptedException, KubernetesClientException, IOException {
    RequestBody body = RequestBody.create(JSON, JSON_MAPPER.writeValueAsString(resource));
    Request.Builder requestBuilder = new Request.Builder().post(body)
            .url(getNamespacedUrl(checkNamespace(resource)));
    return handleResponse(requestBuilder, 201, outputType);
}

From source file:io.fabric8.kubernetes.client.dsl.base.OperationSupport.java

License:Apache License

protected <T> T handleReplace(T updated, Class<T> type)
        throws ExecutionException, InterruptedException, KubernetesClientException, IOException {
    RequestBody body = RequestBody.create(JSON, JSON_MAPPER.writeValueAsString(updated));
    Request.Builder requestBuilder = new Request.Builder().put(body)
            .url(getResourceUrl(checkNamespace(updated), checkName(updated)));
    return handleResponse(requestBuilder, 200, type);
}

From source file:io.fabric8.kubernetes.server.mock.WebSocketSession.java

License:Apache License

private void send(final WebSocketMessage message) {
    executor.schedule(new Runnable() {
        @Override//from  ww  w .j a v a  2s.com
        public void run() {
            try {
                WebSocket ws = webSocketRef.get();
                if (ws != null) {
                    ws.sendMessage(RequestBody.create(WebSocket.BINARY, message.getBytes()));
                }
            } catch (IOException e) {
                throw KubernetesClientException.launderThrowable(e);
            }
        }
    }, message.getDelay(), TimeUnit.MILLISECONDS);
}

From source file:io.gameup.android.http.RequestFactory.java

License:Apache License

/**
 * Prepare a PUT request to the given path with the given data.
 *
 * @param uri The URL string to connect to.
 * @param apiKey The API key to use for authentication.
 * @param token The gamer token to use for authentication.
 * @return A Request ready to be sent via a OkHttpClient.
 * @throws UnsupportedEncodingException/*from  w w w . j  a  v  a2  s.co  m*/
 * @throws MalformedURLException
 */
public static Request put(final @NonNull Uri uri, final @NonNull String apiKey, final @NonNull String token,
        final @NonNull String body) throws UnsupportedEncodingException, MalformedURLException {
    return create(uri, apiKey, token)
            .put(RequestBody.create(APPLICATION_JSON_MEDIA_TYPE, body.getBytes("UTF-8"))).build();
}

From source file:io.gameup.android.http.RequestFactory.java

License:Apache License

/**
 * Prepare a POST request to the given path with the given data.
 *
 * @param uri The URL string to connect to.
 * @param apiKey The API key to use for authentication.
 * @param token The gamer token to use for authentication.
 * @return A Request ready to be sent via a OkHttpClient.
 * @throws UnsupportedEncodingException//w w  w . j a  v  a  2  s.c  o  m
 * @throws MalformedURLException
 */
public static Request post(final @NonNull Uri uri, final @NonNull String apiKey, final @NonNull String token,
        final @NonNull String body) throws UnsupportedEncodingException, MalformedURLException {
    return create(uri, apiKey, token)
            .post(RequestBody.create(APPLICATION_JSON_MEDIA_TYPE, body.getBytes("UTF-8"))).build();
}

From source file:io.kodokojo.bdd.stage.ApplicationThen.java

License:Open Source License

public SELF add_user_$_to_project_configuration_as_user_$_will_fail(@Quoted String usernameToAdd,
        @Quoted String usernameOwner) {
    UserInfo currentUser = currentUsers.get(usernameOwner);
    UserInfo userToAdd = currentUsers.get(usernameToAdd);

    String json = "[\n" + "    \"" + userToAdd.getIdentifier() + "\"\n" + "  ]";

    OkHttpClient httpClient = new OkHttpClient();

    RequestBody body = RequestBody.create(com.squareup.okhttp.MediaType.parse("application/json"),
            json.getBytes());//from w  w w.j a  va 2s.  c  o m
    Request.Builder builder = new Request.Builder()
            .url(getApiBaseUrl() + "/projectconfig/" + projectConfigurationId + "/user").put(body);
    Request request = HttpUserSupport.addBasicAuthentification(currentUser, builder).build();
    Response response = null;
    try {
        response = httpClient.newCall(request).execute();
        assertThat(response.code()).isEqualTo(403);
    } catch (IOException e) {
        fail(e.getMessage());
    } finally {
        if (response != null) {
            IOUtils.closeQuietly(response.body());
        }
    }
    return self();
}

From source file:io.kodokojo.bdd.stage.ApplicationWhen.java

License:Open Source License

public SELF retrive_a_new_id() {
    OkHttpClient httpClient = new OkHttpClient();
    RequestBody emptyBody = RequestBody.create(null, new byte[0]);
    String baseUrl = getBaseUrl();
    Request request = new Request.Builder().post(emptyBody).url(baseUrl + "/api/v1/user").build();
    Response response = null;//from   ww  w.  j a v a2s  . c o  m
    try {
        response = httpClient.newCall(request).execute();
        if (response.code() != 200) {
            fail("Invalid HTTP code status " + response.code() + " expected 200");
        }
        newUserId = response.body().string();

    } catch (IOException e) {
        fail(e.getMessage(), e);
    } finally {
        if (response != null) {
            IOUtils.closeQuietly(response.body());
        }
    }
    return self();
}

From source file:io.kodokojo.bdd.stage.ApplicationWhen.java

License:Open Source License

private SELF create_user(String email, boolean success) {
    if (isBlank(email)) {
        throw new IllegalArgumentException("email must be defined.");
    }/*from  w w w.  j ava 2s .  c o  m*/
    if (success && StringUtils.isBlank(newUserId)) {
        retrive_a_new_id();
    }

    OkHttpClient httpClient = new OkHttpClient();
    RequestBody body = RequestBody.create(MediaType.parse("application/json"),
            ("{\"email\": \"" + email + "\"}").getBytes());
    String baseUrl = getBaseUrl();

    Request.Builder builder = new Request.Builder().post(body)
            .url(baseUrl + "/api/v1/user" + (newUserId != null ? "/" + newUserId : ""));
    if (isNotBlank(currentUserLogin)) {
        UserInfo currentUser = currentUsers.get(currentUserLogin);
        if (currentUser != null) {
            builder = HttpUserSupport.addBasicAuthentification(currentUser, builder);
        }
    }
    Request request = builder.build();
    Response response = null;
    try {
        response = httpClient.newCall(request).execute();
        if (success) {
            assertThat(response.code()).isEqualTo(201);
            JsonParser parser = new JsonParser();
            String bodyResponse = response.body().string();
            JsonObject json = (JsonObject) parser.parse(bodyResponse);
            String currentUsername = json.getAsJsonPrimitive("username").getAsString();
            String currentUserPassword = json.getAsJsonPrimitive("password").getAsString();
            String currentUserEmail = json.getAsJsonPrimitive("email").getAsString();
            String currentUserIdentifier = json.getAsJsonPrimitive("identifier").getAsString();
            String currentUserEntityIdentifier = json.getAsJsonPrimitive("entityIdentifier").getAsString();
            currentUsers.put(currentUsername, new UserInfo(currentUsername, currentUserIdentifier,
                    currentUserEntityIdentifier, currentUserPassword, currentUserEmail));
            if (isBlank(currentUserLogin)) {
                currentUserLogin = currentUsername;
            }
            Attachment privateKey = Attachment.plainText(bodyResponse).withTitle(currentUsername + " response");
            currentStep.addAttachment(privateKey);
        } else {
            assertThat(response.code()).isNotEqualTo(201);
        }
        response.body().close();
    } catch (IOException e) {
        if (success) {
            fail(e.getMessage(), e);
        }
    } finally {
        if (response != null) {
            IOUtils.closeQuietly(response.body());
        }
    }
    return self();
}