-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15226 from wordpress-mobile/issue/ab_testing_ttl
AB testing: schedule a refresh based on TTL
- Loading branch information
Showing
4 changed files
with
192 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,79 @@ | ||
import Foundation | ||
|
||
protocol ExPlatConfiguration { | ||
var platform: String { get } | ||
var oAuthToken: String? { get } | ||
var userAgent: String? { get } | ||
var anonId: String? { get } | ||
} | ||
|
||
class ExPlatService { | ||
let wordPressComRestApi: WordPressComRestApi | ||
let platform: String | ||
let oAuthToken: String? | ||
let userAgent: String? | ||
let anonId: String? | ||
|
||
let assignmentsPath = "wpcom/v2/experiments/0.1.0/assignments/calypso" | ||
var assignmentsEndpoint: String { | ||
return "https://public-api.wordpress.com/wpcom/v2/experiments/0.1.0/assignments/\(platform)" | ||
} | ||
|
||
init(wordPressComRestApi: WordPressComRestApi) { | ||
self.wordPressComRestApi = wordPressComRestApi | ||
init(configuration: ExPlatConfiguration) { | ||
self.platform = configuration.platform | ||
self.oAuthToken = configuration.oAuthToken | ||
self.userAgent = configuration.userAgent | ||
self.anonId = configuration.anonId | ||
} | ||
|
||
func getAssignments(completion: @escaping (Assignments?) -> Void) { | ||
wordPressComRestApi.GET(assignmentsPath, | ||
parameters: nil, | ||
success: { responseObject, _ in | ||
do { | ||
let decoder = JSONDecoder() | ||
let data = try JSONSerialization.data(withJSONObject: responseObject, options: []) | ||
let assignments = try decoder.decode(Assignments.self, from: data) | ||
completion(assignments) | ||
} catch { | ||
DDLogError("Error parsing the experiment response: \(error)") | ||
completion(nil) | ||
} | ||
}, failure: { error, _ in | ||
guard var urlComponents = URLComponents(string: assignmentsEndpoint) else { | ||
completion(nil) | ||
}) | ||
} | ||
} | ||
return | ||
} | ||
|
||
// Query items | ||
urlComponents.queryItems = [URLQueryItem(name: "_locale", value: Locale.current.languageCode)] | ||
|
||
if let anonId = anonId { | ||
urlComponents.queryItems?.append(URLQueryItem.init(name: "anon_id", value: anonId)) | ||
} | ||
|
||
guard let url = urlComponents.url else { | ||
completion(nil) | ||
return | ||
} | ||
|
||
var request = URLRequest(url: url) | ||
request.httpMethod = "GET" | ||
|
||
// HTTP fields (including oAuthToken if provided) | ||
request.addValue("application/json", forHTTPHeaderField: "Content-Type") | ||
request.addValue("application/json", forHTTPHeaderField: "Accept") | ||
|
||
if let oAuthToken = oAuthToken { | ||
request.setValue( "Bearer \(oAuthToken)", forHTTPHeaderField: "Authorization") | ||
} | ||
|
||
// User-Agent | ||
if let userAgent = userAgent { | ||
request.setValue(userAgent, forHTTPHeaderField: "User-Agent") | ||
} | ||
|
||
let task = URLSession.shared.dataTask(with: request) { data, response, error in | ||
guard let data = data, error == nil else { | ||
completion(nil) | ||
return | ||
} | ||
|
||
extension ExPlatService { | ||
class func withDefaultApi() -> ExPlatService { | ||
let accountService = AccountService(managedObjectContext: ContextManager.shared.mainContext) | ||
let defaultAccount = accountService.defaultWordPressComAccount() | ||
let token: String? = defaultAccount?.authToken | ||
do { | ||
let decoder = JSONDecoder() | ||
let assignments = try decoder.decode(Assignments.self, from: data) | ||
completion(assignments) | ||
} catch { | ||
DDLogError("Error parsing the experiment response: \(error)") | ||
completion(nil) | ||
} | ||
} | ||
|
||
let api = WordPressComRestApi.defaultApi(oAuthToken: token, | ||
userAgent: WPUserAgent.wordPress(), | ||
localeKey: WordPressComRestApi.LocaleKeyV2) | ||
return ExPlatService(wordPressComRestApi: api) | ||
task.resume() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters