1
1
import Foundation
2
- import Alamofire
3
2
4
3
private struct PluginDirectoryRemoteConstants {
5
4
static let dateFormatter : DateFormatter = {
@@ -47,7 +46,7 @@ public enum PluginDirectoryFeedType: Hashable {
47
46
}
48
47
}
49
48
50
- public struct PluginDirectoryGetInformationEndpoint : Endpoint {
49
+ public struct PluginDirectoryGetInformationEndpoint {
51
50
public enum Error : Swift . Error {
52
51
case pluginNotFound
53
52
}
@@ -57,20 +56,18 @@ public struct PluginDirectoryGetInformationEndpoint: Endpoint {
57
56
self . slug = slug
58
57
}
59
58
60
- public func buildRequest( ) throws -> URLRequest {
61
- let url = PluginDirectoryRemoteConstants . getInformationEndpoint
62
- . appendingPathComponent ( slug)
63
- . appendingPathExtension ( " json " )
64
- let request = URLRequest ( url: url)
65
- let encodedRequest = try URLEncoding . default. encode ( request, with: [ " fields " : " icons,banners " ] )
66
- return encodedRequest
59
+ func buildRequest( ) throws -> URLRequest {
60
+ try HTTPRequestBuilder ( url: PluginDirectoryRemoteConstants . getInformationEndpoint)
61
+ . append ( percentEncodedPath: " \( slug) .json " )
62
+ . query ( name: " fields " , value: " icons,banners " )
63
+ . build ( )
67
64
}
68
65
69
- public func parseResponse( data: Data ) throws -> PluginDirectoryEntry {
66
+ func parseResponse( data: Data ) throws -> PluginDirectoryEntry {
70
67
return try PluginDirectoryRemoteConstants . jsonDecoder. decode ( PluginDirectoryEntry . self, from: data)
71
68
}
72
69
73
- public func validate( request : URLRequest ? , response: HTTPURLResponse , data: Data ? ) throws {
70
+ func validate( response: HTTPURLResponse , data: Data ? ) throws {
74
71
// api.wordpress.org has an odd way of responding to plugin info requests for
75
72
// plugins not in the directory: it will return `null` with an HTTP 200 OK.
76
73
// This turns that case into a `.pluginNotFound` error.
@@ -83,20 +80,20 @@ public struct PluginDirectoryGetInformationEndpoint: Endpoint {
83
80
}
84
81
}
85
82
86
- public struct PluginDirectoryFeedEndpoint : Endpoint {
83
+ public struct PluginDirectoryFeedEndpoint {
87
84
public enum Error : Swift . Error {
88
85
case genericError
89
86
}
90
87
91
88
let feedType : PluginDirectoryFeedType
92
89
let pageNumber : Int
93
90
94
- public init ( feedType: PluginDirectoryFeedType ) {
91
+ init ( feedType: PluginDirectoryFeedType ) {
95
92
self . feedType = feedType
96
93
self . pageNumber = 1
97
94
}
98
95
99
- public func buildRequest( ) throws -> URLRequest {
96
+ func buildRequest( ) throws -> URLRequest {
100
97
var parameters : [ String : Any ] = [ " action " : " query_plugins " ,
101
98
" request[per_page] " : PluginDirectoryRemoteConstants . pluginsPerPage,
102
99
" request[fields][icons] " : 1 ,
@@ -113,17 +110,16 @@ public struct PluginDirectoryFeedEndpoint: Endpoint {
113
110
114
111
}
115
112
116
- let request = URLRequest ( url: PluginDirectoryRemoteConstants . feedEndpoint)
117
- let encodedRequest = try URLEncoding . default. encode ( request, with: parameters)
118
-
119
- return encodedRequest
113
+ return try HTTPRequestBuilder ( url: PluginDirectoryRemoteConstants . feedEndpoint)
114
+ . query ( parameters)
115
+ . build ( )
120
116
}
121
117
122
- public func parseResponse( data: Data ) throws -> PluginDirectoryFeedPage {
118
+ func parseResponse( data: Data ) throws -> PluginDirectoryFeedPage {
123
119
return try PluginDirectoryRemoteConstants . jsonDecoder. decode ( PluginDirectoryFeedPage . self, from: data)
124
120
}
125
121
126
- public func validate( request : URLRequest ? , response: HTTPURLResponse , data: Data ? ) throws {
122
+ func validate( response: HTTPURLResponse , data: Data ? ) throws {
127
123
if response. statusCode != 200 { throw Error . genericError}
128
124
}
129
125
}
@@ -132,13 +128,19 @@ public struct PluginDirectoryServiceRemote {
132
128
133
129
public init ( ) { }
134
130
135
- public func getPluginFeed( _ feedType: PluginDirectoryFeedType ,
136
- pageNumber: Int = 1 ,
137
- completion: @escaping ( Result < PluginDirectoryFeedPage > ) -> Void ) {
138
- PluginDirectoryFeedEndpoint ( feedType: feedType) . request ( completion: completion)
131
+ public func getPluginFeed( _ feedType: PluginDirectoryFeedType , pageNumber: Int = 1 ) async throws -> PluginDirectoryFeedPage {
132
+ let endpoint = PluginDirectoryFeedEndpoint ( feedType: feedType)
133
+ let ( data, response) = try await URLSession . shared. data ( for: endpoint. buildRequest ( ) )
134
+ let httpResponse = response as! HTTPURLResponse
135
+ try endpoint. validate ( response: httpResponse, data: data)
136
+ return try endpoint. parseResponse ( data: data)
139
137
}
140
138
141
- public func getPluginInformation( slug: String , completion: @escaping ( Result < PluginDirectoryEntry > ) -> Void ) {
142
- PluginDirectoryGetInformationEndpoint ( slug: slug) . request ( completion: completion)
139
+ public func getPluginInformation( slug: String ) async throws -> PluginDirectoryEntry {
140
+ let endpoint = PluginDirectoryGetInformationEndpoint ( slug: slug)
141
+ let ( data, response) = try await URLSession . shared. data ( for: endpoint. buildRequest ( ) )
142
+ let httpResponse = response as! HTTPURLResponse
143
+ try endpoint. validate ( response: httpResponse, data: data)
144
+ return try endpoint. parseResponse ( data: data)
143
145
}
144
146
}
0 commit comments