-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8e98077
commit 5e50e4a
Showing
30 changed files
with
349 additions
and
278 deletions.
There are no files selected for viewing
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,12 @@ | ||
enum Coders { | ||
|
||
// MARK: HTTP Body | ||
|
||
static var defaultHTTPBodyEncoder: HTTPBodyEncoder = .json() | ||
static var defaultHTTPBodyDecoder: HTTPBodyDecoder = .json() | ||
|
||
// MARK: Query | ||
|
||
static var defaultQueryEncoder = URLEncodedFormEncoder() | ||
static var defaultQueryDecoder = URLEncodedFormDecoder() | ||
} |
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,9 @@ | ||
extension String { | ||
static func randomMultipartBoundary() -> String { | ||
let first = UInt32.random(in: UInt32.min...UInt32.max) | ||
let second = UInt32.random(in: UInt32.min...UInt32.max) | ||
return String(format: "papyrus.boundary.%08x%08x", first, second) | ||
} | ||
|
||
static let crlf = "\r\n" | ||
} |
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,53 @@ | ||
import Foundation | ||
|
||
public protocol HTTPBodyDecoder: KeyMappable { | ||
func decode<D: Decodable>(_ type: D.Type, from: Data) throws -> D | ||
} | ||
|
||
// MARK: application/json | ||
|
||
extension HTTPBodyDecoder where Self == JSONDecoder { | ||
public static func json(_ decoder: JSONDecoder = JSONDecoder()) -> Self { | ||
decoder | ||
} | ||
} | ||
|
||
extension JSONDecoder: HTTPBodyDecoder { | ||
public func with(keyMapping: KeyMapping) -> Self { | ||
let new = JSONDecoder() | ||
new.userInfo = userInfo | ||
new.dataDecodingStrategy = dataDecodingStrategy | ||
new.dateDecodingStrategy = dateDecodingStrategy | ||
new.nonConformingFloatDecodingStrategy = nonConformingFloatDecodingStrategy | ||
#if os(Linux) | ||
#else | ||
if #available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) { | ||
new.assumesTopLevelDictionary = assumesTopLevelDictionary | ||
new.allowsJSON5 = allowsJSON5 | ||
} | ||
#endif | ||
new.keyDecodingStrategy = keyMapping.jsonDecodingStrategy | ||
return new as! Self | ||
} | ||
} | ||
|
||
// MARK: application/x-www-form-urlencoded | ||
|
||
extension HTTPBodyDecoder where Self == URLEncodedFormDecoder { | ||
public static func urlForm(_ decoder: URLEncodedFormDecoder = URLEncodedFormDecoder()) -> Self { | ||
decoder | ||
} | ||
} | ||
|
||
extension URLEncodedFormDecoder: HTTPBodyDecoder { | ||
public func decode<D: Decodable>(_ type: D.Type, from data: Data) throws -> D { | ||
let string = String(decoding: data, as: UTF8.self) | ||
return try decode(type, from: string) | ||
} | ||
|
||
public func with(keyMapping: KeyMapping) -> Self { | ||
var copy = self | ||
copy.keyMapping = keyMapping | ||
return copy | ||
} | ||
} |
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,12 @@ | ||
import Foundation | ||
|
||
public protocol KeyMappable { | ||
func with(keyMapping: KeyMapping) -> Self | ||
} | ||
|
||
extension KeyMappable { | ||
func with(keyMapping: KeyMapping?) -> Self { | ||
guard let keyMapping else { return self } | ||
return with(keyMapping: keyMapping) | ||
} | ||
} |
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,18 @@ | ||
import Foundation | ||
|
||
public struct MultipartDecoder: HTTPBodyDecoder { | ||
public let boundary: String | ||
|
||
public init(boundary: String? = nil) { | ||
self.boundary = boundary ?? .randomMultipartBoundary() | ||
} | ||
|
||
public func with(keyMapping: KeyMapping) -> MultipartDecoder { | ||
// KeyMapping isn't relevant since each part has already encoded data. | ||
self | ||
} | ||
|
||
public func decode<D>(_ type: D.Type, from: Data) throws -> D where D : Decodable { | ||
fatalError("multipart decoding isn't supported, yet") | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.