Skip to content

Instantly share code, notes, and snippets.

@Andrea-Scuderi
Last active August 1, 2019 10:02
Show Gist options
  • Save Andrea-Scuderi/8603c522bc2fd646b1d3fc21498a07e8 to your computer and use it in GitHub Desktop.
Save Andrea-Scuderi/8603c522bc2fd646b1d3fc21498a07e8 to your computer and use it in GitHub Desktop.
Part 5
// Completing the API with Combine and dataTaskPublisher
// POST - Login
func buildLoginRequest(email: String, password: String) -> 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 {
fatalError("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) -> URLSession.DataTaskPublisher {
let request = buildLoginRequest(email: email, password: password)
return session.dataTaskPublisher(for: request)
}
//Todo API implementation with dataTaskPublisher
struct Todo: Codable {
let id: Int?
let title: String
}
// POST - todos
func buildPostTodoRequest(authToken: String, body: Todo) -> 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 {
fatalError("APIError.invalidBody")
}
guard let url = URL(string: baseURL + "/todos" ) else {
fatalError("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) -> URLSession.DataTaskPublisher {
let request = buildPostTodoRequest(authToken: authToken, body: body)
return session.dataTaskPublisher(for: request)
}
// GET - todos
func buildGetTodoRequest(authToken: String) -> URLRequest {
let headers = [
"Content-Type": "application/json",
"cache-control": "no-cache",
"Authorization": "Bearer \(authToken)"
]
guard let url = URL(string: baseURL + "/todos" ) else {
fatalError("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) -> URLSession.DataTaskPublisher {
let request = buildGetTodoRequest(authToken: authToken)
return session.dataTaskPublisher(for: request)
}
// DELETE - todos
func buildDeleteTodoRequest(authToken: String, id: Int) -> URLRequest {
let headers = [
"Content-Type": "application/json",
"cache-control": "no-cache",
"Authorization": "Bearer \(authToken)"
]
guard let url = URL(string: baseURL + "/todos/\(id)" ) else {
fatalError("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) -> URLSession.DataTaskPublisher {
let request = 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