Skip to content

Commit eebe7a6

Browse files
authored
Post notifications when requested with invalid authentication (#703)
* Post notifications when requested with invalid authentication * Fix Swift unit tests compiling issues * Add `WpAppNotifier` argument * Fix the Swift example project * Remove a unused notification constant
1 parent 0c33684 commit eebe7a6

File tree

7 files changed

+57
-30
lines changed

7 files changed

+57
-30
lines changed

native/swift/Example/Example/WordPressAPI+Extensions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ extension WordPressAPI {
2020
return WordPressAPI(
2121
urlSession: .shared,
2222
apiRootUrl: apiRootUrl,
23-
authenticationStategy: loginCredentials
23+
authentication: loginCredentials
2424
)
2525
}
2626
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import Foundation
2+
import WordPressAPIInternal
3+
4+
class EmptyAppNotifier: @unchecked Sendable, WpAppNotifier {
5+
func requestedWithInvalidAuthentication() async {
6+
// no-op
7+
}
8+
}

native/swift/Sources/wordpress-api/JetpackConnectionClient+Extensions.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@ extension JetpackConnectionClient {
1010
apiRootUrl: ParsedUrl,
1111
urlSession: URLSession,
1212
authentication: WpAuthentication,
13-
middlewarePipeline: MiddlewarePipeline = .default
13+
middlewarePipeline: MiddlewarePipeline = .default,
14+
appNotifier: WpAppNotifier? = nil
1415
) {
1516
self.init(
1617
apiRootUrl: apiRootUrl,
1718
delegate: .init(
1819
authProvider: .staticWithAuth(auth: authentication),
1920
requestExecutor: WpRequestExecutor(urlSession: urlSession),
2021
middlewarePipeline: middlewarePipeline,
21-
appNotifier: NoopAppNotifier()
22+
appNotifier: appNotifier ?? EmptyAppNotifier()
2223
)
2324
)
2425
}

native/swift/Sources/wordpress-api/WordPressAPI.swift

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,54 @@ public actor WordPressAPI {
1111
case unableToParseResponse
1212
}
1313

14-
private let internalExecutor: SafeRequestExecutor
14+
private let apiClientDelegate: WpApiClientDelegate
1515
package let requestBuilder: UniffiWpApiClient
1616

17-
public init(urlSession: URLSession, apiRootUrl: ParsedUrl, authenticationStategy: WpAuthentication) {
17+
public init(
18+
urlSession: URLSession,
19+
apiRootUrl: ParsedUrl,
20+
authentication: WpAuthentication,
21+
middlewarePipeline: MiddlewarePipeline = .default
22+
) {
23+
self.init(
24+
apiRootUrl: apiRootUrl,
25+
authenticationProvider: .staticWithAuth(auth: authentication),
26+
executor: WpRequestExecutor(urlSession: urlSession),
27+
middlewarePipeline: middlewarePipeline
28+
)
29+
}
30+
31+
public init(
32+
urlSession: URLSession,
33+
apiRootUrl: ParsedUrl,
34+
authenticationProvider: WpAuthenticationProvider,
35+
middlewarePipeline: MiddlewarePipeline = .default,
36+
appNotifier: WpAppNotifier? = nil
37+
) {
1838
self.init(
1939
apiRootUrl: apiRootUrl,
20-
authenticationStategy: authenticationStategy,
21-
executor: WpRequestExecutor(urlSession: urlSession)
40+
authenticationProvider: authenticationProvider,
41+
executor: WpRequestExecutor(urlSession: urlSession),
42+
middlewarePipeline: middlewarePipeline
2243
)
2344
}
2445

2546
init(
2647
apiRootUrl: ParsedUrl,
27-
authenticationStategy: WpAuthentication,
48+
authenticationProvider: WpAuthenticationProvider,
2849
executor: SafeRequestExecutor,
29-
middlewarePipeline: MiddlewarePipeline = .default
50+
middlewarePipeline: MiddlewarePipeline,
51+
appNotifier: WpAppNotifier? = nil
3052
) {
31-
self.internalExecutor = executor
32-
53+
self.apiClientDelegate = WpApiClientDelegate(
54+
authProvider: authenticationProvider,
55+
requestExecutor: executor,
56+
middlewarePipeline: middlewarePipeline,
57+
appNotifier: appNotifier ?? EmptyAppNotifier()
58+
)
3359
self.requestBuilder = UniffiWpApiClient(
3460
apiRootUrl: apiRootUrl,
35-
delegate: .init(
36-
authProvider: .staticWithAuth(auth: authenticationStategy),
37-
requestExecutor: executor,
38-
middlewarePipeline: middlewarePipeline,
39-
appNotifier: NoopAppNotifier()
40-
)
61+
delegate: self.apiClientDelegate
4162
)
4263
}
4364

@@ -170,9 +191,3 @@ public extension ParsedUrl {
170191
try parse(input: url.absoluteString)
171192
}
172193
}
173-
174-
class NoopAppNotifier: @unchecked Sendable, WpAppNotifier {
175-
func requestedWithInvalidAuthentication() async {
176-
// Do nothing.
177-
}
178-
}

native/swift/Tests/wordpress-api/Endpoints/Users.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ struct UsersTests {
4040

4141
let api = try WordPressAPI(
4242
apiRootUrl: ParsedUrl.parse(input: "https://wordpress.org/wp-json"),
43-
authenticationStategy: .none,
44-
executor: stubs
43+
authenticationProvider: .none(),
44+
executor: stubs,
45+
middlewarePipeline: .default
4546
)
4647
let user = try await api.users.retrieveWithViewContext(userId: 1)
4748
#expect(user.data.name == "User Name")

native/swift/Tests/wordpress-api/HttpTests.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ class HTTPErrorTests {
1010

1111
let api = try WordPressAPI(
1212
apiRootUrl: ParsedUrl.parse(input: "https://wordpress.org/wp-json"),
13-
authenticationStategy: .none,
14-
executor: stubs
13+
authenticationProvider: .none(),
14+
executor: stubs,
15+
middlewarePipeline: .default
1516
)
1617

1718
await #expect(throws: WpApiError.self, performing: {

native/swift/Tests/wordpress-api/WordPressAPITests.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ struct WordPressAPITests {
4444
let stubs = try createStubs()
4545
let api = try WordPressAPI(
4646
apiRootUrl: ParsedUrl.parse(input: "https://wordpress.org/wp-json"),
47-
authenticationStategy: .none,
48-
executor: stubs
47+
authenticationProvider: .none(),
48+
executor: stubs,
49+
middlewarePipeline: .default
4950
)
5051
let user = try await api.users.retrieveWithViewContext(userId: 1)
5152
#expect(user.data.name == "User Name")
@@ -57,7 +58,7 @@ struct WordPressAPITests {
5758
let counter = CounterMiddleware()
5859
let api = try WordPressAPI(
5960
apiRootUrl: ParsedUrl.parse(input: "https://wordpress.org/wp-json"),
60-
authenticationStategy: .none,
61+
authenticationProvider: .none(),
6162
executor: stubs,
6263
middlewarePipeline: .init(middlewares: [counter])
6364
)

0 commit comments

Comments
 (0)