@@ -19,8 +19,7 @@ public typealias FavoriteFriendDetail = (favoriteGroupId: String, friends: [User
19
19
@available ( macOS 12 . 0 , * )
20
20
@available ( iOS 15 . 0 , * )
21
21
public struct FavoriteService {
22
- private static let favoriteUrl = " \( baseUrl) /favorites "
23
- private static let favoriteGroupUrl = " \( baseUrl) /favorite/groups "
22
+ private static let path = " favorites "
24
23
25
24
/// Asynchronously retrieves a list of favorite groups from the server.
26
25
/// - Parameter client: The API client used to make the network request.
@@ -29,10 +28,8 @@ public struct FavoriteService {
29
28
public static func listFavoriteGroups(
30
29
_ client: APIClient
31
30
) async throws -> [ FavoriteGroup ] {
32
- let response = try await client. request (
33
- url: URL ( string: favoriteGroupUrl) !,
34
- httpMethod: . get
35
- )
31
+ let path = " favorite/groups "
32
+ let response = try await client. request ( path: path, httpMethod: . get)
36
33
return try Util . shared. decode ( response. data)
37
34
}
38
35
@@ -42,22 +39,14 @@ public struct FavoriteService {
42
39
type: FavoriteType ,
43
40
tag: String ? = nil
44
41
) async throws -> [ Favorite ] {
45
- var request = URLComponents ( string: favoriteUrl) !
46
- request. queryItems = [
42
+ var queryItems = [
47
43
URLQueryItem ( name: " n " , value: n. description) ,
48
44
URLQueryItem ( name: " type " , value: type. rawValue)
49
45
]
50
46
if let tag = tag {
51
- request. queryItems? . append ( URLQueryItem ( name: " tag " , value: tag. description) )
52
- }
53
- guard let url = request. url else {
54
- throw URLError ( . badURL, userInfo: [ NSLocalizedDescriptionKey: " Invalid URL: \( favoriteUrl) " ] )
47
+ queryItems. append ( URLQueryItem ( name: " tag " , value: tag. description) )
55
48
}
56
-
57
- let response = try await client. request (
58
- url: url,
59
- httpMethod: . get
60
- )
49
+ let response = try await client. request ( path: path, httpMethod: . get, queryItems: queryItems)
61
50
return try Util . shared. decode ( response. data)
62
51
}
63
52
@@ -121,22 +110,15 @@ public struct FavoriteService {
121
110
let requestData = try Util . shared. encode (
122
111
RequestToAddFavorite ( type: type, favoriteId: favoriteId, tags: [ tag] )
123
112
)
124
- let response = try await client. request (
125
- url: URL ( string: " \( favoriteUrl) " ) !,
126
- httpMethod: . post,
127
- httpBody: requestData
128
- )
113
+ let response = try await client. request ( path: path, httpMethod: . post, httpBody: requestData)
129
114
return try Util . shared. decode ( response. data)
130
115
}
131
116
132
117
public static func removeFavorite(
133
118
_ client: APIClient ,
134
119
favoriteId: String
135
120
) async throws -> SuccessResponse {
136
- let response = try await client. request (
137
- url: URL ( string: " \( favoriteUrl) / \( favoriteId) " ) !,
138
- httpMethod: . delete
139
- )
121
+ let response = try await client. request ( path: path, httpMethod: . delete)
140
122
return try Util . shared. decode ( response. data)
141
123
}
142
124
}
0 commit comments