// set up the session let session = URLSession.shared // set up the request let todoEndpoint = "https://jsonplaceholder.typicode.com/todos/1" guard let url = URL(string: todoEndpoint) else { print("Error: cannot create URL") return } let urlRequest = URLRequest(url: url) // make the request let task = session.dataTask(with: urlRequest) { (data, response, error) in // convert data to JSON // do stuff with response as JSON } task.resume()
let todoEndpoint = "https://jsonplaceholder.typicode.com/todos/1" Alamofire.request(todoEndpoint) .responseJSON { response in // do stuff with response as JSON } .responseString { response in // do stuff with response as String } .response { response in // do stuff with response as Data } .responsePropertyList { response in // do stuff with response as plist }
let todosEndpoint = "https://jsonplaceholder.typicode.com/todos" let newTodo: [String: Any] = ["title": "My First Todo", "completed": false, "userId": 1] Alamofire.request(todosEndpoint, method: .post, parameters: newTodo, encoding: JSONEncoding.default, headers: nil) .responseJSON { ... }
let todoEndpoint = "https://jsonplaceholder.typicode.com/todos/1" guard let url = URL(string: todoEndpoint) else { print("Error: cannot create URL") return } let urlRequest = URLRequest(url: url) Alamofire.request(urlRequest) .responseJSON { response in // do stuff with response as JSON }
let alamofireRequest = Alamofire.request(...) .responseJSON { response in // do stuff with response as JSON } debugPrint(alamofireRequest)
curl -i \ -H "Accept-Language: en;q=1.0" \ -H "User-Agent: grokRouterUpdate/1.0 \ (com.teakmobile.grokRouterUpdate; build:1; iOS 10.3.0) \ Alamofire/4.4.0" \ -H "Accept-Encoding: gzip;q=1.0, compress;q=0.5" \ "https://jsonplaceholder.typicode.com/todos/1"
Alamofire.request(TodoRouter.get(1)) .responseJSON { response in // do stuff with response as JSON }
import Foundation import Alamofire enum TodoRouter: URLRequestConvertible { static let baseURLString = "https://jsonplaceholder.typicode.com/" case get(Int) case create([String: Any]) case delete(Int) func asURLRequest() throws -> URLRequest { // TODO: implement } }
/// Types adopting the `URLRequestConvertible` protocol can be /// used to construct URL requests. public protocol URLRequestConvertible { /// Returns a URL request or throws if an `Error` was /// encountered. /// /// - throws: An `Error` if the underlying `URLRequest` /// is `nil`. /// /// - returns: A URL request. func asURLRequest() throws -> URLRequest }
func asURLRequest() throws -> URLRequest { let method: HTTPMethod = { switch self { case .get: return .get case .create: return .post case .delete: return .delete } }() // ... }
func asURLRequest() throws -> URLRequest { let method: HTTPMethod = { // ... }() let params: ([String: Any]?) = { switch self { case .get, .delete: return nil case .create(let newTodo): return newTodo } }() }
func asURLRequest() throws -> URLRequest { // ... let url: URL = { // build up and return the URL for each endpoint let relativePath: String? switch self { case .get(let number), .delete(let number): relativePath = "todos/\(number)" case .create: relativePath = "todos" } var url = URL(string: TodoRouter.baseURLString)! if let relativePath = relativePath { url = url.appendingPathComponent(relativePath) } return url }() }
func asURLRequest() throws -> URLRequest { let method: HTTPMethod = { ... }() let params: ([String: Any]?) = { ... }() let url: URL = { ... }() var urlRequest = URLRequest(url: url) urlRequest.httpMethod = method.rawValue let encoding = JSONEncoding.default return try encoding.encode(urlRequest, with: params) }
func asURLRequest() throws -> URLRequest { // ... var urlRequest = URLRequest(url: url) urlRequest.httpMethod = method.rawValue // modify urlRequest as needed here if let token = APIManager.authToken { urlRequest.setValue("\(token)", forHTTPHeaderField: "Authorization") } let encoding = JSONEncoding.default return try encoding.encode(urlRequest, with: params) }
struct GetTodoURLRequestProvider: URLRequestConvertible { let idNumber: Int func asURLRequest() throws -> URLRequest { let urlString = "https://jsonplaceholder.typicode.com/todos/\(idNumber)" return URLRequest(url: URL(string: urlString)!) } }
let urlRequestProvider = GetPostURLRequestProvider(idNumber: 2) Alamofire.request(urlRequestProvider) .responseJSON { ... }
protocol Route { // ... } struct GetTodo: Route { // ... } struct Router: URLRequestConvertible { let route: Route func asURLRequest() throws -> URLRequest { let method: HTTPMethod = route.method() let params: ([String: Any]?) = route.params() let url: URL = route.url() var urlRequest = URLRequest(url: url) urlRequest.httpMethod = method.rawValue let encoding = route.encoding() return try encoding.encode(urlRequest, with: params) } }