-
-
Notifications
You must be signed in to change notification settings - Fork 60
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 #45 from freshOS/decodable
Makes Decodable usable without NetworkingJSONDecodable + tests
- Loading branch information
Showing
12 changed files
with
444 additions
and
44 deletions.
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
Sources/Networking/Calls/NetworkingClient+Decodable.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,112 @@ | ||
// | ||
// NetworkingClient+Decodable.swift | ||
// | ||
// | ||
// Created by Sacha DSO on 12/04/2022. | ||
// | ||
|
||
import Foundation | ||
import Combine | ||
|
||
public extension NetworkingClient { | ||
|
||
func get<T: Decodable>(_ route: String, | ||
params: Params = Params(), | ||
keypath: String? = nil) -> AnyPublisher<T, Error> { | ||
return get(route, params: params) | ||
.tryMap { json -> T in try NetworkingParser().toModel(json, keypath: keypath) } | ||
.receive(on: DispatchQueue.main) | ||
.eraseToAnyPublisher() | ||
} | ||
|
||
// Array version | ||
func get<T: Decodable>(_ route: String, | ||
params: Params = Params(), | ||
keypath: String? = nil) -> AnyPublisher<T, Error> where T: Collection { | ||
let keypath = keypath ?? defaultCollectionParsingKeyPath | ||
return get(route, params: params) | ||
.tryMap { json -> T in try NetworkingParser().toModel(json, keypath: keypath) } | ||
.receive(on: DispatchQueue.main) | ||
.eraseToAnyPublisher() | ||
} | ||
|
||
func post<T: Decodable>(_ route: String, | ||
params: Params = Params(), | ||
keypath: String? = nil) -> AnyPublisher<T, Error> { | ||
return post(route, params: params) | ||
.tryMap { json -> T in try NetworkingParser().toModel(json, keypath: keypath) } | ||
.receive(on: DispatchQueue.main) | ||
.eraseToAnyPublisher() | ||
} | ||
|
||
// Array version | ||
func post<T: Decodable>(_ route: String, | ||
params: Params = Params(), | ||
keypath: String? = nil) -> AnyPublisher<T, Error> where T: Collection { | ||
let keypath = keypath ?? defaultCollectionParsingKeyPath | ||
return post(route, params: params) | ||
.tryMap { json -> T in try NetworkingParser().toModel(json, keypath: keypath) } | ||
.receive(on: DispatchQueue.main) | ||
.eraseToAnyPublisher() | ||
} | ||
|
||
func put<T: Decodable>(_ route: String, | ||
params: Params = Params(), | ||
keypath: String? = nil) -> AnyPublisher<T, Error> { | ||
return put(route, params: params) | ||
.tryMap { json -> T in try NetworkingParser().toModel(json, keypath: keypath) } | ||
.receive(on: DispatchQueue.main) | ||
.eraseToAnyPublisher() | ||
} | ||
|
||
// Array version | ||
func put<T: Decodable>(_ route: String, | ||
params: Params = Params(), | ||
keypath: String? = nil) -> AnyPublisher<T, Error> where T: Collection { | ||
let keypath = keypath ?? defaultCollectionParsingKeyPath | ||
return put(route, params: params) | ||
.tryMap { json -> T in try NetworkingParser().toModel(json, keypath: keypath) } | ||
.receive(on: DispatchQueue.main) | ||
.eraseToAnyPublisher() | ||
} | ||
|
||
func patch<T: Decodable>(_ route: String, | ||
params: Params = Params(), | ||
keypath: String? = nil) -> AnyPublisher<T, Error> { | ||
return patch(route, params: params) | ||
.tryMap { json -> T in try NetworkingParser().toModel(json, keypath: keypath) } | ||
.receive(on: DispatchQueue.main) | ||
.eraseToAnyPublisher() | ||
} | ||
|
||
// Array version | ||
func patch<T: Decodable>(_ route: String, | ||
params: Params = Params(), | ||
keypath: String? = nil) -> AnyPublisher<T, Error> where T: Collection { | ||
let keypath = keypath ?? defaultCollectionParsingKeyPath | ||
return patch(route, params: params) | ||
.tryMap { json -> T in try NetworkingParser().toModel(json, keypath: keypath) } | ||
.receive(on: DispatchQueue.main) | ||
.eraseToAnyPublisher() | ||
} | ||
|
||
func delete<T: Decodable>(_ route: String, | ||
params: Params = Params(), | ||
keypath: String? = nil) -> AnyPublisher<T, Error> { | ||
return delete(route, params: params) | ||
.tryMap { json -> T in try NetworkingParser().toModel(json, keypath: keypath) } | ||
.receive(on: DispatchQueue.main) | ||
.eraseToAnyPublisher() | ||
} | ||
|
||
// Array version | ||
func delete<T: Decodable>(_ route: String, | ||
params: Params = Params(), | ||
keypath: String? = nil) -> AnyPublisher<T, Error> where T: Collection { | ||
let keypath = keypath ?? defaultCollectionParsingKeyPath | ||
return delete(route, params: params) | ||
.tryMap { json -> T in try NetworkingParser().toModel(json, keypath: keypath) } | ||
.receive(on: DispatchQueue.main) | ||
.eraseToAnyPublisher() | ||
} | ||
} |
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
Oops, something went wrong.