-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* switch out old storyboard * revamp project - begin adding in screens * add some more example requests * fix home screen layout * add post example * fix links in readme, add small unredacted params example * fix ios version * fix login button name change swiftermanager to a less silly name
- Loading branch information
1 parent
e5c82e0
commit 19bb74f
Showing
53 changed files
with
1,169 additions
and
1,352 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,14 @@ | ||
// | ||
// Post.swift | ||
// NetableExample | ||
// | ||
// Created by Brendan on 2021-09-15. | ||
// Copyright © 2021 Steamclock Software. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
struct Post: Decodable { | ||
let title: String | ||
let content: String | ||
} |
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,17 @@ | ||
// | ||
// User.swift | ||
// NetableExample | ||
// | ||
// Created by Brendan on 2021-09-02. | ||
// Copyright © 2021 Steamclock Software. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
struct User: Decodable { | ||
let email: String | ||
let token: String | ||
let firstName: String? | ||
let lastName: String? | ||
let location: String? | ||
} |
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,18 @@ | ||
// | ||
// Version.swift | ||
// NetableExample | ||
// | ||
// Created by Brendan on 2021-09-15. | ||
// Copyright © 2021 Steamclock Software. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
struct SimpleVersion: Decodable { | ||
let buildNumber: String | ||
} | ||
|
||
struct Version: Decodable { | ||
let buildNumber: String | ||
let deprecatedBuilds: [String] | ||
} |
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,57 @@ | ||
// | ||
// PostRepository.swift | ||
// NetableExample | ||
// | ||
// Created by Brendan on 2021-09-03. | ||
// | ||
|
||
import Combine | ||
import Foundation | ||
import Netable | ||
|
||
class PostRepository { | ||
static var shared = PostRepository() | ||
|
||
/// If we aren't concerned with logging results from a particular instance, pass in the EmptyLogDestination as the logDestination | ||
private let netable = Netable(baseURL: URL(string: "http://localhost:8080/posts/")!) | ||
|
||
var posts: CurrentValueSubject<[Post], Never> | ||
var cancellables = [AnyCancellable]() | ||
|
||
private init() { | ||
posts = CurrentValueSubject<[Post], Never>([]) | ||
} | ||
|
||
func checkVersion() { | ||
netable.request(VersionCheckRequest()) { result in | ||
switch result { | ||
case .success: | ||
print("Version check successful!") | ||
case .failure(let error): | ||
if case NetableError.fallbackDecode = error { | ||
print("Version check fallback successful!") | ||
return | ||
} | ||
|
||
print("Version check failed. Better handle that.") | ||
} | ||
} | ||
} | ||
|
||
func getPosts() { | ||
netable.request(GetPostsRequest()) { [weak self] result in | ||
if case .success(let posts) = result { | ||
self?.posts.send(posts) | ||
} | ||
} | ||
} | ||
|
||
func create(_ title: String, content: String, onComplete: @escaping () -> Void) { | ||
let params = CreatePostParams(title: title, content: content) | ||
|
||
netable.request(CreatePostRequest(parameters: params)) { result in | ||
print(result) | ||
onComplete() | ||
} | ||
} | ||
} |
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,74 @@ | ||
// | ||
// UserRepository.swift | ||
// NetableExample | ||
// | ||
// Created by Brendan on 2021-09-03. | ||
// | ||
|
||
import Combine | ||
import Foundation | ||
import Netable | ||
|
||
class UserRepository { | ||
static var shared = UserRepository() | ||
|
||
let netable = Netable(baseURL: URL(string: "http://localhost:8080/user/")!) | ||
|
||
var user: CurrentValueSubject<User?, Never> | ||
var cancellables = [AnyCancellable]() | ||
|
||
private init() { | ||
user = CurrentValueSubject<User?, Never>(nil) | ||
|
||
// Listen for 401 errors and if we get one, clear the current user | ||
netable.requestFailurePublisher.sink { [weak self] error in | ||
guard case let NetableError.httpError(statusCode, _) = error, statusCode == 401 else { | ||
return | ||
} | ||
|
||
self?.user.send(nil) | ||
}.store(in: &cancellables) | ||
} | ||
|
||
func login(email: String, password: String) { | ||
let params = LoginParams(email: email, password: password) | ||
|
||
netable.request(LoginRequest(parameters: params)) { [weak self] result in | ||
guard let self = self else { return } | ||
|
||
if case .success(let user) = result { | ||
self.user.send(user) | ||
} | ||
} | ||
} | ||
|
||
func getUserDetails() { | ||
let params = UserDetailsParams(email: "", token: "") | ||
|
||
netable.request(GetUserDetailsRequest(parameters: params)) { [weak self] result in | ||
guard let self = self else { return } | ||
|
||
if case .success(let user) = result { | ||
self.user.send(user) | ||
} | ||
} | ||
} | ||
|
||
public func unauthorizedRequest() { | ||
netable.request(UnauthorizedRequest()) { _ in | ||
// Since we know this request is going to fail, do nothing. | ||
return | ||
} | ||
} | ||
|
||
public func failedRequest() { | ||
netable.request(FailedRequest()) { _ in | ||
// Since we know this request is going to fail, do nothing. | ||
return | ||
} | ||
} | ||
|
||
public func logout() { | ||
self.user.send(nil) | ||
} | ||
} |
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,25 @@ | ||
// | ||
// CreatePostRequest.swift | ||
// NetableExample | ||
// | ||
// Created by Brendan on 2021-09-16. | ||
// Copyright © 2021 Steamclock Software. All rights reserved. | ||
// | ||
|
||
import Netable | ||
|
||
struct CreatePostParams: Encodable { | ||
let title: String | ||
let content: String | ||
} | ||
|
||
struct CreatePostRequest: Request { | ||
typealias Parameters = CreatePostParams | ||
typealias RawResource = Empty | ||
|
||
var method = HTTPMethod.post | ||
|
||
var path = "create" | ||
|
||
var parameters: CreatePostParams | ||
} |
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,18 @@ | ||
// | ||
// FailedRequest.swift | ||
// NetableExample | ||
// | ||
// Created by Brendan on 2021-09-15. | ||
// Copyright © 2021 Steamclock Software. All rights reserved. | ||
// | ||
|
||
import Netable | ||
|
||
struct FailedRequest: Request { | ||
typealias Parameters = Empty | ||
typealias RawResource = Empty | ||
|
||
var method = HTTPMethod.get | ||
|
||
var path = "failed" | ||
} |
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,23 @@ | ||
// | ||
// GetPostsRequest.swift | ||
// NetableExample | ||
// | ||
// Created by Brendan on 2021-09-15. | ||
// Copyright © 2021 Steamclock Software. All rights reserved. | ||
// | ||
|
||
import Netable | ||
|
||
struct GetPostsRequest: Request { | ||
typealias Parameters = Empty | ||
typealias RawResource = SmartUnwrap<[Post]> | ||
typealias FinalResource = [Post] | ||
|
||
var method = HTTPMethod.get | ||
|
||
var path = "all" | ||
|
||
var unredactedParameterKeys: Set<String> { | ||
["title", "content"] | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
Netable/NetableExample/Request/GetUserDetailsRequest.swift
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,26 @@ | ||
// | ||
// GetUserDetailsRequest.swift | ||
// NetableExample | ||
// | ||
// Created by Brendan on 2021-09-14. | ||
// Copyright © 2021 Steamclock Software. All rights reserved. | ||
// | ||
|
||
import Netable | ||
|
||
struct UserDetailsParams: Encodable { | ||
let email: String | ||
let token: String | ||
} | ||
|
||
struct GetUserDetailsRequest: Request { | ||
typealias Parameters = UserDetailsParams | ||
typealias RawResource = SmartUnwrap<User> | ||
typealias FinalResource = User | ||
|
||
var method = HTTPMethod.get | ||
|
||
var path = "me" | ||
|
||
var parameters: UserDetailsParams | ||
} |
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,24 @@ | ||
// | ||
// LoginRequest.swift | ||
// NetableExample | ||
// | ||
// Created by Brendan on 2021-09-03. | ||
// | ||
|
||
import Netable | ||
|
||
struct LoginParams: Encodable { | ||
let email: String | ||
let password: String | ||
} | ||
|
||
struct LoginRequest: Request { | ||
typealias Parameters = LoginParams | ||
typealias RawResource = User | ||
|
||
var method = HTTPMethod.get | ||
|
||
var path = "login" | ||
|
||
var parameters: LoginParams | ||
} |
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,18 @@ | ||
// | ||
// UnauthorizedRequest.swift | ||
// NetableExample | ||
// | ||
// Created by Brendan on 2021-09-15. | ||
// Copyright © 2021 Steamclock Software. All rights reserved. | ||
// | ||
|
||
import Netable | ||
|
||
struct UnauthorizedRequest: Request { | ||
typealias Parameters = Empty | ||
typealias RawResource = Empty | ||
|
||
var method = HTTPMethod.get | ||
|
||
var path = "unauthorized" | ||
} |
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,19 @@ | ||
// | ||
// VersionCheckRequest.swift | ||
// NetableExample | ||
// | ||
// Created by Brendan on 2021-09-15. | ||
// Copyright © 2021 Steamclock Software. All rights reserved. | ||
// | ||
|
||
import Netable | ||
|
||
struct VersionCheckRequest: Request { | ||
typealias Parameters = Empty | ||
typealias RawResource = Version | ||
typealias FallbackResource = SimpleVersion | ||
|
||
var method = HTTPMethod.get | ||
|
||
var path = "version" | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.