|
| 1 | +package test_with_remote_apis.methods.samples; |
| 2 | + |
| 3 | +import com.slack.api.Slack; |
| 4 | +import com.slack.api.methods.response.files.FilesUploadResponse; |
| 5 | +import config.Constants; |
| 6 | +import okhttp3.OkHttpClient; |
| 7 | +import okhttp3.Request; |
| 8 | +import okhttp3.Response; |
| 9 | +import org.junit.Test; |
| 10 | + |
| 11 | +import java.nio.file.Files; |
| 12 | +import java.nio.file.Path; |
| 13 | +import java.nio.file.Paths; |
| 14 | + |
| 15 | +import static java.util.stream.Collectors.joining; |
| 16 | +import static org.hamcrest.CoreMatchers.is; |
| 17 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 18 | + |
| 19 | +/** |
| 20 | + * For https://github.com/slackapi/java-slack-sdk/issues/453 |
| 21 | + */ |
| 22 | +public class Issue453_DownloadingFiles_Test { |
| 23 | + |
| 24 | + @Test |
| 25 | + public void sample() throws Exception { |
| 26 | + Slack slack = Slack.getInstance(); |
| 27 | + String token = System.getenv(Constants.SLACK_SDK_TEST_BOT_TOKEN); |
| 28 | + FilesUploadResponse uploadResult = slack.methods(token) |
| 29 | + .filesUpload(r -> r.content("This is a file!").filename("sample.txt")); |
| 30 | + |
| 31 | + OkHttpClient okHttpClient = new OkHttpClient(); |
| 32 | + Request request = new Request.Builder() |
| 33 | + .get() |
| 34 | + .url(uploadResult.getFile().getUrlPrivateDownload()) |
| 35 | + .header("Authorization", "Bearer " + token) |
| 36 | + .build(); |
| 37 | + Response response = okHttpClient.newCall(request).execute(); |
| 38 | + |
| 39 | + // You can find the file at slack-api-client/target/sample.txt |
| 40 | + Path localFilepath = Paths.get("target/sample.txt"); |
| 41 | + Files.write(localFilepath, response.body().bytes()); |
| 42 | + |
| 43 | + String body = Files.readAllLines(localFilepath).stream().collect(joining()); |
| 44 | + assertThat(body, is("This is a file!")); |
| 45 | + } |
| 46 | +} |
0 commit comments