Created
September 19, 2018 07:09
-
-
Save YutoMizutani/8844631bbea6fdb47e78ea3e36d45221 to your computer and use it in GitHub Desktop.
Genericsを用いて安全にJSONをパースする (Alamofire x Swift4) ref: https://qiita.com/YutoMizutani/items/c61fbb0060095939e7ff
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
import Alamofire | |
class Foo { | |
/// Success handler | |
public typealias SuccessHandler<T> = (_ model: T) -> Void | |
/// Failure handler | |
public typealias FailureHandler = (_ error: Error) -> Void | |
/** | |
Alamofire Request | |
- Parameters: | |
- url: API URL | |
- method: HTTP method | |
- parameters: Query parameters | |
- success: Success handler | |
- failure: Failure handler | |
*/ | |
func baseRequest<T: Decodable>(_ url: String, | |
method: HTTPMethod = .get, | |
parameters: Parameters? = nil, | |
success: SuccessHandler<T>?, | |
failure: FailureHandler?) { | |
Alamofire.request(url, method: method, parameters: parameters, encoding: encoding, headers: headers) | |
.responseData { response in | |
switch response.result { | |
case .success: | |
guard let success = success, let data = response.data else { return } | |
let decoder: JSONDecoder = JSONDecoder() | |
guard let model = try? decoder.decode(T.self, from: data) else { return } | |
success?(model) | |
case .failure(let error): | |
failure?(error) | |
} | |
} | |
} | |
} |
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
public struct FooModel: Codable { | |
public let bar: String | |
} |
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
func request(_ url: String, | |
method: HTTPMethod = .get, | |
parameters: Parameters? = nil, | |
success: SuccessHandler<FooModel>?, | |
failure: FailureHandler?) { | |
self.baseRequest(url, | |
method: method, | |
parameters: parameters, | |
success: success, | |
failure: failure) | |
} |
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
self.request(url, success: { fooModel in | |
print(fooModel.bar) | |
}) |
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
import XCTest | |
@testable import Foo | |
final class FooTests: XCTestCase { | |
var foo: Foo! | |
override func setUp() { | |
super.setUp() | |
self.foo = Foo() | |
} | |
func decode<T: Decodable>(_ url: String, parameters: Parameters?, model: T.Type) { | |
let expect: XCTestExpectation = self.expectation(description: url) | |
let success: Foo.SuccessHandler<T> = { model in | |
expect.fulfill() | |
XCTAssertNotNil(model) | |
} | |
let failure: Foo.FailureHandler = { error in | |
expect.fulfill() | |
XCTFail(error.localizedDescription) | |
} | |
self.foo.baseRequest(url, success: success, parameters: parameters, failure: failure) | |
self.wait(for: [expect], timeout: 10) | |
} | |
} |
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
func testDecodeFoo() { | |
decode(url, parameters: parameters. model: FooModel.self) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment