This library is initalize Swift struct from Web API. It is made of very proper and lazy. It can be used only when the following conditions are satisfied.
When request format is JSON.When API response format is JSON.When I was prepared to be unable to use a kind JSON Parser.- If you want to process asynchronously, use GCD.😇
I used Codable from 0.7.0. No need for Any to Object processing.
// Basic pattern
public struct BasicStruct : Decodable{
let message:String
}
extension BasicStruct : WebInitializable {
public typealias bodyType = RequestStruct
public typealias errorType = ApplicationError
}
// Body struct
public struct RequestStruct : Encodable{
let value:String
}
extension RequestStruct : WebDeserializable {
}
public struct ApplicationError : Swift.Error, Decodable{
let code:Int
let reason:String
}
extension ApplicationError : WebSerializable{
}
let basic = try? BasicStruct(
path: "http://localhost:8080/basic",
body: RequestStruct(value: "hello")
)
do{
let _ = try ErrorStruct(
path: "http://localhost:8080/error",
body: RequestStruct(value: "hello")
)
}
catch let error as WebStruct.Error{
switch (error) {
case .network( _):
// Network error
XCTAssert( false,"Network error is unexpected.")
case .http( _):
// HTTP error
XCTAssert( false,"HTTP error is unexpected.")
case .ignoreData:
// Unexpected response data
XCTAssert( false,"IgnoreData error is unexpected.")
case .parse( _):
// Failed parse response data
XCTAssert( false,"Parse error is unexpected.")
case .application(let e):
// Server side defined error
XCTAssert( e is ApplicationError, "Serialize for ApplicationError is fail.")
}
}
catch {
// Unexpected throws
XCTAssert( false,"Unexpected error.")
}
You can customize the behavior by implementing request and session.
// TimeoutInterval extended
extension CustomRequestStruct : WebInitializable {
...
static var request:URLRequest {
guard let url = URL(string: CustomRequestStruct.path ) else{ fatalError() }
var request = URLRequest(url:url, cachePolicy:.reloadIgnoringLocalCacheData, timeoutInterval:10.0 )
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField:"Content-Type")
return request
}
...
}
// Cellular access disabled
extension CustomSessionStruct : WebInitializable {
...
static var session:URLSession {
let def = URLSessionConfiguration.default
def.allowsCellularAccess = false
return URLSession(configuration: def, delegate: nil, delegateQueue: nil)
}
...
}
cd Server
swift package clean
swift package update
swift package generate-xcodeproj
open WebStructTestServer.xcodeproj
Build & Run "TestServer" Target in Xcode.
or
.build/debug/TestServer
Command & U in WebStruct.xcodeproj
- There was a problem that a segmentation fault occurred when used with Ubuntu.
- I looked up this problem is solved on DEVELOPMENT-SNAPSHOT-2017-02-09-a.