@@ -268,16 +268,18 @@ open class WordPressComRestApi: NSObject {
268268 - parameter success: callback to be called on successful request
269269 - parameter failure: callback to be called on failed request
270270
271- - returns: a NSProgress object that can be used to track the progress of the upload and to cancel the upload. If the method
271+ - returns: a `Progerss` object that can be used to track the progress of the upload and to cancel the upload. If the method
272272 returns nil it's because something happened on the request serialization and the network request was not started, but the failure callback
273273 will be invoked with the error specificing the serialization issues.
274274 */
275- @objc @discardableResult open func multipartPOST( _ URLString: String ,
276- parameters: [ String : AnyObject ] ? ,
277- fileParts: [ FilePart ] ,
278- requestEnqueued: RequestEnqueuedBlock ? = nil ,
279- success: @escaping SuccessResponseBlock ,
280- failure: @escaping FailureReponseBlock ) -> Progress ? {
275+ @nonobjc @discardableResult open func multipartPOST(
276+ _ URLString: String ,
277+ parameters: [ String : AnyObject ] ? ,
278+ fileParts: [ FilePart ] ,
279+ requestEnqueued: RequestEnqueuedBlock ? = nil ,
280+ success: @escaping SuccessResponseBlock ,
281+ failure: @escaping FailureReponseBlock
282+ ) -> Progress ? {
281283 let progress = Progress . discreteProgress ( totalUnitCount: 100 )
282284
283285 Task { @MainActor in
@@ -452,7 +454,7 @@ open class WordPressComRestApi: NSObject {
452454 let builder : HTTPRequestBuilder
453455 do {
454456 let form = try fileParts. map {
455- try MultipartFormField ( fileAtPath: $0. url. path, name: $0. parameterName, filename: $0. filename , mimeType: $0. mimeType)
457+ try MultipartFormField ( fileAtPath: $0. url. path, name: $0. parameterName, filename: $0. fileName , mimeType: $0. mimeType)
456458 }
457459 builder = try requestBuilder ( URLString: URLString)
458460 . method ( . post)
@@ -476,23 +478,6 @@ open class WordPressComRestApi: NSObject {
476478
477479}
478480
479- // MARK: - FilePart
480-
481- /// FilePart represents the infomartion needed to encode a file on a multipart form request
482- public final class FilePart : NSObject {
483- @objc let parameterName : String
484- @objc let url : URL
485- @objc let filename : String
486- @objc let mimeType : String
487-
488- @objc public init ( parameterName: String , url: URL , filename: String , mimeType: String ) {
489- self . parameterName = parameterName
490- self . url = url
491- self . filename = filename
492- self . mimeType = mimeType
493- }
494- }
495-
496481// MARK: - Error processing
497482
498483extension WordPressComRestApi {
@@ -622,7 +607,6 @@ extension WordPressAPIError<WordPressComRestApiEndpointError> {
622607}
623608
624609extension WordPressComRestApi : WordPressComRESTAPIInterfacing {
625-
626610 // A note on the naming: Even if defined as `GET` in Objective-C, then method gets converted to Swift as `get`.
627611 //
628612 // Also, there is no Objective-C direct equivalent of `AnyObject`, which here is used in `parameters: [String: AnyObject]?`.
@@ -646,4 +630,28 @@ extension WordPressComRestApi: WordPressComRESTAPIInterfacing {
646630 ) -> Progress ? {
647631 POST ( URLString, parameters: parameters, success: success, failure: failure)
648632 }
633+
634+ public func multipartPOST(
635+ _ URLString: String ,
636+ parameters: [ String : NSObject ] ? ,
637+ fileParts: [ FilePart ] ,
638+ // Notice this does not require @escaping because it is Optional.
639+ //
640+ // Annotate with @escaping, and the compiler will fail with:
641+ // > Closure is already escaping in optional type argument
642+ //
643+ // It is necessary to explicitly set this as Optional because of the _Nullable parameter requirement in the Objective-C protocol.
644+ requestEnqueued: ( ( NSNumber ) -> Void ) ? ,
645+ success: @escaping ( Any , HTTPURLResponse ? ) -> Void ,
646+ failure: @escaping ( any Error , HTTPURLResponse ? ) -> Void
647+ ) -> Progress ? {
648+ multipartPOST (
649+ URLString,
650+ parameters: parameters,
651+ fileParts: fileParts,
652+ requestEnqueued: requestEnqueued,
653+ success: success as SuccessResponseBlock ,
654+ failure: failure as FailureReponseBlock
655+ )
656+ }
649657}
0 commit comments