-
Notifications
You must be signed in to change notification settings - Fork 121
[Privacy Choices] Use a consolidated API to fetch the user ip country code #9851
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
902fdcb
Make UnauthenticatedRequest to conform to Request
Ecarrion 5f2be25
Add test for ip location remote type
Ecarrion b1f396f
Add documentation
Ecarrion 42fb94e
Add ip location action
Ecarrion 84b6b5d
Integrate 3rd ip party code on PrivacyBannerPresentationUseCase
Ecarrion 59782fb
Prevent unnecessary IP location api calls to be made & add unit tests
Ecarrion 1cefd8f
Update feature to use WPCOM geo public API
Ecarrion e89bad0
Remove account country code
Ecarrion 24b9505
Update path
Ecarrion 37cbe5f
Use request with user agent
Ecarrion File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import Foundation | ||
|
|
||
| /// Remote type to fetch the user's IP Location using the public `geo` API. | ||
| /// | ||
| public final class IPLocationRemote: Remote { | ||
|
|
||
| /// Fetches the country code from the device ip. | ||
| /// | ||
| public func getIPCountryCode(onCompletion: @escaping (Result<String, Error>) -> Void) { | ||
| let path = "geo/" // Needs the trailing slash otherwise the request will fail. | ||
| guard let url = URL(string: Settings.wordpressApiBaseURL + path) else { | ||
| return onCompletion(.failure(IPLocationError.malformedURL)) // Should not happen. | ||
| } | ||
|
|
||
| let request = UnauthenticatedRequest(request: .init(url: url)) | ||
| let mapper = IPCountryCodeMapper() | ||
| enqueue(request, mapper: mapper, completion: onCompletion) | ||
| } | ||
| } | ||
|
|
||
| /// `IPLocationRemote` known errors | ||
| /// | ||
| public extension IPLocationRemote { | ||
| enum IPLocationError: Error { | ||
| case malformedURL | ||
| } | ||
| } | ||
|
|
||
| /// Private mapper used to extract the country code from the `IPLocationRemote` response. | ||
| /// | ||
| private struct IPCountryCodeMapper: Mapper { | ||
|
|
||
| /// Response envelope | ||
| /// | ||
| struct Response: Decodable { | ||
| enum CodingKeys: String, CodingKey { | ||
| case countryCode = "country_short" | ||
| } | ||
|
|
||
| let countryCode: String | ||
| } | ||
|
|
||
| func map(response: Data) throws -> String { | ||
| try JSONDecoder().decode(Response.self, from: response).countryCode | ||
| } | ||
| } | ||
This file contains hidden or 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
33 changes: 33 additions & 0 deletions
33
Networking/NetworkingTests/Remote/IPLocationRemoteTests.swift
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import XCTest | ||
| import TestKit | ||
| @testable import Networking | ||
|
|
||
|
|
||
| final class IPLocationRemoteTests: XCTestCase { | ||
| /// Dummy Network Wrapper | ||
| /// | ||
| private var network: MockNetwork! | ||
|
|
||
| override func setUp() { | ||
| super.setUp() | ||
| network = MockNetwork() | ||
| } | ||
|
|
||
| func test_country_code_is_correctly_parsed() { | ||
| // Given | ||
| let remote = IPLocationRemote(network: network) | ||
| network.simulateResponse(requestUrlSuffix: "geo/", filename: "ip-location") | ||
|
|
||
| // When | ||
| let countryCode = waitFor { promise in | ||
| remote.getIPCountryCode { result in | ||
| if case let .success(code) = result { | ||
| promise(code) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Then | ||
| XCTAssertEqual(countryCode, "CO") | ||
| } | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "latitude": "25.77427", | ||
| "longitude": "-80.1936", | ||
| "country_short": "CO", | ||
| "country_long": "United States of America", | ||
| "region": "Florida", | ||
| "city": "Miami" | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -104,7 +104,7 @@ final class JetpackSetupCoordinatorTests: XCTestCase { | |
| let coordinator = JetpackSetupCoordinator(site: testSite, dotcomAuthScheme: expectedScheme, rootViewController: navigationController, stores: stores) | ||
| let url = try XCTUnwrap(URL(string: "scheme://magic-login?token=test")) | ||
|
|
||
| let expectedAccount = Account(userID: 123, displayName: "Test", email: "[email protected]", username: "test", gravatarUrl: nil, ipCountryCode: "US") | ||
| let expectedAccount = Account(userID: 123, displayName: "Test", email: "[email protected]", username: "test", gravatarUrl: nil) | ||
| stores.whenReceivingAction(ofType: JetpackConnectionAction.self) { action in | ||
| switch action { | ||
| case let .loadWPComAccount(_, onCompletion): | ||
|
|
@@ -133,7 +133,7 @@ final class JetpackSetupCoordinatorTests: XCTestCase { | |
| let coordinator = JetpackSetupCoordinator(site: testSite, dotcomAuthScheme: expectedScheme, rootViewController: navigationController, stores: stores) | ||
| let url = try XCTUnwrap(URL(string: "scheme://magic-login?token=test")) | ||
|
|
||
| let expectedAccount = Account(userID: 123, displayName: "Test", email: "[email protected]", username: "test", gravatarUrl: nil, ipCountryCode: "US") | ||
| let expectedAccount = Account(userID: 123, displayName: "Test", email: "[email protected]", username: "test", gravatarUrl: nil) | ||
| stores.whenReceivingAction(ofType: JetpackConnectionAction.self) { action in | ||
| switch action { | ||
| case let .loadWPComAccount(_, onCompletion): | ||
|
|
@@ -162,7 +162,7 @@ final class JetpackSetupCoordinatorTests: XCTestCase { | |
| let coordinator = JetpackSetupCoordinator(site: testSite, dotcomAuthScheme: expectedScheme, rootViewController: navigationController, stores: stores) | ||
| let url = try XCTUnwrap(URL(string: "scheme://magic-login?token=test")) | ||
|
|
||
| let expectedAccount = Account(userID: 123, displayName: "Test", email: "[email protected]", username: "test", gravatarUrl: nil, ipCountryCode: "US") | ||
| let expectedAccount = Account(userID: 123, displayName: "Test", email: "[email protected]", username: "test", gravatarUrl: nil) | ||
| stores.whenReceivingAction(ofType: JetpackConnectionAction.self) { action in | ||
| switch action { | ||
| case let .loadWPComAccount(_, onCompletion): | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we be adding the custom user-agent header with
.asURLRequest()here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, I totally missed it!