[DISCUSSTION]Alamofire를 이용한 네트워크 레이어 레퍼런스 #59
Replies: 4 comments
-
Beta Was this translation helpful? Give feedback.
-
URLRequestConvertibleopen func request(_ urlRequest: URLRequestConvertible,
interceptor: RequestInterceptor? = nil) -> DataRequest /// Types adopting the `URLRequestConvertible` protocol can be used to safely construct `URLRequest`s.
public protocol URLRequestConvertible {
/// Returns a `URLRequest` or throws if an `Error` was encountered.
///
/// - Returns: A `URLRequest`.
/// - Throws: Any error thrown while constructing the `URLRequest`.
func asURLRequest() throws -> URLRequest
}
extension URLRequestConvertible {
/// The `URLRequest` returned by discarding any `Error` encountered.
public var urlRequest: URLRequest? { try? asURLRequest() }
}
extension URLRequest: URLRequestConvertible {
/// Returns `self`.
public func asURLRequest() throws -> URLRequest { self }
} Alamofire의 request 파이프라인을 통과하는 모든 request의 기반으로 URLRequestConvertible을 사용하게 됩니다. Alamofire의 AdvancedUsage에서는 해당 프로토콜과 Router Pattern을 같이 적절하게 활용하면 복잡한 앱의 API를 쉽게 관리하고 설계할 수 있다며 이를 권장하고 있습니다. enum Router: URLRequestConvertible {
case get([String: String]), post([String: String])
var baseURL: URL {
return URL(string: "https://httpbin.org")!
}
var method: HTTPMethod {
switch self {
case .get: return .get
case .post: return .post
}
}
var path: String {
switch self {
case .get: return "get"
case .post: return "post"
}
}
func asURLRequest() throws -> URLRequest {
let url = baseURL.appendingPathComponent(path)
var request = URLRequest(url: url)
request.method = method
switch self {
case let .get(parameters):
request = try URLEncodedFormParameterEncoder().encode(parameters, into: request)
case let .post(parameters):
request = try JSONParameterEncoder().encode(parameters, into: request)
}
return request
}
} URLRequestConvertible과 Router Pattern을 이용하면 우리 앱에서도 엔드포인트를 쉽게 관리할 수 있는 장점이 있어보입니다. |
Beta Was this translation helpful? Give feedback.
-
토큰갱신하는방법https://ios-development.tistory.com/730?category=899471
|
Beta Was this translation helpful? Give feedback.
-
Alamofire를 사용할 때 Alamofire의 Request 파이프라인을 이해하고 있으면 여러므로 도움이 될 것 같아서 아래 링크 또 따로 올리겠습니다. https://github.com/Alamofire/Alamofire/blob/master/Documentation/AdvancedUsage.md#requests
|
Beta Was this translation helpful? Give feedback.
-
기존 프로젝트에서는 async/await과 urlsession을 통한 네트워크 레이어를 구축했습니다
현재 적용 예정인 네트워크 레이어 구조
기존 프로젝트
변경될 레이어의 역할
하지만 이번 프로젝트에서는 Alamofire과 async/await 그리고 combine을 활용한 네트워크 레이어를 구축하기로 결정을해서 많은 레퍼런스를 참고해서 하나의 레퍼런스를 결정해서 해당 방식으로 구현을 하는건 어떨까요
주말을 이용해서 여러 레퍼런스를 참고해서 본인이 괜찮다싶은 레퍼런스 링크와 특징을 기록해보시죠
Beta Was this translation helpful? Give feedback.
All reactions