Finish updating login requests to match newest API spec

This commit is contained in:
William Brawner 2021-09-11 06:19:46 -06:00
parent 7a9c9432b1
commit ec16456e77
3 changed files with 58 additions and 36 deletions

View file

@ -30,23 +30,23 @@ class BudgetAppApiService {
}
func getBudget(_ id: String) -> AnyPublisher<Budget, NetworkError> {
return requestHelper.get("/budgets/\(id)")
return requestHelper.get("/api/budgets/\(id)")
}
func getBudgetBalance(_ id: String) -> AnyPublisher<Int, NetworkError> {
return requestHelper.get("/budgets/\(id)/balance")
return requestHelper.get("/api/budgets/\(id)/balance")
}
func newBudget(_ budget: Budget) -> AnyPublisher<Budget, NetworkError> {
return requestHelper.post("/budgets/new", data: budget, type: Budget.self)
return requestHelper.post("/api/budgets/new", data: budget, type: Budget.self)
}
func updateBudget(_ budget: Budget) -> AnyPublisher<Budget, NetworkError> {
return requestHelper.put("/budgets/\(budget.id)", data: budget)
return requestHelper.put("/api/budgets/\(budget.id)", data: budget)
}
func deleteBudget(_ id: String) -> AnyPublisher<Empty, NetworkError> {
return requestHelper.delete("/budgets/\(id)")
return requestHelper.delete("/api/budgets/\(id)")
}
// MARK: Transactions
@ -78,23 +78,23 @@ class BudgetAppApiService {
if (page != nil) {
queries["page"] = [String(page!)]
}
return requestHelper.get("/transactions", queries: queries)
return requestHelper.get("/api/transactions", queries: queries)
}
func getTransaction(_ id: String) -> AnyPublisher<Transaction, NetworkError> {
return requestHelper.get("/transactions/\(id)")
return requestHelper.get("/api/transactions/\(id)")
}
func newTransaction(_ transaction: Transaction) -> AnyPublisher<Transaction, NetworkError> {
return requestHelper.post("/transactions/new", data: transaction, type: Transaction.self)
return requestHelper.post("/api/transactions/new", data: transaction, type: Transaction.self)
}
func updateTransaction(_ transaction: Transaction) -> AnyPublisher<Transaction, NetworkError> {
return requestHelper.put("/transactions/\(transaction.id)", data: transaction)
return requestHelper.put("/api/transactions/\(transaction.id)", data: transaction)
}
func deleteTransaction(_ id: String) -> AnyPublisher<Empty, NetworkError> {
return requestHelper.delete("/transactions/\(id)")
return requestHelper.delete("/api/transactions/\(id)")
}
// MARK: Categories
@ -110,33 +110,33 @@ class BudgetAppApiService {
if (page != nil) {
queries["page"] = [String(page!)]
}
return requestHelper.get("/categories", queries: queries)
return requestHelper.get("/api/categories", queries: queries)
}
func getCategory(_ id: String) -> AnyPublisher<Category, NetworkError> {
return requestHelper.get("/categories/\(id)")
return requestHelper.get("/api/categories/\(id)")
}
func getCategoryBalance(_ id: String) -> AnyPublisher<Int, NetworkError> {
return requestHelper.get("/categories/\(id)/balance")
return requestHelper.get("/api/categories/\(id)/balance")
}
func newCategory(_ category: Category) -> AnyPublisher<Category, NetworkError> {
return requestHelper.post("/categories/new", data: category, type: Category.self)
return requestHelper.post("/api/categories/new", data: category, type: Category.self)
}
func updateCategory(_ category: Category) -> AnyPublisher<Category, NetworkError> {
return requestHelper.put("/categories/\(category.id)", data: category)
return requestHelper.put("/api/categories/\(category.id)", data: category)
}
func deleteCategory(_ id: String) -> AnyPublisher<Empty, NetworkError> {
return requestHelper.delete("/categories/\(id)")
return requestHelper.delete("/api/categories/\(id)")
}
// MARK: Users
func login(username: String, password: String) -> AnyPublisher<LoginResponse, NetworkError> {
return requestHelper.post(
"/users/login",
"/api/users/login",
data: LoginRequest(username: username, password: password),
type: LoginResponse.self
).map { (session) -> LoginResponse in
@ -146,7 +146,7 @@ class BudgetAppApiService {
func register(username: String, email: String, password: String) -> AnyPublisher<User, NetworkError> {
return requestHelper.post(
"/users/new",
"/api/users/new",
data: RegistrationRequest(username: username, email: email, password: password),
type: User.self
).map { (user) -> User in
@ -156,12 +156,12 @@ class BudgetAppApiService {
}
func getUser(id: String) -> AnyPublisher<User, NetworkError> {
return requestHelper.get("/users/\(id)")
return requestHelper.get("/api/users/\(id)")
}
func searchUsers(query: String) -> AnyPublisher<[User], NetworkError> {
return requestHelper.get(
"/users/search",
"/api/users/search",
queries: ["query": [query]]
)
}
@ -174,25 +174,26 @@ class BudgetAppApiService {
if (page != nil) {
queries["page"] = [String(page!)]
}
return requestHelper.get("/Users", queries: queries)
return requestHelper.get("/api/Users", queries: queries)
}
func newUser(_ user: User) -> AnyPublisher<User, NetworkError> {
return requestHelper.post("/users/new", data: user, type: User.self)
return requestHelper.post("/api/users/new", data: user, type: User.self)
}
func updateUser(_ user: User) -> AnyPublisher<User, NetworkError> {
return requestHelper.put("/users/\(user.id)", data: user)
return requestHelper.put("/api/users/\(user.id)", data: user)
}
func deleteUser(_ user: User) -> AnyPublisher<Empty, NetworkError> {
return requestHelper.delete("/users/\(user.id)")
return requestHelper.delete("/api/users/\(user.id)")
}
}
class RequestHelper {
let decoder = JSONDecoder()
let baseUrl: String
var token: String?
init(_ baseUrl: String) {
self.baseUrl = baseUrl
@ -260,10 +261,10 @@ class RequestHelper {
}
}
return Empty()
}
.mapError {
return NetworkError.jsonParsingFailed($0)
}
}
.mapError {
return NetworkError.jsonParsingFailed($0)
}
return task.eraseToAnyPublisher()
}
@ -281,8 +282,11 @@ class RequestHelper {
request.httpBody = data?.toJSONData()
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpMethod = method
if let token = self.token {
request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
}
let task = URLSession.shared.dataTaskPublisher(for: request)
return URLSession.shared.dataTaskPublisher(for: request)
.tryMap { (data, res) -> Data in
guard let response = res as? HTTPURLResponse, 200...299 ~= response.statusCode else {
switch (res as? HTTPURLResponse)?.statusCode {
@ -293,12 +297,12 @@ class RequestHelper {
}
}
return data
}
.decode(type: ResultType.self, decoder: self.decoder)
.mapError {
return NetworkError.jsonParsingFailed($0)
}
return task.eraseToAnyPublisher()
}
.decode(type: ResultType.self, decoder: self.decoder)
.mapError {
return NetworkError.jsonParsingFailed($0)
}
.eraseToAnyPublisher()
}
}

View file

@ -24,6 +24,7 @@ class AuthenticationDataStore: ObservableObject {
return
// Do nothing it means the network request just ended
case .failure(let error):
print("Login failed")
self.currentRequest = nil
switch error {
case .jsonParsingFailed(let jsonError):
@ -35,7 +36,6 @@ class AuthenticationDataStore: ObservableObject {
self.currentUser = .failure(.failedAuthentication)
}
}) { (session) in
print("Login succeeded, loading user")
UserDefaults.standard.set(session.token, forKey: TOKEN)
UserDefaults.standard.set(session.userId, forKey: USER_ID)
self.loadProfile()
@ -69,10 +69,17 @@ class AuthenticationDataStore: ObservableObject {
private func loadProfile() {
guard let userId = UserDefaults.standard.string(forKey: USER_ID) else {
print("No saved userId, unable to load profile")
self.currentUser = .failure(.unauthenticated)
return
}
guard let token = UserDefaults.standard.string(forKey: TOKEN) else {
print("No saved token, unable to load profile")
self.currentUser = .failure(.unauthenticated)
return
}
self.currentUser = .failure(.authenticating)
self.userRepository.setToken(token)
currentRequest = self.userRepository.getUser(userId)
.receive(on: DispatchQueue.main)
.sink(receiveCompletion: { (status) in
@ -81,6 +88,7 @@ class AuthenticationDataStore: ObservableObject {
self.currentRequest = nil
return
case .failure(_):
print("Failed to load user")
self.currentUser = .failure(.unauthenticated)
}
}) { (user) in

View file

@ -10,6 +10,7 @@ import Foundation
import Combine
protocol UserRepository {
func setToken(_ token: String)
func getUser(_ id: String) -> AnyPublisher<User, NetworkError>
func searchUsers(_ withUsername: String) -> AnyPublisher<[User], NetworkError>
func login(username: String, password: String) -> AnyPublisher<LoginResponse, NetworkError>
@ -23,6 +24,10 @@ class NetworkUserRepository: UserRepository {
self.apiService = apiService
}
func setToken(_ token: String) {
self.apiService.requestHelper.token = token
}
func getUser(_ id: String) -> AnyPublisher<User, NetworkError> {
return apiService.getUser(id: id)
}
@ -45,6 +50,11 @@ class NetworkUserRepository: UserRepository {
class MockUserRepository: UserRepository {
static let loginResponse = LoginResponse(token: "token", expiration: "2020-01-01T12:00:00Z", userId: "0")
static let user = User(id: "0", username: "root", email: "root@localhost", avatar: nil)
static var token: String? = nil
func setToken(_ token: String) {
MockUserRepository.token = token
}
func getUser(_ id: String) -> AnyPublisher<User, NetworkError> {
return Result<User, NetworkError>.Publisher(MockUserRepository.user)