11import Foundation
2- import Alamofire
32
43private struct PluginDirectoryRemoteConstants {
54 static let dateFormatter : DateFormatter = {
@@ -47,7 +46,7 @@ public enum PluginDirectoryFeedType: Hashable {
4746 }
4847}
4948
50- public struct PluginDirectoryGetInformationEndpoint : Endpoint {
49+ public struct PluginDirectoryGetInformationEndpoint {
5150 public enum Error : Swift . Error {
5251 case pluginNotFound
5352 }
@@ -57,20 +56,18 @@ public struct PluginDirectoryGetInformationEndpoint: Endpoint {
5756 self . slug = slug
5857 }
5958
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 ( )
6764 }
6865
69- public func parseResponse( data: Data ) throws -> PluginDirectoryEntry {
66+ func parseResponse( data: Data ) throws -> PluginDirectoryEntry {
7067 return try PluginDirectoryRemoteConstants . jsonDecoder. decode ( PluginDirectoryEntry . self, from: data)
7168 }
7269
73- public func validate( request : URLRequest ? , response: HTTPURLResponse , data: Data ? ) throws {
70+ func validate( response: HTTPURLResponse , data: Data ? ) throws {
7471 // api.wordpress.org has an odd way of responding to plugin info requests for
7572 // plugins not in the directory: it will return `null` with an HTTP 200 OK.
7673 // This turns that case into a `.pluginNotFound` error.
@@ -83,20 +80,20 @@ public struct PluginDirectoryGetInformationEndpoint: Endpoint {
8380 }
8481}
8582
86- public struct PluginDirectoryFeedEndpoint : Endpoint {
83+ public struct PluginDirectoryFeedEndpoint {
8784 public enum Error : Swift . Error {
8885 case genericError
8986 }
9087
9188 let feedType : PluginDirectoryFeedType
9289 let pageNumber : Int
9390
94- public init ( feedType: PluginDirectoryFeedType ) {
91+ init ( feedType: PluginDirectoryFeedType ) {
9592 self . feedType = feedType
9693 self . pageNumber = 1
9794 }
9895
99- public func buildRequest( ) throws -> URLRequest {
96+ func buildRequest( ) throws -> URLRequest {
10097 var parameters : [ String : Any ] = [ " action " : " query_plugins " ,
10198 " request[per_page] " : PluginDirectoryRemoteConstants . pluginsPerPage,
10299 " request[fields][icons] " : 1 ,
@@ -113,17 +110,16 @@ public struct PluginDirectoryFeedEndpoint: Endpoint {
113110
114111 }
115112
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 ( )
120116 }
121117
122- public func parseResponse( data: Data ) throws -> PluginDirectoryFeedPage {
118+ func parseResponse( data: Data ) throws -> PluginDirectoryFeedPage {
123119 return try PluginDirectoryRemoteConstants . jsonDecoder. decode ( PluginDirectoryFeedPage . self, from: data)
124120 }
125121
126- public func validate( request : URLRequest ? , response: HTTPURLResponse , data: Data ? ) throws {
122+ func validate( response: HTTPURLResponse , data: Data ? ) throws {
127123 if response. statusCode != 200 { throw Error . genericError}
128124 }
129125}
@@ -132,13 +128,19 @@ public struct PluginDirectoryServiceRemote {
132128
133129 public init ( ) { }
134130
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)
139137 }
140138
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)
143145 }
144146}
0 commit comments