11import Foundation
22
33public class BlockEditorSettingsServiceRemote {
4- let remoteAPI : WordPressRestApi
5- public init ( remoteAPI: WordPressRestApi ) {
4+ let remoteAPI : WordPressOrgRestApi
5+ public init ( remoteAPI: WordPressOrgRestApi ) {
66 self . remoteAPI = remoteAPI
77 }
88}
@@ -11,30 +11,15 @@ public class BlockEditorSettingsServiceRemote {
1111public extension BlockEditorSettingsServiceRemote {
1212 typealias EditorThemeCompletionHandler = ( Swift . Result < RemoteEditorTheme ? , Error > ) -> Void
1313
14- func fetchTheme( forSiteID siteID : Int ? , _ completion: @escaping EditorThemeCompletionHandler ) {
14+ func fetchTheme( completion: @escaping EditorThemeCompletionHandler ) {
1515 let requestPath = " /wp/v2/themes "
16- let parameters : [ String : AnyObject ] = [ " status " : " active " as AnyObject ]
17- let modifiedPath = remoteAPI. requestPath ( fromOrgPath: requestPath, with: siteID)
18- remoteAPI. GET ( modifiedPath, parameters: parameters) { [ weak self] ( result, _) in
19- guard let `self` = self else { return }
20- switch result {
21- case . success( let response) :
22- self . processEditorThemeResponse ( response) { editorTheme in
23- completion ( . success( editorTheme) )
24- }
25- case . failure( let error) :
26- completion ( . failure( error) )
27- }
28- }
29- }
30-
31- private func processEditorThemeResponse( _ response: Any , completion: ( _ editorTheme: RemoteEditorTheme ? ) -> Void ) {
32- guard let responseData = try ? JSONSerialization . data ( withJSONObject: response, options: [ ] ) ,
33- let editorThemes = try ? JSONDecoder ( ) . decode ( [ RemoteEditorTheme ] . self, from: responseData) else {
34- completion ( nil )
35- return
16+ let parameters = [ " status " : " active " ]
17+ Task { @MainActor in
18+ let result = await self . remoteAPI. get ( path: requestPath, parameters: parameters, type: [ RemoteEditorTheme ] . self)
19+ . map { $0. first }
20+ . mapError { error -> Error in error }
21+ completion ( result)
3622 }
37- completion ( editorThemes. first)
3823 }
3924
4025}
@@ -43,30 +28,18 @@ public extension BlockEditorSettingsServiceRemote {
4328public extension BlockEditorSettingsServiceRemote {
4429 typealias BlockEditorSettingsCompletionHandler = ( Swift . Result < RemoteBlockEditorSettings ? , Error > ) -> Void
4530
46- func fetchBlockEditorSettings( forSiteID siteID: Int ? , _ completion: @escaping BlockEditorSettingsCompletionHandler ) {
47- let requestPath = " /wp-block-editor/v1/settings "
48- let parameters : [ String : AnyObject ] = [ " context " : " mobile " as AnyObject ]
49- let modifiedPath = remoteAPI. requestPath ( fromOrgPath: requestPath, with: siteID)
50-
51- remoteAPI. GET ( modifiedPath, parameters: parameters) { [ weak self] ( result, _) in
52- guard let `self` = self else { return }
53- switch result {
54- case . success( let response) :
55- self . processBlockEditorSettingsResponse ( response) { blockEditorSettings in
56- completion ( . success( blockEditorSettings) )
31+ func fetchBlockEditorSettings( completion: @escaping BlockEditorSettingsCompletionHandler ) {
32+ Task { @MainActor in
33+ let result = await self . remoteAPI. get ( path: " /wp-block-editor/v1/settings " , parameters: [ " context " : " mobile " ] , type: RemoteBlockEditorSettings . self)
34+ . map { settings -> RemoteBlockEditorSettings ? in settings }
35+ . flatMapError { original in
36+ if case let . unparsableResponse( response, _, underlyingError) = original, response? . statusCode == 200 , underlyingError is DecodingError {
37+ return . success( nil )
38+ }
39+ return . failure( original)
5740 }
58- case . failure( let error) :
59- completion ( . failure( error) )
60- }
61- }
62- }
63-
64- private func processBlockEditorSettingsResponse( _ response: Any , completion: ( _ editorTheme: RemoteBlockEditorSettings ? ) -> Void ) {
65- guard let responseData = try ? JSONSerialization . data ( withJSONObject: response, options: [ ] ) ,
66- let blockEditorSettings = try ? JSONDecoder ( ) . decode ( RemoteBlockEditorSettings . self, from: responseData) else {
67- completion ( nil )
68- return
41+ . mapError { error -> Error in error }
42+ completion ( result)
6943 }
70- completion ( blockEditorSettings)
7144 }
7245}
0 commit comments