All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.jamesnetherton.zulip.client.api.message.request.FileUploadApiRequest Maven / Gradle / Ivy

The newest version!
package com.github.jamesnetherton.zulip.client.api.message.request;

import static com.github.jamesnetherton.zulip.client.api.message.request.MessageRequestConstants.USER_UPLOADS_API_PATH;

import com.github.jamesnetherton.zulip.client.api.core.ExecutableApiRequest;
import com.github.jamesnetherton.zulip.client.api.core.ZulipApiRequest;
import com.github.jamesnetherton.zulip.client.api.message.response.FileUploadApiResponse;
import com.github.jamesnetherton.zulip.client.exception.ZulipClientException;
import com.github.jamesnetherton.zulip.client.http.ZulipHttpClient;
import java.io.File;

/**
 * Zulip API request builder for uploading a file to the Zulip server.
 *
 * @see https://zulip.com/api/upload-file
 */
public class FileUploadApiRequest extends ZulipApiRequest implements ExecutableApiRequest {

    private final File file;

    /**
     * Constructs a {@link FileUploadApiRequest}.
     *
     * @param client The Zulip HTTP client
     * @param file   The file to upload
     */
    public FileUploadApiRequest(ZulipHttpClient client, File file) {
        super(client);
        this.file = file;
    }

    /**
     * Executes the Zulip API request for uploading a file.
     *
     * @throws ZulipClientException if the request was not successful
     */
    @Override
    public String execute() throws ZulipClientException {
        FileUploadApiResponse response = client().upload(USER_UPLOADS_API_PATH, file, FileUploadApiResponse.class);
        if (response.getUri() != null) {
            return response.getUri();
        }
        return response.getUrl();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy