Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create /media endpoint #410

Merged
merged 8 commits into from
Dec 2, 2024
Merged

Create /media endpoint #410

merged 8 commits into from
Dec 2, 2024

Conversation

oguzkocer
Copy link
Contributor

@oguzkocer oguzkocer commented Nov 27, 2024

Implements create /media endpoint. Due to its custom nature, it does NOT follow the established patterns, but instead requires some new patterns:

  • Adds a custom MediaUploadRequest that's similar to WpNetworkRequest, but it includes information such as file_path & file_content_type.
  • Adds a new error type MediaUploadRequestExecutionError which is similar to RequestExecutionError but it has an extra variant MediaFileNotFound. We can add more error types specific to media uploads here.
  • Adds a new method to RequestExecutor to upload media that uses the new MediaUploadRequest and MediaUploadRequestExecutionError.
  • MediaUploadRequest uses multipart/form-data content type header value and adds the parameters as form values. The header value is included in its header_map and the key value pairs for MediaCreateParams can be accessed from its media_params() function.
  • Adds Kotlin & Rust integration tests to verify that media upload is working correctly.

I am a little concerned about the clones in WpNetworkRequest & MediaUploadRequest and not entirely sure about the correct way to do this. From pure Rust perspective, this is definitely wrong. However, these types have to be cloned for FFI - or shared as a pointer. So, maybe we need to separate how Rust uses it and how it's exposed through FFI. I don't want to hold up this PR for this, so I opened a separate issue for it: #416

multipartBodyBuilder.addFormDataPart(k, v)
}
val file =
WpRequestExecutor::class.java.classLoader?.getResource(mediaUploadRequest.filePath())?.file?.let {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if this is the correct way to get the file in Android. I think it's correct, but we might need to pass a function in the initializer so it can be configured.

I am leaving it as is for now and we can address it if we find that it's not working when we integrate it to WordPress-Android.

@oguzkocer oguzkocer marked this pull request as ready for review December 2, 2024 15:43
@oguzkocer oguzkocer requested review from jkmassel and crazytonyli and removed request for jkmassel December 2, 2024 15:47
Copy link
Contributor

@jkmassel jkmassel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works for me over in #412.

The code looks fine, the Android stuff included. That might be worth adding to the example app to be sure, but that could be a subsequent PR.

I took the liberty of pushing a commit to put Swift stubs in place to unblock CI.

@jkmassel jkmassel merged commit c265a69 into trunk Dec 2, 2024
22 checks passed
@jkmassel jkmassel deleted the create-media-2 branch December 2, 2024 22:43
jkmassel added a commit that referenced this pull request Dec 3, 2024
* Create `/media` endpoint

* Manually implement create media response type and endpoint

* Restore test server from Kotlin

* impl From<MediaCreateParams> for HashMap<String, String>

* Implement MediaUploadRequestExecutionError

* Removed addressed TODO from Kotlin WpRequestExecutor

* Add file_path to MediaFileNotFound error

* Patch up failing Swift stuff

---------

Co-authored-by: Jeremy Massel <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants