Created
July 31, 2019 16:37
-
-
Save Andrea-Scuderi/66bd76060c7397af6ce9973ab73aa153 to your computer and use it in GitHub Desktop.
Vapor API Client for auth-template - Part 4
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
//Login | |
func buildLoginRequest(email: String, password: String) throws -> URLRequest { | |
let loginString = String(format: "%@:%@", email, password) | |
let loginData: Data = loginString.data(using: .utf8)! | |
let base64LoginString = loginData.base64EncodedString() | |
let headers = [ | |
"Content-Type": "application/json", | |
"cache-control": "no-cache", | |
"Authorization": "Basic \(base64LoginString)" | |
] | |
guard let url = URL(string: baseURL + "/login" ) else { | |
throw APIError.invalidEndpoint | |
} | |
var request = URLRequest(url: url, | |
cachePolicy: .useProtocolCachePolicy, | |
timeoutInterval: 10.0) | |
request.httpMethod = "POST" | |
request.allHTTPHeaderFields = headers | |
return request | |
} | |
func postLogin(email: String,password: String, session: URLSession = URLSession.shared) throws -> URLSession.DataTaskPublisher { | |
let request = try buildLoginRequest(email: email, password: password) | |
return session.dataTaskPublisher(for: request) | |
} | |
//Todo | |
struct Todo: Codable { | |
let id: Int? | |
let title: String | |
} | |
func buildPostTodoRequest(authToken: String, body: Todo) throws -> URLRequest { | |
let headers = [ | |
"Content-Type": "application/json", | |
"cache-control": "no-cache", | |
"Authorization": "Bearer \(authToken)" | |
] | |
let encoder = JSONEncoder() | |
guard let postData = try? encoder.encode(body) else { | |
throw APIError.invalidBody | |
} | |
guard let url = URL(string: baseURL + "/todos" ) else { | |
throw APIError.invalidEndpoint | |
} | |
var request = URLRequest(url: url, | |
cachePolicy: .useProtocolCachePolicy, | |
timeoutInterval: 10.0) | |
request.httpMethod = "POST" | |
request.allHTTPHeaderFields = headers | |
request.httpBody = postData | |
return request | |
} | |
func postTodo(authToken: String, body: Todo, session: URLSession = URLSession.shared) throws -> URLSession.DataTaskPublisher { | |
let request = try buildPostTodoRequest(authToken: authToken, body: body) | |
return session.dataTaskPublisher(for: request) | |
} | |
func buildGetTodoRequest(authToken: String) throws -> URLRequest { | |
let headers = [ | |
"Content-Type": "application/json", | |
"cache-control": "no-cache", | |
"Authorization": "Bearer \(authToken)" | |
] | |
guard let url = URL(string: baseURL + "/todos" ) else { | |
throw APIError.invalidEndpoint | |
} | |
var request = URLRequest(url: url, | |
cachePolicy: .useProtocolCachePolicy, | |
timeoutInterval: 10.0) | |
request.httpMethod = "GET" | |
request.allHTTPHeaderFields = headers | |
return request | |
} | |
func getTodo(authToken: String, session: URLSession = URLSession.shared) throws -> URLSession.DataTaskPublisher { | |
let request = try buildGetTodoRequest(authToken: authToken) | |
return session.dataTaskPublisher(for: request) | |
} | |
func buildDeleteTodoRequest(authToken: String, id: Int) throws -> URLRequest { | |
let headers = [ | |
"Content-Type": "application/json", | |
"cache-control": "no-cache", | |
"Authorization": "Bearer \(authToken)" | |
] | |
guard let url = URL(string: baseURL + "/todos/\(id)" ) else { | |
throw APIError.invalidEndpoint | |
} | |
var request = URLRequest(url: url, | |
cachePolicy: .useProtocolCachePolicy, | |
timeoutInterval: 10.0) | |
request.httpMethod = "DELETE" | |
request.allHTTPHeaderFields = headers | |
return request | |
} | |
func deleteTodo(authToken: String, id: Int, session: URLSession = URLSession.shared) throws -> URLSession.DataTaskPublisher { | |
let request = try buildDeleteTodoRequest(authToken: authToken, id: id) | |
return session.dataTaskPublisher(for: request) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment