Skip to content

Commit

Permalink
Revamp example project (#93)
Browse files Browse the repository at this point in the history
* 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
brendanlensink authored Oct 6, 2021
1 parent e5c82e0 commit 19bb74f
Show file tree
Hide file tree
Showing 53 changed files with 1,169 additions and 1,352 deletions.
273 changes: 155 additions & 118 deletions Netable/Netable.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"object": {
"pins": [
{
"package": "OHHTTPStubs",
"repositoryURL": "https://github.com/AliSoftware/OHHTTPStubs.git",
"package": "Swifter",
"repositoryURL": "https://github.com/httpswift/swifter.git",
"state": {
"branch": null,
"revision": "e92b5a5746ef16add2a1424f1fc19529d9a75cde",
"version": "9.0.0"
"revision": "9483a5d459b45c3ffd059f7b55f9638e268632fd",
"version": "1.5.0"
}
}
]
Expand Down
14 changes: 14 additions & 0 deletions Netable/NetableExample/Model/Post.swift
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
}
17 changes: 17 additions & 0 deletions Netable/NetableExample/Model/User.swift
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?
}
18 changes: 18 additions & 0 deletions Netable/NetableExample/Model/Version.swift
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]
}
57 changes: 57 additions & 0 deletions Netable/NetableExample/Repository/PostRepository.swift
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()
}
}
}
74 changes: 74 additions & 0 deletions Netable/NetableExample/Repository/UserRepository.swift
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)
}
}
25 changes: 25 additions & 0 deletions Netable/NetableExample/Request/CreatePostRequest.swift
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
}
18 changes: 18 additions & 0 deletions Netable/NetableExample/Request/FailedRequest.swift
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"
}
23 changes: 23 additions & 0 deletions Netable/NetableExample/Request/GetPostsRequest.swift
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 Netable/NetableExample/Request/GetUserDetailsRequest.swift
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
}
24 changes: 24 additions & 0 deletions Netable/NetableExample/Request/LoginRequest.swift
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
}
18 changes: 18 additions & 0 deletions Netable/NetableExample/Request/UnauthorizedRequest.swift
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"
}
19 changes: 19 additions & 0 deletions Netable/NetableExample/Request/VersionCheckRequest.swift
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"
}
20 changes: 0 additions & 20 deletions Netable/NetableExample/Requests/DeleteRequest.swift

This file was deleted.

Loading

0 comments on commit 19bb74f

Please sign in to comment.