Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
friends/Friends/AppServerClient.swift
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
144 lines (127 sloc)
5.37 KB
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
// | |
// AppServerClient.swift | |
// Friends | |
// | |
// Created by Jussi Suojanen on 07/11/16. | |
// Copyright © 2016 Jimmy. All rights reserved. | |
// | |
import Alamofire | |
// MARK: - AppServerClient | |
class AppServerClient { | |
// MARK: - GetFriends | |
enum GetFriendsFailureReason: Int, Error { | |
case unAuthorized = 401 | |
case notFound = 404 | |
} | |
typealias GetFriendsResult = Result<[Friend], GetFriendsFailureReason> | |
typealias GetFriendsCompletion = (_ result: GetFriendsResult) -> Void | |
func getFriends(completion: @escaping GetFriendsCompletion) { | |
Alamofire.request("http://friendservice.herokuapp.com/listFriends") | |
.validate() | |
.responseJSON { response in | |
switch response.result { | |
case .success: | |
do { | |
guard let data = response.data else { | |
completion(.failure(nil)) | |
return | |
} | |
let friends = try JSONDecoder().decode([Friend].self, from: data) | |
completion(.success(payload: friends)) | |
} catch { | |
completion(.failure(nil)) | |
} | |
case .failure(_): | |
if let statusCode = response.response?.statusCode, | |
let reason = GetFriendsFailureReason(rawValue: statusCode) { | |
completion(.failure(reason)) | |
} | |
completion(.failure(nil)) | |
} | |
} | |
} | |
// MARK: - PostFriend | |
enum PostFriendFailureReason: Int, Error { | |
case unAuthorized = 401 | |
case notFound = 404 | |
} | |
typealias PostFriendResult = EmptyResult<PostFriendFailureReason> | |
typealias PostFriendCompletion = (_ result: PostFriendResult) -> Void | |
func postFriend(firstname: String, lastname: String, phonenumber: String, completion: @escaping PostFriendCompletion) { | |
let param = ["firstname": firstname, | |
"lastname": lastname, | |
"phonenumber": phonenumber] | |
Alamofire.request("https://friendservice.herokuapp.com/addFriend", method: .post, parameters: param, encoding: JSONEncoding.default) | |
.validate() | |
.responseJSON { response in | |
switch response.result { | |
case .success: | |
completion(.success) | |
case .failure(_): | |
if let statusCode = response.response?.statusCode, | |
let reason = PostFriendFailureReason(rawValue: statusCode) { | |
completion(.failure(reason)) | |
} | |
completion(.failure(nil)) | |
} | |
} | |
} | |
// MARK: - PatchFriend | |
enum PatchFriendFailureReason: Int, Error { | |
case unAuthorized = 401 | |
case notFound = 404 | |
} | |
typealias PatchFriendResult = Result<Friend, PatchFriendFailureReason> | |
typealias PatchFriendCompletion = (_ result: PatchFriendResult) -> Void | |
func patchFriend(firstname: String, lastname: String, phonenumber: String, id: Int, completion: @escaping PatchFriendCompletion) { | |
let param = ["firstname": firstname, | |
"lastname": lastname, | |
"phonenumber": phonenumber] | |
Alamofire.request("https://friendservice.herokuapp.com/editFriend/\(id)", method: .patch, parameters: param, encoding: JSONEncoding.default) | |
.validate() | |
.responseJSON { response in | |
switch response.result { | |
case .success: | |
do { | |
guard let data = response.data else { | |
completion(.failure(nil)) | |
return | |
} | |
let friend = try JSONDecoder().decode(Friend.self, from: data) | |
completion(.success(payload: friend)) | |
} catch { | |
completion(.failure(nil)) | |
} | |
case .failure(_): | |
if let statusCode = response.response?.statusCode, | |
let reason = PatchFriendFailureReason(rawValue: statusCode) { | |
completion(.failure(reason)) | |
} | |
completion(.failure(nil)) | |
} | |
} | |
} | |
// MARK: - DeleteFriend | |
enum DeleteFriendFailureReason: Int, Error { | |
case unAuthorized = 401 | |
case notFound = 404 | |
} | |
typealias DeleteFriendResult = EmptyResult<DeleteFriendFailureReason> | |
typealias DeleteFriendCompletion = (_ result: DeleteFriendResult) -> Void | |
func deleteFriend(id: Int, completion: @escaping DeleteFriendCompletion) { | |
Alamofire.request("https://friendservice.herokuapp.com/editFriend/\(id)", method: .delete, parameters: nil, encoding: JSONEncoding.default) | |
.validate() | |
.responseJSON { response in | |
switch response.result { | |
case .success: | |
completion(.success) | |
case .failure(_): | |
if let statusCode = response.response?.statusCode, | |
let reason = DeleteFriendFailureReason(rawValue: statusCode) { | |
completion(.failure(reason)) | |
} | |
completion(.failure(nil)) | |
} | |
} | |
} | |
} |