Skip to content

Instantly share code, notes, and snippets.

@ohlulu
Last active September 10, 2019 02:55
Show Gist options
  • Save ohlulu/c039953c41f6dfb81ee99e9c32247f98 to your computer and use it in GitHub Desktop.
Save ohlulu/c039953c41f6dfb81ee99e9c32247f98 to your computer and use it in GitHub Desktop.
APIRouter Alamofire
import Foundation
import Alamofire
struct HTTPHeader {
enum Field: String {
case contentType = "Content-Type"
case acceptType = "Accept"
}
enum ContentType: String {
case json = "application/json"
}
static let `default`: [String: String] = [
ContentType.json.rawValue: Field.contentType.rawValue,
ContentType.json.rawValue: Field.acceptType.rawValue
]
}
struct APIRouter: URLRequestConvertible {
var tag: String
let path: String
let method: HTTPMethod
var parameters: Parameters?
func asURLRequest() throws -> URLRequest {
let url = URL(string: "https://www.google.com")!
.appendingPathComponent(path)
var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = method.rawValue
urlRequest.timeoutInterval = 30
// Http common header
urlRequest.allHTTPHeaderFields = HTTPHeader.default
// add custom
do {
return try JSONEncoding.default.encode(urlRequest, with: parameters)
} catch {
fatalError("\(error)")
}
}
}
extension APIRouter {
static func login(requestModel model: LoginRequestModel) -> APIRouter {
return APIRouter(tag: "[201]",
path: "/login",
method: .post,
parameters: model.convertToParameter())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment