-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create HTTP Post #16
Merged
Merged
Create HTTP Post #16
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0f64fdc
Create NetworkPost
ronanrodrigo c1e5341
Create NetworkPost
ronanrodrigo 235bc30
Update NetworkPost adding request body
ronanrodrigo ec051d9
Update linux main
ronanrodrigo 56f95cf
Update linux main
ronanrodrigo fa57a11
Update file paths + Extract some test doubles
ronanrodrigo 60d9efb
Update test assert
ronanrodrigo f23978f
Move integration tests to Requestables folder
ronanrodrigo df706c4
Create OnComplete typealias + Replace generic types placeholder
ronanrodrigo b1b021e
Create DataTaskRunner extracting urlSession.dataTask
ronanrodrigo d04b354
Add swift flag for warn compile time under 100 in function bodies and…
ronanrodrigo ff4e7cb
Create BodyBuilder tests + Abstract JSON Encoder and Serializable
ronanrodrigo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import Foundation | ||
|
||
protocol FrisbeeEncodable { | ||
func encode<T: Encodable>(_ value: T) throws -> Data | ||
} |
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,7 @@ | ||
import Foundation | ||
|
||
class FrisbeeJSONEncoder: FrisbeeEncodable { | ||
func encode<T: Encodable>(_ value: T) throws -> Data { | ||
return try JSONEncoder().encode(value) | ||
} | ||
} |
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,7 @@ | ||
import Foundation | ||
|
||
class FrisbeeJSONSerializable: FrisbeeSerializable { | ||
func object(with data: Data, options opt: JSONSerialization.ReadingOptions = []) throws -> Any { | ||
return try JSONSerialization.jsonObject(with: data, options: opt) | ||
} | ||
} |
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,5 @@ | ||
import Foundation | ||
|
||
protocol FrisbeeSerializable { | ||
func object(with data: Data, options opt: JSONSerialization.ReadingOptions) throws -> Any | ||
} |
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,8 @@ | ||
struct BodyBuildableFactory { | ||
|
||
static func make() -> BodyBuildable { | ||
return BodyBuilder(encoder: FrisbeeEncodableFactory.make(), | ||
serializer: FrisbeeJSONSerializableFactroy.make()) | ||
} | ||
|
||
} |
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,7 @@ | ||
class FrisbeeEncodableFactory { | ||
|
||
static func make() -> FrisbeeEncodable { | ||
return FrisbeeJSONEncoder() | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
Sources/Frisbee/Factories/FrisbeeJSONSerializableFactroy.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,7 @@ | ||
class FrisbeeJSONSerializableFactroy { | ||
|
||
static func make() -> FrisbeeSerializable { | ||
return FrisbeeJSONSerializable() | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import Foundation | ||
|
||
protocol BodyBuildable { | ||
func build<T: Encodable>(withBody body: T) throws -> [String: Any] | ||
} |
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 @@ | ||
import Foundation | ||
|
||
struct BodyBuilder: BodyBuildable { | ||
|
||
private let encoder: FrisbeeEncodable | ||
private let serializer: FrisbeeSerializable | ||
|
||
init(encoder: FrisbeeEncodable, serializer: FrisbeeSerializable) { | ||
self.encoder = encoder | ||
self.serializer = serializer | ||
} | ||
|
||
func build<T: Encodable>(withBody body: T) throws -> [String: Any] { | ||
var json: [String: Any] = [:] | ||
|
||
let data = try encoder.encode(body) | ||
let jsonObject = try serializer.object(with: data, options: []) | ||
if let jsonDictionary = jsonObject as? [String: Any] { json = jsonDictionary } | ||
|
||
return json | ||
} | ||
|
||
} |
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 @@ | ||
import Foundation | ||
|
||
final class DataTaskRunner { | ||
|
||
static func run<T: Decodable>(with urlSession: URLSession, request: URLRequest, | ||
onComplete: @escaping OnComplete<T>) { | ||
let task = urlSession.dataTask(with: request) { data, _, error in | ||
onComplete(ResultGeneratorFactory.make().generate(data: data, error: error)) | ||
} | ||
|
||
task.resume() | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import Foundation | ||
|
||
protocol URLWithQueryBuildable { | ||
func build<Query: Encodable>(withUrl url: String, query: Query) throws -> URL | ||
func build<Query: Encodable>(withUrl url: URL, query: Query) throws -> URL | ||
func build<T: Encodable>(withUrl url: String, query: T) throws -> URL | ||
func build<T: Encodable>(withUrl url: URL, query: T) throws -> URL | ||
} |
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 |
---|---|---|
@@ -1,10 +1,8 @@ | ||
import Foundation | ||
|
||
public protocol Getable { | ||
func get<Entity: Decodable>(url: String, onComplete: @escaping (Result<Entity>) -> Void) | ||
func get<Entity: Decodable>(url: URL, onComplete: @escaping (Result<Entity>) -> Void) | ||
func get<Entity: Decodable, Query: Encodable>(url: String, query: Query, | ||
onComplete: @escaping (Result<Entity>) -> Void) | ||
func get<Entity: Decodable, Query: Encodable>(url: URL, query: Query, | ||
onComplete: @escaping (Result<Entity>) -> Void) | ||
func get<T: Decodable>(url: String, onComplete: @escaping OnComplete<T>) | ||
func get<T: Decodable>(url: URL, onComplete: @escaping OnComplete<T>) | ||
func get<T: Decodable, U: Encodable>(url: String, query: U, onComplete: @escaping OnComplete<T>) | ||
func get<T: Decodable, U: Encodable>(url: URL, query: U, onComplete: @escaping OnComplete<T>) | ||
} |
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,60 @@ | ||
import Foundation | ||
|
||
public final class NetworkPost: Postable { | ||
|
||
let urlSession: URLSession | ||
private let bodyBuilder: BodyBuildable | ||
|
||
public convenience init() { | ||
self.init(urlSession: URLSessionFactory.make(), bodyBuilder: BodyBuildableFactory.make()) | ||
} | ||
|
||
public convenience init(urlSession: URLSession) { | ||
self.init(urlSession: urlSession, bodyBuilder: BodyBuildableFactory.make()) | ||
} | ||
|
||
init(urlSession: URLSession, bodyBuilder: BodyBuildable) { | ||
self.urlSession = urlSession | ||
self.bodyBuilder = bodyBuilder | ||
} | ||
|
||
public func post<T: Decodable>(url: String, onComplete: @escaping OnComplete<T>) { | ||
guard let url = URL(string: url) else { | ||
return onComplete(.fail(FrisbeeError.invalidUrl)) | ||
} | ||
makeRequest(url: url, onComplete: onComplete) | ||
} | ||
|
||
public func post<T: Decodable>(url: URL, onComplete: @escaping OnComplete<T>) { | ||
makeRequest(url: url, onComplete: onComplete) | ||
} | ||
|
||
public func post<T: Decodable, U: Encodable>(url: String, body: U, onComplete: @escaping OnComplete<T>) { | ||
guard let url = URL(string: url) else { | ||
return onComplete(.fail(FrisbeeError.invalidUrl)) | ||
} | ||
makeRequest(url: url, body: body, onComplete: onComplete) | ||
} | ||
|
||
public func post<T: Decodable, U: Encodable>(url: URL, body: U, onComplete: @escaping OnComplete<T>) { | ||
makeRequest(url: url, body: body, onComplete: onComplete) | ||
} | ||
|
||
private func makeRequest<T: Decodable>(url: URL, onComplete: @escaping OnComplete<T>) { | ||
let request = URLRequestFactory.make(.POST, url) | ||
DataTaskRunner.run(with: urlSession, request: request, onComplete: onComplete) | ||
} | ||
|
||
private func makeRequest<T: Decodable, U: Encodable>(url: URL, body: U, onComplete: @escaping OnComplete<T>) { | ||
var request = URLRequestFactory.make(.POST, url) | ||
do { | ||
let bodyObject = try bodyBuilder.build(withBody: body) | ||
request.httpBody = try JSONSerialization.data(withJSONObject: bodyObject, options: []) | ||
} catch { | ||
return onComplete(.fail(FrisbeeError(error))) | ||
} | ||
|
||
DataTaskRunner.run(with: urlSession, request: request, onComplete: 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 @@ | ||
public typealias OnComplete<T: Decodable> = (Result<T>) -> Void |
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,8 @@ | ||
import Foundation | ||
|
||
protocol Postable { | ||
func post<T: Decodable>(url: String, onComplete: @escaping OnComplete<T>) | ||
func post<T: Decodable>(url: URL, onComplete: @escaping OnComplete<T>) | ||
func post<T: Decodable, U: Encodable>(url: String, body: U, onComplete: @escaping OnComplete<T>) | ||
func post<T: Decodable, U: Encodable>(url: URL, body: U, onComplete: @escaping OnComplete<T>) | ||
} |
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,31 @@ | ||
import XCTest | ||
@testable import Frisbee | ||
|
||
final class BodyBuilderTests: XCTestCase { | ||
|
||
override func setUp() { | ||
super.setUp() | ||
} | ||
|
||
func testBuildWithBodyWhenEncoderThrowsAnErrorThenThrows() { | ||
let builder = BodyBuilder(encoder: FrisbeeThrowErrorFakeEncoder(), | ||
serializer: FrisbeeJSONSerializableFactroy.make()) | ||
|
||
XCTAssertThrowsError(try builder.build(withBody: Fake(fake: ""))) | ||
} | ||
|
||
func testBuildWithBodyWhenSerializerThrowsAnErrorThenThrows() { | ||
let builder = BodyBuilder(encoder: FrisbeeEncodableFactory.make(), | ||
serializer: FrisbeeThrowErrorFakeSerializer()) | ||
|
||
XCTAssertThrowsError(try builder.build(withBody: Fake(fake: ""))) | ||
} | ||
|
||
static let allTests = [ | ||
("testBuildWithBodyWhenEncoderThrowsAnErrorThenThrows", | ||
testBuildWithBodyWhenEncoderThrowsAnErrorThenThrows), | ||
("testBuildWithBodyWhenSerializerThrowsAnErrorThenThrows", | ||
testBuildWithBodyWhenSerializerThrowsAnErrorThenThrows) | ||
] | ||
|
||
} |
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
File renamed without changes.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Create tests for this struct
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.