Skip to content

Commit

Permalink
don't expose okhttp3.MultipartBody.Part via Media.postMedia()
Browse files Browse the repository at this point in the history
Changes Media.postMedia() to accept a File and a media type String as parameters, then builds the necessary okhttp3.MultipartBody.Part itself instead of exposing this.

A more extensive Builder was suggested in andregasser#62, but I think that would need to live elsewhere, not directly in one of our method classes.
  • Loading branch information
bocops committed Jan 5, 2023
1 parent 3a2cab3 commit 32f0880
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
6 changes: 3 additions & 3 deletions bigbone-rx/src/main/kotlin/social/bigbone/rx/RxMedia.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package social.bigbone.rx

import io.reactivex.rxjava3.core.Single
import okhttp3.MultipartBody
import social.bigbone.MastodonClient
import social.bigbone.api.entity.Attachment
import social.bigbone.api.method.Media
import java.io.File

class RxMedia(client: MastodonClient) {
val media = Media(client)

fun postMedia(part: MultipartBody.Part): Single<Attachment> {
fun postMedia(file: File, mediaType: String): Single<Attachment> {
return Single.create {
try {
val result = media.postMedia(part)
val result = media.postMedia(file, mediaType)
it.onSuccess(result.execute())
} catch (e: Throwable) {
it.onError(e)
Expand Down
15 changes: 12 additions & 3 deletions bigbone/src/main/kotlin/social/bigbone/api/method/Media.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
package social.bigbone.api.method

import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import social.bigbone.MastodonClient
import social.bigbone.MastodonRequest
import social.bigbone.api.entity.Attachment

class Media(private val client: MastodonClient) {
// POST /api/v1/media
fun postMedia(file: MultipartBody.Part): MastodonRequest<Attachment> {
/**
* Creates an attachment to be used with a new status.
* @param file the file that should be uploaded
* @param mediaType media type of the file as a string, e.g. "image/png"
* @see <a href="https://docs.joinmastodon.org/methods/media/#v1">Mastodon API documentation: methods/media/#v1</a>
*/
fun postMedia(file: java.io.File, mediaType: String): MastodonRequest<Attachment> {
val body = file.asRequestBody(mediaType.toMediaTypeOrNull())
val part = MultipartBody.Part.createFormData("file", file.name, body)
val requestBody = MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addPart(file)
.addPart(part)
.build()
return MastodonRequest<Attachment>(
{
Expand Down
11 changes: 7 additions & 4 deletions bigbone/src/test/kotlin/social/bigbone/api/method/MediaTest.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package social.bigbone.api.method

import okhttp3.MultipartBody
import org.amshove.kluent.shouldBeEqualTo
import org.amshove.kluent.shouldNotBe
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import social.bigbone.api.exception.BigboneRequestException
import social.bigbone.extension.emptyRequestBody
import social.bigbone.testtool.MockClient
import java.io.File

class MediaTest {
@Test
fun postMedia() {
val client = MockClient.mock("attachment.json")
val media = Media(client)
val attachment = media.postMedia(MultipartBody.Part.create(emptyRequestBody())).execute()
val file = File("foo.bar")
val mediaType = "image/foo"
val attachment = media.postMedia(file, mediaType).execute()
attachment.id shouldBeEqualTo "10"
attachment.type shouldBeEqualTo "video"
attachment.url shouldBeEqualTo "youtube"
Expand All @@ -28,7 +29,9 @@ class MediaTest {
Assertions.assertThrows(BigboneRequestException::class.java) {
val client = MockClient.ioException()
val media = Media(client)
media.postMedia(MultipartBody.Part.create(emptyRequestBody())).execute()
val file = File("foo.bar")
val mediaType = "image/foo"
media.postMedia(file, mediaType).execute()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package social.bigbone.sample;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import social.bigbone.MastodonClient;
import social.bigbone.api.entity.Attachment;
import social.bigbone.api.entity.Status.Visibility;
Expand Down Expand Up @@ -30,9 +27,7 @@ public static void main(final String[] args) throws IOException {

// Upload image to Mastodon
final Media media = new Media(client);
final RequestBody requestBody = RequestBody.create(uploadFile, MediaType.parse("image/jpg"));
final MultipartBody.Part pFile = MultipartBody.Part.createFormData("file", uploadFile.getName(), requestBody);
final Attachment uploadedFile = media.postMedia(pFile).execute();
final Attachment uploadedFile = media.postMedia(uploadFile, "image/jpg").execute();
final String mediaId = uploadedFile.getId();

// Post status with media attached
Expand Down

0 comments on commit 32f0880

Please sign in to comment.