Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.

Commit 7b1058c

Browse files
Merge pull request #547 from wordpress-mobile/task/19445-remote-config
Add support for fetching remote config values
2 parents 43a0103 + aa7d039 commit 7b1058c

File tree

4 files changed

+140
-1
lines changed

4 files changed

+140
-1
lines changed

WordPressKit.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Pod::Spec.new do |s|
44
s.name = 'WordPressKit'
5-
s.version = '4.58.1'
5+
s.version = '4.58.2-beta.1'
66

77
s.summary = 'WordPressKit offers a clean and simple WordPress.com and WordPress.org API.'
88
s.description = <<-DESC

WordPressKit.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,8 @@
329329
7E3E7A4C20E443AA0075D159 /* NSMutableParagraphStyle+extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7E3E7A4B20E443AA0075D159 /* NSMutableParagraphStyle+extensions.swift */; };
330330
7EC60EBE22DC4F9000FB0336 /* EditorServiceRemoteTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7EC60EBD22DC4F9000FB0336 /* EditorServiceRemoteTests.swift */; };
331331
7EC60EC022DC5D7C00FB0336 /* EditorSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7EC60EBF22DC5D7C00FB0336 /* EditorSettings.swift */; };
332+
803DE80F28FFA787007D4E9C /* RemoteConfigRemote.swift in Sources */ = {isa = PBXBuildFile; fileRef = 803DE80E28FFA787007D4E9C /* RemoteConfigRemote.swift */; };
333+
803DE81128FFA9C4007D4E9C /* RemoteConfigRemoteTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 803DE81028FFA9C4007D4E9C /* RemoteConfigRemoteTests.swift */; };
332334
8236EB4D2024B9F8007C7CF9 /* RemoteBlogJetpackModulesSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8236EB4C2024B9F8007C7CF9 /* RemoteBlogJetpackModulesSettings.swift */; };
333335
826016F11F9FA13A00533B6C /* ActivityServiceRemote.swift in Sources */ = {isa = PBXBuildFile; fileRef = 826016F01F9FA13A00533B6C /* ActivityServiceRemote.swift */; };
334336
826016F31F9FA17B00533B6C /* Activity.swift in Sources */ = {isa = PBXBuildFile; fileRef = 826016F21F9FA17B00533B6C /* Activity.swift */; };
@@ -991,6 +993,8 @@
991993
7E3E7A4B20E443AA0075D159 /* NSMutableParagraphStyle+extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSMutableParagraphStyle+extensions.swift"; sourceTree = "<group>"; };
992994
7EC60EBD22DC4F9000FB0336 /* EditorServiceRemoteTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditorServiceRemoteTests.swift; sourceTree = "<group>"; };
993995
7EC60EBF22DC5D7C00FB0336 /* EditorSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditorSettings.swift; sourceTree = "<group>"; };
996+
803DE80E28FFA787007D4E9C /* RemoteConfigRemote.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RemoteConfigRemote.swift; sourceTree = "<group>"; };
997+
803DE81028FFA9C4007D4E9C /* RemoteConfigRemoteTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RemoteConfigRemoteTests.swift; sourceTree = "<group>"; };
994998
8236EB4C2024B9F8007C7CF9 /* RemoteBlogJetpackModulesSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RemoteBlogJetpackModulesSettings.swift; sourceTree = "<group>"; };
995999
826016F01F9FA13A00533B6C /* ActivityServiceRemote.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActivityServiceRemote.swift; sourceTree = "<group>"; };
9961000
826016F21F9FA17B00533B6C /* Activity.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Activity.swift; sourceTree = "<group>"; };
@@ -1903,6 +1907,7 @@
19031907
D816857021EDACD10049883E /* WordPressComServiceRemote+SiteSegments.swift */,
19041908
730E869E21E44EFD00753E1A /* WordPressComServiceRemote+SiteVerticals.swift */,
19051909
73A2F38921E7F81E00388609 /* WordPressComServiceRemote+SiteVerticalsPrompt.swift */,
1910+
803DE80E28FFA787007D4E9C /* RemoteConfigRemote.swift */,
19061911
);
19071912
name = Services;
19081913
sourceTree = "<group>";
@@ -2499,6 +2504,7 @@
24992504
F9E56DFA24EB18C300916770 /* FeatureFlagRemoteTests.swift */,
25002505
24ADA24D24F9B32D001B5DAE /* FeatureFlagSerializationTest.swift */,
25012506
465F88BE263B54EE00F4C950 /* ChecksumUtilTests.swift */,
2507+
803DE81028FFA9C4007D4E9C /* RemoteConfigRemoteTests.swift */,
25022508
);
25032509
path = Utilities;
25042510
sourceTree = "<group>";
@@ -3064,6 +3070,7 @@
30643070
7430C9A41F1927180051B8E6 /* ReaderPostServiceRemote.m in Sources */,
30653071
C797196D2679007B0072F984 /* JetpackPluginManagementClient.swift in Sources */,
30663072
408197882220A35000A298E4 /* StatsLastPostInsight.swift in Sources */,
3073+
803DE80F28FFA787007D4E9C /* RemoteConfigRemote.swift in Sources */,
30673074
F9E56DF624EB11EF00916770 /* FeatureFlag.swift in Sources */,
30683075
8B2F4BE724ABC8A90056C08A /* ReaderPostServiceRemote+Cards.swift in Sources */,
30693076
8B16CE8E25250039007BE5A9 /* RemoteReaderPost.swift in Sources */,
@@ -3295,6 +3302,7 @@
32953302
32AF21E3236DEB3C001C6502 /* PostServiceRemoteRESTAutosaveTests.swift in Sources */,
32963303
3236F79A24AE406D0088E8F3 /* ReaderTopicServiceRemote+InterestsTests.swift in Sources */,
32973304
FEE4EF5B27302317003CDA3C /* CommentServiceRemoteREST+APIv2Tests.swift in Sources */,
3305+
803DE81128FFA9C4007D4E9C /* RemoteConfigRemoteTests.swift in Sources */,
32983306
74B5F0DE1EF82A9600B411E7 /* BlogServiceRemoteRESTTests.m in Sources */,
32993307
ABD95B7F25DD6C4B00735BEE /* CommentServiceRemoteRESTLikesTests.swift in Sources */,
33003308
8B749E8225AF7DDA00023F03 /* JetpackCapabilitiesServiceRemoteTests.swift in Sources */,
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import Foundation
2+
3+
open class RemoteConfigRemote: ServiceRemoteWordPressComREST {
4+
5+
public typealias RemoteConfigDictionary = [String: Any]
6+
public typealias RemoteConfigResponseCallback = (Result<RemoteConfigDictionary, Error>) -> Void
7+
8+
public enum RemoteConfigRemoteError: Error {
9+
case InvalidDataError
10+
}
11+
12+
open func getRemoteConfig(callback: @escaping RemoteConfigResponseCallback) {
13+
14+
let endpoint = "mobile/remote-config"
15+
let path = self.path(forEndpoint: endpoint, withVersion: ._2_0)
16+
17+
wordPressComRestApi.GET(path,
18+
parameters: nil,
19+
success: { response, _ in
20+
if let remoteConfigDictionary = response as? [String: Any] {
21+
callback(.success(remoteConfigDictionary))
22+
} else {
23+
callback(.failure(RemoteConfigRemoteError.InvalidDataError))
24+
}
25+
26+
}, failure: { error, response in
27+
DDLogError("Error retrieving remote config values")
28+
DDLogError("\(error)")
29+
30+
if let response = response {
31+
DDLogDebug("Response Code: \(response.statusCode)")
32+
}
33+
34+
callback(.failure(error))
35+
})
36+
}
37+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import XCTest
2+
@testable import WordPressKit
3+
4+
final class RemoteConfigRemoteTests: RemoteTestCase, RESTTestable {
5+
6+
// MARK: Variables
7+
8+
private let endpoint = "/wpcom/v2/mobile/remote-config"
9+
10+
// MARK: Tests
11+
12+
func testThatResponsesAreHandledCorrectly() throws {
13+
// Given
14+
let dictionary = ["key1": "value", "key2": "value2"]
15+
let data = try JSONEncoder().encode(dictionary)
16+
stubRemoteResponse(endpoint, data: data, contentType: .ApplicationJSON)
17+
18+
// When
19+
let expectation = XCTestExpectation()
20+
RemoteConfigRemote(wordPressComRestApi: getRestApi()).getRemoteConfig { result in
21+
22+
// Then
23+
let response = try! result.get() as? [String: String]
24+
XCTAssertEqual(response, dictionary)
25+
expectation.fulfill()
26+
}
27+
28+
wait(for: [expectation], timeout: 1)
29+
}
30+
31+
func testThatEmptyResponsesAreHandledCorrectly() throws {
32+
// Given
33+
let emptyDictionary: [String: String] = [:]
34+
let data = try JSONEncoder().encode(emptyDictionary)
35+
stubRemoteResponse(endpoint, data: data, contentType: .ApplicationJSON)
36+
37+
// When
38+
let expectation = XCTestExpectation()
39+
RemoteConfigRemote(wordPressComRestApi: getRestApi()).getRemoteConfig { result in
40+
41+
// Then
42+
XCTAssertEqual(0, try! result.get().count)
43+
expectation.fulfill()
44+
}
45+
46+
wait(for: [expectation], timeout: 1)
47+
}
48+
49+
func testThatMalformedResponsesReturnEmptyArray() throws {
50+
// Given
51+
let data = try toJSON(object: ["Invalid"])
52+
stubRemoteResponse(endpoint, data: data, contentType: .ApplicationJSON)
53+
54+
// When
55+
let expectation = XCTestExpectation()
56+
RemoteConfigRemote(wordPressComRestApi: getRestApi()).getRemoteConfig { result in
57+
58+
// Then
59+
switch result {
60+
case .success: XCTFail()
61+
case .failure: expectation.fulfill()
62+
}
63+
}
64+
65+
wait(for: [expectation], timeout: 1)
66+
}
67+
68+
func testThatRequestErrorReturnsFailureResponse() {
69+
// Given
70+
stubRemoteResponse(endpoint, data: Data(), contentType: .NoContentType, status: 400)
71+
72+
// When
73+
let expectation = XCTestExpectation()
74+
RemoteConfigRemote(wordPressComRestApi: getRestApi()).getRemoteConfig { result in
75+
76+
// Then
77+
if case .success = result {
78+
XCTFail()
79+
}
80+
expectation.fulfill()
81+
}
82+
83+
wait(for: [expectation], timeout: 1)
84+
}
85+
86+
// MARK: Helpers
87+
88+
private func toJSON<T: Codable>(object: T) throws -> Data {
89+
let encoder = JSONEncoder()
90+
encoder.outputFormatting = [.sortedKeys, .prettyPrinted]
91+
return try encoder.encode(object)
92+
}
93+
94+
}

0 commit comments

Comments
 (0)