Skip to content

Commit 940d81e

Browse files
[BWA-155] refactor: Move ConfigAPIService to BitwardenKit (#1499)
1 parent 292a3a2 commit 940d81e

File tree

9 files changed

+39
-19
lines changed

9 files changed

+39
-19
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/// A protocol for an API service used to make config requests.
2+
///
3+
public protocol ConfigAPIService {
4+
/// Performs an API request to get the configuration from the backend.
5+
///
6+
func getConfig() async throws -> ConfigResponseModel
7+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import Foundation
2+
import TestHelpers
3+
4+
public extension APITestData {
5+
/// A valid server configuration to produce a `ConfigResponseModel`.
6+
static let validServerConfig = loadFromJsonBundle(
7+
resource: "ValidServerConfig",
8+
bundle: BitwardenKitMocksBundleFinder.bundle
9+
)
10+
}

BitwardenKit/Core/Platform/Services/API/Fixtures/APITestData+Error.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,3 @@ public extension APITestData {
88
bundle: BitwardenKitMocksBundleFinder.bundle
99
)
1010
}
11-
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import BitwardenKit
2+
import Foundation
3+
import Networking
4+
import TestHelpers
5+
6+
public class MockConfigAPIService: ConfigAPIService {
7+
public var getConfigResult: Result<ConfigResponseModel, Error> =
8+
Result<HTTPResponse, Error>
9+
.httpSuccess(testData: .validServerConfig)
10+
.map { try! ConfigResponseModel(response: $0) } // swiftlint:disable:this force_try
11+
12+
public func getConfig() async throws -> ConfigResponseModel {
13+
try getConfigResult.get()
14+
}
15+
}

BitwardenShared/Core/Platform/Services/API/ConfigAPIService.swift

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
1+
// swiftlint:disable:this file_name
2+
13
import BitwardenKit
24
import Networking
35

4-
/// A protocol for an API service used to make config requests.
5-
///
6-
protocol ConfigAPIService {
7-
/// Performs an API request to get the configuration from the backend.
8-
///
9-
func getConfig() async throws -> ConfigResponseModel
10-
}
11-
126
extension APIService: ConfigAPIService {
137
func getConfig() async throws -> ConfigResponseModel {
148
let isAuthenticated = try? await stateService.isAuthenticated()

BitwardenShared/Core/Platform/Services/API/ConfigAPIServiceTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import BitwardenKit
12
import XCTest
23

34
@testable import BitwardenShared

BitwardenShared/Core/Platform/Services/API/Fixtures/APITestData+Config.swift

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import Foundation
22
import Networking
3-
import TestHelpers
43

5-
extension Result where Success == HTTPResponse, Error: Error {
4+
public extension Result where Success == HTTPResponse, Error: Error {
5+
/// Convenience method to get a successful HTTPResponse result with APITestData.
66
static func httpSuccess(testData: APITestData) -> Result<HTTPResponse, Error> {
77
let response = HTTPResponse.success(
88
body: testData.data
99
)
1010
return .success(response)
1111
}
1212

13+
/// Convenience method to get a successful result with a failed HTTPResponse.
1314
static func httpFailure(
1415
statusCode: Int = 500,
1516
headers: [String: String] = [:],
@@ -23,6 +24,7 @@ extension Result where Success == HTTPResponse, Error: Error {
2324
return .success(response)
2425
}
2526

27+
/// Convenience method to get a failed HTTPResponse result.
2628
static func httpFailure(_ error: Error) -> Result<HTTPResponse, Error> {
2729
.failure(error)
2830
}

0 commit comments

Comments
 (0)