1
1
import Foundation
2
2
3
3
public class BlockEditorSettingsServiceRemote {
4
- let remoteAPI : WordPressRestApi
5
- public init ( remoteAPI: WordPressRestApi ) {
4
+ let remoteAPI : WordPressOrgRestApi
5
+ public init ( remoteAPI: WordPressOrgRestApi ) {
6
6
self . remoteAPI = remoteAPI
7
7
}
8
8
}
@@ -11,30 +11,15 @@ public class BlockEditorSettingsServiceRemote {
11
11
public extension BlockEditorSettingsServiceRemote {
12
12
typealias EditorThemeCompletionHandler = ( Swift . Result < RemoteEditorTheme ? , Error > ) -> Void
13
13
14
- func fetchTheme( forSiteID siteID : Int ? , _ completion: @escaping EditorThemeCompletionHandler ) {
14
+ func fetchTheme( completion: @escaping EditorThemeCompletionHandler ) {
15
15
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)
36
22
}
37
- completion ( editorThemes. first)
38
23
}
39
24
40
25
}
@@ -43,30 +28,18 @@ public extension BlockEditorSettingsServiceRemote {
43
28
public extension BlockEditorSettingsServiceRemote {
44
29
typealias BlockEditorSettingsCompletionHandler = ( Swift . Result < RemoteBlockEditorSettings ? , Error > ) -> Void
45
30
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)
57
40
}
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)
69
43
}
70
- completion ( blockEditorSettings)
71
44
}
72
45
}
0 commit comments