-
Notifications
You must be signed in to change notification settings - Fork 191
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 #1309 from WalletConnect/push-unregister
unregister push on logout
- Loading branch information
Showing
5 changed files
with
73 additions
and
6 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
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import Foundation | ||
|
||
actor UnregisterService { | ||
private let httpClient: HTTPClient | ||
private let projectId: String | ||
private let logger: ConsoleLogging | ||
private let environment: APNSEnvironment | ||
private let pushAuthenticator: PushAuthenticating | ||
private let clientIdStorage: ClientIdStoring | ||
private let pushHost: String | ||
|
||
init(httpClient: HTTPClient, | ||
projectId: String, | ||
clientIdStorage: ClientIdStoring, | ||
pushAuthenticator: PushAuthenticating, | ||
logger: ConsoleLogging, | ||
pushHost: String, | ||
environment: APNSEnvironment) { | ||
self.httpClient = httpClient | ||
self.clientIdStorage = clientIdStorage | ||
self.pushAuthenticator = pushAuthenticator | ||
self.projectId = projectId | ||
self.logger = logger | ||
self.pushHost = pushHost | ||
self.environment = environment | ||
} | ||
|
||
func unregister() async throws { | ||
let pushAuthToken = try pushAuthenticator.createAuthToken() | ||
let clientId = try clientIdStorage.getClientId() | ||
|
||
guard let url = URL(string: "https://\(pushHost)/\(projectId)/clients/\(clientId)") else { | ||
logger.error("Invalid URL") | ||
return | ||
} | ||
|
||
var request = URLRequest(url: url) | ||
request.httpMethod = "DELETE" | ||
request.addValue("\(pushAuthToken)", forHTTPHeaderField: "Authorization") | ||
|
||
do { | ||
let (_, response) = try await URLSession.shared.data(for: request) | ||
|
||
guard let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 else { | ||
logger.error("Failed to unregister from Push Server") | ||
throw NSError(domain: "UnregisterService", code: 0, userInfo: [NSLocalizedDescriptionKey: "Failed to unregister"]) | ||
} | ||
|
||
logger.debug("Successfully unregistered from Push Server") | ||
} catch { | ||
logger.error("Push Server unregistration error: \(error.localizedDescription)") | ||
throw error | ||
} | ||
} | ||
} |
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