Adds service to send APNs device token

sync
Laurent 1 year ago
parent 6981ebeb23
commit 79601f9e90
  1. 19
      LeStorage/Services.swift

@ -20,10 +20,11 @@ fileprivate enum ServiceConf: String {
case logout = "api-token-logout/" case logout = "api-token-logout/"
case getUser = "user-by-token/" case getUser = "user-by-token/"
case changePassword = "change-password/" case changePassword = "change-password/"
case postDeviceToken = "device-token/"
var method: HTTPMethod { var method: HTTPMethod {
switch self { switch self {
case .createAccount, .requestToken, .logout: case .createAccount, .requestToken, .logout, .postDeviceToken:
return .post return .post
case .changePassword: case .changePassword:
return .put return .put
@ -36,7 +37,7 @@ fileprivate enum ServiceConf: String {
switch self { switch self {
case .createAccount, .requestToken: case .createAccount, .requestToken:
return false return false
case .getUser, .changePassword, .logout: case .getUser, .changePassword, .logout, .postDeviceToken:
return true return true
// default: // default:
// return nil // return nil
@ -343,6 +344,17 @@ public class Services {
let _: Empty = try await self._runRequest(logoutRequest) let _: Empty = try await self._runRequest(logoutRequest)
} }
/// A login method that actually requests a token from the server, and stores the appropriate data for later usage
/// - Parameters:
/// - username: the account's username
/// - password: the account's password
public func postDeviceToken(deviceToken: Data) async throws {
let tokenString = deviceToken.map { String(format: "%02x", $0) }.joined()
let token = DeviceToken(value: tokenString)
Logger.log("Send device token = \(tokenString)")
let _: DeviceToken = try await self._runRequest(serviceConf: .postDeviceToken, payload: token)
}
/// A method that sends a request to change a user's password /// A method that sends a request to change a user's password
/// - Parameters: /// - Parameters:
/// - oldPassword: the account's old password /// - oldPassword: the account's old password
@ -445,6 +457,9 @@ struct Email: Codable {
struct Empty: Codable { struct Empty: Codable {
} }
struct DeviceToken: Codable {
var value: String
}
public protocol UserBase: Codable { public protocol UserBase: Codable {
var id: String { get } var id: String { get }

Loading…
Cancel
Save