Example usage for com.squareup.okhttp Response isSuccessful

List of usage examples for com.squareup.okhttp Response isSuccessful

Introduction

In this page you can find the example usage for com.squareup.okhttp Response isSuccessful.

Prototype

public boolean isSuccessful() 

Source Link

Document

Returns true if the code is in [200..300), which means the request was successfully received, understood, and accepted.

Usage

From source file:com.he5ed.lib.cloudprovider.apis.CloudDriveApi.java

License:Apache License

@Override
public void deleteFolder(@NonNull CFolder folder) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }/*  ww  w.  j  av  a 2  s  .  c o  m*/

    Uri uri = Uri.parse(mMetadataUrl);
    String url = uri.buildUpon().appendEncodedPath("trash/" + folder.getId()).build().toString();

    // create parameter as json
    final JSONObject params = new JSONObject();
    try {
        params.put("kind", "FOLDER");
    } catch (JSONException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }

    RequestBody body = new RequestBody() {
        @Override
        public MediaType contentType() {
            return JSON;
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            sink.writeUtf8(params.toString());
        }
    };

    Request request = new Request.Builder().url(url)
            .header("Authorization", String.format("Bearer %s", mAccessToken)).put(body).build();

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            Log.d(TAG, "CFolder with the id: " + folder.getName() + " deleted");
        } else {
            throw new RequestFailException(response.message(), response.code());
        }
    } catch (IOException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }
}

From source file:com.he5ed.lib.cloudprovider.apis.CloudDriveApi.java

License:Apache License

@Override
public CFile getFileInfo(@NonNull String fileId) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }/*  w ww . j a  v  a 2 s.  c  o m*/

    Uri uri = Uri.parse(mMetadataUrl);
    String url = uri.buildUpon().appendEncodedPath("nodes/" + fileId).build().toString();

    Request request = new Request.Builder().url(url)
            .header("Authorization", String.format("Bearer %s", mAccessToken)).get().build();

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            JSONObject jsonObject = new JSONObject(response.body().string());
            return buildFile(jsonObject);
        } else {
            throw new RequestFailException(response.message(), response.code());
        }
    } catch (JSONException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    } catch (IOException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }
}

From source file:com.he5ed.lib.cloudprovider.apis.CloudDriveApi.java

License:Apache License

@Override
public CFile uploadFile(@NonNull File file, @Nullable CFolder parent) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }/*from   w w  w.j a v a  2 s .c  o m*/

    Uri uri = Uri.parse(mContentUrl);
    String url = uri.buildUpon().appendEncodedPath("nodes").build().toString();

    // create parameter as json
    final JSONObject params = new JSONObject();
    try {
        params.put("name", file.getName());
        params.put("kind", "FILE");
        ArrayList<String> parentList = new ArrayList<>();
        parentList.add(parent != null ? parent.getId() : getRoot().getId());
        params.put("parents", new JSONArray(parentList));
    } catch (JSONException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }

    // create multipart body
    MediaType fileType = MediaType.parse(FilesUtils.getFileType(file));
    RequestBody multipart = new MultipartBuilder().type(MultipartBuilder.FORM)
            .addFormDataPart("metadata", params.toString())
            .addFormDataPart("content", file.getName(), RequestBody.create(fileType, file)).build();

    Request request = new Request.Builder().url(url)
            .header("Authorization", String.format("Bearer %s", mAccessToken)).post(multipart).build();

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            JSONObject jsonObject = new JSONObject(response.body().string());
            return buildFile(jsonObject);
        } else {
            throw new RequestFailException(response.message(), response.code());
        }
    } catch (JSONException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    } catch (IOException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }
}

From source file:com.he5ed.lib.cloudprovider.apis.CloudDriveApi.java

License:Apache License

@Override
public CFile updateFile(@NonNull CFile file, File content) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }//from   ww w . j a  va2 s  .  c  om

    Uri uri = Uri.parse(mContentUrl);
    String url = uri.buildUpon().appendEncodedPath("nodes/" + file.getId() + "/content").build().toString();

    // create multipart body
    MediaType fileType = MediaType.parse(FilesUtils.getFileType(content));
    RequestBody multipart = new MultipartBuilder().type(MultipartBuilder.FORM)
            .addFormDataPart("content", file.getName(), RequestBody.create(fileType, content)).build();

    Request request = new Request.Builder().url(url)
            .header("Authorization", String.format("Bearer %s", mAccessToken)).put(multipart).build();

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            JSONObject jsonObject = new JSONObject(response.body().string());
            return buildFile(jsonObject);
        } else {
            throw new RequestFailException(response.message(), response.code());
        }
    } catch (JSONException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    } catch (IOException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }
}

From source file:com.he5ed.lib.cloudprovider.apis.CloudDriveApi.java

License:Apache License

@Override
public CFile renameFile(@NonNull CFile file, String name) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }/*from www .j a  v  a 2  s  .  c o m*/

    // exist if same filename
    if (file.getName().equals(name))
        return file;

    // create parameter as json
    final JSONObject params = new JSONObject();
    try {
        params.put("name", name);
    } catch (JSONException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }

    RequestBody body = new RequestBody() {
        @Override
        public MediaType contentType() {
            return JSON;
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            sink.writeUtf8(params.toString());
        }
    };

    Uri uri = Uri.parse(mMetadataUrl);
    String url = uri.buildUpon().appendEncodedPath("nodes/" + file.getId()).build().toString();

    Request request = new Request.Builder().url(url)
            .header("Authorization", String.format("Bearer %s", mAccessToken)).patch(body).build();

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            // return file object
            return buildFile(new JSONObject(response.body().string()));
        } else {
            throw new RequestFailException(response.message(), response.code());
        }
    } catch (JSONException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    } catch (IOException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }
}

From source file:com.he5ed.lib.cloudprovider.apis.CloudDriveApi.java

License:Apache License

@Override
public CFile moveFile(@NonNull CFile file, @Nullable CFolder folder) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }/*from   ww  w .  j  ava 2 s . c  om*/

    // create parameter as json
    final JSONObject params = new JSONObject();
    try {
        ArrayList<String> parentList = new ArrayList<>();
        parentList.add(folder != null ? folder.getId() : getRoot().getId());
        params.put("parents", new JSONArray(parentList));
    } catch (JSONException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }

    RequestBody body = new RequestBody() {
        @Override
        public MediaType contentType() {
            return JSON;
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            sink.writeUtf8(params.toString());
        }
    };

    Uri uri = Uri.parse(mMetadataUrl);
    String url = uri.buildUpon().appendEncodedPath("nodes/" + file.getId()).build().toString();

    Request request = new Request.Builder().url(url)
            .header("Authorization", String.format("Bearer %s", mAccessToken)).patch(body).build();

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            // return file object
            return buildFile(new JSONObject(response.body().string()));
        } else {
            throw new RequestFailException(response.message(), response.code());
        }
    } catch (JSONException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    } catch (IOException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }
}

From source file:com.he5ed.lib.cloudprovider.apis.CloudDriveApi.java

License:Apache License

@Override
public File downloadFile(@NonNull CFile file, String filename) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }/*from   w  w  w  .  j  a v a  2  s  .co m*/

    Uri uri = Uri.parse(mContentUrl);
    String url = uri.buildUpon().appendEncodedPath("nodes/" + file.getId() + "/content").build().toString();

    Request request = new Request.Builder().url(url)
            .header("Authorization", String.format("Bearer %s", mAccessToken)).get().build();

    try {
        File localFile = new File(mContext.getFilesDir(),
                TextUtils.isEmpty(filename) ? file.getName() : filename);

        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            FilesUtils.copyFile(response.body().byteStream(), new FileOutputStream(localFile));
        } else {
            throw new RequestFailException(response.message(), response.code());
        }
        return localFile;
    } catch (IOException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }
}

From source file:com.he5ed.lib.cloudprovider.apis.CloudDriveApi.java

License:Apache License

@Override
public void deleteFile(@NonNull CFile file) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }/*from  w  w  w .  j a  v a  2s  .c  om*/

    Uri uri = Uri.parse(mMetadataUrl);
    String url = uri.buildUpon().appendEncodedPath("trash/" + file.getId()).build().toString();

    // create parameter as json
    final JSONObject params = new JSONObject();
    try {
        params.put("kind", "FOLDER");
    } catch (JSONException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }

    RequestBody body = new RequestBody() {
        @Override
        public MediaType contentType() {
            return JSON;
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            sink.writeUtf8(params.toString());
        }
    };

    Request request = new Request.Builder().url(url)
            .header("Authorization", String.format("Bearer %s", mAccessToken)).put(body).build();

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            Log.d(TAG, "File with the id: " + file.getName() + " deleted");
        } else {
            throw new RequestFailException(response.message(), response.code());
        }
    } catch (IOException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }
}

From source file:com.he5ed.lib.cloudprovider.apis.CloudDriveApi.java

License:Apache License

@Override
public File getThumbnail(@NonNull CFile file) throws RequestFailException {
    if (TextUtils.isEmpty(mAccessToken)) {
        throw new RequestFailException("Access token not available");
    }//  w  w  w . ja  va  2  s  .  c o  m

    Uri uri = Uri.parse(mMetadataUrl);
    String url = uri.buildUpon().appendEncodedPath("nodes/" + file.getId()).appendQueryParameter("asset", "ALL")
            .appendQueryParameter("tempLink", "true").build().toString();

    Request request = new Request.Builder().url(url)
            .header("Authorization", String.format("Bearer %s", mAccessToken)).get().build();

    try {
        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            JSONObject jsonObject = new JSONObject(response.body().string());
            if (jsonObject.has("assets")) {
                JSONArray assets = jsonObject.getJSONArray("assets");
                JSONObject asset = assets.getJSONObject(0);
                if (asset.has("tempLink")) {
                    String fileUrl = asset.getString("tempLink");
                    String filename = asset.getString("name");
                    return downloadThumbnail(fileUrl, filename);
                }
                return null;
            } else {
                return null;
            }
        } else {
            throw new RequestFailException(response.message(), response.code());
        }
    } catch (IOException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.he5ed.lib.cloudprovider.apis.CloudDriveApi.java

License:Apache License

/**
 * Download thumbnail via the temperary url
 *
 * @param url/*www.j a v  a2  s . co m*/
 * @param filename
 * @return
 * @throws RequestFailException
 */
private File downloadThumbnail(@NonNull String url, String filename) throws RequestFailException {

    Request request = new Request.Builder().url(url).get().build();

    try {
        File localFile = new File(mContext.getFilesDir(), filename);

        Response response = mHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            FilesUtils.copyFile(response.body().byteStream(), new FileOutputStream(localFile));
        } else {
            throw new RequestFailException(response.message(), response.code());
        }
        return localFile;
    } catch (IOException e) {
        e.printStackTrace();
        throw new RequestFailException(e.getMessage());
    }
}